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 "test/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 | LIBC_NAMESPACE::cpp::Atomic<int> aint(123); |
17 | ASSERT_EQ(aint.load(LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED), 123); |
18 | |
19 | aint.store(rhs: 100, LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED); |
20 | ASSERT_EQ(aint.load(LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED), 100); |
21 | |
22 | aint = 1234; // Equivalent of store |
23 | ASSERT_EQ(aint.load(LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED), 1234); |
24 | } |
25 | |
26 | TEST(LlvmLibcAtomicTest, CompareExchangeStrong) { |
27 | int desired = 123; |
28 | LIBC_NAMESPACE::cpp::Atomic<int> aint(desired); |
29 | ASSERT_TRUE(aint.compare_exchange_strong(desired, 100)); |
30 | ASSERT_EQ(aint.load(LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED), 100); |
31 | |
32 | ASSERT_FALSE(aint.compare_exchange_strong(desired, 100)); |
33 | ASSERT_EQ(aint.load(LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED), 100); |
34 | } |
35 | |