1251558Sed/*-
2251558Sed * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org>
3251558Sed * All rights reserved.
4251558Sed *
5251558Sed * Redistribution and use in source and binary forms, with or without
6251558Sed * modification, are permitted provided that the following conditions
7251558Sed * are met:
8251558Sed * 1. Redistributions of source code must retain the above copyright
9251558Sed *    notice, this list of conditions and the following disclaimer.
10251558Sed * 2. Redistributions in binary form must reproduce the above copyright
11251558Sed *    notice, this list of conditions and the following disclaimer in the
12251558Sed *    documentation and/or other materials provided with the distribution.
13251558Sed *
14251558Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15251558Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16251558Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17251558Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18251558Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19251558Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20251558Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21251558Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22251558Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23251558Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24251558Sed * SUCH DAMAGE.
25251558Sed */
26251558Sed
27251558Sed#include <sys/cdefs.h>
28251558Sed__FBSDID("$FreeBSD$");
29251558Sed
30251558Sed#include <assert.h>
31251558Sed#include <stdatomic.h>
32251558Sed#include <stdint.h>
33251558Sed#include <stdlib.h>
34251558Sed#include <string.h>
35251558Sed
36251558Sed/*
37251558Sed * Tool for testing the logical behaviour of operations on atomic
38251558Sed * integer types. These tests make no attempt to actually test whether
39251558Sed * the functions are atomic or provide the right barrier semantics.
40251558Sed *
41251558Sed * For every type, we create an array of 16 elements and repeat the test
42251558Sed * on every element in the array. This allows us to test whether the
43251558Sed * atomic operations have no effect on surrounding values. This is
44251558Sed * especially useful for the smaller integer types, as it may be the
45251558Sed * case that these operations are implemented by processing entire words
46251558Sed * (e.g. on MIPS).
47251558Sed */
48251558Sed
49251558Sedstatic inline intmax_t
50251558Sedrndnum(void)
51251558Sed{
52251558Sed	intmax_t v;
53251558Sed
54251558Sed	arc4random_buf(&v, sizeof(v));
55251558Sed	return (v);
56251558Sed}
57251558Sed
58251558Sed#define	DO_FETCH_TEST(T, a, name, result) do {				\
59251558Sed	T v1 = atomic_load(a);						\
60251558Sed	T v2 = rndnum();						\
61251558Sed	assert(atomic_##name(a, v2) == v1); 				\
62251558Sed	assert(atomic_load(a) == (T)(result)); 				\
63251558Sed} while (0)
64251558Sed
65251558Sed#define	DO_COMPARE_EXCHANGE_TEST(T, a, name) do {			\
66251558Sed	T v1 = atomic_load(a);						\
67251558Sed	T v2 = rndnum();						\
68251558Sed	T v3 = rndnum();						\
69251558Sed	if (atomic_compare_exchange_##name(a, &v2, v3))			\
70251558Sed		assert(v1 == v2);					\
71251558Sed	else								\
72251558Sed		assert(atomic_compare_exchange_##name(a, &v2, v3));	\
73251558Sed	assert(atomic_load(a) == v3);					\
74251558Sed} while (0)
75251558Sed
76251558Sed#define	DO_ALL_TESTS(T, a) do {						\
77251558Sed	{								\
78251558Sed		T v1 = rndnum();					\
79251558Sed		atomic_init(a, v1);					\
80251558Sed		assert(atomic_load(a) == v1);				\
81251558Sed	}								\
82251558Sed	{								\
83251558Sed		T v1 = rndnum();					\
84251558Sed		atomic_store(a, v1);					\
85251558Sed		assert(atomic_load(a) == v1);				\
86251558Sed	}								\
87251558Sed									\
88251558Sed	DO_FETCH_TEST(T, a, exchange, v2);				\
89251558Sed	DO_FETCH_TEST(T, a, fetch_add, v1 + v2);			\
90251558Sed	DO_FETCH_TEST(T, a, fetch_and, v1 & v2);			\
91251558Sed	DO_FETCH_TEST(T, a, fetch_or, v1 | v2);				\
92251558Sed	DO_FETCH_TEST(T, a, fetch_sub, v1 - v2);			\
93251558Sed	DO_FETCH_TEST(T, a, fetch_xor, v1 ^ v2);			\
94251558Sed									\
95251558Sed	DO_COMPARE_EXCHANGE_TEST(T, a, weak);				\
96251558Sed	DO_COMPARE_EXCHANGE_TEST(T, a, strong);				\
97251558Sed} while (0)
98251558Sed
99251558Sed#define	TEST_TYPE(T) do {						\
100251558Sed	int j;								\
101251558Sed	struct { _Atomic(T) v[16]; } list, cmp;				\
102251558Sed	arc4random_buf(&cmp, sizeof(cmp));				\
103251558Sed	for (j = 0; j < 16; j++) {					\
104251558Sed		list = cmp;						\
105251558Sed		DO_ALL_TESTS(T, &list.v[j]);				\
106251558Sed		list.v[j] = cmp.v[j];					\
107251558Sed		assert(memcmp(&list, &cmp, sizeof(list)) == 0);		\
108251558Sed	}								\
109251558Sed} while (0)
110251558Sed
111251558Sedint
112251558Sedmain(void)
113251558Sed{
114251558Sed	int i;
115251558Sed
116251558Sed	for (i = 0; i < 1000; i++) {
117251558Sed		TEST_TYPE(int8_t);
118251558Sed		TEST_TYPE(uint8_t);
119251558Sed		TEST_TYPE(int16_t);
120251558Sed		TEST_TYPE(uint16_t);
121251558Sed		TEST_TYPE(int32_t);
122251558Sed		TEST_TYPE(uint32_t);
123251558Sed		TEST_TYPE(int64_t);
124251558Sed		TEST_TYPE(uint64_t);
125251558Sed	}
126251558Sed
127251558Sed	return (0);
128251558Sed}
129