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 | ||
cac31bf0 MD |
12 | #define BYTE_PER_LONG (sizeof(unsigned long) / sizeof(unsigned char)) |
13 | #define SHORT_PER_LONG (sizeof(unsigned long) / sizeof(unsigned short)) | |
14 | #define INT_PER_LONG (sizeof(unsigned long) / sizeof(unsigned int)) | |
15 | ||
a81b8e5e | 16 | struct testvals { |
95bc7fb9 | 17 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
cac31bf0 | 18 | unsigned char c[BYTE_PER_LONG]; |
95bc7fb9 MD |
19 | #endif |
20 | #ifdef UATOMIC_HAS_ATOMIC_SHORT | |
cac31bf0 | 21 | unsigned short s[SHORT_PER_LONG]; |
95bc7fb9 | 22 | #endif |
cac31bf0 | 23 | unsigned int i[INT_PER_LONG]; |
a81b8e5e MD |
24 | unsigned long l; |
25 | }; | |
26 | ||
27 | static struct testvals vals; | |
28 | ||
87322fe8 MD |
29 | #define do_test(ptr) \ |
30 | do { \ | |
6edb297e | 31 | __typeof__(*(ptr)) v; \ |
87322fe8 | 32 | \ |
ec4e58a3 | 33 | uatomic_add(ptr, 10); \ |
1b387491 MJ |
34 | ok1(uatomic_read(ptr) == 10); \ |
35 | \ | |
36 | uatomic_add(ptr, -11UL); \ | |
37 | ok1(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ | |
38 | \ | |
39 | v = uatomic_cmpxchg(ptr, -1UL, 22); \ | |
40 | ok1(uatomic_read(ptr) == 22); \ | |
41 | ok1(v == (__typeof__(*(ptr)))-1UL); \ | |
42 | \ | |
43 | v = uatomic_cmpxchg(ptr, 33, 44); \ | |
44 | ok1(uatomic_read(ptr) == 22); \ | |
45 | ok1(v == 22); \ | |
46 | \ | |
47 | v = uatomic_xchg(ptr, 55); \ | |
48 | ok1(uatomic_read(ptr) == 55); \ | |
49 | ok1(v == 22); \ | |
50 | \ | |
ec4e58a3 MD |
51 | uatomic_set(ptr, 22); \ |
52 | uatomic_inc(ptr); \ | |
1b387491 MJ |
53 | ok1(uatomic_read(ptr) == 23); \ |
54 | \ | |
ec4e58a3 | 55 | uatomic_dec(ptr); \ |
1b387491 MJ |
56 | ok1(uatomic_read(ptr) == 22); \ |
57 | \ | |
985b35b1 | 58 | v = uatomic_add_return(ptr, 74); \ |
1b387491 MJ |
59 | ok1(v == 96); \ |
60 | ok1(uatomic_read(ptr) == 96); \ | |
61 | \ | |
985b35b1 | 62 | uatomic_or(ptr, 58); \ |
1b387491 MJ |
63 | ok1(uatomic_read(ptr) == 122); \ |
64 | \ | |
ec4e58a3 | 65 | v = uatomic_sub_return(ptr, 1); \ |
1b387491 MJ |
66 | ok1(v == 121); \ |
67 | \ | |
e56d99bf | 68 | uatomic_sub(ptr, (unsigned int) 2); \ |
1b387491 MJ |
69 | ok1(uatomic_read(ptr) == 119); \ |
70 | \ | |
e56d99bf MD |
71 | uatomic_inc(ptr); \ |
72 | uatomic_inc(ptr); \ | |
1b387491 MJ |
73 | ok1(uatomic_read(ptr) == 121); \ |
74 | \ | |
bf33aaea | 75 | uatomic_and(ptr, 129); \ |
1b387491 MJ |
76 | ok1(uatomic_read(ptr) == 1); \ |
77 | \ | |
87322fe8 MD |
78 | } while (0) |
79 | ||
70469b43 | 80 | int main(void) |
a81b8e5e | 81 | { |
cac31bf0 MD |
82 | int nr_run = INT_PER_LONG + 1; |
83 | unsigned long i; | |
84 | ||
1b387491 | 85 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
cac31bf0 | 86 | nr_run += BYTE_PER_LONG; |
1b387491 MJ |
87 | #endif |
88 | #ifdef UATOMIC_HAS_ATOMIC_SHORT | |
cac31bf0 | 89 | nr_run += SHORT_PER_LONG; |
1b387491 MJ |
90 | #endif |
91 | ||
92 | plan_tests(nr_run * NR_TESTS); | |
f469d839 | 93 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
cac31bf0 MD |
94 | for (i = 0; i < BYTE_PER_LONG; i++) { |
95 | diag("Test atomic ops on byte with %lu byte offset from long alignment", | |
96 | i); | |
97 | do_test(&vals.c[i]); | |
98 | } | |
4d78cb54 | 99 | #endif |
f469d839 | 100 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
cac31bf0 MD |
101 | for (i = 0; i < SHORT_PER_LONG; i++) { |
102 | diag("Test atomic ops on short with %lu byte offset from long alignment", | |
103 | i * sizeof(unsigned short)); | |
104 | do_test(&vals.s[i]); | |
105 | } | |
4d78cb54 | 106 | #endif |
cac31bf0 MD |
107 | for (i = 0; i < INT_PER_LONG; i++) { |
108 | diag("Test atomic ops on int with %lu byte offset from long alignment", | |
109 | i * sizeof(unsigned int)); | |
110 | do_test(&vals.i[i]); | |
111 | } | |
1b387491 | 112 | diag("Test atomic ops on long"); |
87322fe8 | 113 | do_test(&vals.l); |
87322fe8 | 114 | |
1b387491 | 115 | return exit_status(); |
a81b8e5e | 116 | } |