1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-localization
13 // template<class T, class charT, class traits>
14 // basic_ostream<charT, traits>&
15 // operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
21 #include "test_macros.h"
23 int main(int, char**) {
26 std::complex<double> const c(1, 2);
27 std::ostringstream os
;
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
;
55 assert(os
.width() == 0);
56 assert(os
.str() == "(1,2)");
59 std::complex<double> const c(1, 2);
60 std::ostringstream os
;
64 assert(os
.width() == 0);
65 assert(os
.str() == "_(1,2)");
68 std::complex<double> const c(1, 2);
69 std::ostringstream os
;
73 assert(os
.width() == 0);
74 assert(os
.str() == "__(1,2)");
77 std::complex<double> const c(1, 2);
78 std::ostringstream os
;
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
;
93 assert(os
.width() == 0);
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
;
107 assert(os
.width() == 0);
108 assert(os
.str() == "(123,456)");
111 std::complex<double> const c(123, 456);
112 std::ostringstream os
;
116 assert(os
.width() == 0);
117 assert(os
.str() == "___(123,456)");
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
;
129 os
<< std::left
<< c
;
130 assert(os
.width() == 0);
131 assert(os
.str() == "(123,456)___");
134 assert(os
.str() == "(123,456)___xy");