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 #include <assert.h> 29 #include <stdatomic.h> 30 #include <stdint.h> 31 #include <stdlib.h> 32 #include <string.h> 33 34 /* 35 * Tool for testing the logical behaviour of operations on atomic 36 * integer types. These tests make no attempt to actually test whether 37 * the functions are atomic or provide the right barrier semantics. 38 * 39 * For every type, we create an array of 16 elements and repeat the test 40 * on every element in the array. This allows us to test whether the 41 * atomic operations have no effect on surrounding values. This is 42 * especially useful for the smaller integer types, as it may be the 43 * case that these operations are implemented by processing entire words 44 * (e.g. on MIPS). 45 */ 46 47 static inline intmax_t 48 rndnum(void) 49 { 50 intmax_t v; 51 52 arc4random_buf(&v, sizeof(v)); 53 return (v); 54 } 55 56 #define DO_FETCH_TEST(T, a, name, result) do { \ 57 T v1 = atomic_load(a); \ 58 T v2 = rndnum(); \ 59 assert(atomic_##name(a, v2) == v1); \ 60 assert(atomic_load(a) == (T)(result)); \ 61 } while (0) 62 63 #define DO_COMPARE_EXCHANGE_TEST(T, a, name) do { \ 64 T v1 = atomic_load(a); \ 65 T v2 = rndnum(); \ 66 T v3 = rndnum(); \ 67 if (atomic_compare_exchange_##name(a, &v2, v3)) \ 68 assert(v1 == v2); \ 69 else \ 70 assert(atomic_compare_exchange_##name(a, &v2, v3)); \ 71 assert(atomic_load(a) == v3); \ 72 } while (0) 73 74 #define DO_ALL_TESTS(T, a) do { \ 75 { \ 76 T v1 = rndnum(); \ 77 atomic_init(a, v1); \ 78 assert(atomic_load(a) == v1); \ 79 } \ 80 { \ 81 T v1 = rndnum(); \ 82 atomic_store(a, v1); \ 83 assert(atomic_load(a) == v1); \ 84 } \ 85 \ 86 DO_FETCH_TEST(T, a, exchange, v2); \ 87 DO_FETCH_TEST(T, a, fetch_add, v1 + v2); \ 88 DO_FETCH_TEST(T, a, fetch_and, v1 & v2); \ 89 DO_FETCH_TEST(T, a, fetch_or, v1 | v2); \ 90 DO_FETCH_TEST(T, a, fetch_sub, v1 - v2); \ 91 DO_FETCH_TEST(T, a, fetch_xor, v1 ^ v2); \ 92 \ 93 DO_COMPARE_EXCHANGE_TEST(T, a, weak); \ 94 DO_COMPARE_EXCHANGE_TEST(T, a, strong); \ 95 } while (0) 96 97 #define TEST_TYPE(T) do { \ 98 int j; \ 99 struct { _Atomic(T) v[16]; } list, cmp; \ 100 arc4random_buf(&cmp, sizeof(cmp)); \ 101 for (j = 0; j < 16; j++) { \ 102 list = cmp; \ 103 DO_ALL_TESTS(T, &list.v[j]); \ 104 list.v[j] = cmp.v[j]; \ 105 assert(memcmp(&list, &cmp, sizeof(list)) == 0); \ 106 } \ 107 } while (0) 108 109 int 110 main(void) 111 { 112 int i; 113 114 for (i = 0; i < 1000; i++) { 115 TEST_TYPE(int8_t); 116 TEST_TYPE(uint8_t); 117 TEST_TYPE(int16_t); 118 TEST_TYPE(uint16_t); 119 TEST_TYPE(int32_t); 120 TEST_TYPE(uint32_t); 121 TEST_TYPE(int64_t); 122 TEST_TYPE(uint64_t); 123 } 124 125 return (0); 126 } 127