libio: Update internal fmemopen position after write (BZ #20005)
[glibc.git] / csu / tst-atomic.c
blobf7dcac159da4a98a34928543a063ab8c72ef025c
1 /* Tests for atomic.h macros.
2 Copyright (C) 2003-2016 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, expected;
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 = 2;
117 if (atomic_exchange_and_add_acq (&mem, 11) != 2
118 || mem != 13)
120 puts ("atomic_exchange_and_add test failed");
121 ret = 1;
124 mem = 2;
125 if (atomic_exchange_and_add_rel (&mem, 11) != 2
126 || mem != 13)
128 puts ("atomic_exchange_and_add test failed");
129 ret = 1;
132 mem = -21;
133 atomic_add (&mem, 22);
134 if (mem != 1)
136 puts ("atomic_add test failed");
137 ret = 1;
140 mem = -1;
141 atomic_increment (&mem);
142 if (mem != 0)
144 puts ("atomic_increment test failed");
145 ret = 1;
148 mem = 2;
149 if (atomic_increment_val (&mem) != 3)
151 puts ("atomic_increment_val test failed");
152 ret = 1;
155 mem = 0;
156 if (atomic_increment_and_test (&mem)
157 || mem != 1)
159 puts ("atomic_increment_and_test test 1 failed");
160 ret = 1;
163 mem = 35;
164 if (atomic_increment_and_test (&mem)
165 || mem != 36)
167 puts ("atomic_increment_and_test test 2 failed");
168 ret = 1;
171 mem = -1;
172 if (! atomic_increment_and_test (&mem)
173 || mem != 0)
175 puts ("atomic_increment_and_test test 3 failed");
176 ret = 1;
179 mem = 17;
180 atomic_decrement (&mem);
181 if (mem != 16)
183 puts ("atomic_decrement test failed");
184 ret = 1;
187 if (atomic_decrement_val (&mem) != 15)
189 puts ("atomic_decrement_val test failed");
190 ret = 1;
193 mem = 0;
194 if (atomic_decrement_and_test (&mem)
195 || mem != -1)
197 puts ("atomic_decrement_and_test test 1 failed");
198 ret = 1;
201 mem = 15;
202 if (atomic_decrement_and_test (&mem)
203 || mem != 14)
205 puts ("atomic_decrement_and_test test 2 failed");
206 ret = 1;
209 mem = 1;
210 if (! atomic_decrement_and_test (&mem)
211 || mem != 0)
213 puts ("atomic_decrement_and_test test 3 failed");
214 ret = 1;
217 mem = 1;
218 if (atomic_decrement_if_positive (&mem) != 1
219 || mem != 0)
221 puts ("atomic_decrement_if_positive test 1 failed");
222 ret = 1;
225 mem = 0;
226 if (atomic_decrement_if_positive (&mem) != 0
227 || mem != 0)
229 puts ("atomic_decrement_if_positive test 2 failed");
230 ret = 1;
233 mem = -1;
234 if (atomic_decrement_if_positive (&mem) != -1
235 || mem != -1)
237 puts ("atomic_decrement_if_positive test 3 failed");
238 ret = 1;
241 mem = -12;
242 if (! atomic_add_negative (&mem, 10)
243 || mem != -2)
245 puts ("atomic_add_negative test 1 failed");
246 ret = 1;
249 mem = 0;
250 if (atomic_add_negative (&mem, 100)
251 || mem != 100)
253 puts ("atomic_add_negative test 2 failed");
254 ret = 1;
257 mem = 15;
258 if (atomic_add_negative (&mem, -10)
259 || mem != 5)
261 puts ("atomic_add_negative test 3 failed");
262 ret = 1;
265 mem = -12;
266 if (atomic_add_negative (&mem, 14)
267 || mem != 2)
269 puts ("atomic_add_negative test 4 failed");
270 ret = 1;
273 mem = 0;
274 if (! atomic_add_negative (&mem, -1)
275 || mem != -1)
277 puts ("atomic_add_negative test 5 failed");
278 ret = 1;
281 mem = -31;
282 if (atomic_add_negative (&mem, 31)
283 || mem != 0)
285 puts ("atomic_add_negative test 6 failed");
286 ret = 1;
289 mem = -34;
290 if (atomic_add_zero (&mem, 31)
291 || mem != -3)
293 puts ("atomic_add_zero test 1 failed");
294 ret = 1;
297 mem = -36;
298 if (! atomic_add_zero (&mem, 36)
299 || mem != 0)
301 puts ("atomic_add_zero test 2 failed");
302 ret = 1;
305 mem = 113;
306 if (atomic_add_zero (&mem, -13)
307 || mem != 100)
309 puts ("atomic_add_zero test 3 failed");
310 ret = 1;
313 mem = -18;
314 if (atomic_add_zero (&mem, 20)
315 || mem != 2)
317 puts ("atomic_add_zero test 4 failed");
318 ret = 1;
321 mem = 10;
322 if (atomic_add_zero (&mem, -20)
323 || mem != -10)
325 puts ("atomic_add_zero test 5 failed");
326 ret = 1;
329 mem = 10;
330 if (! atomic_add_zero (&mem, -10)
331 || mem != 0)
333 puts ("atomic_add_zero test 6 failed");
334 ret = 1;
337 mem = 0;
338 atomic_bit_set (&mem, 1);
339 if (mem != 2)
341 puts ("atomic_bit_set test 1 failed");
342 ret = 1;
345 mem = 8;
346 atomic_bit_set (&mem, 3);
347 if (mem != 8)
349 puts ("atomic_bit_set test 2 failed");
350 ret = 1;
353 #ifdef TEST_ATOMIC64
354 mem = 16;
355 atomic_bit_set (&mem, 35);
356 if (mem != 0x800000010LL)
358 puts ("atomic_bit_set test 3 failed");
359 ret = 1;
361 #endif
363 mem = 0;
364 if (atomic_bit_test_set (&mem, 1)
365 || mem != 2)
367 puts ("atomic_bit_test_set test 1 failed");
368 ret = 1;
371 mem = 8;
372 if (! atomic_bit_test_set (&mem, 3)
373 || mem != 8)
375 puts ("atomic_bit_test_set test 2 failed");
376 ret = 1;
379 #ifdef TEST_ATOMIC64
380 mem = 16;
381 if (atomic_bit_test_set (&mem, 35)
382 || mem != 0x800000010LL)
384 puts ("atomic_bit_test_set test 3 failed");
385 ret = 1;
388 mem = 0x100000000LL;
389 if (! atomic_bit_test_set (&mem, 32)
390 || mem != 0x100000000LL)
392 puts ("atomic_bit_test_set test 4 failed");
393 ret = 1;
395 #endif
397 #ifdef catomic_compare_and_exchange_val_acq
398 mem = 24;
399 if (catomic_compare_and_exchange_val_acq (&mem, 35, 24) != 24
400 || mem != 35)
402 puts ("catomic_compare_and_exchange_val_acq test 1 failed");
403 ret = 1;
406 mem = 12;
407 if (catomic_compare_and_exchange_val_acq (&mem, 10, 15) != 12
408 || mem != 12)
410 puts ("catomic_compare_and_exchange_val_acq test 2 failed");
411 ret = 1;
414 mem = -15;
415 if (catomic_compare_and_exchange_val_acq (&mem, -56, -15) != -15
416 || mem != -56)
418 puts ("catomic_compare_and_exchange_val_acq test 3 failed");
419 ret = 1;
422 mem = -1;
423 if (catomic_compare_and_exchange_val_acq (&mem, 17, 0) != -1
424 || mem != -1)
426 puts ("catomic_compare_and_exchange_val_acq test 4 failed");
427 ret = 1;
429 #endif
431 mem = 24;
432 if (catomic_compare_and_exchange_bool_acq (&mem, 35, 24)
433 || mem != 35)
435 puts ("catomic_compare_and_exchange_bool_acq test 1 failed");
436 ret = 1;
439 mem = 12;
440 if (! catomic_compare_and_exchange_bool_acq (&mem, 10, 15)
441 || mem != 12)
443 puts ("catomic_compare_and_exchange_bool_acq test 2 failed");
444 ret = 1;
447 mem = -15;
448 if (catomic_compare_and_exchange_bool_acq (&mem, -56, -15)
449 || mem != -56)
451 puts ("catomic_compare_and_exchange_bool_acq test 3 failed");
452 ret = 1;
455 mem = -1;
456 if (! catomic_compare_and_exchange_bool_acq (&mem, 17, 0)
457 || mem != -1)
459 puts ("catomic_compare_and_exchange_bool_acq test 4 failed");
460 ret = 1;
463 mem = 2;
464 if (catomic_exchange_and_add (&mem, 11) != 2
465 || mem != 13)
467 puts ("catomic_exchange_and_add test failed");
468 ret = 1;
471 mem = -21;
472 catomic_add (&mem, 22);
473 if (mem != 1)
475 puts ("catomic_add test failed");
476 ret = 1;
479 mem = -1;
480 catomic_increment (&mem);
481 if (mem != 0)
483 puts ("catomic_increment test failed");
484 ret = 1;
487 mem = 2;
488 if (catomic_increment_val (&mem) != 3)
490 puts ("catomic_increment_val test failed");
491 ret = 1;
494 mem = 17;
495 catomic_decrement (&mem);
496 if (mem != 16)
498 puts ("catomic_decrement test failed");
499 ret = 1;
502 if (catomic_decrement_val (&mem) != 15)
504 puts ("catomic_decrement_val test failed");
505 ret = 1;
508 /* Tests for C11-like atomics. */
509 mem = 11;
510 if (atomic_load_relaxed (&mem) != 11 || atomic_load_acquire (&mem) != 11)
512 puts ("atomic_load_{relaxed,acquire} test failed");
513 ret = 1;
516 atomic_store_relaxed (&mem, 12);
517 if (mem != 12)
519 puts ("atomic_store_relaxed test failed");
520 ret = 1;
522 atomic_store_release (&mem, 13);
523 if (mem != 13)
525 puts ("atomic_store_release test failed");
526 ret = 1;
529 mem = 14;
530 expected = 14;
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");
535 ret = 1;
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");
541 ret = 1;
543 mem = 14;
544 expected = 14;
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");
549 ret = 1;
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");
555 ret = 1;
557 mem = 14;
558 expected = 14;
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");
563 ret = 1;
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");
569 ret = 1;
572 mem = 23;
573 if (atomic_exchange_acquire (&mem, 42) != 23 || mem != 42)
575 puts ("atomic_exchange_acquire test failed");
576 ret = 1;
578 mem = 23;
579 if (atomic_exchange_release (&mem, 42) != 23 || mem != 42)
581 puts ("atomic_exchange_release test failed");
582 ret = 1;
585 mem = 23;
586 if (atomic_fetch_add_relaxed (&mem, 1) != 23 || mem != 24)
588 puts ("atomic_fetch_add_relaxed test failed");
589 ret = 1;
591 mem = 23;
592 if (atomic_fetch_add_acquire (&mem, 1) != 23 || mem != 24)
594 puts ("atomic_fetch_add_acquire test failed");
595 ret = 1;
597 mem = 23;
598 if (atomic_fetch_add_release (&mem, 1) != 23 || mem != 24)
600 puts ("atomic_fetch_add_release test failed");
601 ret = 1;
603 mem = 23;
604 if (atomic_fetch_add_acq_rel (&mem, 1) != 23 || mem != 24)
606 puts ("atomic_fetch_add_acq_rel test failed");
607 ret = 1;
610 mem = 3;
611 if (atomic_fetch_and_acquire (&mem, 2) != 3 || mem != 2)
613 puts ("atomic_fetch_and_acquire test failed");
614 ret = 1;
617 mem = 4;
618 if (atomic_fetch_or_relaxed (&mem, 2) != 4 || mem != 6)
620 puts ("atomic_fetch_or_relaxed test failed");
621 ret = 1;
623 mem = 4;
624 if (atomic_fetch_or_acquire (&mem, 2) != 4 || mem != 6)
626 puts ("atomic_fetch_or_acquire test failed");
627 ret = 1;
630 /* This is a single-threaded test, so we can't test the effects of the
631 fences. */
632 atomic_thread_fence_acquire ();
633 atomic_thread_fence_release ();
634 atomic_thread_fence_seq_cst ();
636 return ret;
639 #define TEST_FUNCTION do_test ()
640 #include "../test-skeleton.c"