]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/complex/complex_inserters_extractors.cc
re PR fortran/31540 ([Regression 4.2 only] character((constant expression)) for exter...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / complex / complex_inserters_extractors.cc
CommitLineData
b2dad0e3
BK
1// 2000-02-10
2// Petter Urkedal <petter@matfys.lth.se>
3
cb584bcf 4// Copyright (C) 2000, 2003 Free Software Foundation
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
9// Free Software Foundation; either version 2, or (at your option)
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
18// with this library; see the file COPYING. If not, write to the Free
83f51799 19// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
b2dad0e3
BK
20// USA.
21
22
23#include <iostream>
24#include <string>
25#include <sstream>
26#include <complex>
fe413112 27#include <testsuite_hooks.h>
5c61f0f2 28#include <cmath>
b2dad0e3
BK
29
30template<typename R>
31inline bool flteq(R x, R y)
32{
33 if (x == R(0)) return y == R(0);
4bc95009 34 else return std::fabs(x-y) < 1e-6*std::fabs(x);
b2dad0e3
BK
35}
36
37template<typename R>
aa1b2f7d
BV
38int
39test_good(std::string str, R x, R y)
b2dad0e3 40{
11f10e6b 41 bool test __attribute__((unused)) = true;
b2dad0e3
BK
42 std::complex<R> z;
43 char ch;
44 std::istringstream iss(str);
45 iss >> z >> ch;
aa1b2f7d
BV
46 VERIFY( iss.good() );
47 VERIFY( flteq(z.real(), x) );
48 VERIFY( flteq(z.imag(), y) );
49 VERIFY( ch == '#' );
aa1b2f7d 50 return 0;
b2dad0e3
BK
51}
52
53template<typename R>
aa1b2f7d
BV
54int
55test_fail(std::string str)
b2dad0e3 56{
11f10e6b 57 bool test __attribute__((unused)) = true;
b2dad0e3
BK
58 std::complex<R> z;
59 std::istringstream iss(str);
60 iss >> z;
cb584bcf 61 VERIFY( iss.fail() && !iss.bad() );
aa1b2f7d 62 return 0;
b2dad0e3
BK
63}
64
65template<typename R>
aa1b2f7d
BV
66int
67testall()
b2dad0e3
BK
68{
69 test_good<R>("(-1.1,3.7)#", -1.1, 3.7);
70 test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1);
71 test_good<R>("(\t0,-1)#", 0.0, -1.0);
72 test_good<R>("(-3.14)#", -3.14, 0.0);
73 test_good<R>("-.1#", -.1, 0.0);
74 test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0);
75 test_good<R>(" -.1#", -.1, 0.0);
76 test_fail<R>("(a,1)");
77 test_fail<R>("(,1)");
78 test_fail<R>("(1,a)");
79 test_fail<R>("(1, )");
80 test_fail<R>("|1,1)");
81 test_fail<R>("(1|1)");
82 test_fail<R>("(1,1|");
aa1b2f7d 83 return 0;
b2dad0e3
BK
84}
85
bfa1e6b1
BK
86// libstdc++/2970
87void test01()
88{
89 using namespace std;
11f10e6b 90 bool test __attribute__((unused)) = true;
bfa1e6b1
BK
91
92 complex<float> cf01(-1.1, -333.2);
93 stringstream ss;
94 ss << cf01;
95 string str = ss.str();
96 VERIFY( str == "(-1.1,-333.2)" );
97}
98
99// libstdc++/2985
100struct gnu_char_traits : public std::char_traits<char>
101{ };
102
103typedef std::basic_ostringstream<char, gnu_char_traits> gnu_sstream;
5b5e609d 104template class std::basic_string<char, gnu_char_traits, std::allocator<char> >;
bfa1e6b1
BK
105
106void test02()
107{
11f10e6b 108 bool test __attribute__((unused)) = true;
bfa1e6b1
BK
109
110 // Construct locale with specialized facets.
2803847d
BK
111 typedef gnu_sstream::__num_put_type numput_type;
112 typedef gnu_sstream::__num_get_type numget_type;
bfa1e6b1
BK
113 std::locale loc_c = std::locale::classic();
114 std::locale loc_1(loc_c, new numput_type);
115 std::locale loc_2(loc_1, new numget_type);
116 VERIFY( std::has_facet<numput_type>(loc_2) );
117 VERIFY( std::has_facet<numget_type>(loc_2) );
118
119 gnu_sstream sstr;
bfa1e6b1
BK
120 sstr.imbue(loc_2);
121
122
123 std::complex<double> x(3, 4);
124 sstr << x;
125 VERIFY( sstr.str() == "(3,4)" );
126}
127
aa1b2f7d
BV
128int
129main()
b2dad0e3
BK
130{
131 testall<float>();
132 testall<double>();
133 testall<long double>();
bfa1e6b1
BK
134
135 test01();
136 test02();
137
b2dad0e3
BK
138 return 0;
139}
bfa1e6b1 140
5b5e609d
BK
141
142
143