e94f191e9532ad8e02cf041ec0237de11409a5d1
[llvm-toolchain-10.git] /
1 //===----------------------------------------------------------------------===//
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 // UNSUPPORTED: libcpp-has-no-threads
10 //  ... assertion fails line 38
11
12 // <atomic>
13
14 // template <class T>
15 //     bool
16 //     atomic_compare_exchange_strong_explicit(volatile atomic<T>* obj, T* expc,
17 //                                           T desr,
18 //                                           memory_order s, memory_order f);
19 //
20 // template <class T>
21 //     bool
22 //     atomic_compare_exchange_strong_explicit(atomic<T>* obj, T* expc, T desr,
23 //                                           memory_order s, memory_order f);
24
25 #include <atomic>
26 #include <type_traits>
27 #include <cassert>
28
29 #include "test_macros.h"
30 #include "atomic_helpers.h"
31
32 template <class T>
33 struct TestFn {
34   void operator()() const {
35     {
36         typedef std::atomic<T> A;
37         A a;
38         T t(T(1));
39         std::atomic_init(&a, t);
40         assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(2),
41                std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
42         assert(a == T(2));
43         assert(t == T(1));
44         assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(3),
45                std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
46         assert(a == T(2));
47         assert(t == T(2));
48     }
49     {
50         typedef std::atomic<T> A;
51         volatile A a;
52         T t(T(1));
53         std::atomic_init(&a, t);
54         assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(2),
55                std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
56         assert(a == T(2));
57         assert(t == T(1));
58         assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(3),
59                std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
60         assert(a == T(2));
61         assert(t == T(2));
62     }
63   }
64 };
65
66 int main(int, char**)
67 {
68     TestEachAtomicType<TestFn>()();
69
70   return 0;
71 }