Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/dr2714.cc

raw
1 // Copyright (C) 2017-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 #include <complex>
19 #include <sstream>
20 #include <testsuite_hooks.h>
21
22 void
23 test01()
24 {
25 std::istringstream in(" 1 (2) ( 2.0 , 0.5 ) ");
26 std::complex<double> c1, c2, c3;
27 in >> c1 >> c2 >> c3;
28 VERIFY( in.good() );
29 VERIFY( c1.real() == 1 && c1.imag() == 0 );
30 VERIFY( c2.real() == 2 && c2.imag() == 0 );
31 VERIFY( c3.real() == 2 && c3.imag() == 0.5 );
32 }
33
34 void
35 test02()
36 {
37 std::istringstream in;
38 std::complex<double> c(-1, -1);
39 const std::complex<double> c0 = c;
40
41 in.str("a");
42 in >> c;
43 VERIFY( in.fail() );
44 in.clear();
45 VERIFY( in.get() == 'a' );
46
47 in.str(" ( ) ");
48 in >> c;
49 VERIFY( in.fail() );
50 in.clear();
51 VERIFY( in.get() == ')' );
52
53 in.str("(,");
54 in >> c;
55 VERIFY( in.fail() );
56 in.clear();
57 VERIFY( in.get() == ',' );
58
59 in.str("(b)");
60 in >> c;
61 VERIFY( in.fail() );
62 in.clear();
63 VERIFY( in.get() == 'b' );
64
65 in.str("( c)");
66 in >> c;
67 VERIFY( in.fail() );
68 in.clear();
69 VERIFY( in.get() == 'c' );
70
71 in.str("(99d");
72 in >> c;
73 VERIFY( in.fail() );
74 in.clear();
75 VERIFY( in.get() == 'd' );
76
77 in.str("(99 e");
78 in >> c;
79 VERIFY( in.fail() );
80 in.clear();
81 VERIFY( in.get() == 'e' );
82
83 in.str("(99, f");
84 in >> c;
85 VERIFY( in.fail() );
86 in.clear();
87 VERIFY( in.get() == 'f' );
88
89 in.str("(99, 88g");
90 in >> c;
91 VERIFY( in.fail() );
92 in.clear();
93 VERIFY( in.get() == 'g' );
94
95 in.str("(99, 88 h");
96 in >> c;
97 VERIFY( in.fail() );
98 in.clear();
99 VERIFY( in.get() == 'h' );
100
101 in.str("(99, )");
102 in >> c;
103 VERIFY( in.fail() );
104 in.clear();
105 VERIFY( in.get() == ')' );
106
107 VERIFY( c == c0 );
108 }
109
110 void
111 test03()
112 {
113 // PR libstdc++/59568
114 std::istringstream in;
115 std::complex<double> c;
116
117 in.str("");
118 in >> c;
119 VERIFY( in.fail() );
120 VERIFY( in.eof() );
121 in.clear();
122
123 in.str(" ");
124 in >> c;
125 VERIFY( in.fail() );
126 VERIFY( in.eof() );
127 in.clear();
128
129 in.str("(99");
130 in >> c;
131 VERIFY( in.fail() );
132 VERIFY( in.eof() );
133 in.clear();
134
135 in.str("(99,");
136 in >> c;
137 VERIFY( in.fail() );
138 VERIFY( in.eof() );
139 in.clear();
140
141 in.str("(99,99");
142 in >> c;
143 VERIFY( in.fail() );
144 VERIFY( in.eof() );
145 in.clear();
146 }
147
148 void
149 test04()
150 {
151 // Test noskipws handling
152 std::istringstream in;
153 const char* bad_inputs[] = {
154 " 1", " (2)", "( 2)", "(2 )", "(2 ,3)", "(2,3 )", 0
155 };
156 const std::complex<double> c0(-1, -1);
157 std::complex<double> c;
158 for (int i = 0; bad_inputs[i]; ++i)
159 {
160 c = c0;
161 in.clear();
162 in.str(bad_inputs[i]);
163 in >> std::noskipws >> c;
164 VERIFY( in.fail() );
165 VERIFY( c == c0 );
166
167 in.clear();
168 in.str(bad_inputs[i]);
169 in >> std::skipws >> c;
170 VERIFY( !in.fail() );
171 VERIFY( c != c0 );
172 }
173 }
174
175 int
176 main()
177 {
178 test01();
179 test02();
180 test03();
181 test04();
182 }