v2 / vlib / x / atomics / i32_test.v
221 lines · 187 sloc · 3.23 KB · 1c4bb916e5cdf6aecbc6c6315f95453a9e8778f3
Raw
1// vtest build: !(macos || windows)
2
3module atomics
4
5fn test_cas_i32_basic() {
6 mut x := i32(10)
7 ok := cas_i32(&x, 10, 20)
8 assert ok == true
9 assert x == 20
10}
11
12fn test_cas_fail() {
13 mut x := i32(5)
14 assert !cas_i32(&x, 10, 42)
15 assert x == 5
16}
17
18fn test_cas_fail_memory_unchanged() {
19 mut x := i32(7)
20 cas_i32(&x, 1, 2)
21 assert x == 7
22}
23
24fn test_cas_exact_match() {
25 mut x := i32(-1)
26 assert !cas_i32(&x, 0, 999)
27 assert x == -1
28}
29
30fn test_cas_twice() {
31 mut x := i32(1)
32 assert cas_i32(&x, 1, 2)
33 assert cas_i32(&x, 2, 3)
34 assert x == 3
35}
36
37fn test_cas_with_negative() {
38 mut x := i32(-123)
39 assert cas_i32(&x, -123, 8)
40 assert x == 8
41}
42
43fn test_add_i32_basic() {
44 mut x := i32(0)
45 for _ in 0 .. 1000 {
46 add_i32(&x, 1)
47 }
48 assert x == 1000
49}
50
51fn test_add_i32_negative() {
52 mut x := i32(10)
53 add_i32(&x, -3)
54 assert x == 7
55}
56
57fn test_add_i32_return_value() {
58 mut x := i32(5)
59 r := add_i32(&x, 7)
60 assert r == 12
61 assert x == 12
62}
63
64fn test_add_i32_overflow_wraps() {
65 mut x := i32(2147483647)
66 add_i32(&x, 1)
67 assert x == -2147483648
68}
69
70fn test_swap_i32_basic() {
71 mut x := i32(5)
72 old := swap_i32(&x, 99)
73 assert old == 5
74 assert x == 99
75}
76
77fn test_swap_i32_twice() {
78 mut x := i32(1)
79 assert swap_i32(&x, 2) == 1
80 assert swap_i32(&x, 3) == 2
81 assert x == 3
82}
83
84fn test_swap_i32_with_cas() {
85 mut x := i32(10)
86 assert cas_i32(&x, 10, 20)
87 old := swap_i32(&x, 30)
88 assert old == 20
89 assert x == 30
90}
91
92fn test_load_i32_basic() {
93 mut x := i32(123456)
94 assert load_i32(&x) == 123456
95}
96
97fn test_store_i32_basic() {
98 mut x := i32(5)
99 store_i32(&x, 777)
100 assert x == 777
101}
102
103fn test_add_i32_concurrent() {
104 mut x := i32(0)
105 mut threads := []thread{}
106
107 for _ in 0 .. 9 {
108 threads << spawn fn (ptr &i32) {
109 for _ in 0 .. 100_000 {
110 add_i32(ptr, 1)
111 }
112 }(&x)
113 }
114
115 for t in threads {
116 t.wait()
117 }
118
119 assert x == 900_000
120}
121
122fn test_swap_i32_concurrent() {
123 mut x := i32(0)
124 mut threads := []thread{}
125
126 for _ in 0 .. 8 {
127 threads << spawn fn (ptr &i32) {
128 for _ in 0 .. 50_000 {
129 swap_i32(ptr, 123)
130 }
131 }(&x)
132 }
133
134 for t in threads {
135 t.wait()
136 }
137
138 assert x == 123
139}
140
141fn test_cas_i32_concurrent() {
142 mut x := i32(0)
143 mut threads := []thread{}
144
145 for _ in 0 .. 8 {
146 threads << spawn fn (ptr &i32) {
147 for _ in 0 .. 100_000 {
148 for {
149 old := load_i32(ptr)
150 if cas_i32(ptr, old, old + 1) {
151 break
152 }
153 }
154 }
155 }(&x)
156 }
157
158 for t in threads {
159 t.wait()
160 }
161
162 assert x == 800_000
163}
164
165fn test_load_store_i32_concurrent() {
166 mut x := i32(0)
167 mut threads := []thread{}
168
169 for _ in 0 .. 4 {
170 threads << spawn fn (px &i32) {
171 for _ in 0 .. 50_000 {
172 add_i32(px, 1)
173 }
174 }(&x)
175 }
176 for _ in 0 .. 4 {
177 threads << spawn fn (px &i32) {
178 for _ in 0 .. 50_000 {
179 _ = load_i32(px)
180 }
181 }(&x)
182 }
183
184 for t in threads {
185 t.wait()
186 }
187
188 assert x == 4 * 50_000
189}
190
191fn test_and_i32_concurrent() {
192 mut x := i32(0x7fffffff)
193 mut threads := []thread{}
194 for _ in 0 .. 8 {
195 threads << spawn fn (px &i32) {
196 for _ in 0 .. 100_000 {
197 and_i32(px, 0x0fffffff)
198 }
199 }(&x)
200 }
201 for t in threads {
202 t.wait()
203 }
204 assert x == 0x0fffffff
205}
206
207fn test_or_i32_concurrent() {
208 mut x := i32(0)
209 mut threads := []thread{}
210 for _ in 0 .. 8 {
211 threads << spawn fn (px &i32) {
212 for _ in 0 .. 100_000 {
213 or_i32(px, 0x12345678)
214 }
215 }(&x)
216 }
217 for t in threads {
218 t.wait()
219 }
220 assert x == 0x12345678
221}
222