xref: /freebsd/tools/regression/include/stdatomic/logic.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <assert.h>
31 #include <stdatomic.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 /*
37  * Tool for testing the logical behaviour of operations on atomic
38  * integer types. These tests make no attempt to actually test whether
39  * the functions are atomic or provide the right barrier semantics.
40  *
41  * For every type, we create an array of 16 elements and repeat the test
42  * on every element in the array. This allows us to test whether the
43  * atomic operations have no effect on surrounding values. This is
44  * especially useful for the smaller integer types, as it may be the
45  * case that these operations are implemented by processing entire words
46  * (e.g. on MIPS).
47  */
48 
49 static inline intmax_t
50 rndnum(void)
51 {
52 	intmax_t v;
53 
54 	arc4random_buf(&v, sizeof(v));
55 	return (v);
56 }
57 
58 #define	DO_FETCH_TEST(T, a, name, result) do {				\
59 	T v1 = atomic_load(a);						\
60 	T v2 = rndnum();						\
61 	assert(atomic_##name(a, v2) == v1); 				\
62 	assert(atomic_load(a) == (T)(result)); 				\
63 } while (0)
64 
65 #define	DO_COMPARE_EXCHANGE_TEST(T, a, name) do {			\
66 	T v1 = atomic_load(a);						\
67 	T v2 = rndnum();						\
68 	T v3 = rndnum();						\
69 	if (atomic_compare_exchange_##name(a, &v2, v3))			\
70 		assert(v1 == v2);					\
71 	else								\
72 		assert(atomic_compare_exchange_##name(a, &v2, v3));	\
73 	assert(atomic_load(a) == v3);					\
74 } while (0)
75 
76 #define	DO_ALL_TESTS(T, a) do {						\
77 	{								\
78 		T v1 = rndnum();					\
79 		atomic_init(a, v1);					\
80 		assert(atomic_load(a) == v1);				\
81 	}								\
82 	{								\
83 		T v1 = rndnum();					\
84 		atomic_store(a, v1);					\
85 		assert(atomic_load(a) == v1);				\
86 	}								\
87 									\
88 	DO_FETCH_TEST(T, a, exchange, v2);				\
89 	DO_FETCH_TEST(T, a, fetch_add, v1 + v2);			\
90 	DO_FETCH_TEST(T, a, fetch_and, v1 & v2);			\
91 	DO_FETCH_TEST(T, a, fetch_or, v1 | v2);				\
92 	DO_FETCH_TEST(T, a, fetch_sub, v1 - v2);			\
93 	DO_FETCH_TEST(T, a, fetch_xor, v1 ^ v2);			\
94 									\
95 	DO_COMPARE_EXCHANGE_TEST(T, a, weak);				\
96 	DO_COMPARE_EXCHANGE_TEST(T, a, strong);				\
97 } while (0)
98 
99 #define	TEST_TYPE(T) do {						\
100 	int j;								\
101 	struct { _Atomic(T) v[16]; } list, cmp;				\
102 	arc4random_buf(&cmp, sizeof(cmp));				\
103 	for (j = 0; j < 16; j++) {					\
104 		list = cmp;						\
105 		DO_ALL_TESTS(T, &list.v[j]);				\
106 		list.v[j] = cmp.v[j];					\
107 		assert(memcmp(&list, &cmp, sizeof(list)) == 0);		\
108 	}								\
109 } while (0)
110 
111 int
112 main(void)
113 {
114 	int i;
115 
116 	for (i = 0; i < 1000; i++) {
117 		TEST_TYPE(int8_t);
118 		TEST_TYPE(uint8_t);
119 		TEST_TYPE(int16_t);
120 		TEST_TYPE(uint16_t);
121 		TEST_TYPE(int32_t);
122 		TEST_TYPE(uint32_t);
123 		TEST_TYPE(int64_t);
124 		TEST_TYPE(uint64_t);
125 	}
126 
127 	return (0);
128 }
129