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