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 //     T
14 //     atomic_exchange_explicit(volatile atomic<T>* obj, T desr, memory_order m);
15 //
16 // template <class T>
17 //     T
18 //     atomic_exchange_explicit(atomic<T>* obj, T desr, memory_order m);
19 
20 #include <atomic>
21 #include <type_traits>
22 #include <cassert>
23 
24 template <class T>
25 void
test()26 test()
27 {
28     typedef std::atomic<T> A;
29     A t;
30     std::atomic_init(&t, T(1));
31     assert(std::atomic_exchange_explicit(&t, T(2), std::memory_order_seq_cst)
32            == T(1));
33     assert(t == T(2));
34     volatile A vt;
35     std::atomic_init(&vt, T(3));
36     assert(std::atomic_exchange_explicit(&vt, T(4), std::memory_order_seq_cst)
37            == T(3));
38     assert(vt == T(4));
39 }
40 
41 struct A
42 {
43     int i;
44 
AA45     explicit A(int d = 0) : i(d) {}
46 
operator ==(const A & x,const A & y)47     friend bool operator==(const A& x, const A& y)
48         {return x.i == y.i;}
49 };
50 
main()51 int main()
52 {
53     test<A>();
54     test<char>();
55     test<signed char>();
56     test<unsigned char>();
57     test<short>();
58     test<unsigned short>();
59     test<int>();
60     test<unsigned int>();
61     test<long>();
62     test<unsigned long>();
63     test<long long>();
64     test<unsigned long long>();
65     test<wchar_t>();
66 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
67     test<char16_t>();
68     test<char32_t>();
69 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
70     test<int*>();
71     test<const int*>();
72 }
73