]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 2000-02-10 |
2 | // Petter Urkedal <petter@matfys.lth.se> | |
3 | ||
7adcbafe | 4 | // Copyright (C) 2000-2022 Free Software Foundation, Inc. |
b2dad0e3 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
20 | |
21 | ||
22 | #include <iostream> | |
23 | #include <string> | |
24 | #include <sstream> | |
25 | #include <complex> | |
fe413112 | 26 | #include <testsuite_hooks.h> |
5c61f0f2 | 27 | #include <cmath> |
b2dad0e3 BK |
28 | |
29 | template<typename R> | |
30 | inline bool flteq(R x, R y) | |
31 | { | |
32 | if (x == R(0)) return y == R(0); | |
4bc95009 | 33 | else return std::fabs(x-y) < 1e-6*std::fabs(x); |
b2dad0e3 BK |
34 | } |
35 | ||
36 | template<typename R> | |
aa1b2f7d BV |
37 | int |
38 | test_good(std::string str, R x, R y) | |
b2dad0e3 | 39 | { |
b2dad0e3 BK |
40 | std::complex<R> z; |
41 | char ch; | |
42 | std::istringstream iss(str); | |
43 | iss >> z >> ch; | |
aa1b2f7d BV |
44 | VERIFY( iss.good() ); |
45 | VERIFY( flteq(z.real(), x) ); | |
46 | VERIFY( flteq(z.imag(), y) ); | |
47 | VERIFY( ch == '#' ); | |
aa1b2f7d | 48 | return 0; |
b2dad0e3 BK |
49 | } |
50 | ||
51 | template<typename R> | |
aa1b2f7d BV |
52 | int |
53 | test_fail(std::string str) | |
b2dad0e3 BK |
54 | { |
55 | std::complex<R> z; | |
56 | std::istringstream iss(str); | |
57 | iss >> z; | |
cb584bcf | 58 | VERIFY( iss.fail() && !iss.bad() ); |
aa1b2f7d | 59 | return 0; |
b2dad0e3 BK |
60 | } |
61 | ||
62 | template<typename R> | |
aa1b2f7d BV |
63 | int |
64 | testall() | |
b2dad0e3 BK |
65 | { |
66 | test_good<R>("(-1.1,3.7)#", -1.1, 3.7); | |
67 | test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1); | |
68 | test_good<R>("(\t0,-1)#", 0.0, -1.0); | |
69 | test_good<R>("(-3.14)#", -3.14, 0.0); | |
70 | test_good<R>("-.1#", -.1, 0.0); | |
71 | test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0); | |
72 | test_good<R>(" -.1#", -.1, 0.0); | |
73 | test_fail<R>("(a,1)"); | |
74 | test_fail<R>("(,1)"); | |
75 | test_fail<R>("(1,a)"); | |
76 | test_fail<R>("(1, )"); | |
77 | test_fail<R>("|1,1)"); | |
78 | test_fail<R>("(1|1)"); | |
79 | test_fail<R>("(1,1|"); | |
aa1b2f7d | 80 | return 0; |
b2dad0e3 BK |
81 | } |
82 | ||
bfa1e6b1 BK |
83 | // libstdc++/2970 |
84 | void test01() | |
85 | { | |
86 | using namespace std; | |
bfa1e6b1 BK |
87 | |
88 | complex<float> cf01(-1.1, -333.2); | |
89 | stringstream ss; | |
90 | ss << cf01; | |
91 | string str = ss.str(); | |
92 | VERIFY( str == "(-1.1,-333.2)" ); | |
93 | } | |
94 | ||
95 | // libstdc++/2985 | |
96 | struct gnu_char_traits : public std::char_traits<char> | |
97 | { }; | |
98 | ||
99 | typedef std::basic_ostringstream<char, gnu_char_traits> gnu_sstream; | |
5b5e609d | 100 | template class std::basic_string<char, gnu_char_traits, std::allocator<char> >; |
bfa1e6b1 BK |
101 | |
102 | void test02() | |
103 | { | |
bfa1e6b1 | 104 | // Construct locale with specialized facets. |
2803847d BK |
105 | typedef gnu_sstream::__num_put_type numput_type; |
106 | typedef gnu_sstream::__num_get_type numget_type; | |
bfa1e6b1 BK |
107 | std::locale loc_c = std::locale::classic(); |
108 | std::locale loc_1(loc_c, new numput_type); | |
109 | std::locale loc_2(loc_1, new numget_type); | |
110 | VERIFY( std::has_facet<numput_type>(loc_2) ); | |
111 | VERIFY( std::has_facet<numget_type>(loc_2) ); | |
112 | ||
113 | gnu_sstream sstr; | |
bfa1e6b1 BK |
114 | sstr.imbue(loc_2); |
115 | ||
116 | ||
117 | std::complex<double> x(3, 4); | |
118 | sstr << x; | |
119 | VERIFY( sstr.str() == "(3,4)" ); | |
120 | } | |
121 | ||
aa1b2f7d BV |
122 | int |
123 | main() | |
b2dad0e3 BK |
124 | { |
125 | testall<float>(); | |
126 | testall<double>(); | |
127 | testall<long double>(); | |
bfa1e6b1 BK |
128 | |
129 | test01(); | |
130 | test02(); | |
131 | ||
b2dad0e3 BK |
132 | return 0; |
133 | } |