blob: f667ab7f139b1f1af46c175d00be058ae3db4eca [file] [log] [blame]
Howard Hinnant4777bf22010-12-06 23:10:08 +00001//===----------------------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
11// ... assertion fails line 38
Howard Hinnant4777bf22010-12-06 23:10:08 +000012
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>
Richard Smith9efdc0b2012-04-19 00:50:47 +000027#include <type_traits>
Howard Hinnant4777bf22010-12-06 23:10:08 +000028#include <cassert>
29
30template <class T>
Dan Albert1d4a1ed2016-05-25 22:36:09 -070031void
32test()
33{
Howard Hinnant4777bf22010-12-06 23:10:08 +000034 {
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));
David Chisnalld3eca752012-04-05 13:48:16 +000046 assert(t == T(2));
Howard Hinnant4777bf22010-12-06 23:10:08 +000047 }
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));
David Chisnalld3eca752012-04-05 13:48:16 +000060 assert(t == T(2));
Howard Hinnant4777bf22010-12-06 23:10:08 +000061 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070062}
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;}
Howard Hinnant4777bf22010-12-06 23:10:08 +000072};
73
74int main()
75{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070076 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*>();
Howard Hinnant4777bf22010-12-06 23:10:08 +000095}