1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// 2000-02-10
// Petter Urkedal <petter@matfys.lth.se>
// Copyright (C) 2000-2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <iostream>
#include <string>
#include <sstream>
#include <complex>
#include <testsuite_hooks.h>
#include <cmath>
template<typename R>
inline bool flteq(R x, R y)
{
if (x == R(0)) return y == R(0);
else return std::fabs(x-y) < 1e-6*std::fabs(x);
}
template<typename R>
int
test_good(std::string str, R x, R y)
{
bool test __attribute__((unused)) = true;
std::complex<R> z;
char ch;
std::istringstream iss(str);
iss >> z >> ch;
VERIFY( iss.good() );
VERIFY( flteq(z.real(), x) );
VERIFY( flteq(z.imag(), y) );
VERIFY( ch == '#' );
return 0;
}
template<typename R>
int
test_fail(std::string str)
{
bool test __attribute__((unused)) = true;
std::complex<R> z;
std::istringstream iss(str);
iss >> z;
VERIFY( iss.fail() && !iss.bad() );
return 0;
}
template<typename R>
int
testall()
{
test_good<R>("(-1.1,3.7)#", -1.1, 3.7);
test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1);
test_good<R>("(\t0,-1)#", 0.0, -1.0);
test_good<R>("(-3.14)#", -3.14, 0.0);
test_good<R>("-.1#", -.1, 0.0);
test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0);
test_good<R>(" -.1#", -.1, 0.0);
test_fail<R>("(a,1)");
test_fail<R>("(,1)");
test_fail<R>("(1,a)");
test_fail<R>("(1, )");
test_fail<R>("|1,1)");
test_fail<R>("(1|1)");
test_fail<R>("(1,1|");
return 0;
}
// libstdc++/2970
void test01()
{
using namespace std;
bool test __attribute__((unused)) = true;
complex<float> cf01(-1.1, -333.2);
stringstream ss;
ss << cf01;
string str = ss.str();
VERIFY( str == "(-1.1,-333.2)" );
}
// libstdc++/2985
struct gnu_char_traits : public std::char_traits<char>
{ };
typedef std::basic_ostringstream<char, gnu_char_traits> gnu_sstream;
template class std::basic_string<char, gnu_char_traits, std::allocator<char> >;
void test02()
{
bool test __attribute__((unused)) = true;
// Construct locale with specialized facets.
typedef gnu_sstream::__num_put_type numput_type;
typedef gnu_sstream::__num_get_type numget_type;
std::locale loc_c = std::locale::classic();
std::locale loc_1(loc_c, new numput_type);
std::locale loc_2(loc_1, new numget_type);
VERIFY( std::has_facet<numput_type>(loc_2) );
VERIFY( std::has_facet<numget_type>(loc_2) );
gnu_sstream sstr;
sstr.imbue(loc_2);
std::complex<double> x(3, 4);
sstr << x;
VERIFY( sstr.str() == "(3,4)" );
}
int
main()
{
testall<float>();
testall<double>();
testall<long double>();
test01();
test02();
return 0;
}
|