1// Copyright (C) 2004, 2005, 2009 Free Software Foundation, Inc. 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// 27.6.1.2.2 arithmetic extractors 19 20#include <istream> 21#include <sstream> 22#include <locale> 23#include <testsuite_hooks.h> 24 25namespace std { 26 class test_numpunct1 : public numpunct<wchar_t> 27 { 28 protected: 29 string 30 do_grouping() const 31 { return string(1, '\003'); } 32 }; 33} // namespace std 34 35void test07() 36{ 37 // manufactured locale, grouping is turned on 38 bool test __attribute__((unused)) = true; 39 unsigned int h4 = 0, h3 = 0, h2 = 0; 40 float f1 = 0.0; 41 const std::wstring s1(L"205,199 23,445.25 1,024,365 123,22,24"); 42 std::wistringstream is(s1); 43 is.imbue(std::locale(std::locale(), new std::test_numpunct1)); 44 45 // Basic operation. 46 is >> h4; 47 VERIFY( h4 == 205199 ); 48 VERIFY( is.good() ); 49 50 is.clear(); 51 is >> f1; 52 VERIFY( f1 == 23445.25 ); 53 VERIFY( is.good() ); 54 55 is.clear(); 56 is >> h3; 57 VERIFY( h3 == 1024365 ); 58 VERIFY( is.good() ); 59 60 is.clear(); 61 is >> h2; 62 VERIFY( h2 == 1232224 ); 63 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) ); 64 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::eofbit) ); 65 66 // Stress tests for explicit errors in grouping corner cases. The 67 // validity of these tests and results have been hammered out in 68 // private email between bkoz and ncm between Jan 25 and Jan 27, 2000. 69 // Thanks nate -- benjamin 70 const std::wstring s2(L",111 4,,4 0.25,345 5..25 156,, 1,000000 1000000 1234,567"); 71 h3 = h4 = h2 = 0; 72 f1 = 0.0; 73 const wchar_t c_control = L'?'; 74 wchar_t c = c_control; 75 is.clear(); 76 is.str(s2); 77 78 is >> h4; 79 VERIFY( h4 == 0 ); 80 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) ); 81 is.clear(); 82 is >> c; 83 VERIFY( c == L',' ); 84 VERIFY( is.good() ); 85 86 is.ignore(3); 87 is >> f1; 88 VERIFY( f1 == 0.0 ); 89 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) ); 90 is.clear(); 91 is >> c; 92 VERIFY( c == L',' ); 93 is >> c; 94 VERIFY( c == L'4' ); 95 VERIFY( is.good() ); 96 97 is >> f1; 98 VERIFY( f1 == 0.25 ); 99 VERIFY( is.good() ); 100 is >> c; 101 VERIFY( c == L',' ); 102 is >> h2; 103 VERIFY( h2 == 345 ); 104 VERIFY( is.good() ); 105 f1 = 0.0; 106 h2 = 0; 107 108 is >> f1; 109 VERIFY( f1 == 5.0 ); 110 VERIFY( is.good() ); 111 is >> f1; 112 VERIFY( f1 == .25 ); 113 VERIFY( is.good() ); 114 115 is >> h3; 116 VERIFY( h3 == 0 ); 117 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) ); 118 is.clear(); 119 is >> c; 120 VERIFY( c == L',' ); // second one 121 VERIFY( is.good() ); 122 123 is >> h2; 124 VERIFY( h2 == 1000000 ); 125 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) ); 126 h2 = 0; 127 is.clear(); 128 129 is >> h2; 130 VERIFY( h2 == 1000000 ); 131 VERIFY( is.good() ); 132 h2 = 0; 133 134 is >> h2; 135 VERIFY( h2 == 1234567 ); 136 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) ); 137 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::eofbit) ); 138 is.clear(); 139} 140 141int main() 142{ 143 test07(); 144 return 0; 145} 146