Add HAS_RTM
[glibc.git] / csu / tst-atomic.c
blobda4ee4f26707ac560b650e2ecf7dbd0e8ed51cf4
1 /* Tests for atomic.h macros.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <atomic.h>
23 #ifndef atomic_t
24 # define atomic_t int
25 #endif
27 /* Test various atomic.h macros. */
28 static int
29 do_test (void)
31 atomic_t mem;
32 int ret = 0;
34 #ifdef atomic_compare_and_exchange_val_acq
35 mem = 24;
36 if (atomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
37 || mem != 35)
39 puts ("atomic_compare_and_exchange_val_acq test 1 failed");
40 ret = 1;
43 mem = 12;
44 if (atomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
45 || mem != 12)
47 puts ("atomic_compare_and_exchange_val_acq test 2 failed");
48 ret = 1;
51 mem = -15;
52 if (atomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
53 || mem != -56)
55 puts ("atomic_compare_and_exchange_val_acq test 3 failed");
56 ret = 1;
59 mem = -1;
60 if (atomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
61 || mem != -1)
63 puts ("atomic_compare_and_exchange_val_acq test 4 failed");
64 ret = 1;
66 #endif
68 mem = 24;
69 if (atomic_compare_and_exchange_bool_acq (&mem, 35, 24)
70 || mem != 35)
72 puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
73 ret = 1;
76 mem = 12;
77 if (! atomic_compare_and_exchange_bool_acq (&mem, 10, 15)
78 || mem != 12)
80 puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
81 ret = 1;
84 mem = -15;
85 if (atomic_compare_and_exchange_bool_acq (&mem, -56, -15)
86 || mem != -56)
88 puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
89 ret = 1;
92 mem = -1;
93 if (! atomic_compare_and_exchange_bool_acq (&mem, 17, 0)
94 || mem != -1)
96 puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
97 ret = 1;
100 mem = 64;
101 if (atomic_exchange_acq (&mem, 31) != 64
102 || mem != 31)
104 puts ("atomic_exchange_acq test failed");
105 ret = 1;
108 mem = 2;
109 if (atomic_exchange_and_add (&mem, 11) != 2
110 || mem != 13)
112 puts ("atomic_exchange_and_add test failed");
113 ret = 1;
116 mem = -21;
117 atomic_add (&mem, 22);
118 if (mem != 1)
120 puts ("atomic_add test failed");
121 ret = 1;
124 mem = -1;
125 atomic_increment (&mem);
126 if (mem != 0)
128 puts ("atomic_increment test failed");
129 ret = 1;
132 mem = 2;
133 if (atomic_increment_val (&mem) != 3)
135 puts ("atomic_increment_val test failed");
136 ret = 1;
139 mem = 0;
140 if (atomic_increment_and_test (&mem)
141 || mem != 1)
143 puts ("atomic_increment_and_test test 1 failed");
144 ret = 1;
147 mem = 35;
148 if (atomic_increment_and_test (&mem)
149 || mem != 36)
151 puts ("atomic_increment_and_test test 2 failed");
152 ret = 1;
155 mem = -1;
156 if (! atomic_increment_and_test (&mem)
157 || mem != 0)
159 puts ("atomic_increment_and_test test 3 failed");
160 ret = 1;
163 mem = 17;
164 atomic_decrement (&mem);
165 if (mem != 16)
167 puts ("atomic_decrement test failed");
168 ret = 1;
171 if (atomic_decrement_val (&mem) != 15)
173 puts ("atomic_decrement_val test failed");
174 ret = 1;
177 mem = 0;
178 if (atomic_decrement_and_test (&mem)
179 || mem != -1)
181 puts ("atomic_decrement_and_test test 1 failed");
182 ret = 1;
185 mem = 15;
186 if (atomic_decrement_and_test (&mem)
187 || mem != 14)
189 puts ("atomic_decrement_and_test test 2 failed");
190 ret = 1;
193 mem = 1;
194 if (! atomic_decrement_and_test (&mem)
195 || mem != 0)
197 puts ("atomic_decrement_and_test test 3 failed");
198 ret = 1;
201 mem = 1;
202 if (atomic_decrement_if_positive (&mem) != 1
203 || mem != 0)
205 puts ("atomic_decrement_if_positive test 1 failed");
206 ret = 1;
209 mem = 0;
210 if (atomic_decrement_if_positive (&mem) != 0
211 || mem != 0)
213 puts ("atomic_decrement_if_positive test 2 failed");
214 ret = 1;
217 mem = -1;
218 if (atomic_decrement_if_positive (&mem) != -1
219 || mem != -1)
221 puts ("atomic_decrement_if_positive test 3 failed");
222 ret = 1;
225 mem = -12;
226 if (! atomic_add_negative (&mem, 10)
227 || mem != -2)
229 puts ("atomic_add_negative test 1 failed");
230 ret = 1;
233 mem = 0;
234 if (atomic_add_negative (&mem, 100)
235 || mem != 100)
237 puts ("atomic_add_negative test 2 failed");
238 ret = 1;
241 mem = 15;
242 if (atomic_add_negative (&mem, -10)
243 || mem != 5)
245 puts ("atomic_add_negative test 3 failed");
246 ret = 1;
249 mem = -12;
250 if (atomic_add_negative (&mem, 14)
251 || mem != 2)
253 puts ("atomic_add_negative test 4 failed");
254 ret = 1;
257 mem = 0;
258 if (! atomic_add_negative (&mem, -1)
259 || mem != -1)
261 puts ("atomic_add_negative test 5 failed");
262 ret = 1;
265 mem = -31;
266 if (atomic_add_negative (&mem, 31)
267 || mem != 0)
269 puts ("atomic_add_negative test 6 failed");
270 ret = 1;
273 mem = -34;
274 if (atomic_add_zero (&mem, 31)
275 || mem != -3)
277 puts ("atomic_add_zero test 1 failed");
278 ret = 1;
281 mem = -36;
282 if (! atomic_add_zero (&mem, 36)
283 || mem != 0)
285 puts ("atomic_add_zero test 2 failed");
286 ret = 1;
289 mem = 113;
290 if (atomic_add_zero (&mem, -13)
291 || mem != 100)
293 puts ("atomic_add_zero test 3 failed");
294 ret = 1;
297 mem = -18;
298 if (atomic_add_zero (&mem, 20)
299 || mem != 2)
301 puts ("atomic_add_zero test 4 failed");
302 ret = 1;
305 mem = 10;
306 if (atomic_add_zero (&mem, -20)
307 || mem != -10)
309 puts ("atomic_add_zero test 5 failed");
310 ret = 1;
313 mem = 10;
314 if (! atomic_add_zero (&mem, -10)
315 || mem != 0)
317 puts ("atomic_add_zero test 6 failed");
318 ret = 1;
321 mem = 0;
322 atomic_bit_set (&mem, 1);
323 if (mem != 2)
325 puts ("atomic_bit_set test 1 failed");
326 ret = 1;
329 mem = 8;
330 atomic_bit_set (&mem, 3);
331 if (mem != 8)
333 puts ("atomic_bit_set test 2 failed");
334 ret = 1;
337 #ifdef TEST_ATOMIC64
338 mem = 16;
339 atomic_bit_set (&mem, 35);
340 if (mem != 0x800000010LL)
342 puts ("atomic_bit_set test 3 failed");
343 ret = 1;
345 #endif
347 mem = 0;
348 if (atomic_bit_test_set (&mem, 1)
349 || mem != 2)
351 puts ("atomic_bit_test_set test 1 failed");
352 ret = 1;
355 mem = 8;
356 if (! atomic_bit_test_set (&mem, 3)
357 || mem != 8)
359 puts ("atomic_bit_test_set test 2 failed");
360 ret = 1;
363 #ifdef TEST_ATOMIC64
364 mem = 16;
365 if (atomic_bit_test_set (&mem, 35)
366 || mem != 0x800000010LL)
368 puts ("atomic_bit_test_set test 3 failed");
369 ret = 1;
372 mem = 0x100000000LL;
373 if (! atomic_bit_test_set (&mem, 32)
374 || mem != 0x100000000LL)
376 puts ("atomic_bit_test_set test 4 failed");
377 ret = 1;
379 #endif
381 #ifdef catomic_compare_and_exchange_val_acq
382 mem = 24;
383 if (catomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
384 || mem != 35)
386 puts ("catomic_compare_and_exchange_val_acq test 1 failed");
387 ret = 1;
390 mem = 12;
391 if (catomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
392 || mem != 12)
394 puts ("catomic_compare_and_exchange_val_acq test 2 failed");
395 ret = 1;
398 mem = -15;
399 if (catomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
400 || mem != -56)
402 puts ("catomic_compare_and_exchange_val_acq test 3 failed");
403 ret = 1;
406 mem = -1;
407 if (catomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
408 || mem != -1)
410 puts ("catomic_compare_and_exchange_val_acq test 4 failed");
411 ret = 1;
413 #endif
415 mem = 24;
416 if (catomic_compare_and_exchange_bool_acq (&mem, 35, 24)
417 || mem != 35)
419 puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
420 ret = 1;
423 mem = 12;
424 if (! catomic_compare_and_exchange_bool_acq (&mem, 10, 15)
425 || mem != 12)
427 puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
428 ret = 1;
431 mem = -15;
432 if (catomic_compare_and_exchange_bool_acq (&mem, -56, -15)
433 || mem != -56)
435 puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
436 ret = 1;
439 mem = -1;
440 if (! catomic_compare_and_exchange_bool_acq (&mem, 17, 0)
441 || mem != -1)
443 puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
444 ret = 1;
447 mem = 2;
448 if (catomic_exchange_and_add (&mem, 11) != 2
449 || mem != 13)
451 puts ("catomic_exchange_and_add test failed");
452 ret = 1;
455 mem = -21;
456 catomic_add (&mem, 22);
457 if (mem != 1)
459 puts ("catomic_add test failed");
460 ret = 1;
463 mem = -1;
464 catomic_increment (&mem);
465 if (mem != 0)
467 puts ("catomic_increment test failed");
468 ret = 1;
471 mem = 2;
472 if (catomic_increment_val (&mem) != 3)
474 puts ("catomic_increment_val test failed");
475 ret = 1;
478 mem = 17;
479 catomic_decrement (&mem);
480 if (mem != 16)
482 puts ("catomic_decrement test failed");
483 ret = 1;
486 if (catomic_decrement_val (&mem) != 15)
488 puts ("catomic_decrement_val test failed");
489 ret = 1;
492 return ret;
495 #define TEST_FUNCTION do_test ()
496 #include "../test-skeleton.c"