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(volatile atomic<T>* obj, T desr);
15 //
16 // template <class T>
17 //     T
18 //     atomic_exchange(atomic<T>* obj, T desr);
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(&t, T(2)) == T(1));
32     assert(t == T(2));
33     volatile A vt;
34     std::atomic_init(&vt, T(3));
35     assert(std::atomic_exchange(&vt, T(4)) == T(3));
36     assert(vt == T(4));
37 }
38 
39 struct A
40 {
41     int i;
42 
AA43     explicit A(int d = 0) : i(d) {}
44 
operator ==(const A & x,const A & y)45     friend bool operator==(const A& x, const A& y)
46         {return x.i == y.i;}
47 };
48 
main()49 int main()
50 {
51     test<A>();
52     test<char>();
53     test<signed char>();
54     test<unsigned char>();
55     test<short>();
56     test<unsigned short>();
57     test<int>();
58     test<unsigned int>();
59     test<long>();
60     test<unsigned long>();
61     test<long long>();
62     test<unsigned long long>();
63     test<wchar_t>();
64 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
65     test<char16_t>();
66     test<char32_t>();
67 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
68     test<int*>();
69     test<const int*>();
70 }
71