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