Commit | Line | Data |
---|---|---|
ce29b371 MJ |
1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: GPL-2.0-or-later | |
3282a76b | 4 | |
a81b8e5e | 5 | #include <stdio.h> |
a2e7bf9c | 6 | #include <urcu/uatomic.h> |
a81b8e5e | 7 | |
1b387491 MJ |
8 | #include "tap.h" |
9 | ||
10 | #define NR_TESTS 17 | |
11 | ||
a81b8e5e | 12 | struct testvals { |
95bc7fb9 | 13 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
a81b8e5e | 14 | unsigned char c; |
95bc7fb9 MD |
15 | #endif |
16 | #ifdef UATOMIC_HAS_ATOMIC_SHORT | |
a81b8e5e | 17 | unsigned short s; |
95bc7fb9 | 18 | #endif |
a81b8e5e MD |
19 | unsigned int i; |
20 | unsigned long l; | |
21 | }; | |
22 | ||
23 | static struct testvals vals; | |
24 | ||
87322fe8 MD |
25 | #define do_test(ptr) \ |
26 | do { \ | |
6edb297e | 27 | __typeof__(*(ptr)) v; \ |
87322fe8 | 28 | \ |
ec4e58a3 | 29 | uatomic_add(ptr, 10); \ |
1b387491 MJ |
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 | \ | |
ec4e58a3 MD |
47 | uatomic_set(ptr, 22); \ |
48 | uatomic_inc(ptr); \ | |
1b387491 MJ |
49 | ok1(uatomic_read(ptr) == 23); \ |
50 | \ | |
ec4e58a3 | 51 | uatomic_dec(ptr); \ |
1b387491 MJ |
52 | ok1(uatomic_read(ptr) == 22); \ |
53 | \ | |
985b35b1 | 54 | v = uatomic_add_return(ptr, 74); \ |
1b387491 MJ |
55 | ok1(v == 96); \ |
56 | ok1(uatomic_read(ptr) == 96); \ | |
57 | \ | |
985b35b1 | 58 | uatomic_or(ptr, 58); \ |
1b387491 MJ |
59 | ok1(uatomic_read(ptr) == 122); \ |
60 | \ | |
ec4e58a3 | 61 | v = uatomic_sub_return(ptr, 1); \ |
1b387491 MJ |
62 | ok1(v == 121); \ |
63 | \ | |
e56d99bf | 64 | uatomic_sub(ptr, (unsigned int) 2); \ |
1b387491 MJ |
65 | ok1(uatomic_read(ptr) == 119); \ |
66 | \ | |
e56d99bf MD |
67 | uatomic_inc(ptr); \ |
68 | uatomic_inc(ptr); \ | |
1b387491 MJ |
69 | ok1(uatomic_read(ptr) == 121); \ |
70 | \ | |
bf33aaea | 71 | uatomic_and(ptr, 129); \ |
1b387491 MJ |
72 | ok1(uatomic_read(ptr) == 1); \ |
73 | \ | |
87322fe8 MD |
74 | } while (0) |
75 | ||
70469b43 | 76 | int main(void) |
a81b8e5e | 77 | { |
1b387491 MJ |
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); | |
f469d839 | 87 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
1b387491 | 88 | diag("Test atomic ops on byte"); |
87322fe8 | 89 | do_test(&vals.c); |
4d78cb54 | 90 | #endif |
f469d839 | 91 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
1b387491 | 92 | diag("Test atomic ops on short"); |
87322fe8 | 93 | do_test(&vals.s); |
4d78cb54 | 94 | #endif |
1b387491 | 95 | diag("Test atomic ops on int"); |
87322fe8 | 96 | do_test(&vals.i); |
1b387491 | 97 | diag("Test atomic ops on long"); |
87322fe8 | 98 | do_test(&vals.l); |
87322fe8 | 99 | |
1b387491 | 100 | return exit_status(); |
a81b8e5e | 101 | } |