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 Integral>
13 //     Integral
14 //     atomic_fetch_and_explicit(volatile atomic<Integral>* obj, Integral op);
15 //
16 // template <class Integral>
17 //     Integral
18 //     atomic_fetch_and_explicit(atomic<Integral>* obj, Integral op);
19 
20 #include <atomic>
21 #include <type_traits>
22 #include <cassert>
23 
24 template <class T>
25 void
26 test()
27 {
28     {
29         typedef std::atomic<T> A;
30         A t;
31         std::atomic_init(&t, T(1));
32         assert(std::atomic_fetch_and_explicit(&t, T(2),
33                std::memory_order_seq_cst) == T(1));
34         assert(t == T(0));
35     }
36     {
37         typedef std::atomic<T> A;
38         volatile A t;
39         std::atomic_init(&t, T(3));
40         assert(std::atomic_fetch_and_explicit(&t, T(2),
41                std::memory_order_seq_cst) == T(3));
42         assert(t == T(2));
43     }
44 }
45 
46 int main()
47 {
48     test<char>();
49     test<signed char>();
50     test<unsigned char>();
51     test<short>();
52     test<unsigned short>();
53     test<int>();
54     test<unsigned int>();
55     test<long>();
56     test<unsigned long>();
57     test<long long>();
58     test<unsigned long long>();
59     test<wchar_t>();
60 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
61     test<char16_t>();
62     test<char32_t>();
63 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
64 }
65