1 /* Tests for atomic.h macros.
2 Copyright (C) 2003-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 /* Test various atomic.h macros. */
30 atomic_t mem
, expected
;
33 #ifdef atomic_compare_and_exchange_val_acq
35 if (atomic_compare_and_exchange_val_acq (&mem
, 35, 24) != 24
38 puts ("atomic_compare_and_exchange_val_acq test 1 failed");
43 if (atomic_compare_and_exchange_val_acq (&mem
, 10, 15) != 12
46 puts ("atomic_compare_and_exchange_val_acq test 2 failed");
51 if (atomic_compare_and_exchange_val_acq (&mem
, -56, -15) != -15
54 puts ("atomic_compare_and_exchange_val_acq test 3 failed");
59 if (atomic_compare_and_exchange_val_acq (&mem
, 17, 0) != -1
62 puts ("atomic_compare_and_exchange_val_acq test 4 failed");
68 if (atomic_compare_and_exchange_bool_acq (&mem
, 35, 24)
71 puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
76 if (! atomic_compare_and_exchange_bool_acq (&mem
, 10, 15)
79 puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
84 if (atomic_compare_and_exchange_bool_acq (&mem
, -56, -15)
87 puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
92 if (! atomic_compare_and_exchange_bool_acq (&mem
, 17, 0)
95 puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
100 if (atomic_exchange_acq (&mem
, 31) != 64
103 puts ("atomic_exchange_acq test failed");
108 if (atomic_exchange_and_add (&mem
, 11) != 2
111 puts ("atomic_exchange_and_add test failed");
116 if (atomic_exchange_and_add_acq (&mem
, 11) != 2
119 puts ("atomic_exchange_and_add test failed");
124 if (atomic_exchange_and_add_rel (&mem
, 11) != 2
127 puts ("atomic_exchange_and_add test failed");
132 atomic_add (&mem
, 22);
135 puts ("atomic_add test failed");
140 atomic_increment (&mem
);
143 puts ("atomic_increment test failed");
148 if (atomic_increment_val (&mem
) != 3)
150 puts ("atomic_increment_val test failed");
155 if (atomic_increment_and_test (&mem
)
158 puts ("atomic_increment_and_test test 1 failed");
163 if (atomic_increment_and_test (&mem
)
166 puts ("atomic_increment_and_test test 2 failed");
171 if (! atomic_increment_and_test (&mem
)
174 puts ("atomic_increment_and_test test 3 failed");
179 atomic_decrement (&mem
);
182 puts ("atomic_decrement test failed");
186 if (atomic_decrement_val (&mem
) != 15)
188 puts ("atomic_decrement_val test failed");
193 if (atomic_decrement_and_test (&mem
)
196 puts ("atomic_decrement_and_test test 1 failed");
201 if (atomic_decrement_and_test (&mem
)
204 puts ("atomic_decrement_and_test test 2 failed");
209 if (! atomic_decrement_and_test (&mem
)
212 puts ("atomic_decrement_and_test test 3 failed");
217 if (atomic_decrement_if_positive (&mem
) != 1
220 puts ("atomic_decrement_if_positive test 1 failed");
225 if (atomic_decrement_if_positive (&mem
) != 0
228 puts ("atomic_decrement_if_positive test 2 failed");
233 if (atomic_decrement_if_positive (&mem
) != -1
236 puts ("atomic_decrement_if_positive test 3 failed");
241 if (! atomic_add_negative (&mem
, 10)
244 puts ("atomic_add_negative test 1 failed");
249 if (atomic_add_negative (&mem
, 100)
252 puts ("atomic_add_negative test 2 failed");
257 if (atomic_add_negative (&mem
, -10)
260 puts ("atomic_add_negative test 3 failed");
265 if (atomic_add_negative (&mem
, 14)
268 puts ("atomic_add_negative test 4 failed");
273 if (! atomic_add_negative (&mem
, -1)
276 puts ("atomic_add_negative test 5 failed");
281 if (atomic_add_negative (&mem
, 31)
284 puts ("atomic_add_negative test 6 failed");
289 if (atomic_add_zero (&mem
, 31)
292 puts ("atomic_add_zero test 1 failed");
297 if (! atomic_add_zero (&mem
, 36)
300 puts ("atomic_add_zero test 2 failed");
305 if (atomic_add_zero (&mem
, -13)
308 puts ("atomic_add_zero test 3 failed");
313 if (atomic_add_zero (&mem
, 20)
316 puts ("atomic_add_zero test 4 failed");
321 if (atomic_add_zero (&mem
, -20)
324 puts ("atomic_add_zero test 5 failed");
329 if (! atomic_add_zero (&mem
, -10)
332 puts ("atomic_add_zero test 6 failed");
337 atomic_bit_set (&mem
, 1);
340 puts ("atomic_bit_set test 1 failed");
345 atomic_bit_set (&mem
, 3);
348 puts ("atomic_bit_set test 2 failed");
354 atomic_bit_set (&mem
, 35);
355 if (mem
!= 0x800000010LL
)
357 puts ("atomic_bit_set test 3 failed");
363 if (atomic_bit_test_set (&mem
, 1)
366 puts ("atomic_bit_test_set test 1 failed");
371 if (! atomic_bit_test_set (&mem
, 3)
374 puts ("atomic_bit_test_set test 2 failed");
380 if (atomic_bit_test_set (&mem
, 35)
381 || mem
!= 0x800000010LL
)
383 puts ("atomic_bit_test_set test 3 failed");
388 if (! atomic_bit_test_set (&mem
, 32)
389 || mem
!= 0x100000000LL
)
391 puts ("atomic_bit_test_set test 4 failed");
396 #ifdef catomic_compare_and_exchange_val_acq
398 if (catomic_compare_and_exchange_val_acq (&mem
, 35, 24) != 24
401 puts ("catomic_compare_and_exchange_val_acq test 1 failed");
406 if (catomic_compare_and_exchange_val_acq (&mem
, 10, 15) != 12
409 puts ("catomic_compare_and_exchange_val_acq test 2 failed");
414 if (catomic_compare_and_exchange_val_acq (&mem
, -56, -15) != -15
417 puts ("catomic_compare_and_exchange_val_acq test 3 failed");
422 if (catomic_compare_and_exchange_val_acq (&mem
, 17, 0) != -1
425 puts ("catomic_compare_and_exchange_val_acq test 4 failed");
431 if (catomic_compare_and_exchange_bool_acq (&mem
, 35, 24)
434 puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
439 if (! catomic_compare_and_exchange_bool_acq (&mem
, 10, 15)
442 puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
447 if (catomic_compare_and_exchange_bool_acq (&mem
, -56, -15)
450 puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
455 if (! catomic_compare_and_exchange_bool_acq (&mem
, 17, 0)
458 puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
463 if (catomic_exchange_and_add (&mem
, 11) != 2
466 puts ("catomic_exchange_and_add test failed");
471 catomic_add (&mem
, 22);
474 puts ("catomic_add test failed");
479 catomic_increment (&mem
);
482 puts ("catomic_increment test failed");
487 if (catomic_increment_val (&mem
) != 3)
489 puts ("catomic_increment_val test failed");
494 catomic_decrement (&mem
);
497 puts ("catomic_decrement test failed");
501 if (catomic_decrement_val (&mem
) != 15)
503 puts ("catomic_decrement_val test failed");
507 /* Tests for C11-like atomics. */
509 if (atomic_load_relaxed (&mem
) != 11 || atomic_load_acquire (&mem
) != 11)
511 puts ("atomic_load_{relaxed,acquire} test failed");
515 atomic_store_relaxed (&mem
, 12);
518 puts ("atomic_store_relaxed test failed");
521 atomic_store_release (&mem
, 13);
524 puts ("atomic_store_release test failed");
530 if (!atomic_compare_exchange_weak_relaxed (&mem
, &expected
, 25)
531 || mem
!= 25 || expected
!= 14)
533 puts ("atomic_compare_exchange_weak_relaxed test 1 failed");
536 if (atomic_compare_exchange_weak_relaxed (&mem
, &expected
, 14)
537 || mem
!= 25 || expected
!= 25)
539 puts ("atomic_compare_exchange_weak_relaxed test 2 failed");
544 if (!atomic_compare_exchange_weak_acquire (&mem
, &expected
, 25)
545 || mem
!= 25 || expected
!= 14)
547 puts ("atomic_compare_exchange_weak_acquire test 1 failed");
550 if (atomic_compare_exchange_weak_acquire (&mem
, &expected
, 14)
551 || mem
!= 25 || expected
!= 25)
553 puts ("atomic_compare_exchange_weak_acquire test 2 failed");
558 if (!atomic_compare_exchange_weak_release (&mem
, &expected
, 25)
559 || mem
!= 25 || expected
!= 14)
561 puts ("atomic_compare_exchange_weak_release test 1 failed");
564 if (atomic_compare_exchange_weak_release (&mem
, &expected
, 14)
565 || mem
!= 25 || expected
!= 25)
567 puts ("atomic_compare_exchange_weak_release test 2 failed");
572 if (atomic_exchange_acquire (&mem
, 42) != 23 || mem
!= 42)
574 puts ("atomic_exchange_acquire test failed");
578 if (atomic_exchange_release (&mem
, 42) != 23 || mem
!= 42)
580 puts ("atomic_exchange_release test failed");
585 if (atomic_fetch_add_relaxed (&mem
, 1) != 23 || mem
!= 24)
587 puts ("atomic_fetch_add_relaxed test failed");
591 if (atomic_fetch_add_acquire (&mem
, 1) != 23 || mem
!= 24)
593 puts ("atomic_fetch_add_acquire test failed");
597 if (atomic_fetch_add_release (&mem
, 1) != 23 || mem
!= 24)
599 puts ("atomic_fetch_add_release test failed");
603 if (atomic_fetch_add_acq_rel (&mem
, 1) != 23 || mem
!= 24)
605 puts ("atomic_fetch_add_acq_rel test failed");
610 if (atomic_fetch_and_acquire (&mem
, 2) != 3 || mem
!= 2)
612 puts ("atomic_fetch_and_acquire test failed");
617 if (atomic_fetch_or_relaxed (&mem
, 2) != 4 || mem
!= 6)
619 puts ("atomic_fetch_or_relaxed test failed");
623 if (atomic_fetch_or_acquire (&mem
, 2) != 4 || mem
!= 6)
625 puts ("atomic_fetch_or_acquire test failed");
629 /* This is a single-threaded test, so we can't test the effects of the
631 atomic_thread_fence_acquire ();
632 atomic_thread_fence_release ();
633 atomic_thread_fence_seq_cst ();
638 #include <support/test-driver.c>