1 //===-- Unittests for Atomic ----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/__support/CPP/atomic.h" 10 #include "utils/UnitTest/Test.h" 11 12 // Tests in this file do not test atomicity as it would require using 13 // threads, at which point it becomes a chicken and egg problem. 14 15 TEST(LlvmLibcAtomicTest, LoadStore) { 16 __llvm_libc::cpp::Atomic<int> aint(123); 17 ASSERT_EQ(aint.load(), 123); 18 19 aint.store(100); 20 ASSERT_EQ(aint.load(), 100); 21 22 aint = 1234; // Equivalent of store 23 ASSERT_EQ(aint.load(), 1234); 24 } 25 26 TEST(LlvmLibcAtomicTest, CompareExchangeStrong) { 27 int desired = 123; 28 __llvm_libc::cpp::Atomic<int> aint(desired); 29 ASSERT_TRUE(aint.compare_exchange_strong(desired, 100)); 30 ASSERT_EQ(aint.load(), 100); 31 32 ASSERT_FALSE(aint.compare_exchange_strong(desired, 100)); 33 ASSERT_EQ(aint.load(), 100); 34 } 35