v2 / vlib / x / atomics / i64_test.v
193 lines · 162 sloc · 2.92 KB · 1c4bb916e5cdf6aecbc6c6315f95453a9e8778f3
Raw
1// vtest build: !(macos || windows)
2
3module atomics
4
5fn test_load_i64_basic() {
6 mut x := i64(1234567890123)
7 assert load_i64(&x) == 1234567890123
8}
9
10fn test_store_i64_basic() {
11 mut x := i64(1)
12 store_i64(&x, 9999999)
13 assert x == 9999999
14}
15
16fn test_swap_i64_basic() {
17 mut x := i64(50)
18 old := swap_i64(&x, 777)
19 assert old == 50
20 assert x == 777
21}
22
23fn test_swap_i64_same_value() {
24 mut x := i64(-123)
25 old := swap_i64(&x, -123)
26 assert old == -123
27 assert x == -123
28}
29
30fn test_add_i64_basic() {
31 mut x := i64(0)
32 for _ in 0 .. 1000 {
33 add_i64(&x, 3)
34 }
35 assert x == 3000
36}
37
38fn test_add_i64_negative_delta() {
39 mut x := i64(100)
40 add_i64(&x, -7)
41 assert x == 93
42}
43
44fn test_add_i64_wraparound_behavior() {
45 mut x := i64(0x7fffffffffffffff)
46 add_i64(&x, 1)
47 assert x == -0x8000000000000000
48}
49
50fn test_add_i64_return_value() {
51 mut x := i64(10)
52 r := add_i64(&x, 100)
53 assert r == 110
54 assert x == 110
55}
56
57fn test_cas_i64_basic() {
58 mut x := i64(111)
59 assert cas_i64(&x, 111, 222)
60 assert x == 222
61}
62
63fn test_cas_i64_fail() {
64 mut x := i64(555)
65 assert !cas_i64(&x, 123, 999)
66 assert x == 555
67}
68
69fn test_cas_i64_nochange_on_fail() {
70 mut x := i64(-999)
71 cas_i64(&x, 1, 5)
72 assert x == -999
73}
74
75fn test_cas_i64_negative_values() {
76 mut x := i64(-123456)
77 assert cas_i64(&x, -123456, 777)
78 assert x == 777
79}
80
81fn test_add_i64_concurrent() {
82 mut x := i64(0)
83 mut threads := []thread{}
84
85 for _ in 0 .. 8 {
86 threads << spawn fn (px &i64) {
87 for _ in 0 .. 100_000 {
88 add_i64(px, 1)
89 }
90 }(&x)
91 }
92
93 for t in threads {
94 t.wait()
95 }
96
97 assert x == 800_000
98}
99
100fn test_swap_i64_concurrent() {
101 mut x := i64(0)
102 mut threads := []thread{}
103
104 for _ in 0 .. 8 {
105 threads << spawn fn (px &i64) {
106 for _ in 0 .. 50_000 {
107 swap_i64(px, 12345)
108 }
109 }(&x)
110 }
111
112 for t in threads {
113 t.wait()
114 }
115
116 assert x == 12345
117}
118
119fn test_cas_i64_concurrent_inc() {
120 mut x := i64(0)
121 mut threads := []thread{}
122
123 for _ in 0 .. 8 {
124 threads << spawn fn (px &i64) {
125 for _ in 0 .. 50_000 {
126 for {
127 old := load_i64(px)
128 if cas_i64(px, old, old + 1) {
129 break
130 }
131 }
132 }
133 }(&x)
134 }
135
136 for t in threads {
137 t.wait()
138 }
139
140 assert x == 400_000
141}
142
143fn test_cas_i64_contended_flip() {
144 mut x := i64(0)
145 mut threads := []thread{}
146
147 for _ in 0 .. 4 {
148 threads << spawn fn (px &i64) {
149 for _ in 0 .. 200_000 {
150 cas_i64(px, 0, 1)
151 cas_i64(px, 1, 0)
152 }
153 }(&x)
154 }
155
156 for t in threads {
157 t.wait()
158 }
159
160 assert x == 0 || x == 1
161}
162
163fn test_and_i64_concurrent() {
164 mut x := i64(0x7fffffffffffffff)
165 mut threads := []thread{}
166 for _ in 0 .. 8 {
167 threads << spawn fn (px &i64) {
168 for _ in 0 .. 100_000 {
169 and_i64(px, 0x00ffffffffffffff)
170 }
171 }(&x)
172 }
173 for t in threads {
174 t.wait()
175 }
176 assert x == 0x00ffffffffffffff
177}
178
179fn test_or_i64_concurrent() {
180 mut x := i64(0)
181 mut threads := []thread{}
182 for _ in 0 .. 8 {
183 threads << spawn fn (px &i64) {
184 for _ in 0 .. 100_000 {
185 or_i64(px, 0x0123456789abcdef)
186 }
187 }(&x)
188 }
189 for t in threads {
190 t.wait()
191 }
192 assert x == 0x0123456789abcdef
193}
194