1 | /* Tests for atomic.h macros.
|
---|
2 | Copyright (C) 2003, 2004 Free Software Foundation, Inc.
|
---|
3 | This file is part of the GNU C Library.
|
---|
4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
|
---|
5 |
|
---|
6 | The GNU C Library is free software; you can redistribute it and/or
|
---|
7 | modify it under the terms of the GNU Lesser General Public
|
---|
8 | License as published by the Free Software Foundation; either
|
---|
9 | version 2.1 of the License, or (at your option) any later version.
|
---|
10 |
|
---|
11 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU Lesser General Public
|
---|
17 | License along with the GNU C Library; if not, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <atomic.h>
|
---|
23 |
|
---|
24 | #ifndef atomic_t
|
---|
25 | # define atomic_t int
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | /* Test various atomic.h macros. */
|
---|
29 | static int
|
---|
30 | do_test (void)
|
---|
31 | {
|
---|
32 | atomic_t mem;
|
---|
33 | int ret = 0;
|
---|
34 |
|
---|
35 | #ifdef atomic_compare_and_exchange_val_acq
|
---|
36 | mem = 24;
|
---|
37 | if (atomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
|
---|
38 | || mem != 35)
|
---|
39 | {
|
---|
40 | puts ("atomic_compare_and_exchange_val_acq test 1 failed");
|
---|
41 | ret = 1;
|
---|
42 | }
|
---|
43 |
|
---|
44 | mem = 12;
|
---|
45 | if (atomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
|
---|
46 | || mem != 12)
|
---|
47 | {
|
---|
48 | puts ("atomic_compare_and_exchange_val_acq test 2 failed");
|
---|
49 | ret = 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | mem = -15;
|
---|
53 | if (atomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
|
---|
54 | || mem != -56)
|
---|
55 | {
|
---|
56 | puts ("atomic_compare_and_exchange_val_acq test 3 failed");
|
---|
57 | ret = 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | mem = -1;
|
---|
61 | if (atomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
|
---|
62 | || mem != -1)
|
---|
63 | {
|
---|
64 | puts ("atomic_compare_and_exchange_val_acq test 4 failed");
|
---|
65 | ret = 1;
|
---|
66 | }
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | mem = 24;
|
---|
70 | if (atomic_compare_and_exchange_bool_acq (&mem, 35, 24)
|
---|
71 | || mem != 35)
|
---|
72 | {
|
---|
73 | puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
|
---|
74 | ret = 1;
|
---|
75 | }
|
---|
76 |
|
---|
77 | mem = 12;
|
---|
78 | if (! atomic_compare_and_exchange_bool_acq (&mem, 10, 15)
|
---|
79 | || mem != 12)
|
---|
80 | {
|
---|
81 | puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
|
---|
82 | ret = 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | mem = -15;
|
---|
86 | if (atomic_compare_and_exchange_bool_acq (&mem, -56, -15)
|
---|
87 | || mem != -56)
|
---|
88 | {
|
---|
89 | puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
|
---|
90 | ret = 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | mem = -1;
|
---|
94 | if (! atomic_compare_and_exchange_bool_acq (&mem, 17, 0)
|
---|
95 | || mem != -1)
|
---|
96 | {
|
---|
97 | puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
|
---|
98 | ret = 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | mem = 64;
|
---|
102 | if (atomic_exchange_acq (&mem, 31) != 64
|
---|
103 | || mem != 31)
|
---|
104 | {
|
---|
105 | puts ("atomic_exchange_acq test failed");
|
---|
106 | ret = 1;
|
---|
107 | }
|
---|
108 |
|
---|
109 | mem = 2;
|
---|
110 | if (atomic_exchange_and_add (&mem, 11) != 2
|
---|
111 | || mem != 13)
|
---|
112 | {
|
---|
113 | puts ("atomic_exchange_and_add test failed");
|
---|
114 | ret = 1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | mem = -21;
|
---|
118 | atomic_add (&mem, 22);
|
---|
119 | if (mem != 1)
|
---|
120 | {
|
---|
121 | puts ("atomic_add test failed");
|
---|
122 | ret = 1;
|
---|
123 | }
|
---|
124 |
|
---|
125 | mem = -1;
|
---|
126 | atomic_increment (&mem);
|
---|
127 | if (mem != 0)
|
---|
128 | {
|
---|
129 | puts ("atomic_increment test failed");
|
---|
130 | ret = 1;
|
---|
131 | }
|
---|
132 |
|
---|
133 | mem = 2;
|
---|
134 | if (atomic_increment_val (&mem) != 3)
|
---|
135 | {
|
---|
136 | puts ("atomic_increment_val test failed");
|
---|
137 | ret = 1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | mem = 0;
|
---|
141 | if (atomic_increment_and_test (&mem)
|
---|
142 | || mem != 1)
|
---|
143 | {
|
---|
144 | puts ("atomic_increment_and_test test 1 failed");
|
---|
145 | ret = 1;
|
---|
146 | }
|
---|
147 |
|
---|
148 | mem = 35;
|
---|
149 | if (atomic_increment_and_test (&mem)
|
---|
150 | || mem != 36)
|
---|
151 | {
|
---|
152 | puts ("atomic_increment_and_test test 2 failed");
|
---|
153 | ret = 1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | mem = -1;
|
---|
157 | if (! atomic_increment_and_test (&mem)
|
---|
158 | || mem != 0)
|
---|
159 | {
|
---|
160 | puts ("atomic_increment_and_test test 3 failed");
|
---|
161 | ret = 1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | mem = 17;
|
---|
165 | atomic_decrement (&mem);
|
---|
166 | if (mem != 16)
|
---|
167 | {
|
---|
168 | puts ("atomic_decrement test failed");
|
---|
169 | ret = 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (atomic_decrement_val (&mem) != 15)
|
---|
173 | {
|
---|
174 | puts ("atomic_decrement_val test failed");
|
---|
175 | ret = 1;
|
---|
176 | }
|
---|
177 |
|
---|
178 | mem = 0;
|
---|
179 | if (atomic_decrement_and_test (&mem)
|
---|
180 | || mem != -1)
|
---|
181 | {
|
---|
182 | puts ("atomic_decrement_and_test test 1 failed");
|
---|
183 | ret = 1;
|
---|
184 | }
|
---|
185 |
|
---|
186 | mem = 15;
|
---|
187 | if (atomic_decrement_and_test (&mem)
|
---|
188 | || mem != 14)
|
---|
189 | {
|
---|
190 | puts ("atomic_decrement_and_test test 2 failed");
|
---|
191 | ret = 1;
|
---|
192 | }
|
---|
193 |
|
---|
194 | mem = 1;
|
---|
195 | if (! atomic_decrement_and_test (&mem)
|
---|
196 | || mem != 0)
|
---|
197 | {
|
---|
198 | puts ("atomic_decrement_and_test test 3 failed");
|
---|
199 | ret = 1;
|
---|
200 | }
|
---|
201 |
|
---|
202 | mem = 1;
|
---|
203 | if (atomic_decrement_if_positive (&mem) != 1
|
---|
204 | || mem != 0)
|
---|
205 | {
|
---|
206 | puts ("atomic_decrement_if_positive test 1 failed");
|
---|
207 | ret = 1;
|
---|
208 | }
|
---|
209 |
|
---|
210 | mem = 0;
|
---|
211 | if (atomic_decrement_if_positive (&mem) != 0
|
---|
212 | || mem != 0)
|
---|
213 | {
|
---|
214 | puts ("atomic_decrement_if_positive test 2 failed");
|
---|
215 | ret = 1;
|
---|
216 | }
|
---|
217 |
|
---|
218 | mem = -1;
|
---|
219 | if (atomic_decrement_if_positive (&mem) != -1
|
---|
220 | || mem != -1)
|
---|
221 | {
|
---|
222 | puts ("atomic_decrement_if_positive test 3 failed");
|
---|
223 | ret = 1;
|
---|
224 | }
|
---|
225 |
|
---|
226 | mem = -12;
|
---|
227 | if (! atomic_add_negative (&mem, 10)
|
---|
228 | || mem != -2)
|
---|
229 | {
|
---|
230 | puts ("atomic_add_negative test 1 failed");
|
---|
231 | ret = 1;
|
---|
232 | }
|
---|
233 |
|
---|
234 | mem = 0;
|
---|
235 | if (atomic_add_negative (&mem, 100)
|
---|
236 | || mem != 100)
|
---|
237 | {
|
---|
238 | puts ("atomic_add_negative test 2 failed");
|
---|
239 | ret = 1;
|
---|
240 | }
|
---|
241 |
|
---|
242 | mem = 15;
|
---|
243 | if (atomic_add_negative (&mem, -10)
|
---|
244 | || mem != 5)
|
---|
245 | {
|
---|
246 | puts ("atomic_add_negative test 3 failed");
|
---|
247 | ret = 1;
|
---|
248 | }
|
---|
249 |
|
---|
250 | mem = -12;
|
---|
251 | if (atomic_add_negative (&mem, 14)
|
---|
252 | || mem != 2)
|
---|
253 | {
|
---|
254 | puts ("atomic_add_negative test 4 failed");
|
---|
255 | ret = 1;
|
---|
256 | }
|
---|
257 |
|
---|
258 | mem = 0;
|
---|
259 | if (! atomic_add_negative (&mem, -1)
|
---|
260 | || mem != -1)
|
---|
261 | {
|
---|
262 | puts ("atomic_add_negative test 5 failed");
|
---|
263 | ret = 1;
|
---|
264 | }
|
---|
265 |
|
---|
266 | mem = -31;
|
---|
267 | if (atomic_add_negative (&mem, 31)
|
---|
268 | || mem != 0)
|
---|
269 | {
|
---|
270 | puts ("atomic_add_negative test 6 failed");
|
---|
271 | ret = 1;
|
---|
272 | }
|
---|
273 |
|
---|
274 | mem = -34;
|
---|
275 | if (atomic_add_zero (&mem, 31)
|
---|
276 | || mem != -3)
|
---|
277 | {
|
---|
278 | puts ("atomic_add_zero test 1 failed");
|
---|
279 | ret = 1;
|
---|
280 | }
|
---|
281 |
|
---|
282 | mem = -36;
|
---|
283 | if (! atomic_add_zero (&mem, 36)
|
---|
284 | || mem != 0)
|
---|
285 | {
|
---|
286 | puts ("atomic_add_zero test 2 failed");
|
---|
287 | ret = 1;
|
---|
288 | }
|
---|
289 |
|
---|
290 | mem = 113;
|
---|
291 | if (atomic_add_zero (&mem, -13)
|
---|
292 | || mem != 100)
|
---|
293 | {
|
---|
294 | puts ("atomic_add_zero test 3 failed");
|
---|
295 | ret = 1;
|
---|
296 | }
|
---|
297 |
|
---|
298 | mem = -18;
|
---|
299 | if (atomic_add_zero (&mem, 20)
|
---|
300 | || mem != 2)
|
---|
301 | {
|
---|
302 | puts ("atomic_add_zero test 4 failed");
|
---|
303 | ret = 1;
|
---|
304 | }
|
---|
305 |
|
---|
306 | mem = 10;
|
---|
307 | if (atomic_add_zero (&mem, -20)
|
---|
308 | || mem != -10)
|
---|
309 | {
|
---|
310 | puts ("atomic_add_zero test 5 failed");
|
---|
311 | ret = 1;
|
---|
312 | }
|
---|
313 |
|
---|
314 | mem = 10;
|
---|
315 | if (! atomic_add_zero (&mem, -10)
|
---|
316 | || mem != 0)
|
---|
317 | {
|
---|
318 | puts ("atomic_add_zero test 6 failed");
|
---|
319 | ret = 1;
|
---|
320 | }
|
---|
321 |
|
---|
322 | mem = 0;
|
---|
323 | atomic_bit_set (&mem, 1);
|
---|
324 | if (mem != 2)
|
---|
325 | {
|
---|
326 | puts ("atomic_bit_set test 1 failed");
|
---|
327 | ret = 1;
|
---|
328 | }
|
---|
329 |
|
---|
330 | mem = 8;
|
---|
331 | atomic_bit_set (&mem, 3);
|
---|
332 | if (mem != 8)
|
---|
333 | {
|
---|
334 | puts ("atomic_bit_set test 2 failed");
|
---|
335 | ret = 1;
|
---|
336 | }
|
---|
337 |
|
---|
338 | #ifdef TEST_ATOMIC64
|
---|
339 | mem = 16;
|
---|
340 | atomic_bit_set (&mem, 35);
|
---|
341 | if (mem != 0x800000010LL)
|
---|
342 | {
|
---|
343 | puts ("atomic_bit_set test 3 failed");
|
---|
344 | ret = 1;
|
---|
345 | }
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | mem = 0;
|
---|
349 | if (atomic_bit_test_set (&mem, 1)
|
---|
350 | || mem != 2)
|
---|
351 | {
|
---|
352 | puts ("atomic_bit_test_set test 1 failed");
|
---|
353 | ret = 1;
|
---|
354 | }
|
---|
355 |
|
---|
356 | mem = 8;
|
---|
357 | if (! atomic_bit_test_set (&mem, 3)
|
---|
358 | || mem != 8)
|
---|
359 | {
|
---|
360 | puts ("atomic_bit_test_set test 2 failed");
|
---|
361 | ret = 1;
|
---|
362 | }
|
---|
363 |
|
---|
364 | #ifdef TEST_ATOMIC64
|
---|
365 | mem = 16;
|
---|
366 | if (atomic_bit_test_set (&mem, 35)
|
---|
367 | || mem != 0x800000010LL)
|
---|
368 | {
|
---|
369 | puts ("atomic_bit_test_set test 3 failed");
|
---|
370 | ret = 1;
|
---|
371 | }
|
---|
372 |
|
---|
373 | mem = 0x100000000LL;
|
---|
374 | if (! atomic_bit_test_set (&mem, 32)
|
---|
375 | || mem != 0x100000000LL)
|
---|
376 | {
|
---|
377 | puts ("atomic_bit_test_set test 4 failed");
|
---|
378 | ret = 1;
|
---|
379 | }
|
---|
380 | #endif
|
---|
381 |
|
---|
382 | return ret;
|
---|
383 | }
|
---|
384 |
|
---|
385 | #define TEST_FUNCTION do_test ()
|
---|
386 | #include "../test-skeleton.c"
|
---|