| 1 | // { dg-do compile { target c++26 } } |
| 2 | |
| 3 | #include <complex> |
| 4 | #include <ranges> |
| 5 | #include <algorithm> |
| 6 | #include <string_view> |
| 7 | #include <type_traits> |
| 8 | #include <tuple> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <testsuite_hooks.h> |
| 12 | |
| 13 | template <typename T> |
| 14 | constexpr |
| 15 | void |
| 16 | test_sanity() |
| 17 | { |
| 18 | using C = std::complex<T>; |
| 19 | |
| 20 | static_assert(std::tuple_size_v<C> == 2); |
| 21 | static_assert(std::is_same_v<std::tuple_element_t<0, C>, T>); |
| 22 | static_assert(std::is_same_v<std::tuple_element_t<1, C>, T>); |
| 23 | |
| 24 | static_assert(std::is_same_v<decltype(get<0>(std::declval<C&>())), T&>); |
| 25 | static_assert(std::is_same_v<decltype(get<1>(std::declval<C&>())), T&>); |
| 26 | static_assert(std::is_same_v<decltype(get<0>(std::declval<const C&>())), const T&>); |
| 27 | static_assert(std::is_same_v<decltype(get<1>(std::declval<const C&>())), const T&>); |
| 28 | static_assert(std::is_same_v<decltype(get<0>(std::declval<C>())), T&&>); |
| 29 | static_assert(std::is_same_v<decltype(get<1>(std::declval<C>())), T&&>); |
| 30 | static_assert(std::is_same_v<decltype(get<0>(std::declval<const C>())), const T&&>); |
| 31 | static_assert(std::is_same_v<decltype(get<1>(std::declval<const C>())), const T&&>); |
| 32 | } |
| 33 | |
| 34 | template <typename T> |
| 35 | constexpr |
| 36 | void |
| 37 | test_get() |
| 38 | { |
| 39 | using C = std::complex<T>; |
| 40 | |
| 41 | C cpx(T(1), T(2)); |
| 42 | VERIFY(std::get<0>(cpx) == T(1)); |
| 43 | VERIFY(std::get<1>(cpx) == T(2)); |
| 44 | |
| 45 | const C cpx2(T(3), T(4)); |
| 46 | VERIFY(std::get<0>(cpx2) == T(3)); |
| 47 | VERIFY(std::get<1>(cpx2) == T(4)); |
| 48 | |
| 49 | struct derived : public C { using C::C; }; |
| 50 | derived cpx3(T(5), T(6)); |
| 51 | VERIFY(std::get<0>(cpx3) == T(5)); |
| 52 | VERIFY(std::get<1>(cpx3) == T(6)); |
| 53 | } |
| 54 | |
| 55 | template <typename T> |
| 56 | constexpr |
| 57 | void |
| 58 | test_structured_binding() |
| 59 | { |
| 60 | using C = std::complex<T>; |
| 61 | C cpx(T(1), T(2)); |
| 62 | |
| 63 | auto& [r, i] = cpx; |
| 64 | VERIFY(r == T(1)); |
| 65 | VERIFY(i == T(2)); |
| 66 | |
| 67 | r = T(3); |
| 68 | VERIFY(cpx.real() == T(3)); |
| 69 | VERIFY(cpx.imag() == T(2)); |
| 70 | |
| 71 | i = T(4); |
| 72 | VERIFY(cpx.real() == T(3)); |
| 73 | VERIFY(cpx.imag() == T(4)); |
| 74 | |
| 75 | const C cpx2(T(5), T(6)); |
| 76 | auto& [r2, i2] = cpx2; |
| 77 | VERIFY(r2 == T(5)); |
| 78 | VERIFY(i2 == T(6)); |
| 79 | } |
| 80 | |
| 81 | template <typename T> |
| 82 | constexpr |
| 83 | void |
| 84 | test_tuple_cat() |
| 85 | { |
| 86 | std::complex<T> cpx(T(1), T(2)); |
| 87 | std::pair<int, std::string_view> p(42, "hello"); |
| 88 | |
| 89 | auto r = std::tuple_cat(cpx, p, cpx); |
| 90 | static_assert(std::is_same_v<decltype(r), std::tuple<T, T, int, std::string_view, T, T>>); |
| 91 | VERIFY(std::get<0>(r) == T(1)); |
| 92 | VERIFY(std::get<1>(r) == T(2)); |
| 93 | VERIFY(std::get<2>(r) == 42); |
| 94 | VERIFY(std::get<3>(r) == "hello"); |
| 95 | VERIFY(std::get<4>(r) == T(1)); |
| 96 | VERIFY(std::get<5>(r) == T(2)); |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | constexpr |
| 101 | void |
| 102 | test_element_view() |
| 103 | { |
| 104 | std::complex<T> array[5] = { |
| 105 | { T(0), T(1) }, |
| 106 | { T(2), T(3) }, |
| 107 | { T(4), T(5) }, |
| 108 | { T(6), T(7) }, |
| 109 | { T(8), T(9) } |
| 110 | }; |
| 111 | |
| 112 | T real_reduction = std::ranges::fold_left(array | std::views::keys, {}, std::plus{}); |
| 113 | VERIFY(real_reduction == T(20)); |
| 114 | |
| 115 | T imag_reduction = std::ranges::fold_left(array | std::views::values, {}, std::plus{}); |
| 116 | VERIFY(imag_reduction == T(25)); |
| 117 | } |
| 118 | |
| 119 | template <typename T> |
| 120 | constexpr |
| 121 | void |
| 122 | test_apply() |
| 123 | { |
| 124 | std::complex<T> cpx(T(1), T(2)); |
| 125 | |
| 126 | auto f = [](T a, T b) { return a + b; }; |
| 127 | auto result = std::apply(f, cpx); |
| 128 | |
| 129 | VERIFY(result == T(3)); |
| 130 | } |
| 131 | |
| 132 | template <typename T> |
| 133 | constexpr |
| 134 | bool |
| 135 | all_tests() |
| 136 | { |
| 137 | test_sanity<T>(); |
| 138 | test_structured_binding<T>(); |
| 139 | test_tuple_cat<T>(); |
| 140 | test_element_view<T>(); |
| 141 | test_apply<T>(); |
| 142 | test_get<T>(); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | #define TEST(T) \ |
| 147 | static_assert(all_tests<T>()); \ |
| 148 | template T& std::get<0, T>(std::complex<T>&); \ |
| 149 | template T& std::get<1, T>(std::complex<T>&); \ |
| 150 | template T&& std::get<0, T>(std::complex<T>&&); \ |
| 151 | template T&& std::get<1, T>(std::complex<T>&&); \ |
| 152 | template const T& std::get<0, T>(const std::complex<T>&); \ |
| 153 | template const T& std::get<1, T>(const std::complex<T>&); \ |
| 154 | template const T&& std::get<0, T>(const std::complex<T>&&); \ |
| 155 | template const T&& std::get<1, T>(const std::complex<T>&&); \ |
| 156 | |
| 157 | TEST(float) |
| 158 | TEST(double) |
| 159 | TEST(long double) |
| 160 | |
| 161 | #ifdef __STDCPP_FLOAT16_T__ |
| 162 | TEST(_Float16) |
| 163 | #endif |
| 164 | #ifdef __STDCPP_FLOAT32_T__ |
| 165 | TEST(_Float32) |
| 166 | #endif |
| 167 | #ifdef __STDCPP_FLOAT64_T__ |
| 168 | TEST(_Float64) |
| 169 | #endif |
| 170 | #ifdef __STDCPP_FLOAT128_T__ |
| 171 | TEST(_Float128) |
| 172 | #endif |
| 173 | #ifdef __STDCPP_BFLOAT16_T__ |
| 174 | TEST(__gnu_cxx::__bfloat16_t) |
| 175 | #endif |
| 176 | |
| 177 | TEST(char) |
| 178 | TEST(int) |
| 179 | TEST(unsigned int) |
| 180 | TEST(long) |