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