OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | |
6 #include <stdio.h> | 5 #include <stdio.h> |
7 #include <stdlib.h> | 6 #include <stdlib.h> |
8 #include <algorithm> // for min() | 7 #include <algorithm> // for min() |
9 | |
10 #include "base/atomicops.h" | 8 #include "base/atomicops.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
12 | 10 |
13 // Number of bits in a size_t. | 11 // Number of bits in a size_t. |
14 static const int kSizeBits = 8 * sizeof(size_t); | 12 static const int kSizeBits = 8 * sizeof(size_t); |
15 // The maximum size of a size_t. | 13 // The maximum size of a size_t. |
16 static const size_t kMaxSize = ~static_cast<size_t>(0); | 14 static const size_t kMaxSize = ~static_cast<size_t>(0); |
17 // Maximum positive size of a size_t if it were signed. | 15 // Maximum positive size of a size_t if it were signed. |
18 static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1); | 16 static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1); |
19 // An allocation size which is not too big to be reasonable. | 17 // An allocation size which is not too big to be reasonable. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 if (size == power-1) | 74 if (size == power-1) |
77 return power; | 75 return power; |
78 | 76 |
79 assert(size == power); | 77 assert(size == power); |
80 return power+1; | 78 return power+1; |
81 } else { | 79 } else { |
82 return -1; | 80 return -1; |
83 } | 81 } |
84 } | 82 } |
85 | 83 |
| 84 #define GG_ULONGLONG(x) static_cast<uint64>(x) |
| 85 |
86 template <class AtomicType> | 86 template <class AtomicType> |
87 static void TestAtomicIncrement() { | 87 static void TestAtomicIncrement() { |
88 // For now, we just test single threaded execution | 88 // For now, we just test single threaded execution |
89 | 89 |
90 // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go | 90 // use a guard value to make sure the NoBarrier_AtomicIncrement doesn't go |
91 // outside the expected address bounds. This is in particular to | 91 // outside the expected address bounds. This is in particular to |
92 // test that some future change to the asm code doesn't cause the | 92 // test that some future change to the asm code doesn't cause the |
93 // 32-bit NoBarrier_AtomicIncrement to do the wrong thing on 64-bit machines. | 93 // 32-bit NoBarrier_AtomicIncrement to do the wrong thing on 64-bit machines. |
94 struct { | 94 struct { |
95 AtomicType prev_word; | 95 AtomicType prev_word; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 157 |
158 template <class AtomicType> | 158 template <class AtomicType> |
159 static void TestCompareAndSwap() { | 159 static void TestCompareAndSwap() { |
160 AtomicType value = 0; | 160 AtomicType value = 0; |
161 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1); | 161 AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1); |
162 EXPECT_EQ(1, value); | 162 EXPECT_EQ(1, value); |
163 EXPECT_EQ(0, prev); | 163 EXPECT_EQ(0, prev); |
164 | 164 |
165 // Use test value that has non-zero bits in both halves, more for testing | 165 // Use test value that has non-zero bits in both halves, more for testing |
166 // 64-bit implementation on 32-bit platforms. | 166 // 64-bit implementation on 32-bit platforms. |
167 const AtomicType k_test_val = (static_cast<uint64_t>(1) << | 167 const AtomicType k_test_val = (GG_ULONGLONG(1) << |
168 (NUM_BITS(AtomicType) - 2)) + 11; | 168 (NUM_BITS(AtomicType) - 2)) + 11; |
169 value = k_test_val; | 169 value = k_test_val; |
170 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); | 170 prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5); |
171 EXPECT_EQ(k_test_val, value); | 171 EXPECT_EQ(k_test_val, value); |
172 EXPECT_EQ(k_test_val, prev); | 172 EXPECT_EQ(k_test_val, prev); |
173 | 173 |
174 value = k_test_val; | 174 value = k_test_val; |
175 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5); | 175 prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5); |
176 EXPECT_EQ(5, value); | 176 EXPECT_EQ(5, value); |
177 EXPECT_EQ(k_test_val, prev); | 177 EXPECT_EQ(k_test_val, prev); |
178 } | 178 } |
179 | 179 |
180 | 180 |
181 template <class AtomicType> | 181 template <class AtomicType> |
182 static void TestAtomicExchange() { | 182 static void TestAtomicExchange() { |
183 AtomicType value = 0; | 183 AtomicType value = 0; |
184 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1); | 184 AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1); |
185 EXPECT_EQ(1, value); | 185 EXPECT_EQ(1, value); |
186 EXPECT_EQ(0, new_value); | 186 EXPECT_EQ(0, new_value); |
187 | 187 |
188 // Use test value that has non-zero bits in both halves, more for testing | 188 // Use test value that has non-zero bits in both halves, more for testing |
189 // 64-bit implementation on 32-bit platforms. | 189 // 64-bit implementation on 32-bit platforms. |
190 const AtomicType k_test_val = (static_cast<uint64_t>(1) << | 190 const AtomicType k_test_val = (GG_ULONGLONG(1) << |
191 (NUM_BITS(AtomicType) - 2)) + 11; | 191 (NUM_BITS(AtomicType) - 2)) + 11; |
192 value = k_test_val; | 192 value = k_test_val; |
193 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val); | 193 new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val); |
194 EXPECT_EQ(k_test_val, value); | 194 EXPECT_EQ(k_test_val, value); |
195 EXPECT_EQ(k_test_val, new_value); | 195 EXPECT_EQ(k_test_val, new_value); |
196 | 196 |
197 value = k_test_val; | 197 value = k_test_val; |
198 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5); | 198 new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5); |
199 EXPECT_EQ(5, value); | 199 EXPECT_EQ(5, value); |
200 EXPECT_EQ(k_test_val, new_value); | 200 EXPECT_EQ(k_test_val, new_value); |
201 } | 201 } |
202 | 202 |
203 | 203 |
204 template <class AtomicType> | 204 template <class AtomicType> |
205 static void TestAtomicIncrementBounds() { | 205 static void TestAtomicIncrementBounds() { |
206 // Test increment at the half-width boundary of the atomic type. | 206 // Test increment at the half-width boundary of the atomic type. |
207 // It is primarily for testing at the 32-bit boundary for 64-bit atomic type. | 207 // It is primarily for testing at the 32-bit boundary for 64-bit atomic type. |
208 AtomicType test_val = static_cast<uint64_t>(1) << (NUM_BITS(AtomicType) / 2); | 208 AtomicType test_val = GG_ULONGLONG(1) << (NUM_BITS(AtomicType) / 2); |
209 AtomicType value = test_val - 1; | 209 AtomicType value = test_val - 1; |
210 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); | 210 AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1); |
211 EXPECT_EQ(test_val, value); | 211 EXPECT_EQ(test_val, value); |
212 EXPECT_EQ(value, new_value); | 212 EXPECT_EQ(value, new_value); |
213 | 213 |
214 base::subtle::NoBarrier_AtomicIncrement(&value, -1); | 214 base::subtle::NoBarrier_AtomicIncrement(&value, -1); |
215 EXPECT_EQ(test_val - 1, value); | 215 EXPECT_EQ(test_val - 1, value); |
216 } | 216 } |
217 | 217 |
218 // This is a simple sanity check that values are correct. Not testing | 218 // This is a simple sanity check that values are correct. Not testing |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 } | 512 } |
513 } | 513 } |
514 | 514 |
515 #endif | 515 #endif |
516 | 516 |
517 | 517 |
518 int main(int argc, char** argv) { | 518 int main(int argc, char** argv) { |
519 testing::InitGoogleTest(&argc, argv); | 519 testing::InitGoogleTest(&argc, argv); |
520 return RUN_ALL_TESTS(); | 520 return RUN_ALL_TESTS(); |
521 } | 521 } |
OLD | NEW |