1 /**************************************************************************
3 * Copyright 2014 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 /* Force assertions, even on debug builds. */
40 /* Test only assignment-like operations, which are supported on all types */
41 #define test_atomic_assign(type, ones) \
42 static void test_atomic_assign_##type (void) { \
45 p_atomic_set(&v, ones); \
46 assert(v == ones && "p_atomic_set"); \
48 r = p_atomic_read(&v); \
49 assert(r == ones && "p_atomic_read"); \
52 r = p_atomic_cmpxchg(&v, 0, 1); \
53 assert(v == ones && "p_atomic_cmpxchg"); \
54 assert(r == ones && "p_atomic_cmpxchg"); \
55 r = p_atomic_cmpxchg(&v, ones, 0); \
56 assert(v == 0 && "p_atomic_cmpxchg"); \
57 assert(r == ones && "p_atomic_cmpxchg"); \
63 /* Test arithmetic operations that are supported on 8 bits integer types */
64 #define test_atomic_8bits(type, ones) \
65 test_atomic_assign(type, ones) \
67 static void test_atomic_8bits_##type (void) { \
70 test_atomic_assign_##type(); \
73 p_atomic_add(&v, 42); \
74 r = p_atomic_read(&v); \
75 assert(r == 65 && "p_atomic_add"); \
81 /* Test all operations */
82 #define test_atomic(type, ones) \
83 test_atomic_8bits(type, ones) \
85 static void test_atomic_##type (void) { \
89 test_atomic_8bits_##type(); \
92 b = p_atomic_dec_zero(&v); \
93 assert(v == 1 && "p_atomic_dec_zero"); \
94 assert(b == false && "p_atomic_dec_zero"); \
95 b = p_atomic_dec_zero(&v); \
96 assert(v == 0 && "p_atomic_dec_zero"); \
97 assert(b == true && "p_atomic_dec_zero"); \
98 b = p_atomic_dec_zero(&v); \
99 assert(v == ones && "p_atomic_dec_zero"); \
100 assert(b == false && "p_atomic_dec_zero"); \
104 assert(v == 0 && "p_atomic_inc"); \
107 r = p_atomic_inc_return(&v); \
108 assert(v == 0 && "p_atomic_inc_return"); \
109 assert(r == v && "p_atomic_inc_return"); \
113 assert(v == ones && "p_atomic_dec"); \
116 r = p_atomic_dec_return(&v); \
117 assert(v == ones && "p_atomic_dec_return"); \
118 assert(r == v && "p_atomic_dec_return"); \
126 test_atomic(unsigned, ~0U)
128 test_atomic(int16_t, INT16_C(-1))
129 test_atomic(uint16_t, UINT16_C(0xffff))
130 test_atomic(int32_t, INT32_C(-1))
131 test_atomic(uint32_t, UINT32_C(0xffffffff))
132 test_atomic(int64_t, INT64_C(-1))
133 test_atomic(uint64_t, UINT64_C(0xffffffffffffffff))
135 test_atomic_8bits(int8_t, INT8_C(-1))
136 test_atomic_8bits(uint8_t, UINT8_C(0xff))
137 test_atomic_assign(bool, true)
143 test_atomic_unsigned();
145 test_atomic_int16_t();
146 test_atomic_uint16_t();
147 test_atomic_int32_t();
148 test_atomic_uint32_t();
149 test_atomic_int64_t();
150 test_atomic_uint64_t();
152 test_atomic_8bits_int8_t();
153 test_atomic_8bits_uint8_t();
154 test_atomic_assign_bool();