v2 / vlib / v / tests / init_global_test.v
194 lines · 179 sloc · 3.26 KB · 66946738fbcdad4ce810a04a868b4d0f05cf55b2
Raw
1@[has_globals]
2module main
3
4// vtest retry: 2
5import sync
6
7fn one() int {
8 return 1
9}
10
11fn pushf64() {
12 ch <- 12.5
13}
14
15fn test_global_init() {
16 intmap['two'] = 27
17 key := 'two'
18 assert intmap[key] == 27
19 t := spawn pushf64()
20 numberfns['one'] = one
21 numberfns['two'] = fn () int {
22 return 2
23 }
24 f := numberfns['one']
25 n := f()
26 assert n == 1
27 m := numberfns['two']()
28 assert m == 2
29 got := <-ch
30 assert got == 12.5
31 t.wait()
32 assert true
33}
34
35fn get_u64() u64 {
36 return 27
37}
38
39fn test_no_type() {
40 assert test == 0
41 assert typeof(test).name == 'int'
42 assert testf == 1.25
43 assert typeof(testf).name == 'f64'
44 assert testneg == -2
45 assert typeof(testneg).name == 'int'
46 assert testnegf == -1250000
47 assert typeof(testnegf).name == 'f64'
48 assert testexpl == 7
49 assert typeof(testexpl).name == 'f32'
50 assert testfn == 27
51 assert typeof(testfn).name == 'u64'
52 assert typeof(testarr).name == '[]f64'
53 assert testarr.len == 10
54 assert testarr[9] == 2.75
55 assert typeof(testmap).name == 'map[string]f64'
56 assert testmap['asd'] == -7.25
57}
58
59fn test_fn_type() {
60 assert func2(22) == 22
61 assert func3(22) == '22'
62}
63
64__global (
65 intmap map[string]int
66 numberfns map[string]fn () int
67 ch chan f64
68 mys shared MyStruct
69 sem sync.Semaphore
70 shmap shared map[string]f64
71 mtx sync.RwMutex
72 f1 = f64(545 / (sizeof(f64) + f32(8))) // directly initialized
73 f2 f64
74 test = 0 // int
75 testf = 1.25 // f64
76 testneg = -2 // int
77 testnegf = -1.25e06 // f64
78 testexpl = f32(7)
79 testfn = get_u64()
80 testarr = []f64{len: 10, init: 2.75}
81 testmap = {
82 'qwe': 2.5
83 'asd': -7.25
84 'yxc': 3.125
85 }
86 func1 = fn () {}
87 func2 = fn (n int) int {
88 return n
89 }
90 func3 = fn (n int) string {
91 return '${n}'
92 }
93)
94
95fn init() {
96 // semaphores and mutexes must not be moved in memory, so for technical
97 // reasons they cannot have a "default constructor" at the moment and must
98 // be initialized "manually"
99 sem.init(0)
100 mtx.init()
101}
102
103struct MyStruct {
104mut:
105 x f64
106 y f64
107}
108
109fn switch() {
110 for !sem.try_wait() {
111 lock mys {
112 if mys.x == 13.0 {
113 mys.x = 13.75
114 } else if mys.y == 13.0 {
115 mys.y = 13.75
116 }
117 }
118 lock mys {
119 mys.x, mys.y = mys.y, mys.x
120 }
121 }
122}
123
124fn test_global_shared() {
125 lock mys {
126 mys.x = 13.0
127 mys.y = -35.125
128 }
129 t := spawn switch()
130 for _ in 0 .. 2500000 {
131 lock mys {
132 mys.x, mys.y = mys.y, mys.x
133 }
134 }
135 a, b := rlock mys {
136 mys.x, mys.y
137 }
138 sem.post()
139 t.wait()
140 eprintln('> a: ${a} | b: ${b}')
141 c1 := dump(a == 13.75 && b == -35.125)
142 c2 := dump(a == -35.125 && b == 13.75)
143 assert c1 || c2
144}
145
146fn test_global_shared_map() {
147 lock shmap {
148 shmap['one'] = 1.25
149 shmap['two'] = -0.75
150 }
151 x, y := rlock shmap {
152 shmap['two'], shmap['one']
153 }
154 assert x == -0.75
155 assert y == 1.25
156}
157
158fn switch2() u64 {
159 mut cnt := u64(0)
160 for {
161 cnt++
162 mtx.lock()
163 f1, f2 = f2, f1
164 if f1 == 17.0 || f2 == 17.0 {
165 mtx.unlock()
166 return cnt
167 }
168 mtx.unlock()
169 }
170 return 0
171}
172
173fn test_global_mutex() {
174 assert f1 == 34.0625
175 t := spawn switch2()
176 for _ in 0 .. 25000 {
177 mtx.lock()
178 f1, f2 = f2, f1
179 mtx.unlock()
180 }
181 mtx.lock()
182 if f1 == 0.0 {
183 f1 = 17.0
184 } else {
185 f2 = 17.0
186 }
187 mtx.unlock()
188 mtx.rlock()
189 assert (f1 == 17.0 && f2 == 34.0625) || (f1 == 34.0625 && f2 == 17.0)
190 mtx.runlock()
191 n := t.wait()
192 eprintln('> n: ${n} | f1: ${f1} | f2: ${f2}')
193 assert n > 0
194}
195