| 1 | // { dg-do compile { target c++20 } } |
| 2 | |
| 3 | // Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | #include <complex> |
| 21 | #include <testsuite_common_types.h> |
| 22 | |
| 23 | namespace __gnu_test |
| 24 | { |
| 25 | // Test constexpr real(val) imag(val). |
| 26 | template<typename _Tp, const int _Val = 42> |
| 27 | inline void |
| 28 | set_real(std::complex<_Tp>& a) |
| 29 | { a.real(_Val); } |
| 30 | |
| 31 | template<typename _Tp, const int _Val = 42> |
| 32 | inline void |
| 33 | set_imag(std::complex<_Tp>& a) |
| 34 | { a.imag(_Val); } |
| 35 | |
| 36 | template<typename _Tp> |
| 37 | void |
| 38 | test_members() |
| 39 | { |
| 40 | constexpr std::complex<_Tp> a{1.1, 2.2}; |
| 41 | |
| 42 | std::complex<_Tp> z = a; |
| 43 | |
| 44 | set_real<_Tp, 33>(z); |
| 45 | set_imag<_Tp, 44>(z); |
| 46 | } |
| 47 | |
| 48 | // Test operators @=complex and @=real. |
| 49 | template<typename _Tp, typename _Up> |
| 50 | constexpr std::complex<_Tp> |
| 51 | sum(const std::complex<_Tp>& z, const std::complex<_Up>& w) |
| 52 | { |
| 53 | std::complex<_Tp> x = z; |
| 54 | x += w; |
| 55 | return x; |
| 56 | } |
| 57 | |
| 58 | template<typename _Tp, typename _Up> |
| 59 | constexpr std::complex<_Tp> |
| 60 | sum(const std::complex<_Tp>& z, _Up w) |
| 61 | { |
| 62 | std::complex<_Tp> x = z; |
| 63 | x += w; |
| 64 | return x; |
| 65 | } |
| 66 | |
| 67 | template<typename _Tp, typename _Up> |
| 68 | constexpr std::complex<_Tp> |
| 69 | dif(const std::complex<_Tp>& z, const std::complex<_Up>& w) |
| 70 | { |
| 71 | std::complex<_Tp> x = z; |
| 72 | x -= w; |
| 73 | return x; |
| 74 | } |
| 75 | |
| 76 | template<typename _Tp, typename _Up> |
| 77 | constexpr std::complex<_Tp> |
| 78 | dif(const std::complex<_Tp>& z, _Up w) |
| 79 | { |
| 80 | std::complex<_Tp> x = z; |
| 81 | x -= w; |
| 82 | return x; |
| 83 | } |
| 84 | |
| 85 | template<typename _Tp, typename _Up> |
| 86 | constexpr std::complex<_Tp> |
| 87 | prod(const std::complex<_Tp>& z, const std::complex<_Up>& w) |
| 88 | { |
| 89 | std::complex<_Tp> x = z; |
| 90 | x *= w; |
| 91 | return x; |
| 92 | } |
| 93 | |
| 94 | template<typename _Tp, typename _Up> |
| 95 | constexpr std::complex<_Tp> |
| 96 | prod(const std::complex<_Tp>& z, _Up w) |
| 97 | { |
| 98 | std::complex<_Tp> x = z; |
| 99 | x *= w; |
| 100 | return x; |
| 101 | } |
| 102 | |
| 103 | template<typename _Tp, typename _Up> |
| 104 | constexpr std::complex<_Tp> |
| 105 | quot(const std::complex<_Tp>& z, const std::complex<_Up>& w) |
| 106 | { |
| 107 | std::complex<_Tp> x = z; |
| 108 | x /= w; |
| 109 | return x; |
| 110 | } |
| 111 | |
| 112 | template<typename _Tp, typename _Up> |
| 113 | constexpr std::complex<_Tp> |
| 114 | quot(const std::complex<_Tp>& z, _Up w) |
| 115 | { |
| 116 | std::complex<_Tp> x = z; |
| 117 | x /= w; |
| 118 | return x; |
| 119 | } |
| 120 | |
| 121 | template<typename _Tp, typename _Up> |
| 122 | void |
| 123 | test_operator_members() |
| 124 | { |
| 125 | constexpr std::complex<_Tp> a{10, 20}; |
| 126 | constexpr std::complex<_Up> b{6, 8}; |
| 127 | constexpr _Up c{10}; |
| 128 | |
| 129 | constexpr auto apc = sum(a, c); |
| 130 | static_assert(apc == std::complex<_Tp>{20, 20}); |
| 131 | constexpr auto amc = dif(a, c); |
| 132 | static_assert(amc == std::complex<_Tp>{0, 20}); |
| 133 | constexpr auto atc = prod(a, c); |
| 134 | static_assert(atc == std::complex<_Tp>{100, 200}); |
| 135 | constexpr auto adc = quot(a, c); |
| 136 | static_assert(adc == std::complex<_Tp>{1, 2}); |
| 137 | |
| 138 | constexpr auto apb = sum(a, b); |
| 139 | static_assert(apb == std::complex<_Tp>{16, 28}); |
| 140 | constexpr auto amb = dif(a, b); |
| 141 | static_assert(amb == std::complex<_Tp>{4, 12}); |
| 142 | constexpr auto atb = prod(a, b); |
| 143 | static_assert(atb == std::complex<_Tp>{-100, 200}); |
| 144 | constexpr auto adb = quot(a, b); |
| 145 | static_assert(adb == std::complex<_Tp>{11/_Tp{5}, 2/_Tp{5}}); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int main() |
| 150 | { |
| 151 | __gnu_test::test_members<float>(); |
| 152 | __gnu_test::test_members<double>(); |
| 153 | __gnu_test::test_members<long double>(); |
| 154 | |
| 155 | __gnu_test::test_operator_members<float, float>(); |
| 156 | __gnu_test::test_operator_members<float, double>(); |
| 157 | __gnu_test::test_operator_members<float, long double>(); |
| 158 | __gnu_test::test_operator_members<double, float>(); |
| 159 | __gnu_test::test_operator_members<double, double>(); |
| 160 | __gnu_test::test_operator_members<double, long double>(); |
| 161 | #ifndef __LONG_DOUBLE_IBM128__ // IBM128 format is not constexpr foldable |
| 162 | __gnu_test::test_operator_members<long double, float>(); |
| 163 | __gnu_test::test_operator_members<long double, double>(); |
| 164 | __gnu_test::test_operator_members<long double, long double>(); |
| 165 | #endif |
| 166 | |
| 167 | #if defined(_GLIBCXX_USE_FLOAT128) |
| 168 | // Test primary template. |
| 169 | __gnu_test::test_operator_members<__float128, __float128>(); |
| 170 | #endif |
| 171 | |
| 172 | return 0; |
| 173 | } |