]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/complex_inserters_extractors.cc
install.texi (Configuration): Add html links for --with-gnu-as & --with-gnu-ld and...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / complex_inserters_extractors.cc
CommitLineData
b2dad0e3
BK
1// 2000-02-10
2// Petter Urkedal <petter@matfys.lth.se>
3
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.
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
19// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20// USA.
21
22
23#include <iostream>
24#include <string>
25#include <sstream>
26#include <complex>
aa1b2f7d 27#include <debug_assert.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
BK
40{
41 bool test = true;
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 == '#' );
b2dad0e3
BK
50
51#ifdef DEBUG_ASSERT
52 assert(test);
53#endif
aa1b2f7d 54 return 0;
b2dad0e3
BK
55}
56
57template<typename R>
aa1b2f7d
BV
58int
59test_fail(std::string str)
b2dad0e3
BK
60{
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
aa1b2f7d 67 return 0;
b2dad0e3
BK
68}
69
70template<typename R>
aa1b2f7d
BV
71int
72testall()
b2dad0e3
BK
73{
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|");
aa1b2f7d 88 return 0;
b2dad0e3
BK
89}
90
aa1b2f7d
BV
91int
92main()
b2dad0e3
BK
93{
94 testall<float>();
95 testall<double>();
96 testall<long double>();
97 return 0;
98}