1 //===-- sanitizer_atomic_test.cc ------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_atomic.h"
14 #include "gtest/gtest.h"
15
16 namespace __sanitizer {
17
18 // Clang crashes while compiling this test for Android:
19 // http://llvm.org/bugs/show_bug.cgi?id=15587
20 #if !SANITIZER_ANDROID
21 template<typename T>
CheckAtomicCompareExchange()22 void CheckAtomicCompareExchange() {
23 typedef typename T::Type Type;
24 {
25 Type old_val = 42;
26 Type new_val = 24;
27 Type var = old_val;
28 EXPECT_TRUE(atomic_compare_exchange_strong((T*)&var, &old_val, new_val,
29 memory_order_relaxed));
30 EXPECT_FALSE(atomic_compare_exchange_strong((T*)&var, &old_val, new_val,
31 memory_order_relaxed));
32 EXPECT_EQ(new_val, old_val);
33 }
34 {
35 Type old_val = 42;
36 Type new_val = 24;
37 Type var = old_val;
38 EXPECT_TRUE(atomic_compare_exchange_weak((T*)&var, &old_val, new_val,
39 memory_order_relaxed));
40 EXPECT_FALSE(atomic_compare_exchange_weak((T*)&var, &old_val, new_val,
41 memory_order_relaxed));
42 EXPECT_EQ(new_val, old_val);
43 }
44 }
45
TEST(SanitizerCommon,AtomicCompareExchangeTest)46 TEST(SanitizerCommon, AtomicCompareExchangeTest) {
47 CheckAtomicCompareExchange<atomic_uint8_t>();
48 CheckAtomicCompareExchange<atomic_uint16_t>();
49 CheckAtomicCompareExchange<atomic_uint32_t>();
50 CheckAtomicCompareExchange<atomic_uint64_t>();
51 CheckAtomicCompareExchange<atomic_uintptr_t>();
52 }
53 #endif //!SANITIZER_ANDROID
54
55 } // namespace __sanitizer
56