util/u_atomic: Add a simple test.
[mesa.git] / src / util / u_atomic_test.c
1 /**************************************************************************
2 *
3 * Copyright 2014 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /* Force assertions, even on debug builds. */
30 #undef NDEBUG
31
32
33 #include <stdint.h>
34 #include <inttypes.h>
35 #include <assert.h>
36
37 #include "u_atomic.h"
38
39
40 #define test_atomic_cmpxchg(type, ones) \
41 static void test_atomic_cmpxchg_##type (void) { \
42 type v, r; \
43 \
44 p_atomic_set(&v, ones); \
45 assert(v == ones && "p_atomic_set"); \
46 \
47 r = p_atomic_read(&v); \
48 assert(r == ones && "p_atomic_read"); \
49 \
50 v = ones; \
51 r = p_atomic_cmpxchg(&v, 0, 1); \
52 assert(v == ones && "p_atomic_cmpxchg"); \
53 assert(r == ones && "p_atomic_cmpxchg"); \
54 r = p_atomic_cmpxchg(&v, ones, 0); \
55 assert(v == 0 && "p_atomic_cmpxchg"); \
56 assert(r == ones && "p_atomic_cmpxchg"); \
57 \
58 (void) r; \
59 }
60
61
62 #define test_atomic(type, ones) \
63 test_atomic_cmpxchg(type, ones) \
64 \
65 static void test_atomic_##type (void) { \
66 type v, r; \
67 bool b; \
68 \
69 test_atomic_cmpxchg_##type(); \
70 \
71 v = 2; \
72 b = p_atomic_dec_zero(&v); \
73 assert(v == 1 && "p_atomic_dec_zero"); \
74 assert(b == false && "p_atomic_dec_zero"); \
75 b = p_atomic_dec_zero(&v); \
76 assert(v == 0 && "p_atomic_dec_zero"); \
77 assert(b == true && "p_atomic_dec_zero"); \
78 b = p_atomic_dec_zero(&v); \
79 assert(v == ones && "p_atomic_dec_zero"); \
80 assert(b == false && "p_atomic_dec_zero"); \
81 \
82 v = ones; \
83 p_atomic_inc(&v); \
84 assert(v == 0 && "p_atomic_inc"); \
85 \
86 v = ones; \
87 r = p_atomic_inc_return(&v); \
88 assert(v == 0 && "p_atomic_inc_return"); \
89 assert(r == v && "p_atomic_inc_return"); \
90 \
91 v = 0; \
92 p_atomic_dec(&v); \
93 assert(v == ones && "p_atomic_dec"); \
94 \
95 v = 0; \
96 r = p_atomic_dec_return(&v); \
97 assert(v == ones && "p_atomic_dec_return"); \
98 assert(r == v && "p_atomic_dec_return"); \
99 \
100 (void) r; \
101 (void) b; \
102 }
103
104
105 test_atomic(int, -1)
106 test_atomic(unsigned, ~0U)
107
108 test_atomic(int16_t, INT16_C(-1))
109 test_atomic(uint16_t, UINT16_C(0xffff))
110 test_atomic(int32_t, INT32_C(-1))
111 test_atomic(uint32_t, UINT32_C(0xffffffff))
112 test_atomic(int64_t, INT64_C(-1))
113 test_atomic(uint64_t, UINT64_C(0xffffffffffffffff))
114
115 test_atomic_cmpxchg(int8_t, INT8_C(-1))
116 test_atomic_cmpxchg(uint8_t, UINT8_C(0xff))
117 test_atomic_cmpxchg(bool, true)
118
119 int
120 main()
121 {
122 test_atomic_int();
123 test_atomic_unsigned();
124
125 test_atomic_int16_t();
126 test_atomic_uint16_t();
127 test_atomic_int32_t();
128 test_atomic_uint32_t();
129 test_atomic_int64_t();
130 test_atomic_uint64_t();
131
132 test_atomic_cmpxchg_int8_t();
133 test_atomic_cmpxchg_uint8_t();
134 test_atomic_cmpxchg_bool();
135
136 return 0;
137 }