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