]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | // Copyright (C) 2007-2024 Free Software Foundation, Inc. |
b2dad0e3 BK |
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 | |
748086b7 | 6 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
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 | |
748086b7 JJ |
15 | // with this library; see the file COPYING3. If not see |
16 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
17 | |
18 | ||
19 | #include <iostream> | |
20 | #include <string> | |
21 | #include <sstream> | |
22 | #include <complex> | |
fe413112 | 23 | #include <testsuite_hooks.h> |
5c61f0f2 | 24 | #include <cmath> |
b2dad0e3 BK |
25 | |
26 | template<typename R> | |
27 | inline bool flteq(R x, R y) | |
28 | { | |
29 | if (x == R(0)) return y == R(0); | |
4bc95009 | 30 | else return std::fabs(x-y) < 1e-6*std::fabs(x); |
b2dad0e3 BK |
31 | } |
32 | ||
33 | template<typename R> | |
aa1b2f7d | 34 | int |
74b332b8 | 35 | test_good(std::wstring str, R x, R y) |
b2dad0e3 | 36 | { |
b2dad0e3 | 37 | std::complex<R> z; |
74b332b8 PC |
38 | wchar_t ch; |
39 | std::wistringstream iss(str); | |
b2dad0e3 | 40 | iss >> z >> ch; |
aa1b2f7d BV |
41 | VERIFY( iss.good() ); |
42 | VERIFY( flteq(z.real(), x) ); | |
43 | VERIFY( flteq(z.imag(), y) ); | |
74b332b8 | 44 | VERIFY( ch == L'#' ); |
aa1b2f7d | 45 | return 0; |
b2dad0e3 BK |
46 | } |
47 | ||
48 | template<typename R> | |
aa1b2f7d | 49 | int |
74b332b8 | 50 | test_fail(std::wstring str) |
b2dad0e3 BK |
51 | { |
52 | std::complex<R> z; | |
74b332b8 | 53 | std::wistringstream iss(str); |
b2dad0e3 | 54 | iss >> z; |
cb584bcf | 55 | VERIFY( iss.fail() && !iss.bad() ); |
aa1b2f7d | 56 | return 0; |
b2dad0e3 BK |
57 | } |
58 | ||
59 | template<typename R> | |
aa1b2f7d BV |
60 | int |
61 | testall() | |
b2dad0e3 | 62 | { |
74b332b8 PC |
63 | test_good<R>(L"(-1.1,3.7)#", -1.1, 3.7); |
64 | test_good<R>(L"( .7e6 , \n-3.1)#", .7e6, -3.1); | |
65 | test_good<R>(L"(\t0,-1)#", 0.0, -1.0); | |
66 | test_good<R>(L"(-3.14)#", -3.14, 0.0); | |
67 | test_good<R>(L"-.1#", -.1, 0.0); | |
68 | test_good<R>(L" ( -2.7e3 )#", -2.7e3, 0.0); | |
69 | test_good<R>(L" -.1#", -.1, 0.0); | |
70 | test_fail<R>(L"(a,1)"); | |
71 | test_fail<R>(L"(,1)"); | |
72 | test_fail<R>(L"(1,a)"); | |
73 | test_fail<R>(L"(1, )"); | |
74 | test_fail<R>(L"|1,1)"); | |
75 | test_fail<R>(L"(1|1)"); | |
76 | test_fail<R>(L"(1,1|"); | |
aa1b2f7d | 77 | return 0; |
b2dad0e3 BK |
78 | } |
79 | ||
bfa1e6b1 BK |
80 | // libstdc++/2970 |
81 | void test01() | |
82 | { | |
83 | using namespace std; | |
bfa1e6b1 BK |
84 | |
85 | complex<float> cf01(-1.1, -333.2); | |
74b332b8 | 86 | wstringstream ss; |
bfa1e6b1 | 87 | ss << cf01; |
74b332b8 PC |
88 | wstring str = ss.str(); |
89 | VERIFY( str == L"(-1.1,-333.2)" ); | |
bfa1e6b1 BK |
90 | } |
91 | ||
92 | // libstdc++/2985 | |
74b332b8 | 93 | struct gnu_char_traits : public std::char_traits<wchar_t> |
bfa1e6b1 BK |
94 | { }; |
95 | ||
74b332b8 PC |
96 | typedef std::basic_ostringstream<wchar_t, gnu_char_traits> gnu_sstream; |
97 | template class std::basic_string<wchar_t, gnu_char_traits, | |
98 | std::allocator<wchar_t> >; | |
bfa1e6b1 BK |
99 | |
100 | void test02() | |
101 | { | |
bfa1e6b1 | 102 | // Construct locale with specialized facets. |
2803847d BK |
103 | typedef gnu_sstream::__num_put_type numput_type; |
104 | typedef gnu_sstream::__num_get_type numget_type; | |
bfa1e6b1 BK |
105 | std::locale loc_c = std::locale::classic(); |
106 | std::locale loc_1(loc_c, new numput_type); | |
107 | std::locale loc_2(loc_1, new numget_type); | |
108 | VERIFY( std::has_facet<numput_type>(loc_2) ); | |
109 | VERIFY( std::has_facet<numget_type>(loc_2) ); | |
110 | ||
111 | gnu_sstream sstr; | |
bfa1e6b1 BK |
112 | sstr.imbue(loc_2); |
113 | ||
114 | ||
115 | std::complex<double> x(3, 4); | |
116 | sstr << x; | |
74b332b8 | 117 | VERIFY( sstr.str() == L"(3,4)" ); |
bfa1e6b1 BK |
118 | } |
119 | ||
aa1b2f7d BV |
120 | int |
121 | main() | |
b2dad0e3 BK |
122 | { |
123 | testall<float>(); | |
124 | testall<double>(); | |
125 | testall<long double>(); | |
bfa1e6b1 BK |
126 | |
127 | test01(); | |
128 | test02(); | |
129 | ||
b2dad0e3 BK |
130 | return 0; |
131 | } |