]>
Commit | Line | Data |
---|---|---|
b9e8095b | 1 | // 2000-02-10 |
2 | // Petter Urkedal <petter@matfys.lth.se> | |
3 | ||
f1717362 | 4 | // Copyright (C) 2000-2016 Free Software Foundation, Inc. |
b9e8095b | 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 | |
6bc9506f | 9 | // Free Software Foundation; either version 3, or (at your option) |
b9e8095b | 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 | |
6bc9506f | 18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
b9e8095b | 20 | |
21 | ||
22 | #include <iostream> | |
23 | #include <string> | |
24 | #include <sstream> | |
25 | #include <complex> | |
0194306c | 26 | #include <testsuite_hooks.h> |
70183561 | 27 | #include <cmath> |
b9e8095b | 28 | |
29 | template<typename R> | |
30 | inline bool flteq(R x, R y) | |
31 | { | |
32 | if (x == R(0)) return y == R(0); | |
36badd1d | 33 | else return std::fabs(x-y) < 1e-6*std::fabs(x); |
b9e8095b | 34 | } |
35 | ||
36 | template<typename R> | |
43a8c05a | 37 | int |
38 | test_good(std::string str, R x, R y) | |
b9e8095b | 39 | { |
f8ef786c | 40 | bool test __attribute__((unused)) = true; |
b9e8095b | 41 | std::complex<R> z; |
42 | char ch; | |
43 | std::istringstream iss(str); | |
44 | iss >> z >> ch; | |
43a8c05a | 45 | VERIFY( iss.good() ); |
46 | VERIFY( flteq(z.real(), x) ); | |
47 | VERIFY( flteq(z.imag(), y) ); | |
48 | VERIFY( ch == '#' ); | |
43a8c05a | 49 | return 0; |
b9e8095b | 50 | } |
51 | ||
52 | template<typename R> | |
43a8c05a | 53 | int |
54 | test_fail(std::string str) | |
b9e8095b | 55 | { |
f8ef786c | 56 | bool test __attribute__((unused)) = true; |
b9e8095b | 57 | std::complex<R> z; |
58 | std::istringstream iss(str); | |
59 | iss >> z; | |
7372455c | 60 | VERIFY( iss.fail() && !iss.bad() ); |
43a8c05a | 61 | return 0; |
b9e8095b | 62 | } |
63 | ||
64 | template<typename R> | |
43a8c05a | 65 | int |
66 | testall() | |
b9e8095b | 67 | { |
68 | test_good<R>("(-1.1,3.7)#", -1.1, 3.7); | |
69 | test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1); | |
70 | test_good<R>("(\t0,-1)#", 0.0, -1.0); | |
71 | test_good<R>("(-3.14)#", -3.14, 0.0); | |
72 | test_good<R>("-.1#", -.1, 0.0); | |
73 | test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0); | |
74 | test_good<R>(" -.1#", -.1, 0.0); | |
75 | test_fail<R>("(a,1)"); | |
76 | test_fail<R>("(,1)"); | |
77 | test_fail<R>("(1,a)"); | |
78 | test_fail<R>("(1, )"); | |
79 | test_fail<R>("|1,1)"); | |
80 | test_fail<R>("(1|1)"); | |
81 | test_fail<R>("(1,1|"); | |
43a8c05a | 82 | return 0; |
b9e8095b | 83 | } |
84 | ||
8878d5e4 | 85 | // libstdc++/2970 |
86 | void test01() | |
87 | { | |
88 | using namespace std; | |
f8ef786c | 89 | bool test __attribute__((unused)) = true; |
8878d5e4 | 90 | |
91 | complex<float> cf01(-1.1, -333.2); | |
92 | stringstream ss; | |
93 | ss << cf01; | |
94 | string str = ss.str(); | |
95 | VERIFY( str == "(-1.1,-333.2)" ); | |
96 | } | |
97 | ||
98 | // libstdc++/2985 | |
99 | struct gnu_char_traits : public std::char_traits<char> | |
100 | { }; | |
101 | ||
102 | typedef std::basic_ostringstream<char, gnu_char_traits> gnu_sstream; | |
08f83147 | 103 | template class std::basic_string<char, gnu_char_traits, std::allocator<char> >; |
8878d5e4 | 104 | |
105 | void test02() | |
106 | { | |
f8ef786c | 107 | bool test __attribute__((unused)) = true; |
8878d5e4 | 108 | |
109 | // Construct locale with specialized facets. | |
5a73ac23 | 110 | typedef gnu_sstream::__num_put_type numput_type; |
111 | typedef gnu_sstream::__num_get_type numget_type; | |
8878d5e4 | 112 | std::locale loc_c = std::locale::classic(); |
113 | std::locale loc_1(loc_c, new numput_type); | |
114 | std::locale loc_2(loc_1, new numget_type); | |
115 | VERIFY( std::has_facet<numput_type>(loc_2) ); | |
116 | VERIFY( std::has_facet<numget_type>(loc_2) ); | |
117 | ||
118 | gnu_sstream sstr; | |
8878d5e4 | 119 | sstr.imbue(loc_2); |
120 | ||
121 | ||
122 | std::complex<double> x(3, 4); | |
123 | sstr << x; | |
124 | VERIFY( sstr.str() == "(3,4)" ); | |
125 | } | |
126 | ||
43a8c05a | 127 | int |
128 | main() | |
b9e8095b | 129 | { |
130 | testall<float>(); | |
131 | testall<double>(); | |
132 | testall<long double>(); | |
8878d5e4 | 133 | |
134 | test01(); | |
135 | test02(); | |
136 | ||
b9e8095b | 137 | return 0; |
138 | } |