1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2014 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include <gtest/gtest.h> 29bf215546Sopenharmony_ci#include <stdint.h> 30bf215546Sopenharmony_ci#include <inttypes.h> 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include "u_atomic.h" 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#ifdef _MSC_VER 35bf215546Sopenharmony_ci#pragma warning( disable : 28112 ) /* Accessing a local variable via an Interlocked function */ 36bf215546Sopenharmony_ci#pragma warning( disable : 28113 ) /* A variable which is accessed via an Interlocked function must always be accessed via an Interlocked function */ 37bf215546Sopenharmony_ci#endif 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_citemplate <typename T> class AtomicAssignment : public testing::Test {}; 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ciusing AtomicAssignmentTypes = 42bf215546Sopenharmony_ci testing::Types<int, unsigned, bool, 43bf215546Sopenharmony_ci int8_t, uint8_t, int16_t, uint16_t, 44bf215546Sopenharmony_ci int32_t, uint32_t, int64_t, uint64_t>; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ciTYPED_TEST_SUITE(AtomicAssignment, AtomicAssignmentTypes); 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ciTYPED_TEST(AtomicAssignment, Test) 49bf215546Sopenharmony_ci{ 50bf215546Sopenharmony_ci TypeParam v, r; 51bf215546Sopenharmony_ci const TypeParam ones = TypeParam(-1); 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci p_atomic_set(&v, ones); 54bf215546Sopenharmony_ci ASSERT_EQ(v, ones) << "p_atomic_set"; 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci r = p_atomic_read(&v); 57bf215546Sopenharmony_ci ASSERT_EQ(r, ones) << "p_atomic_read"; 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci v = ones; 60bf215546Sopenharmony_ci r = p_atomic_cmpxchg(&v, 0, 1); 61bf215546Sopenharmony_ci ASSERT_EQ(v, ones) << "p_atomic_cmpxchg"; 62bf215546Sopenharmony_ci ASSERT_EQ(r, ones) << "p_atomic_cmpxchg"; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci r = p_atomic_cmpxchg(&v, ones, 0); 65bf215546Sopenharmony_ci ASSERT_EQ(v, 0) << "p_atomic_cmpxchg"; 66bf215546Sopenharmony_ci ASSERT_EQ(r, ones) << "p_atomic_cmpxchg"; 67bf215546Sopenharmony_ci} 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_citemplate <typename T> class AtomicIncrementDecrement : public testing::Test {}; 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ciusing AtomicIncrementDecrementTypes = 73bf215546Sopenharmony_ci testing::Types<int, unsigned, 74bf215546Sopenharmony_ci int16_t, uint16_t, 75bf215546Sopenharmony_ci int32_t, uint32_t, int64_t, uint64_t>; 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ciTYPED_TEST_SUITE(AtomicIncrementDecrement, AtomicIncrementDecrementTypes); 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_ciTYPED_TEST(AtomicIncrementDecrement, Test) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci TypeParam v, r; 82bf215546Sopenharmony_ci bool b; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci const TypeParam ones = TypeParam(-1); 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci v = 2; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci b = p_atomic_dec_zero(&v); 89bf215546Sopenharmony_ci ASSERT_EQ(v, 1) << "p_atomic_dec_zero"; 90bf215546Sopenharmony_ci ASSERT_FALSE(b) << "p_atomic_dec_zero"; 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci b = p_atomic_dec_zero(&v); 93bf215546Sopenharmony_ci ASSERT_EQ(v, 0) << "p_atomic_dec_zero"; 94bf215546Sopenharmony_ci ASSERT_TRUE(b) << "p_atomic_dec_zero"; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci b = p_atomic_dec_zero(&v); 97bf215546Sopenharmony_ci ASSERT_EQ(v, ones) << "p_atomic_dec_zero"; 98bf215546Sopenharmony_ci ASSERT_FALSE(b) << "p_atomic_dec_zero"; 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci v = ones; 101bf215546Sopenharmony_ci p_atomic_inc(&v); 102bf215546Sopenharmony_ci ASSERT_EQ(v, 0) << "p_atomic_inc"; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci v = ones; 105bf215546Sopenharmony_ci r = p_atomic_inc_return(&v); 106bf215546Sopenharmony_ci ASSERT_EQ(v, 0) << "p_atomic_inc_return"; 107bf215546Sopenharmony_ci ASSERT_EQ(r, v) << "p_atomic_inc_return"; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci v = 0; 110bf215546Sopenharmony_ci p_atomic_dec(&v); 111bf215546Sopenharmony_ci ASSERT_EQ(v, ones) << "p_atomic_dec"; 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci v = 0; 114bf215546Sopenharmony_ci r = p_atomic_dec_return(&v); 115bf215546Sopenharmony_ci ASSERT_EQ(v, ones) << "p_atomic_dec_return"; 116bf215546Sopenharmony_ci ASSERT_EQ(v, r) << "p_atomic_dec_return"; 117bf215546Sopenharmony_ci} 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_citemplate <typename T> class AtomicAdd : public testing::Test {}; 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ciusing AtomicAddTypes = 122bf215546Sopenharmony_ci testing::Types<int, unsigned, 123bf215546Sopenharmony_ci int8_t, uint8_t, int16_t, uint16_t, 124bf215546Sopenharmony_ci int32_t, uint32_t, int64_t, uint64_t>; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ciTYPED_TEST_SUITE(AtomicAdd, AtomicAddTypes); 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_ciTYPED_TEST(AtomicAdd, Test) 129bf215546Sopenharmony_ci{ 130bf215546Sopenharmony_ci TypeParam v, r; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci v = 23; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci p_atomic_add(&v, 42); 135bf215546Sopenharmony_ci r = p_atomic_read(&v); 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci ASSERT_EQ(r, 65) << "p_atomic_add"; 138bf215546Sopenharmony_ci} 139