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 
31 template <class A, bool Integral>
32 struct test_atomic
33 {
test_atomictest_atomic34     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 
43 template <class A>
44 struct test_atomic<A, true>
45 {
test_atomictest_atomic46     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 
56 template <class A>
57 struct test_atomic<A*, false>
58 {
test_atomictest_atomic59     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, ptrdiff_t>), "");
65 #endif
66     }
67 };
68 
69 template <class T>
70 void
test()71 test()
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 
80 struct TriviallyCopyable {
81     int i_;
82 };
83 
84 struct WeirdTriviallyCopyable
85 {
86     char i, j, k; /* the 3 chars of doom */
87 };
88 
89 struct PaddedTriviallyCopyable
90 {
91     char i; int j; /* probably lock-free? */
92 };
93 
94 struct LargeTriviallyCopyable
95 {
96     int i, j[127]; /* decidedly not lock-free */
97 };
98 
main(int,char **)99 int 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<int_least8_t>   ();
123     test<uint_least8_t>  ();
124     test<int_least16_t>  ();
125     test<uint_least16_t> ();
126     test<int_least32_t>  ();
127     test<uint_least32_t> ();
128     test<int_least64_t>  ();
129     test<uint_least64_t> ();
130 
131     test<int_fast8_t>   ();
132     test<uint_fast8_t>  ();
133     test<int_fast16_t>  ();
134     test<uint_fast16_t> ();
135     test<int_fast32_t>  ();
136     test<uint_fast32_t> ();
137     test<int_fast64_t>  ();
138     test<uint_fast64_t> ();
139 
140     test< int8_t>  ();
141     test<uint8_t>  ();
142     test< int16_t> ();
143     test<uint16_t> ();
144     test< int32_t> ();
145     test<uint32_t> ();
146     test< int64_t> ();
147     test<uint64_t> ();
148 
149     test<intptr_t>  ();
150     test<uintptr_t> ();
151     test<size_t>    ();
152     test<ptrdiff_t> ();
153     test<intmax_t>  ();
154     test<uintmax_t> ();
155 
156     test<uintmax_t> ();
157     test<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     test<std::atomic_unsigned_lock_free::value_type>();
179 /*
180     test<std::shared_ptr<int>>();
181 */
182 #endif
183 
184     return 0;
185 }
186