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// UNSUPPORTED: libcpp-has-no-threads
11//  ... assertion fails line 38
12
13// <atomic>
14
15// template <class T>
16//     bool
17//     atomic_compare_exchange_strong_explicit(volatile atomic<T>* obj, T* expc,
18//                                           T desr,
19//                                           memory_order s, memory_order f);
20//
21// template <class T>
22//     bool
23//     atomic_compare_exchange_strong_explicit(atomic<T>* obj, T* expc, T desr,
24//                                           memory_order s, memory_order f);
25
26#include <atomic>
27#include <type_traits>
28#include <cassert>
29
30template <class T>
31void
32test()
33{
34    {
35        typedef std::atomic<T> A;
36        A a;
37        T t(T(1));
38        std::atomic_init(&a, t);
39        assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(2),
40               std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
41        assert(a == T(2));
42        assert(t == T(1));
43        assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(3),
44               std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
45        assert(a == T(2));
46        assert(t == T(2));
47    }
48    {
49        typedef std::atomic<T> A;
50        volatile A a;
51        T t(T(1));
52        std::atomic_init(&a, t);
53        assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(2),
54               std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
55        assert(a == T(2));
56        assert(t == T(1));
57        assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(3),
58               std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
59        assert(a == T(2));
60        assert(t == T(2));
61    }
62}
63
64struct A
65{
66    int i;
67
68    explicit A(int d = 0) noexcept {i=d;}
69
70    friend bool operator==(const A& x, const A& y)
71        {return x.i == y.i;}
72};
73
74int main()
75{
76    test<A>();
77    test<char>();
78    test<signed char>();
79    test<unsigned char>();
80    test<short>();
81    test<unsigned short>();
82    test<int>();
83    test<unsigned int>();
84    test<long>();
85    test<unsigned long>();
86    test<long long>();
87    test<unsigned long long>();
88    test<wchar_t>();
89#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
90    test<char16_t>();
91    test<char32_t>();
92#endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
93    test<int*>();
94    test<const int*>();
95}
96