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