| 1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <urcu/uatomic.h> |
| 7 | |
| 8 | #include "tap.h" |
| 9 | |
| 10 | #define NR_TESTS 17 |
| 11 | |
| 12 | struct testvals { |
| 13 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
| 14 | unsigned char c; |
| 15 | #endif |
| 16 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
| 17 | unsigned short s; |
| 18 | #endif |
| 19 | unsigned int i; |
| 20 | unsigned long l; |
| 21 | }; |
| 22 | |
| 23 | static struct testvals vals; |
| 24 | |
| 25 | #define do_test(ptr) \ |
| 26 | do { \ |
| 27 | __typeof__(*(ptr)) v; \ |
| 28 | \ |
| 29 | uatomic_add(ptr, 10); \ |
| 30 | ok1(uatomic_read(ptr) == 10); \ |
| 31 | \ |
| 32 | uatomic_add(ptr, -11UL); \ |
| 33 | ok1(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ |
| 34 | \ |
| 35 | v = uatomic_cmpxchg(ptr, -1UL, 22); \ |
| 36 | ok1(uatomic_read(ptr) == 22); \ |
| 37 | ok1(v == (__typeof__(*(ptr)))-1UL); \ |
| 38 | \ |
| 39 | v = uatomic_cmpxchg(ptr, 33, 44); \ |
| 40 | ok1(uatomic_read(ptr) == 22); \ |
| 41 | ok1(v == 22); \ |
| 42 | \ |
| 43 | v = uatomic_xchg(ptr, 55); \ |
| 44 | ok1(uatomic_read(ptr) == 55); \ |
| 45 | ok1(v == 22); \ |
| 46 | \ |
| 47 | uatomic_set(ptr, 22); \ |
| 48 | uatomic_inc(ptr); \ |
| 49 | ok1(uatomic_read(ptr) == 23); \ |
| 50 | \ |
| 51 | uatomic_dec(ptr); \ |
| 52 | ok1(uatomic_read(ptr) == 22); \ |
| 53 | \ |
| 54 | v = uatomic_add_return(ptr, 74); \ |
| 55 | ok1(v == 96); \ |
| 56 | ok1(uatomic_read(ptr) == 96); \ |
| 57 | \ |
| 58 | uatomic_or(ptr, 58); \ |
| 59 | ok1(uatomic_read(ptr) == 122); \ |
| 60 | \ |
| 61 | v = uatomic_sub_return(ptr, 1); \ |
| 62 | ok1(v == 121); \ |
| 63 | \ |
| 64 | uatomic_sub(ptr, (unsigned int) 2); \ |
| 65 | ok1(uatomic_read(ptr) == 119); \ |
| 66 | \ |
| 67 | uatomic_inc(ptr); \ |
| 68 | uatomic_inc(ptr); \ |
| 69 | ok1(uatomic_read(ptr) == 121); \ |
| 70 | \ |
| 71 | uatomic_and(ptr, 129); \ |
| 72 | ok1(uatomic_read(ptr) == 1); \ |
| 73 | \ |
| 74 | } while (0) |
| 75 | |
| 76 | int main(void) |
| 77 | { |
| 78 | int nr_run = 2; |
| 79 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
| 80 | nr_run += 1; |
| 81 | #endif |
| 82 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
| 83 | nr_run += 1; |
| 84 | #endif |
| 85 | |
| 86 | plan_tests(nr_run * NR_TESTS); |
| 87 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
| 88 | diag("Test atomic ops on byte"); |
| 89 | do_test(&vals.c); |
| 90 | #endif |
| 91 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
| 92 | diag("Test atomic ops on short"); |
| 93 | do_test(&vals.s); |
| 94 | #endif |
| 95 | diag("Test atomic ops on int"); |
| 96 | do_test(&vals.i); |
| 97 | diag("Test atomic ops on long"); |
| 98 | do_test(&vals.l); |
| 99 | |
| 100 | return exit_status(); |
| 101 | } |