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