| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 9 | |
| 10 | // <atomic> |
| 11 | |
| 12 | // template <class T> |
| 13 | // struct atomic_ref |
| 14 | // { |
| 15 | // using value_type = T; |
| 16 | // using difference_type = value_type; // only for atomic_ref<Integral> and |
| 17 | // // atomic_ref<Floating> specializations |
| 18 | // using difference_type = std::ptrdiff_t; // only for atomic_ref<T*> specializations |
| 19 | // |
| 20 | // explicit atomic_ref(T&); |
| 21 | // atomic_ref(const atomic_ref&) noexcept; |
| 22 | // atomic_ref& operator=(const atomic_ref&) = delete; |
| 23 | // }; |
| 24 | |
| 25 | #include <atomic> |
| 26 | #include <cstddef> |
| 27 | #include <cstdint> |
| 28 | #include <type_traits> |
| 29 | |
| 30 | #include "test_macros.h" |
| 31 | |
| 32 | template <class T> |
| 33 | concept has_difference_type = requires { typename T::difference_type; }; |
| 34 | |
| 35 | template <class T> |
| 36 | void check_member_types() { |
| 37 | if constexpr ((std::is_integral_v<T> && !std::is_same_v<T, bool>) || std::is_floating_point_v<T>) { |
| 38 | ASSERT_SAME_TYPE(typename std::atomic_ref<T>::value_type, T); |
| 39 | ASSERT_SAME_TYPE(typename std::atomic_ref<T>::difference_type, T); |
| 40 | } else if constexpr (std::is_pointer_v<T>) { |
| 41 | ASSERT_SAME_TYPE(typename std::atomic_ref<T>::value_type, T); |
| 42 | ASSERT_SAME_TYPE(typename std::atomic_ref<T>::difference_type, std::ptrdiff_t); |
| 43 | } else { |
| 44 | ASSERT_SAME_TYPE(typename std::atomic_ref<T>::value_type, T); |
| 45 | static_assert(!has_difference_type<std::atomic_ref<T>>); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | template <class T> |
| 50 | void test() { |
| 51 | // value_type and difference_type (except for primary template) |
| 52 | check_member_types<T>(); |
| 53 | |
| 54 | static_assert(std::is_nothrow_copy_constructible_v<std::atomic_ref<T>>); |
| 55 | |
| 56 | static_assert(!std::is_copy_assignable_v<std::atomic_ref<T>>); |
| 57 | |
| 58 | // explicit constructor |
| 59 | static_assert(!std::is_convertible_v<T, std::atomic_ref<T>>); |
| 60 | static_assert(std::is_constructible_v<std::atomic_ref<T>, T&>); |
| 61 | } |
| 62 | |
| 63 | void testall() { |
| 64 | // Primary template |
| 65 | struct Empty {}; |
| 66 | test<Empty>(); |
| 67 | struct Trivial { |
| 68 | int a; |
| 69 | float b; |
| 70 | }; |
| 71 | test<Trivial>(); |
| 72 | test<bool>(); |
| 73 | |
| 74 | // Partial specialization for pointer types |
| 75 | test<void*>(); |
| 76 | |
| 77 | // Specialization for integral types |
| 78 | // + character types |
| 79 | test<char>(); |
| 80 | test<char8_t>(); |
| 81 | test<char16_t>(); |
| 82 | test<char32_t>(); |
| 83 | test<wchar_t>(); |
| 84 | // + standard signed integer types |
| 85 | test<signed char>(); |
| 86 | test<short>(); |
| 87 | test<int>(); |
| 88 | test<long>(); |
| 89 | test<long long>(); |
| 90 | // + standard unsigned integer types |
| 91 | test<unsigned char>(); |
| 92 | test<unsigned short>(); |
| 93 | test<unsigned int>(); |
| 94 | test<unsigned long>(); |
| 95 | test<unsigned long long>(); |
| 96 | // + any other types needed by the typedefs in the header <cstdint> |
| 97 | test<std::int8_t>(); |
| 98 | test<std::int16_t>(); |
| 99 | test<std::int32_t>(); |
| 100 | test<std::int64_t>(); |
| 101 | test<std::int_fast8_t>(); |
| 102 | test<std::int_fast16_t>(); |
| 103 | test<std::int_fast32_t>(); |
| 104 | test<std::int_fast64_t>(); |
| 105 | test<std::int_least8_t>(); |
| 106 | test<std::int_least16_t>(); |
| 107 | test<std::int_least32_t>(); |
| 108 | test<std::int_least64_t>(); |
| 109 | test<std::intmax_t>(); |
| 110 | test<std::intptr_t>(); |
| 111 | test<std::uint8_t>(); |
| 112 | test<std::uint16_t>(); |
| 113 | test<std::uint32_t>(); |
| 114 | test<std::uint64_t>(); |
| 115 | test<std::uint_fast8_t>(); |
| 116 | test<std::uint_fast16_t>(); |
| 117 | test<std::uint_fast32_t>(); |
| 118 | test<std::uint_fast64_t>(); |
| 119 | test<std::uint_least8_t>(); |
| 120 | test<std::uint_least16_t>(); |
| 121 | test<std::uint_least32_t>(); |
| 122 | test<std::uint_least64_t>(); |
| 123 | test<std::uintmax_t>(); |
| 124 | test<std::uintptr_t>(); |
| 125 | |
| 126 | // Specialization for floating-point types |
| 127 | // + floating-point types |
| 128 | test<float>(); |
| 129 | test<double>(); |
| 130 | test<long double>(); |
| 131 | // + TODO extended floating-point types |
| 132 | } |
| 133 | |