[X86] Add test case for #124871. NFC
[llvm-project.git] / libcxx / test / std / numerics / complex.number / complex.ops / stream_output.pass.cpp
blobb711108625762765c809ed9831be224b1d06d609
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-localization
11 // <complex>
13 // template<class T, class charT, class traits>
14 // basic_ostream<charT, traits>&
15 // operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
17 #include <complex>
18 #include <sstream>
19 #include <cassert>
21 #include "test_macros.h"
23 int main(int, char**) {
24 // Basic test
26 std::complex<double> const c(1, 2);
27 std::ostringstream os;
28 os << c;
29 assert(os.str() == "(1,2)");
32 // Test with various widths.
33 // In particular, make sure the width() is 0 after the operation, which
34 // should be the case because [complex.ops] says about operator<< for complex:
36 // Effects: Inserts the complex number x onto the stream o as if it
37 // were implemented as follows:
39 // basic_ostringstream<charT, traits> s;
40 // s.flags(o.flags());
41 // s.imbue(o.getloc());
42 // s.precision(o.precision());
43 // s << '(' << x.real() << "," << x.imag() << ')';
44 // return o << s.str();
46 // Since operator<< for std::string sets o.width(0), operator<< for
47 // std::complex should do the same.
49 for (int width = 0; width <= 5; ++width) {
50 std::complex<double> const c(1, 2);
51 std::ostringstream os;
52 os.width(width);
53 os.fill('_');
54 os << c;
55 assert(os.width() == 0);
56 assert(os.str() == "(1,2)");
59 std::complex<double> const c(1, 2);
60 std::ostringstream os;
61 os.width(6);
62 os.fill('_');
63 os << c;
64 assert(os.width() == 0);
65 assert(os.str() == "_(1,2)");
68 std::complex<double> const c(1, 2);
69 std::ostringstream os;
70 os.width(7);
71 os.fill('_');
72 os << c;
73 assert(os.width() == 0);
74 assert(os.str() == "__(1,2)");
77 std::complex<double> const c(1, 2);
78 std::ostringstream os;
79 os.width(8);
80 os.fill('_');
81 os << c;
82 assert(os.width() == 0);
83 assert(os.str() == "___(1,2)");
85 // Insert something after the complex and make sure the
86 // stream's width has been reset as expected.
88 std::complex<double> const c(1, 2);
89 std::ostringstream os;
90 os.width(8);
91 os.fill('_');
92 os << c;
93 assert(os.width() == 0);
95 os << "hello";
96 assert(os.str() == "___(1,2)hello");
99 // Test with numbers that result in different output lengths, to
100 // make sure we handle custom width() correctly.
102 std::complex<double> const c(123, 456);
103 std::ostringstream os;
104 os.width(4);
105 os.fill('_');
106 os << c;
107 assert(os.width() == 0);
108 assert(os.str() == "(123,456)");
111 std::complex<double> const c(123, 456);
112 std::ostringstream os;
113 os.width(12);
114 os.fill('_');
115 os << c;
116 assert(os.width() == 0);
117 assert(os.str() == "___(123,456)");
119 os << "hello";
120 assert(os.str() == "___(123,456)hello");
123 // Make sure left fill behaves correctly
125 std::complex<double> const c(123, 456);
126 std::ostringstream os;
127 os.width(12);
128 os.fill('_');
129 os << std::left << c;
130 assert(os.width() == 0);
131 assert(os.str() == "(123,456)___");
133 os << "xy";
134 assert(os.str() == "(123,456)___xy");
138 return 0;