140 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: no-localization10 11// <complex>12 13// template<class T, class charT, class traits>14// basic_ostream<charT, traits>&15// operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);16 17#include <complex>18#include <sstream>19#include <cassert>20 21#include "test_macros.h"22 23int main(int, char**) {24 // Basic test25 {26 std::complex<double> const c(1, 2);27 std::ostringstream os;28 os << c;29 assert(os.str() == "(1,2)");30 }31 32 // Test with various widths.33 // In particular, make sure the width() is 0 after the operation, which34 // should be the case because [complex.ops] says about operator<< for complex:35 //36 // Effects: Inserts the complex number x onto the stream o as if it37 // were implemented as follows:38 //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();45 //46 // Since operator<< for std::string sets o.width(0), operator<< for47 // std::complex should do the same.48 {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)");57 }58 {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)");66 }67 {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)");75 }76 {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)");84 }85 // Insert something after the complex and make sure the86 // stream's width has been reset as expected.87 {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);94 95 os << "hello";96 assert(os.str() == "___(1,2)hello");97 }98 99 // Test with numbers that result in different output lengths, to100 // make sure we handle custom width() correctly.101 {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)");109 }110 {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)");118 119 os << "hello";120 assert(os.str() == "___(123,456)hello");121 }122 123 // Make sure left fill behaves correctly124 {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)___");132 133 os << "xy";134 assert(os.str() == "(123,456)___xy");135 }136 }137 138 return 0;139}140