1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <atomic>
10
11// Test nested types
12
13// template <class T>
14// class atomic
15// {
16// public:
17// typedef T value_type;
18// };
19
20#include <atomic>
21#include <chrono>
22#include <memory>
23#include <type_traits>
24
25#include "test_macros.h"
26
27#ifndef TEST_HAS_NO_THREADS
28# include <thread>
29#endif
30
31template <class A, bool Integral>
32struct test_atomic
33{
34 test_atomic()
35 {
36 A a; (void)a;
37#if TEST_STD_VER >= 17
38 static_assert((std::is_same_v<typename A::value_type, decltype(a.load())>), "");
39#endif
40 }
41};
42
43template <class A>
44struct test_atomic<A, true>
45{
46 test_atomic()
47 {
48 A a; (void)a;
49#if TEST_STD_VER >= 17
50 static_assert((std::is_same_v<typename A::value_type, decltype(a.load())>), "");
51 static_assert((std::is_same_v<typename A::value_type, typename A::difference_type>), "");
52#endif
53 }
54};
55
56template <class A>
57struct test_atomic<A*, false>
58{
59 test_atomic()
60 {
61 A a; (void)a;
62#if TEST_STD_VER >= 17
63 static_assert((std::is_same_v<typename A::value_type, decltype(a.load())>), "");
64 static_assert((std::is_same_v<typename A::difference_type, std::ptrdiff_t>), "");
65#endif
66 }
67};
68
69template <class T>
70void
71test()
72{
73 using A = std::atomic<T>;
74#if TEST_STD_VER >= 17
75 static_assert((std::is_same_v<typename A::value_type, T>), "");
76#endif
77 test_atomic<A, std::is_integral<T>::value && !std::is_same<T, bool>::value>();
78}
79
80struct TriviallyCopyable {
81 int i_;
82};
83
84struct WeirdTriviallyCopyable
85{
86 char i, j, k; /* the 3 chars of doom */
87};
88
89struct PaddedTriviallyCopyable
90{
91 char i; int j; /* probably lock-free? */
92};
93
94struct LargeTriviallyCopyable
95{
96 int i, j[127]; /* decidedly not lock-free */
97};
98
99int main(int, char**)
100{
101 test<bool> ();
102 test<char> ();
103 test<signed char> ();
104 test<unsigned char> ();
105 test<short> ();
106 test<unsigned short> ();
107 test<int> ();
108 test<unsigned int> ();
109 test<long> ();
110 test<unsigned long> ();
111 test<long long> ();
112 test<unsigned long long> ();
113#if TEST_STD_VER > 17 && defined(__cpp_char8_t)
114 test<char8_t> ();
115#endif
116 test<char16_t> ();
117 test<char32_t> ();
118#ifndef TEST_HAS_NO_WIDE_CHARACTERS
119 test<wchar_t> ();
120#endif
121
122 test<std::int_least8_t> ();
123 test<std::uint_least8_t> ();
124 test<std::int_least16_t> ();
125 test<std::uint_least16_t> ();
126 test<std::int_least32_t> ();
127 test<std::uint_least32_t> ();
128 test<std::int_least64_t> ();
129 test<std::uint_least64_t> ();
130
131 test<std::int_fast8_t> ();
132 test<std::uint_fast8_t> ();
133 test<std::int_fast16_t> ();
134 test<std::uint_fast16_t> ();
135 test<std::int_fast32_t> ();
136 test<std::uint_fast32_t> ();
137 test<std::int_fast64_t> ();
138 test<std::uint_fast64_t> ();
139
140 test< std::int8_t> ();
141 test<std::uint8_t> ();
142 test< std::int16_t> ();
143 test<std::uint16_t> ();
144 test< std::int32_t> ();
145 test<std::uint32_t> ();
146 test< std::int64_t> ();
147 test<std::uint64_t> ();
148
149 test<std::intptr_t> ();
150 test<std::uintptr_t> ();
151 test<std::size_t> ();
152 test<std::ptrdiff_t> ();
153 test<std::intmax_t> ();
154 test<std::uintmax_t> ();
155
156 test<std::uintmax_t> ();
157 test<std::uintmax_t> ();
158
159 test<TriviallyCopyable>();
160 test<PaddedTriviallyCopyable>();
161#ifndef __APPLE__ // Apple doesn't ship libatomic
162 /*
163 These aren't going to be lock-free,
164 so some libatomic.a is necessary.
165 */
166 test<WeirdTriviallyCopyable>();
167 test<LargeTriviallyCopyable>();
168#endif
169
170#ifndef TEST_HAS_NO_THREADS
171 test<std::thread::id>();
172#endif
173 test<std::chrono::nanoseconds>();
174 test<float>();
175
176#if TEST_STD_VER >= 20
177 test<std::atomic_signed_lock_free::value_type>();
178 static_assert(std::is_signed_v<std::atomic_signed_lock_free::value_type>);
179 static_assert(std::is_integral_v<std::atomic_signed_lock_free::value_type>);
180
181 test<std::atomic_unsigned_lock_free::value_type>();
182 static_assert(std::is_unsigned_v<std::atomic_unsigned_lock_free::value_type>);
183 static_assert(std::is_integral_v<std::atomic_unsigned_lock_free::value_type>);
184/*
185 test<std::shared_ptr<int>>();
186*/
187#endif
188
189 return 0;
190}
191

source code of libcxx/test/std/atomics/types.pass.cpp