test_atomic: test for byte/short atomic support
[urcu.git] / tests / test_atomic.c
CommitLineData
a81b8e5e
MD
1#include <stdio.h>
2#include <arch_atomic.h>
3#include <assert.h>
4
4d78cb54
MD
5#if (defined(__i386__) || defined(__x86_64__))
6#define HAS_ATOMIC_BYTE
7#define HAS_ATOMIC_SHORT
8#endif
9
a81b8e5e 10struct testvals {
4d78cb54 11#ifdef HAS_ATOMIC_BYTE
a81b8e5e 12 unsigned char c;
4d78cb54
MD
13#endif
14#ifdef HAS_ATOMIC_SHORT
a81b8e5e 15 unsigned short s;
4d78cb54 16#endif
a81b8e5e
MD
17 unsigned int i;
18 unsigned long l;
19};
20
21static struct testvals vals;
22
87322fe8
MD
23#define do_test(ptr) \
24do { \
6edb297e 25 __typeof__(*(ptr)) v; \
87322fe8
MD
26 \
27 atomic_add(ptr, 10); \
0fad128b 28 assert(atomic_read(ptr) == 10); \
6edb297e 29 atomic_add(ptr, -11UL); \
0fad128b 30 assert(atomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \
6edb297e 31 v = cmpxchg(ptr, -1UL, 22); \
0fad128b 32 assert(atomic_read(ptr) == 22); \
6edb297e 33 assert(v == (__typeof__(*(ptr)))-1UL); \
87322fe8 34 v = cmpxchg(ptr, 33, 44); \
0fad128b 35 assert(atomic_read(ptr) == 22); \
87322fe8
MD
36 assert(v == 22); \
37 v = xchg(ptr, 55); \
0fad128b 38 assert(atomic_read(ptr) == 55); \
87322fe8 39 assert(v == 22); \
0fad128b
MD
40 atomic_set(ptr, 22); \
41 atomic_inc(ptr); \
42 assert(atomic_read(ptr) == 23); \
43 atomic_dec(ptr); \
44 assert(atomic_read(ptr) == 22); \
45 v = atomic_add_return(ptr, 100); \
46 assert(v == 122); \
47 assert(atomic_read(ptr) == 122); \
48 v = atomic_sub_return(ptr, 1); \
49 assert(v == 121); \
50 assert(atomic_read(ptr) == 121); \
87322fe8
MD
51} while (0)
52
53int main(int argc, char **argv)
a81b8e5e 54{
4d78cb54 55#ifdef HAS_ATOMIC_BYTE
87322fe8 56 do_test(&vals.c);
4d78cb54
MD
57#endif
58#ifdef HAS_ATOMIC_SHORT
87322fe8 59 do_test(&vals.s);
4d78cb54 60#endif
87322fe8
MD
61 do_test(&vals.i);
62 do_test(&vals.l);
63 printf("Atomic ops test OK\n");
64
65 return 0;
a81b8e5e 66}
This page took 0.034487 seconds and 4 git commands to generate.