1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/atomicops.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 template <class AtomicType>
13 static void TestAtomicIncrement() {
14 // For now, we just test single threaded execution
16 // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go
17 // outside the expected address bounds. This is in particular to
18 // test that some future change to the asm code doesn't cause the
19 // 32-bit NoBarrier_AtomicIncrement doesn't do the wrong thing on 64-bit
27 AtomicType prev_word_value, next_word_value;
28 memset(&prev_word_value, 0xFF, sizeof(AtomicType));
29 memset(&next_word_value, 0xEE, sizeof(AtomicType));
31 s.prev_word = prev_word_value;
33 s.next_word = next_word_value;
35 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 1), 1);
36 EXPECT_EQ(s.count, 1);
37 EXPECT_EQ(s.prev_word, prev_word_value);
38 EXPECT_EQ(s.next_word, next_word_value);
40 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 2), 3);
41 EXPECT_EQ(s.count, 3);
42 EXPECT_EQ(s.prev_word, prev_word_value);
43 EXPECT_EQ(s.next_word, next_word_value);
45 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 3), 6);
46 EXPECT_EQ(s.count, 6);
47 EXPECT_EQ(s.prev_word, prev_word_value);
48 EXPECT_EQ(s.next_word, next_word_value);
50 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -3), 3);
51 EXPECT_EQ(s.count, 3);
52 EXPECT_EQ(s.prev_word, prev_word_value);
53 EXPECT_EQ(s.next_word, next_word_value);
55 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -2), 1);
56 EXPECT_EQ(s.count, 1);
57 EXPECT_EQ(s.prev_word, prev_word_value);
58 EXPECT_EQ(s.next_word, next_word_value);
60 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -1), 0);
61 EXPECT_EQ(s.count, 0);
62 EXPECT_EQ(s.prev_word, prev_word_value);
63 EXPECT_EQ(s.next_word, next_word_value);
65 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -1), -1);
66 EXPECT_EQ(s.count, -1);
67 EXPECT_EQ(s.prev_word, prev_word_value);
68 EXPECT_EQ(s.next_word, next_word_value);
70 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, -4), -5);
71 EXPECT_EQ(s.count, -5);
72 EXPECT_EQ(s.prev_word, prev_word_value);
73 EXPECT_EQ(s.next_word, next_word_value);
75 EXPECT_EQ(base::subtle::NoBarrier_AtomicIncrement(&s.count, 5), 0);
76 EXPECT_EQ(s.count, 0);
77 EXPECT_EQ(s.prev_word, prev_word_value);
78 EXPECT_EQ(s.next_word, next_word_value);
82 #define NUM_BITS(T) (sizeof(T) * 8)
85 template <class AtomicType>
86 static void TestCompareAndSwap() {
88 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1);
92 // Verify that CAS will *not* change "value" if it doesn't match the
93 // expected number. CAS will always return the actual value of the
94 // variable from before any change.
95 AtomicType fail = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 2);
99 // Use test value that has non-zero bits in both halves, more for testing
100 // 64-bit implementation on 32-bit platforms.
101 const AtomicType k_test_val = (static_cast<uint64_t>(1) <<
102 (NUM_BITS(AtomicType) - 2)) + 11;
104 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5);
105 EXPECT_EQ(k_test_val, value);
106 EXPECT_EQ(k_test_val, prev);
109 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5);
111 EXPECT_EQ(k_test_val, prev);
115 template <class AtomicType>
116 static void TestAtomicExchange() {
117 AtomicType value = 0;
118 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1);
120 EXPECT_EQ(0, new_value);
122 // Use test value that has non-zero bits in both halves, more for testing
123 // 64-bit implementation on 32-bit platforms.
124 const AtomicType k_test_val = (static_cast<uint64_t>(1) <<
125 (NUM_BITS(AtomicType) - 2)) + 11;
127 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val);
128 EXPECT_EQ(k_test_val, value);
129 EXPECT_EQ(k_test_val, new_value);
132 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5);
134 EXPECT_EQ(k_test_val, new_value);
138 template <class AtomicType>
139 static void TestAtomicIncrementBounds() {
140 // Test at rollover boundary between int_max and int_min
141 AtomicType test_val = (static_cast<uint64_t>(1) <<
142 (NUM_BITS(AtomicType) - 1));
143 AtomicType value = -1 ^ test_val;
144 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1);
145 EXPECT_EQ(test_val, value);
146 EXPECT_EQ(value, new_value);
148 base::subtle::NoBarrier_AtomicIncrement(&value, -1);
149 EXPECT_EQ(-1 ^ test_val, value);
151 // Test at 32-bit boundary for 64-bit atomic type.
152 test_val = static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) / 2);
153 value = test_val - 1;
154 new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1);
155 EXPECT_EQ(test_val, value);
156 EXPECT_EQ(value, new_value);
158 base::subtle::NoBarrier_AtomicIncrement(&value, -1);
159 EXPECT_EQ(test_val - 1, value);
162 // Return an AtomicType with the value 0xa5a5a5..
163 template <class AtomicType>
164 static AtomicType TestFillValue() {
166 memset(&val, 0xa5, sizeof(AtomicType));
170 // This is a simple sanity check that values are correct. Not testing
172 template <class AtomicType>
173 static void TestStore() {
174 const AtomicType kVal1 = TestFillValue<AtomicType>();
175 const AtomicType kVal2 = static_cast<AtomicType>(-1);
179 base::subtle::NoBarrier_Store(&value, kVal1);
180 EXPECT_EQ(kVal1, value);
181 base::subtle::NoBarrier_Store(&value, kVal2);
182 EXPECT_EQ(kVal2, value);
184 base::subtle::Release_Store(&value, kVal1);
185 EXPECT_EQ(kVal1, value);
186 base::subtle::Release_Store(&value, kVal2);
187 EXPECT_EQ(kVal2, value);
190 // This is a simple sanity check that values are correct. Not testing
192 template <class AtomicType>
193 static void TestLoad() {
194 const AtomicType kVal1 = TestFillValue<AtomicType>();
195 const AtomicType kVal2 = static_cast<AtomicType>(-1);
200 EXPECT_EQ(kVal1, base::subtle::NoBarrier_Load(&value));
202 EXPECT_EQ(kVal2, base::subtle::NoBarrier_Load(&value));
205 EXPECT_EQ(kVal1, base::subtle::Acquire_Load(&value));
207 EXPECT_EQ(kVal2, base::subtle::Acquire_Load(&value));
210 TEST(AtomicOpsTest, Inc) {
211 TestAtomicIncrement<base::subtle::Atomic32>();
212 TestAtomicIncrement<base::subtle::AtomicWord>();
215 TEST(AtomicOpsTest, CompareAndSwap) {
216 TestCompareAndSwap<base::subtle::Atomic32>();
217 TestCompareAndSwap<base::subtle::AtomicWord>();
220 TEST(AtomicOpsTest, Exchange) {
221 TestAtomicExchange<base::subtle::Atomic32>();
222 TestAtomicExchange<base::subtle::AtomicWord>();
225 TEST(AtomicOpsTest, IncrementBounds) {
226 TestAtomicIncrementBounds<base::subtle::Atomic32>();
227 TestAtomicIncrementBounds<base::subtle::AtomicWord>();
230 TEST(AtomicOpsTest, Store) {
231 TestStore<base::subtle::Atomic32>();
232 TestStore<base::subtle::AtomicWord>();
235 TEST(AtomicOpsTest, Load) {
236 TestLoad<base::subtle::Atomic32>();
237 TestLoad<base::subtle::AtomicWord>();