5379f776fa28b1825a735694ca1a98198ec75317
[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 34
11
12 // <atomic>
13
14 // template <class T>
15 //     bool
16 //     atomic_compare_exchange_weak(volatile atomic<T>* obj, T* expc, T desr);
17 //
18 // template <class T>
19 //     bool
20 //     atomic_compare_exchange_weak(atomic<T>* obj, T* expc, T desr);
21
22 #include <atomic>
23 #include <type_traits>
24 #include <cassert>
25
26 #include <cmpxchg_loop.h>
27 #include "test_macros.h"
28 #include "atomic_helpers.h"
29
30 template <class T>
31 struct TestFn {
32   void operator()() const {
33     {
34         typedef std::atomic<T> A;
35         A a;
36         T t(T(1));
37         std::atomic_init(&a, t);
38         assert(c_cmpxchg_weak_loop(&a, &t, T(2)) == true);
39         assert(a == T(2));
40         assert(t == T(1));
41         assert(std::atomic_compare_exchange_weak(&a, &t, T(3)) == false);
42         assert(a == T(2));
43         assert(t == T(2));
44     }
45     {
46         typedef std::atomic<T> A;
47         volatile A a;
48         T t(T(1));
49         std::atomic_init(&a, t);
50         assert(c_cmpxchg_weak_loop(&a, &t, T(2)) == true);
51         assert(a == T(2));
52         assert(t == T(1));
53         assert(std::atomic_compare_exchange_weak(&a, &t, T(3)) == false);
54         assert(a == T(2));
55         assert(t == T(2));
56     }
57   }
58 };
59
60 int main(int, char**)
61 {
62     TestEachAtomicType<TestFn>()();
63
64   return 0;
65 }