add uatomic_generic.h, use it for common definitions
[urcu.git] / tests / test_uatomic.c
1 #include <stdio.h>
2 #include <assert.h>
3
4 #define UATOMIC_NO_LINK_ERROR
5 #include <urcu/uatomic_arch.h>
6
7 #if (defined(__i386__) || defined(__x86_64__))
8 #define HAS_ATOMIC_BYTE
9 #define HAS_ATOMIC_SHORT
10 #endif
11
12 struct testvals {
13 #ifdef HAS_ATOMIC_BYTE
14 unsigned char c;
15 #endif
16 #ifdef 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 assert(uatomic_read(ptr) == 10); \
31 uatomic_add(ptr, -11UL); \
32 assert(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \
33 v = uatomic_cmpxchg(ptr, -1UL, 22); \
34 assert(uatomic_read(ptr) == 22); \
35 assert(v == (__typeof__(*(ptr)))-1UL); \
36 v = uatomic_cmpxchg(ptr, 33, 44); \
37 assert(uatomic_read(ptr) == 22); \
38 assert(v == 22); \
39 v = uatomic_xchg(ptr, 55); \
40 assert(uatomic_read(ptr) == 55); \
41 assert(v == 22); \
42 uatomic_set(ptr, 22); \
43 uatomic_inc(ptr); \
44 assert(uatomic_read(ptr) == 23); \
45 uatomic_dec(ptr); \
46 assert(uatomic_read(ptr) == 22); \
47 v = uatomic_add_return(ptr, 100); \
48 assert(v == 122); \
49 assert(uatomic_read(ptr) == 122); \
50 v = uatomic_sub_return(ptr, 1); \
51 assert(v == 121); \
52 assert(uatomic_read(ptr) == 121); \
53 } while (0)
54
55 int main(int argc, char **argv)
56 {
57 #ifdef HAS_ATOMIC_BYTE
58 do_test(&vals.c);
59 #endif
60 #ifdef HAS_ATOMIC_SHORT
61 do_test(&vals.s);
62 #endif
63 do_test(&vals.i);
64 do_test(&vals.l);
65 printf("Atomic ops test OK\n");
66
67 return 0;
68 }
This page took 0.043851 seconds and 4 git commands to generate.