| 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 | // atomic still has a difference_type in the C++03 frozen headers |
| 21 | // XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 22 | |
| 23 | #include <atomic> |
| 24 | #include <chrono> |
| 25 | #include <cstdint> |
| 26 | #include <memory> |
| 27 | #include <type_traits> |
| 28 | |
| 29 | #include "test_macros.h" |
| 30 | |
| 31 | #ifndef TEST_HAS_NO_THREADS |
| 32 | # include <thread> |
| 33 | #endif |
| 34 | |
| 35 | // detect existence of the difference_type member type |
| 36 | template <class...> |
| 37 | using myvoid_t = void; |
| 38 | template <typename T, typename = void> |
| 39 | struct has_difference_type : std::false_type {}; |
| 40 | template <typename T> |
| 41 | struct has_difference_type<T, myvoid_t<typename T::difference_type> > : std::true_type {}; |
| 42 | |
| 43 | template <class T, |
| 44 | bool Integral = (std::is_integral<T>::value && !std::is_same<T, bool>::value), |
| 45 | bool Floating = std::is_floating_point<T>::value, |
| 46 | bool Pointer = std::is_pointer<T>::value> |
| 47 | struct test_atomic { |
| 48 | test_atomic() { |
| 49 | static_assert(!Integral && !Floating && !Pointer, "" ); |
| 50 | using A = std::atomic<T>; |
| 51 | A a; |
| 52 | (void)a; |
| 53 | static_assert(std::is_same<typename A::value_type, T>::value, "" ); |
| 54 | static_assert(!has_difference_type<A>::value, "" ); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | template <class T> |
| 59 | struct test_atomic<T, /*Integral=*/true, false, false> { |
| 60 | test_atomic() { |
| 61 | static_assert(!std::is_same<T, bool>::value, "" ); |
| 62 | using A = std::atomic<T>; |
| 63 | A a; |
| 64 | (void)a; |
| 65 | static_assert(std::is_same<typename A::value_type, T>::value, "" ); |
| 66 | static_assert(std::is_same<typename A::difference_type, T>::value, "" ); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | template <class T> |
| 71 | struct test_atomic<T, false, /*Foating=*/true, false> { |
| 72 | test_atomic() { |
| 73 | using A = std::atomic<T>; |
| 74 | A a; |
| 75 | (void)a; |
| 76 | static_assert(std::is_same<typename A::value_type, T>::value, "" ); |
| 77 | #if TEST_STD_VER >= 20 |
| 78 | static_assert(std::is_same<typename A::difference_type, T>::value, "" ); |
| 79 | #else |
| 80 | static_assert(!has_difference_type<A>::value, "" ); |
| 81 | #endif |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | template <class T> |
| 86 | struct test_atomic<T, false, false, /*Pointer=*/true> { |
| 87 | test_atomic() { |
| 88 | using A = std::atomic<T>; |
| 89 | A a; |
| 90 | (void)a; |
| 91 | static_assert(std::is_same<typename A::value_type, T>::value, "" ); |
| 92 | static_assert(std::is_same<typename A::difference_type, std::ptrdiff_t>::value, "" ); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | template <class T> |
| 97 | void test() { |
| 98 | using A = std::atomic<T>; |
| 99 | static_assert(std::is_same<typename A::value_type, T>::value, "" ); |
| 100 | test_atomic<T>(); |
| 101 | } |
| 102 | |
| 103 | struct TriviallyCopyable { |
| 104 | int i_; |
| 105 | }; |
| 106 | |
| 107 | struct WeirdTriviallyCopyable { |
| 108 | char i, j, k; /* the 3 chars of doom */ |
| 109 | }; |
| 110 | |
| 111 | struct PaddedTriviallyCopyable { |
| 112 | char i; |
| 113 | int j; /* probably lock-free? */ |
| 114 | }; |
| 115 | |
| 116 | struct LargeTriviallyCopyable { |
| 117 | int i, j[127]; /* decidedly not lock-free */ |
| 118 | }; |
| 119 | |
| 120 | int main(int, char**) { |
| 121 | test<bool>(); |
| 122 | test<char>(); |
| 123 | test<signed char>(); |
| 124 | test<unsigned char>(); |
| 125 | test<short>(); |
| 126 | test<unsigned short>(); |
| 127 | test<int>(); |
| 128 | test<unsigned int>(); |
| 129 | test<long>(); |
| 130 | test<unsigned long>(); |
| 131 | test<long long>(); |
| 132 | test<unsigned long long>(); |
| 133 | #if TEST_STD_VER > 17 && defined(__cpp_char8_t) |
| 134 | test<char8_t>(); |
| 135 | #endif |
| 136 | test<char16_t>(); |
| 137 | test<char32_t>(); |
| 138 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 139 | test<wchar_t>(); |
| 140 | #endif |
| 141 | |
| 142 | test<std::int_least8_t>(); |
| 143 | test<std::uint_least8_t>(); |
| 144 | test<std::int_least16_t>(); |
| 145 | test<std::uint_least16_t>(); |
| 146 | test<std::int_least32_t>(); |
| 147 | test<std::uint_least32_t>(); |
| 148 | test<std::int_least64_t>(); |
| 149 | test<std::uint_least64_t>(); |
| 150 | |
| 151 | test<std::int_fast8_t>(); |
| 152 | test<std::uint_fast8_t>(); |
| 153 | test<std::int_fast16_t>(); |
| 154 | test<std::uint_fast16_t>(); |
| 155 | test<std::int_fast32_t>(); |
| 156 | test<std::uint_fast32_t>(); |
| 157 | test<std::int_fast64_t>(); |
| 158 | test<std::uint_fast64_t>(); |
| 159 | |
| 160 | test< std::int8_t>(); |
| 161 | test<std::uint8_t>(); |
| 162 | test< std::int16_t>(); |
| 163 | test<std::uint16_t>(); |
| 164 | test< std::int32_t>(); |
| 165 | test<std::uint32_t>(); |
| 166 | test< std::int64_t>(); |
| 167 | test<std::uint64_t>(); |
| 168 | |
| 169 | test<std::intptr_t>(); |
| 170 | test<std::uintptr_t>(); |
| 171 | test<std::size_t>(); |
| 172 | test<std::ptrdiff_t>(); |
| 173 | test<std::intmax_t>(); |
| 174 | test<std::uintmax_t>(); |
| 175 | |
| 176 | test<std::uintmax_t>(); |
| 177 | test<std::uintmax_t>(); |
| 178 | |
| 179 | test<void (*)(int)>(); |
| 180 | test<void*>(); |
| 181 | test<const void*>(); |
| 182 | test<int*>(); |
| 183 | test<const int*>(); |
| 184 | |
| 185 | test<TriviallyCopyable>(); |
| 186 | test<PaddedTriviallyCopyable>(); |
| 187 | #ifndef __APPLE__ // Apple doesn't ship libatomic |
| 188 | /* |
| 189 | These aren't going to be lock-free, |
| 190 | so some libatomic.a is necessary. |
| 191 | */ |
| 192 | test<WeirdTriviallyCopyable>(); |
| 193 | test<LargeTriviallyCopyable>(); |
| 194 | #endif |
| 195 | |
| 196 | #ifndef TEST_HAS_NO_THREADS |
| 197 | test<std::thread::id>(); |
| 198 | #endif |
| 199 | test<std::chrono::nanoseconds>(); |
| 200 | test<float>(); |
| 201 | |
| 202 | #if TEST_STD_VER >= 20 |
| 203 | test<std::atomic_signed_lock_free::value_type>(); |
| 204 | static_assert(std::is_signed_v<std::atomic_signed_lock_free::value_type>); |
| 205 | static_assert(std::is_integral_v<std::atomic_signed_lock_free::value_type>); |
| 206 | |
| 207 | test<std::atomic_unsigned_lock_free::value_type>(); |
| 208 | static_assert(std::is_unsigned_v<std::atomic_unsigned_lock_free::value_type>); |
| 209 | static_assert(std::is_integral_v<std::atomic_unsigned_lock_free::value_type>); |
| 210 | /* |
| 211 | test<std::shared_ptr<int>>(); |
| 212 | */ |
| 213 | #endif |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |