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