v2 / vlib / v / tests / pointers / ref_struct_test.v
96 lines · 84 sloc · 873 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1@[heap]
2struct Abc {
3mut:
4 n int
5}
6
7struct St {
8 Abc
9}
10
11struct Qwe {
12mut:
13 f f64
14 a Abc
15}
16
17struct Rtz {
18mut:
19 f f64
20 a &Abc
21}
22
23fn test_ref() {
24 a := &Abc{
25 n: 3
26 }
27 assert a.n == 3
28}
29
30fn test_shared() {
31 shared a := Abc{
32 n: 4
33 }
34 res := rlock a {
35 a.n
36 }
37 assert res == 4
38}
39
40fn test_embed_in_ref_struct() {
41 a := &St{Abc{
42 n: 5
43 }}
44 assert a.n == 5
45}
46
47fn test_field_in_ref_struct() {
48 x := &Qwe{
49 f: 12.25
50 a: Abc{
51 n: 23
52 }
53 }
54 assert x.a.n == 23
55}
56
57fn test_ref_field() {
58 y := Rtz{
59 f: -6.25
60 a: &Abc{
61 n: 29
62 }
63 }
64 assert y.a.n == 29
65}
66
67// Yxc should become @[heap] implicitly because `Abc` is
68
69struct Yxc {
70mut:
71 a []Abc
72}
73
74fn mod_heap_array(mut x Yxc) {
75 x.a << Abc{
76 n: 9
77 }
78 x.a[1] = Abc{
79 n: 13
80 }
81}
82
83fn test_heap_array() {
84 mut z := &Yxc{
85 a: [
86 Abc{
87 n: 4
88 },
89 Abc{
90 n: 7
91 },
92 ]
93 }
94 mod_heap_array(mut z)
95 assert z.a[1].n == 13
96}
97