FSF GCC merge 02/23/03
[official-gcc.git] / libstdc++-v3 / testsuite / 26_numerics / complex_inserters_extractors.cc
blobd304aa47b38905ac45b4aa5ccc5026d6789fd302
1 // 2000-02-10
2 // Petter Urkedal <petter@matfys.lth.se>
4 // Copyright (C) 2000 Free Software Foundation
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.
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.
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
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
23 #include <iostream>
24 #include <string>
25 #include <sstream>
26 #include <complex>
27 #include <testsuite_hooks.h>
28 #include <cmath>
30 template<typename R>
31 inline bool flteq(R x, R y)
33 if (x == R(0)) return y == R(0);
34 else return std::fabs(x-y) < 1e-6*std::fabs(x);
37 template<typename R>
38 int
39 test_good(std::string str, R x, R y)
41 bool test = true;
42 std::complex<R> z;
43 char ch;
44 std::istringstream iss(str);
45 iss >> z >> ch;
46 VERIFY( iss.good() );
47 VERIFY( flteq(z.real(), x) );
48 VERIFY( flteq(z.imag(), y) );
49 VERIFY( ch == '#' );
51 #ifdef DEBUG_ASSERT
52 assert(test);
53 #endif
54 return 0;
57 template<typename R>
58 int
59 test_fail(std::string str)
61 std::complex<R> z;
62 std::istringstream iss(str);
63 iss >> z;
64 #ifdef DEBUG_ASSERT
65 assert(iss.fail() && !iss.bad());
66 #endif
67 return 0;
70 template<typename R>
71 int
72 testall()
74 test_good<R>("(-1.1,3.7)#", -1.1, 3.7);
75 test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1);
76 test_good<R>("(\t0,-1)#", 0.0, -1.0);
77 test_good<R>("(-3.14)#", -3.14, 0.0);
78 test_good<R>("-.1#", -.1, 0.0);
79 test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0);
80 test_good<R>(" -.1#", -.1, 0.0);
81 test_fail<R>("(a,1)");
82 test_fail<R>("(,1)");
83 test_fail<R>("(1,a)");
84 test_fail<R>("(1, )");
85 test_fail<R>("|1,1)");
86 test_fail<R>("(1|1)");
87 test_fail<R>("(1,1|");
88 return 0;
91 // libstdc++/2970
92 void test01()
94 using namespace std;
95 bool test = true;
97 complex<float> cf01(-1.1, -333.2);
98 stringstream ss;
99 ss << cf01;
100 string str = ss.str();
101 VERIFY( str == "(-1.1,-333.2)" );
104 // libstdc++/2985
105 struct gnu_char_traits : public std::char_traits<char>
106 { };
108 typedef std::basic_ostringstream<char, gnu_char_traits> gnu_sstream;
109 template class std::basic_string<char, gnu_char_traits, std::allocator<char> >;
111 void test02()
113 bool test = true;
115 // Construct locale with specialized facets.
116 typedef gnu_sstream::__numput_type numput_type;
117 typedef gnu_sstream::__numget_type numget_type;
118 std::locale loc_c = std::locale::classic();
119 std::locale loc_1(loc_c, new numput_type);
120 std::locale loc_2(loc_1, new numget_type);
121 VERIFY( std::has_facet<numput_type>(loc_2) );
122 VERIFY( std::has_facet<numget_type>(loc_2) );
124 gnu_sstream sstr;
125 std::basic_ios<char, gnu_char_traits>* pios = &sstr;
126 sstr.imbue(loc_2);
129 std::complex<double> x(3, 4);
130 sstr << x;
131 VERIFY( sstr.str() == "(3,4)" );
135 main()
137 testall<float>();
138 testall<double>();
139 testall<long double>();
141 test01();
142 test02();
144 return 0;