1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 #include "Tests.h" 24 #include <UnitTest++.h> 25 #include <MTAtomic.h> 26 27 28 SUITE(AtomicTests) 29 { 30 static const int OLD_VALUE = 1; 31 static const int VALUE = 13; 32 static const int NEW_VALUE = 16; 33 static const int RELAXED_VALUE = 27; 34 35 void TestStatics() 36 { 37 // This variables must be placed to .data / .bss section 38 // 39 // From "Cpp Standard" 40 // 41 // 6.7 Declaration statement 42 // 43 // 4 The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage 44 // duration (3.7.2) is performed before any other initialization takes place. 45 // Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, 46 // is performed before its block is first entered. 47 // 48 static MT::Atomic32Base<int32> test = { 0 }; 49 static MT::AtomicPtrBase<void> pTest = { nullptr }; 50 51 test.Store(13); 52 pTest.Store(nullptr); 53 54 CHECK_EQUAL(13, test.Load()); 55 CHECK(pTest.Load() == nullptr); 56 } 57 58 TEST(AtomicSimpleTest) 59 { 60 TestStatics(); 61 62 MT::Atomic32<int32> test_relaxed; 63 test_relaxed.StoreRelaxed(RELAXED_VALUE); 64 CHECK(test_relaxed.Load() == RELAXED_VALUE); 65 66 MT::Atomic32<int32> test; 67 test.Store(OLD_VALUE); 68 CHECK(test.Load() == OLD_VALUE); 69 70 int prevValue = test.Exchange(VALUE); 71 CHECK(test.Load() == VALUE); 72 CHECK(prevValue == OLD_VALUE); 73 74 int nowValue = test.IncFetch(); 75 CHECK(nowValue == (VALUE+1)); 76 77 nowValue = test.DecFetch(); 78 CHECK(nowValue == VALUE); 79 80 nowValue = test.AddFetch(VALUE); 81 CHECK(nowValue == (VALUE+VALUE)); 82 83 MT::Atomic32<int32> test2(VALUE); 84 CHECK(test2.Load() == VALUE); 85 86 int prevResult = test2.CompareAndSwap(NEW_VALUE, OLD_VALUE); 87 CHECK(prevResult == VALUE); 88 CHECK(test2.Load() == VALUE); 89 90 prevResult = test2.CompareAndSwap(VALUE, NEW_VALUE); 91 CHECK(prevResult == VALUE); 92 CHECK(test2.Load() == NEW_VALUE); 93 94 MT::Atomic32<uint32> test3(UINT32_MAX); 95 CHECK_EQUAL(UINT32_MAX, test3.Load()); 96 97 //check for wraps 98 uint32 uNowValue = test3.IncFetch(); 99 CHECK(uNowValue == 0); 100 101 uNowValue = test3.DecFetch(); 102 CHECK(uNowValue == UINT32_MAX); 103 104 105 char tempObject; 106 char* testPtr = &tempObject; 107 char* testPtrNew = testPtr + 1; 108 109 110 MT::AtomicPtr<char> atomicPtrRelaxed; 111 atomicPtrRelaxed.StoreRelaxed(testPtr); 112 CHECK(atomicPtrRelaxed.Load() == testPtr); 113 114 MT::AtomicPtr<char> atomicPtr; 115 CHECK(atomicPtr.Load() == nullptr); 116 117 atomicPtr.Store(testPtr); 118 CHECK(atomicPtr.Load() == testPtr); 119 120 char* prevPtr = atomicPtr.CompareAndSwap(nullptr, testPtrNew); 121 CHECK(prevPtr == testPtr); 122 CHECK(atomicPtr.Load() == testPtr); 123 124 prevPtr = atomicPtr.CompareAndSwap(testPtr, testPtrNew); 125 CHECK(prevPtr == testPtr); 126 CHECK(atomicPtr.Load() == testPtrNew); 127 128 char* prevPtr2 = atomicPtr.Exchange(nullptr); 129 CHECK(prevPtr2 == testPtrNew); 130 CHECK(atomicPtr.Load() == nullptr); 131 } 132 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 133 } 134