add uatomic_generic.h, use it for common definitions
[urcu.git] / tests / test_uatomic.c
CommitLineData
a81b8e5e 1#include <stdio.h>
a81b8e5e 2#include <assert.h>
8760d94e
PB
3
4#define UATOMIC_NO_LINK_ERROR
48d848c7 5#include <urcu/uatomic_arch.h>
a81b8e5e 6
4d78cb54
MD
7#if (defined(__i386__) || defined(__x86_64__))
8#define HAS_ATOMIC_BYTE
9#define HAS_ATOMIC_SHORT
10#endif
11
a81b8e5e 12struct testvals {
4d78cb54 13#ifdef HAS_ATOMIC_BYTE
a81b8e5e 14 unsigned char c;
4d78cb54
MD
15#endif
16#ifdef HAS_ATOMIC_SHORT
a81b8e5e 17 unsigned short s;
4d78cb54 18#endif
a81b8e5e
MD
19 unsigned int i;
20 unsigned long l;
21};
22
23static struct testvals vals;
24
87322fe8
MD
25#define do_test(ptr) \
26do { \
6edb297e 27 __typeof__(*(ptr)) v; \
87322fe8 28 \
ec4e58a3
MD
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); \
6edb297e 35 assert(v == (__typeof__(*(ptr)))-1UL); \
ec4e58a3
MD
36 v = uatomic_cmpxchg(ptr, 33, 44); \
37 assert(uatomic_read(ptr) == 22); \
87322fe8 38 assert(v == 22); \
ec4e58a3
MD
39 v = uatomic_xchg(ptr, 55); \
40 assert(uatomic_read(ptr) == 55); \
87322fe8 41 assert(v == 22); \
ec4e58a3
MD
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); \
0fad128b 48 assert(v == 122); \
ec4e58a3
MD
49 assert(uatomic_read(ptr) == 122); \
50 v = uatomic_sub_return(ptr, 1); \
0fad128b 51 assert(v == 121); \
ec4e58a3 52 assert(uatomic_read(ptr) == 121); \
87322fe8
MD
53} while (0)
54
55int main(int argc, char **argv)
a81b8e5e 56{
4d78cb54 57#ifdef HAS_ATOMIC_BYTE
87322fe8 58 do_test(&vals.c);
4d78cb54
MD
59#endif
60#ifdef HAS_ATOMIC_SHORT
87322fe8 61 do_test(&vals.s);
4d78cb54 62#endif
87322fe8
MD
63 do_test(&vals.i);
64 do_test(&vals.l);
65 printf("Atomic ops test OK\n");
66
67 return 0;
a81b8e5e 68}
This page took 0.037086 seconds and 4 git commands to generate.