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