Commit | Line | Data |
---|---|---|
3282a76b MD |
1 | /* |
2 | * test_uatomic.c | |
3 | * | |
4 | * Userspace RCU library - test atomic operations | |
5 | * | |
6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
a81b8e5e | 23 | #include <stdio.h> |
a2e7bf9c | 24 | #include <urcu/uatomic.h> |
a81b8e5e | 25 | |
1b387491 MJ |
26 | #include "tap.h" |
27 | ||
28 | #define NR_TESTS 17 | |
29 | ||
a81b8e5e | 30 | struct testvals { |
95bc7fb9 | 31 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
a81b8e5e | 32 | unsigned char c; |
95bc7fb9 MD |
33 | #endif |
34 | #ifdef UATOMIC_HAS_ATOMIC_SHORT | |
a81b8e5e | 35 | unsigned short s; |
95bc7fb9 | 36 | #endif |
a81b8e5e MD |
37 | unsigned int i; |
38 | unsigned long l; | |
39 | }; | |
40 | ||
41 | static struct testvals vals; | |
42 | ||
87322fe8 MD |
43 | #define do_test(ptr) \ |
44 | do { \ | |
6edb297e | 45 | __typeof__(*(ptr)) v; \ |
87322fe8 | 46 | \ |
ec4e58a3 | 47 | uatomic_add(ptr, 10); \ |
1b387491 MJ |
48 | ok1(uatomic_read(ptr) == 10); \ |
49 | \ | |
50 | uatomic_add(ptr, -11UL); \ | |
51 | ok1(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ | |
52 | \ | |
53 | v = uatomic_cmpxchg(ptr, -1UL, 22); \ | |
54 | ok1(uatomic_read(ptr) == 22); \ | |
55 | ok1(v == (__typeof__(*(ptr)))-1UL); \ | |
56 | \ | |
57 | v = uatomic_cmpxchg(ptr, 33, 44); \ | |
58 | ok1(uatomic_read(ptr) == 22); \ | |
59 | ok1(v == 22); \ | |
60 | \ | |
61 | v = uatomic_xchg(ptr, 55); \ | |
62 | ok1(uatomic_read(ptr) == 55); \ | |
63 | ok1(v == 22); \ | |
64 | \ | |
ec4e58a3 MD |
65 | uatomic_set(ptr, 22); \ |
66 | uatomic_inc(ptr); \ | |
1b387491 MJ |
67 | ok1(uatomic_read(ptr) == 23); \ |
68 | \ | |
ec4e58a3 | 69 | uatomic_dec(ptr); \ |
1b387491 MJ |
70 | ok1(uatomic_read(ptr) == 22); \ |
71 | \ | |
985b35b1 | 72 | v = uatomic_add_return(ptr, 74); \ |
1b387491 MJ |
73 | ok1(v == 96); \ |
74 | ok1(uatomic_read(ptr) == 96); \ | |
75 | \ | |
985b35b1 | 76 | uatomic_or(ptr, 58); \ |
1b387491 MJ |
77 | ok1(uatomic_read(ptr) == 122); \ |
78 | \ | |
ec4e58a3 | 79 | v = uatomic_sub_return(ptr, 1); \ |
1b387491 MJ |
80 | ok1(v == 121); \ |
81 | \ | |
e56d99bf | 82 | uatomic_sub(ptr, (unsigned int) 2); \ |
1b387491 MJ |
83 | ok1(uatomic_read(ptr) == 119); \ |
84 | \ | |
e56d99bf MD |
85 | uatomic_inc(ptr); \ |
86 | uatomic_inc(ptr); \ | |
1b387491 MJ |
87 | ok1(uatomic_read(ptr) == 121); \ |
88 | \ | |
bf33aaea | 89 | uatomic_and(ptr, 129); \ |
1b387491 MJ |
90 | ok1(uatomic_read(ptr) == 1); \ |
91 | \ | |
87322fe8 MD |
92 | } while (0) |
93 | ||
70469b43 | 94 | int main(void) |
a81b8e5e | 95 | { |
1b387491 MJ |
96 | int nr_run = 2; |
97 | #ifdef UATOMIC_HAS_ATOMIC_BYTE | |
98 | nr_run += 1; | |
99 | #endif | |
100 | #ifdef UATOMIC_HAS_ATOMIC_SHORT | |
101 | nr_run += 1; | |
102 | #endif | |
103 | ||
104 | plan_tests(nr_run * NR_TESTS); | |
f469d839 | 105 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
1b387491 | 106 | diag("Test atomic ops on byte"); |
87322fe8 | 107 | do_test(&vals.c); |
4d78cb54 | 108 | #endif |
f469d839 | 109 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
1b387491 | 110 | diag("Test atomic ops on short"); |
87322fe8 | 111 | do_test(&vals.s); |
4d78cb54 | 112 | #endif |
1b387491 | 113 | diag("Test atomic ops on int"); |
87322fe8 | 114 | do_test(&vals.i); |
1b387491 | 115 | diag("Test atomic ops on long"); |
87322fe8 | 116 | do_test(&vals.l); |
87322fe8 | 117 | |
1b387491 | 118 | return exit_status(); |
a81b8e5e | 119 | } |