1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <atomic>
11 
12 // template <class T>
13 //     bool
14 //     atomic_compare_exchange_weak_explicit(volatile atomic<T>* obj, T* expc,
15 //                                           T desr,
16 //                                           memory_order s, memory_order f);
17 //
18 // template <class T>
19 //     bool
20 //     atomic_compare_exchange_weak_explicit(atomic<T>* obj, T* expc, T desr,
21 //                                           memory_order s, memory_order f);
22 
23 #include <atomic>
24 #include <type_traits>
25 #include <cassert>
26 
27 template <class T>
28 void
29 test()
30 {
31     {
32         typedef std::atomic<T> A;
33         A a;
34         T t(T(1));
35         std::atomic_init(&a, t);
36         assert(std::atomic_compare_exchange_weak_explicit(&a, &t, T(2),
37                std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
38         assert(a == T(2));
39         assert(t == T(1));
40         assert(std::atomic_compare_exchange_weak_explicit(&a, &t, T(3),
41                std::memory_order_seq_cst, std::memory_order_seq_cst) == 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(std::atomic_compare_exchange_weak_explicit(&a, &t, T(2),
51                std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
52         assert(a == T(2));
53         assert(t == T(1));
54         assert(std::atomic_compare_exchange_weak_explicit(&a, &t, T(3),
55                std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
56         assert(a == T(2));
57         assert(t == T(2));
58     }
59 }
60 
61 struct A
62 {
63     int i;
64 
65     explicit A(int d = 0) : i(d) {}
66 
67     friend bool operator==(const A& x, const A& y)
68         {return x.i == y.i;}
69 };
70 
71 int main()
72 {
73     test<A>();
74     test<char>();
75     test<signed char>();
76     test<unsigned char>();
77     test<short>();
78     test<unsigned short>();
79     test<int>();
80     test<unsigned int>();
81     test<long>();
82     test<unsigned long>();
83     test<long long>();
84     test<unsigned long long>();
85     test<wchar_t>();
86 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
87     test<char16_t>();
88     test<char32_t>();
89 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
90     test<int*>();
91     test<const int*>();
92 }
93