1 /* Tests for atomic.h macros.
2 Copyright (C) 2003-2018 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/>. */
27 /* Test various atomic.h macros. */
31 atomic_t mem
, expected
;
34 #ifdef atomic_compare_and_exchange_val_acq
36 if (atomic_compare_and_exchange_val_acq (&mem
, 35, 24) != 24
39 puts ("atomic_compare_and_exchange_val_acq test 1 failed");
44 if (atomic_compare_and_exchange_val_acq (&mem
, 10, 15) != 12
47 puts ("atomic_compare_and_exchange_val_acq test 2 failed");
52 if (atomic_compare_and_exchange_val_acq (&mem
, -56, -15) != -15
55 puts ("atomic_compare_and_exchange_val_acq test 3 failed");
60 if (atomic_compare_and_exchange_val_acq (&mem
, 17, 0) != -1
63 puts ("atomic_compare_and_exchange_val_acq test 4 failed");
69 if (atomic_compare_and_exchange_bool_acq (&mem
, 35, 24)
72 puts ("atomic_compare_and_exchange_bool_acq test 1 failed");
77 if (! atomic_compare_and_exchange_bool_acq (&mem
, 10, 15)
80 puts ("atomic_compare_and_exchange_bool_acq test 2 failed");
85 if (atomic_compare_and_exchange_bool_acq (&mem
, -56, -15)
88 puts ("atomic_compare_and_exchange_bool_acq test 3 failed");
93 if (! atomic_compare_and_exchange_bool_acq (&mem
, 17, 0)
96 puts ("atomic_compare_and_exchange_bool_acq test 4 failed");
101 if (atomic_exchange_acq (&mem
, 31) != 64
104 puts ("atomic_exchange_acq test failed");
109 if (atomic_exchange_and_add (&mem
, 11) != 2
112 puts ("atomic_exchange_and_add test failed");
117 if (atomic_exchange_and_add_acq (&mem
, 11) != 2
120 puts ("atomic_exchange_and_add test failed");
125 if (atomic_exchange_and_add_rel (&mem
, 11) != 2
128 puts ("atomic_exchange_and_add test failed");
133 atomic_add (&mem
, 22);
136 puts ("atomic_add test failed");
141 atomic_increment (&mem
);
144 puts ("atomic_increment test failed");
149 if (atomic_increment_val (&mem
) != 3)
151 puts ("atomic_increment_val test failed");
156 if (atomic_increment_and_test (&mem
)
159 puts ("atomic_increment_and_test test 1 failed");
164 if (atomic_increment_and_test (&mem
)
167 puts ("atomic_increment_and_test test 2 failed");
172 if (! atomic_increment_and_test (&mem
)
175 puts ("atomic_increment_and_test test 3 failed");
180 atomic_decrement (&mem
);
183 puts ("atomic_decrement test failed");
187 if (atomic_decrement_val (&mem
) != 15)
189 puts ("atomic_decrement_val test failed");
194 if (atomic_decrement_and_test (&mem
)
197 puts ("atomic_decrement_and_test test 1 failed");
202 if (atomic_decrement_and_test (&mem
)
205 puts ("atomic_decrement_and_test test 2 failed");
210 if (! atomic_decrement_and_test (&mem
)
213 puts ("atomic_decrement_and_test test 3 failed");
218 if (atomic_decrement_if_positive (&mem
) != 1
221 puts ("atomic_decrement_if_positive test 1 failed");
226 if (atomic_decrement_if_positive (&mem
) != 0
229 puts ("atomic_decrement_if_positive test 2 failed");
234 if (atomic_decrement_if_positive (&mem
) != -1
237 puts ("atomic_decrement_if_positive test 3 failed");
242 if (! atomic_add_negative (&mem
, 10)
245 puts ("atomic_add_negative test 1 failed");
250 if (atomic_add_negative (&mem
, 100)
253 puts ("atomic_add_negative test 2 failed");
258 if (atomic_add_negative (&mem
, -10)
261 puts ("atomic_add_negative test 3 failed");
266 if (atomic_add_negative (&mem
, 14)
269 puts ("atomic_add_negative test 4 failed");
274 if (! atomic_add_negative (&mem
, -1)
277 puts ("atomic_add_negative test 5 failed");
282 if (atomic_add_negative (&mem
, 31)
285 puts ("atomic_add_negative test 6 failed");
290 if (atomic_add_zero (&mem
, 31)
293 puts ("atomic_add_zero test 1 failed");
298 if (! atomic_add_zero (&mem
, 36)
301 puts ("atomic_add_zero test 2 failed");
306 if (atomic_add_zero (&mem
, -13)
309 puts ("atomic_add_zero test 3 failed");
314 if (atomic_add_zero (&mem
, 20)
317 puts ("atomic_add_zero test 4 failed");
322 if (atomic_add_zero (&mem
, -20)
325 puts ("atomic_add_zero test 5 failed");
330 if (! atomic_add_zero (&mem
, -10)
333 puts ("atomic_add_zero test 6 failed");
338 atomic_bit_set (&mem
, 1);
341 puts ("atomic_bit_set test 1 failed");
346 atomic_bit_set (&mem
, 3);
349 puts ("atomic_bit_set test 2 failed");
355 atomic_bit_set (&mem
, 35);
356 if (mem
!= 0x800000010LL
)
358 puts ("atomic_bit_set test 3 failed");
364 if (atomic_bit_test_set (&mem
, 1)
367 puts ("atomic_bit_test_set test 1 failed");
372 if (! atomic_bit_test_set (&mem
, 3)
375 puts ("atomic_bit_test_set test 2 failed");
381 if (atomic_bit_test_set (&mem
, 35)
382 || mem
!= 0x800000010LL
)
384 puts ("atomic_bit_test_set test 3 failed");
389 if (! atomic_bit_test_set (&mem
, 32)
390 || mem
!= 0x100000000LL
)
392 puts ("atomic_bit_test_set test 4 failed");
397 #ifdef catomic_compare_and_exchange_val_acq
399 if (catomic_compare_and_exchange_val_acq (&mem
, 35, 24) != 24
402 puts ("catomic_compare_and_exchange_val_acq test 1 failed");
407 if (catomic_compare_and_exchange_val_acq (&mem
, 10, 15) != 12
410 puts ("catomic_compare_and_exchange_val_acq test 2 failed");
415 if (catomic_compare_and_exchange_val_acq (&mem
, -56, -15) != -15
418 puts ("catomic_compare_and_exchange_val_acq test 3 failed");
423 if (catomic_compare_and_exchange_val_acq (&mem
, 17, 0) != -1
426 puts ("catomic_compare_and_exchange_val_acq test 4 failed");
432 if (catomic_compare_and_exchange_bool_acq (&mem
, 35, 24)
435 puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
440 if (! catomic_compare_and_exchange_bool_acq (&mem
, 10, 15)
443 puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
448 if (catomic_compare_and_exchange_bool_acq (&mem
, -56, -15)
451 puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
456 if (! catomic_compare_and_exchange_bool_acq (&mem
, 17, 0)
459 puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
464 if (catomic_exchange_and_add (&mem
, 11) != 2
467 puts ("catomic_exchange_and_add test failed");
472 catomic_add (&mem
, 22);
475 puts ("catomic_add test failed");
480 catomic_increment (&mem
);
483 puts ("catomic_increment test failed");
488 if (catomic_increment_val (&mem
) != 3)
490 puts ("catomic_increment_val test failed");
495 catomic_decrement (&mem
);
498 puts ("catomic_decrement test failed");
502 if (catomic_decrement_val (&mem
) != 15)
504 puts ("catomic_decrement_val test failed");
508 /* Tests for C11-like atomics. */
510 if (atomic_load_relaxed (&mem
) != 11 || atomic_load_acquire (&mem
) != 11)
512 puts ("atomic_load_{relaxed,acquire} test failed");
516 atomic_store_relaxed (&mem
, 12);
519 puts ("atomic_store_relaxed test failed");
522 atomic_store_release (&mem
, 13);
525 puts ("atomic_store_release test failed");
531 if (!atomic_compare_exchange_weak_relaxed (&mem
, &expected
, 25)
532 || mem
!= 25 || expected
!= 14)
534 puts ("atomic_compare_exchange_weak_relaxed test 1 failed");
537 if (atomic_compare_exchange_weak_relaxed (&mem
, &expected
, 14)
538 || mem
!= 25 || expected
!= 25)
540 puts ("atomic_compare_exchange_weak_relaxed test 2 failed");
545 if (!atomic_compare_exchange_weak_acquire (&mem
, &expected
, 25)
546 || mem
!= 25 || expected
!= 14)
548 puts ("atomic_compare_exchange_weak_acquire test 1 failed");
551 if (atomic_compare_exchange_weak_acquire (&mem
, &expected
, 14)
552 || mem
!= 25 || expected
!= 25)
554 puts ("atomic_compare_exchange_weak_acquire test 2 failed");
559 if (!atomic_compare_exchange_weak_release (&mem
, &expected
, 25)
560 || mem
!= 25 || expected
!= 14)
562 puts ("atomic_compare_exchange_weak_release test 1 failed");
565 if (atomic_compare_exchange_weak_release (&mem
, &expected
, 14)
566 || mem
!= 25 || expected
!= 25)
568 puts ("atomic_compare_exchange_weak_release test 2 failed");
573 if (atomic_exchange_acquire (&mem
, 42) != 23 || mem
!= 42)
575 puts ("atomic_exchange_acquire test failed");
579 if (atomic_exchange_release (&mem
, 42) != 23 || mem
!= 42)
581 puts ("atomic_exchange_release test failed");
586 if (atomic_fetch_add_relaxed (&mem
, 1) != 23 || mem
!= 24)
588 puts ("atomic_fetch_add_relaxed test failed");
592 if (atomic_fetch_add_acquire (&mem
, 1) != 23 || mem
!= 24)
594 puts ("atomic_fetch_add_acquire test failed");
598 if (atomic_fetch_add_release (&mem
, 1) != 23 || mem
!= 24)
600 puts ("atomic_fetch_add_release test failed");
604 if (atomic_fetch_add_acq_rel (&mem
, 1) != 23 || mem
!= 24)
606 puts ("atomic_fetch_add_acq_rel test failed");
611 if (atomic_fetch_and_acquire (&mem
, 2) != 3 || mem
!= 2)
613 puts ("atomic_fetch_and_acquire test failed");
618 if (atomic_fetch_or_relaxed (&mem
, 2) != 4 || mem
!= 6)
620 puts ("atomic_fetch_or_relaxed test failed");
624 if (atomic_fetch_or_acquire (&mem
, 2) != 4 || mem
!= 6)
626 puts ("atomic_fetch_or_acquire test failed");
630 /* This is a single-threaded test, so we can't test the effects of the
632 atomic_thread_fence_acquire ();
633 atomic_thread_fence_release ();
634 atomic_thread_fence_seq_cst ();
639 #include <support/test-driver.c>