xref: /llvm-project/libcxx/test/std/numerics/complex.number/complex.members/real_imag.pass.cpp (revision 7223bcf04c4ce3c1585df9408c6a6663cc8364ea)
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 // <complex>
10 
11 // void real(T val); // constexpr in C++20
12 // void imag(T val); // constexpr in C++20
13 
14 #include <complex>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 
19 template <class T>
20 TEST_CONSTEXPR_CXX20
21 void
test_constexpr()22 test_constexpr()
23 {
24 #if TEST_STD_VER > 11
25     constexpr std::complex<T> c1;
26     static_assert(c1.real() == 0, "");
27     static_assert(c1.imag() == 0, "");
28     constexpr std::complex<T> c2(3);
29     static_assert(c2.real() == 3, "");
30     static_assert(c2.imag() == 0, "");
31     constexpr std::complex<T> c3(3, 4);
32     static_assert(c3.real() == 3, "");
33     static_assert(c3.imag() == 4, "");
34 #endif
35 }
36 
37 template <class T>
38 TEST_CONSTEXPR_CXX20
39 bool
test()40 test()
41 {
42     std::complex<T> c;
43     assert(c.real() == 0);
44     assert(c.imag() == 0);
45     c.real(3.5);
46     assert(c.real() == 3.5);
47     assert(c.imag() == 0);
48     c.imag(4.5);
49     assert(c.real() == 3.5);
50     assert(c.imag() == 4.5);
51     c.real(-4.5);
52     assert(c.real() == -4.5);
53     assert(c.imag() == 4.5);
54     c.imag(-5.5);
55     assert(c.real() == -4.5);
56     assert(c.imag() == -5.5);
57 
58     test_constexpr<T>();
59     return true;
60 }
61 
main(int,char **)62 int main(int, char**) {
63     test<float>();
64     test<double>();
65     test<long double>();
66     test_constexpr<int>();
67 
68 #if TEST_STD_VER >= 20
69     static_assert(test<float>());
70     static_assert(test<double>());
71     static_assert(test<long double>());
72 #endif
73 
74   return 0;
75 }
76