Path: blob/main/tools/regression/include/stdatomic/logic.c
48254 views
/*-1* Copyright (c) 2013 Ed Schouten <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/cdefs.h>27#include <assert.h>28#include <stdatomic.h>29#include <stdint.h>30#include <stdlib.h>31#include <string.h>3233/*34* Tool for testing the logical behaviour of operations on atomic35* integer types. These tests make no attempt to actually test whether36* the functions are atomic or provide the right barrier semantics.37*38* For every type, we create an array of 16 elements and repeat the test39* on every element in the array. This allows us to test whether the40* atomic operations have no effect on surrounding values. This is41* especially useful for the smaller integer types, as it may be the42* case that these operations are implemented by processing entire words43* (e.g. on MIPS).44*/4546static inline intmax_t47rndnum(void)48{49intmax_t v;5051arc4random_buf(&v, sizeof(v));52return (v);53}5455#define DO_FETCH_TEST(T, a, name, result) do { \56T v1 = atomic_load(a); \57T v2 = rndnum(); \58assert(atomic_##name(a, v2) == v1); \59assert(atomic_load(a) == (T)(result)); \60} while (0)6162#define DO_COMPARE_EXCHANGE_TEST(T, a, name) do { \63T v1 = atomic_load(a); \64T v2 = rndnum(); \65T v3 = rndnum(); \66if (atomic_compare_exchange_##name(a, &v2, v3)) \67assert(v1 == v2); \68else \69assert(atomic_compare_exchange_##name(a, &v2, v3)); \70assert(atomic_load(a) == v3); \71} while (0)7273#define DO_ALL_TESTS(T, a) do { \74{ \75T v1 = rndnum(); \76atomic_init(a, v1); \77assert(atomic_load(a) == v1); \78} \79{ \80T v1 = rndnum(); \81atomic_store(a, v1); \82assert(atomic_load(a) == v1); \83} \84\85DO_FETCH_TEST(T, a, exchange, v2); \86DO_FETCH_TEST(T, a, fetch_add, v1 + v2); \87DO_FETCH_TEST(T, a, fetch_and, v1 & v2); \88DO_FETCH_TEST(T, a, fetch_or, v1 | v2); \89DO_FETCH_TEST(T, a, fetch_sub, v1 - v2); \90DO_FETCH_TEST(T, a, fetch_xor, v1 ^ v2); \91\92DO_COMPARE_EXCHANGE_TEST(T, a, weak); \93DO_COMPARE_EXCHANGE_TEST(T, a, strong); \94} while (0)9596#define TEST_TYPE(T) do { \97int j; \98struct { _Atomic(T) v[16]; } list, cmp; \99arc4random_buf(&cmp, sizeof(cmp)); \100for (j = 0; j < 16; j++) { \101list = cmp; \102DO_ALL_TESTS(T, &list.v[j]); \103list.v[j] = cmp.v[j]; \104assert(memcmp(&list, &cmp, sizeof(list)) == 0); \105} \106} while (0)107108int109main(void)110{111int i;112113for (i = 0; i < 1000; i++) {114TEST_TYPE(int8_t);115TEST_TYPE(uint8_t);116TEST_TYPE(int16_t);117TEST_TYPE(uint16_t);118TEST_TYPE(int32_t);119TEST_TYPE(uint32_t);120TEST_TYPE(int64_t);121TEST_TYPE(uint64_t);122}123124return (0);125}126127128