This source file includes following definitions.
- TestAtomicIncrement
- TestCompareAndSwap
- TestAtomicExchange
- TestAtomicIncrementBounds
- TestStore
- TestLoad
- TestAtomicOps
- main
#include <stdio.h>
#include "base/logging.h"
#include "base/atomicops.h"
#define GG_ULONGLONG(x) static_cast<uint64>(x)
template <class AtomicType>
static void TestAtomicIncrement() {
struct {
AtomicType prev_word;
AtomicType count;
AtomicType next_word;
} s;
AtomicType prev_word_value, next_word_value;
memset(&prev_word_value, 0xFF, sizeof(AtomicType));
memset(&next_word_value, 0xEE, sizeof(AtomicType));
s.prev_word = prev_word_value;
s.count = 0;
s.next_word = next_word_value;
ASSERT_EQ(1, base::subtle::NoBarrier_AtomicIncrement(&s.count, 1));
ASSERT_EQ(1, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(3, base::subtle::NoBarrier_AtomicIncrement(&s.count, 2));
ASSERT_EQ(3, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(6, base::subtle::NoBarrier_AtomicIncrement(&s.count, 3));
ASSERT_EQ(6, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(3, base::subtle::NoBarrier_AtomicIncrement(&s.count, -3));
ASSERT_EQ(3, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(1, base::subtle::NoBarrier_AtomicIncrement(&s.count, -2));
ASSERT_EQ(1, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(0, base::subtle::NoBarrier_AtomicIncrement(&s.count, -1));
ASSERT_EQ(0, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(-1, base::subtle::NoBarrier_AtomicIncrement(&s.count, -1));
ASSERT_EQ(-1, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(-5, base::subtle::NoBarrier_AtomicIncrement(&s.count, -4));
ASSERT_EQ(-5, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
ASSERT_EQ(0, base::subtle::NoBarrier_AtomicIncrement(&s.count, 5));
ASSERT_EQ(0, s.count);
ASSERT_EQ(prev_word_value, s.prev_word);
ASSERT_EQ(next_word_value, s.next_word);
}
#define NUM_BITS(T) (sizeof(T) * 8)
template <class AtomicType>
static void TestCompareAndSwap() {
AtomicType value = 0;
AtomicType prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 1);
ASSERT_EQ(1, value);
ASSERT_EQ(0, prev);
const AtomicType k_test_val = (GG_ULONGLONG(1) <<
(NUM_BITS(AtomicType) - 2)) + 11;
value = k_test_val;
prev = base::subtle::NoBarrier_CompareAndSwap(&value, 0, 5);
ASSERT_EQ(k_test_val, value);
ASSERT_EQ(k_test_val, prev);
value = k_test_val;
prev = base::subtle::NoBarrier_CompareAndSwap(&value, k_test_val, 5);
ASSERT_EQ(5, value);
ASSERT_EQ(k_test_val, prev);
}
template <class AtomicType>
static void TestAtomicExchange() {
AtomicType value = 0;
AtomicType new_value = base::subtle::NoBarrier_AtomicExchange(&value, 1);
ASSERT_EQ(1, value);
ASSERT_EQ(0, new_value);
const AtomicType k_test_val = (GG_ULONGLONG(1) <<
(NUM_BITS(AtomicType) - 2)) + 11;
value = k_test_val;
new_value = base::subtle::NoBarrier_AtomicExchange(&value, k_test_val);
ASSERT_EQ(k_test_val, value);
ASSERT_EQ(k_test_val, new_value);
value = k_test_val;
new_value = base::subtle::NoBarrier_AtomicExchange(&value, 5);
ASSERT_EQ(5, value);
ASSERT_EQ(k_test_val, new_value);
}
template <class AtomicType>
static void TestAtomicIncrementBounds() {
AtomicType test_val = GG_ULONGLONG(1) << (NUM_BITS(AtomicType) / 2);
AtomicType value = test_val - 1;
AtomicType new_value = base::subtle::NoBarrier_AtomicIncrement(&value, 1);
ASSERT_EQ(test_val, value);
ASSERT_EQ(value, new_value);
base::subtle::NoBarrier_AtomicIncrement(&value, -1);
ASSERT_EQ(test_val - 1, value);
}
template <class AtomicType>
static void TestStore() {
const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL);
const AtomicType kVal2 = static_cast<AtomicType>(-1);
AtomicType value;
base::subtle::NoBarrier_Store(&value, kVal1);
ASSERT_EQ(kVal1, value);
base::subtle::NoBarrier_Store(&value, kVal2);
ASSERT_EQ(kVal2, value);
base::subtle::Acquire_Store(&value, kVal1);
ASSERT_EQ(kVal1, value);
base::subtle::Acquire_Store(&value, kVal2);
ASSERT_EQ(kVal2, value);
base::subtle::Release_Store(&value, kVal1);
ASSERT_EQ(kVal1, value);
base::subtle::Release_Store(&value, kVal2);
ASSERT_EQ(kVal2, value);
}
template <class AtomicType>
static void TestLoad() {
const AtomicType kVal1 = static_cast<AtomicType>(0xa5a5a5a5a5a5a5a5LL);
const AtomicType kVal2 = static_cast<AtomicType>(-1);
AtomicType value;
value = kVal1;
ASSERT_EQ(kVal1, base::subtle::NoBarrier_Load(&value));
value = kVal2;
ASSERT_EQ(kVal2, base::subtle::NoBarrier_Load(&value));
value = kVal1;
ASSERT_EQ(kVal1, base::subtle::Acquire_Load(&value));
value = kVal2;
ASSERT_EQ(kVal2, base::subtle::Acquire_Load(&value));
value = kVal1;
ASSERT_EQ(kVal1, base::subtle::Release_Load(&value));
value = kVal2;
ASSERT_EQ(kVal2, base::subtle::Release_Load(&value));
}
template <class AtomicType>
static void TestAtomicOps() {
TestCompareAndSwap<AtomicType>();
TestAtomicExchange<AtomicType>();
TestAtomicIncrementBounds<AtomicType>();
TestStore<AtomicType>();
TestLoad<AtomicType>();
}
int main(int argc, char** argv) {
TestAtomicIncrement<AtomicWord>();
TestAtomicIncrement<Atomic32>();
TestAtomicOps<AtomicWord>();
TestAtomicOps<Atomic32>();
#if 0 and defined(BASE_HAS_ATOMIC64)
TestAtomicIncrement<base::subtle::Atomic64>();
TestAtomicOps<base::subtle::Atomic64>();
#endif
printf("PASS\n");
return 0;
}