]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/complex_inserters_extractors.cc
libstdc++-v3: New directory.
[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>
27#ifdef DEBUG_ASSERT
28#include <assert.h>
29#endif
30
31template<typename R>
32inline bool flteq(R x, R y)
33{
34 if (x == R(0)) return y == R(0);
35 else return fabs(x-y) < 1e-6*fabs(x);
36}
37
38template<typename R>
39void test_good(std::string str, R x, R y)
40{
41 bool test = true;
42 std::complex<R> z;
43 char ch;
44 std::istringstream iss(str);
45 iss >> z >> ch;
46 test &= iss.good();
47 test &= flteq(z.real(), x);
48 test &= flteq(z.imag(), y);
49 test &= ch == '#';
50
51#ifdef DEBUG_ASSERT
52 assert(test);
53#endif
54}
55
56template<typename R>
57void test_fail(std::string str)
58{
59 std::complex<R> z;
60 std::istringstream iss(str);
61 iss >> z;
62#ifdef DEBUG_ASSERT
63 assert(iss.fail() && !iss.bad());
64#endif
65}
66
67template<typename R>
68int testall()
69{
70 test_good<R>("(-1.1,3.7)#", -1.1, 3.7);
71 test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1);
72 test_good<R>("(\t0,-1)#", 0.0, -1.0);
73 test_good<R>("(-3.14)#", -3.14, 0.0);
74 test_good<R>("-.1#", -.1, 0.0);
75 test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0);
76 test_good<R>(" -.1#", -.1, 0.0);
77 test_fail<R>("(a,1)");
78 test_fail<R>("(,1)");
79 test_fail<R>("(1,a)");
80 test_fail<R>("(1, )");
81 test_fail<R>("|1,1)");
82 test_fail<R>("(1|1)");
83 test_fail<R>("(1,1|");
84}
85
86int main()
87{
88 testall<float>();
89 testall<double>();
90 testall<long double>();
91 return 0;
92}
93
94
95