| 1 |
// 2003-12-22 Paolo Carlini <[email protected]> |
| 2 |
|
| 3 |
// Copyright (C) 2003-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 22.2.2.1.1 num_get members |
| 21 |
|
| 22 |
#include <locale> |
| 23 |
#include <sstream> |
| 24 |
#include <testsuite_hooks.h> |
| 25 |
|
| 26 |
struct Punct1: std::numpunct<wchar_t> |
| 27 |
{ |
| 28 |
std::string do_grouping() const { return "\1"; } |
| 29 |
wchar_t do_thousands_sep() const { return L'+'; } |
| 30 |
wchar_t do_decimal_point() const { return L'x'; } |
| 31 |
}; |
| 32 |
|
| 33 |
struct Punct2: std::numpunct<wchar_t> |
| 34 |
{ |
| 35 |
std::string do_grouping() const { return "\1"; } |
| 36 |
wchar_t do_thousands_sep() const { return L'X'; } |
| 37 |
wchar_t do_decimal_point() const { return L'-'; } |
| 38 |
}; |
| 39 |
|
| 40 |
// http://gcc.gnu.org/ml/libstdc++/2003-12/msg00201.html |
| 41 |
void test01() |
| 42 |
{ |
| 43 |
using namespace std; |
| 44 |
typedef istreambuf_iterator<wchar_t> iterator_type; |
| 45 |
|
| 46 |
wistringstream iss1, iss2; |
| 47 |
iss1.imbue(locale(iss1.getloc(), new Punct1)); |
| 48 |
iss2.imbue(locale(iss2.getloc(), new Punct2)); |
| 49 |
const num_get<wchar_t>& ng1 = use_facet<num_get<wchar_t> >(iss1.getloc()); |
| 50 |
const num_get<wchar_t>& ng2 = use_facet<num_get<wchar_t> >(iss2.getloc()); |
| 51 |
|
| 52 |
ios_base::iostate err = ios_base::goodbit; |
| 53 |
iterator_type end; |
| 54 |
long l = 1l; |
| 55 |
long l1 = 0l; |
| 56 |
long l2 = 10l; |
| 57 |
long l3 = 1l; |
| 58 |
long l4 = 63l; |
| 59 |
double d = 0.0; |
| 60 |
double d1 = .4; |
| 61 |
double d2 = 0.0; |
| 62 |
double d3 = .1; |
| 63 |
|
| 64 |
iss1.str(L"+3"); |
| 65 |
err = ios_base::goodbit; |
| 66 |
end = ng1.get(iss1.rdbuf(), 0, iss1, err, l); |
| 67 |
VERIFY( err == ios_base::failbit ); |
| 68 |
VERIFY( *end == L'+' ); |
| 69 |
|
| 70 |
iss1.str(L"0x1"); |
| 71 |
iss1.clear(); |
| 72 |
err = ios_base::goodbit; |
| 73 |
end = ng1.get(iss1.rdbuf(), 0, iss1, err, l); |
| 74 |
VERIFY( err == ios_base::goodbit ); |
| 75 |
VERIFY( *end == L'x' ); |
| 76 |
VERIFY( l == l1 ); |
| 77 |
|
| 78 |
iss1.str(L"0Xa"); |
| 79 |
iss1.clear(); |
| 80 |
iss1.unsetf(ios::basefield); |
| 81 |
err = ios_base::goodbit; |
| 82 |
end = ng1.get(iss1.rdbuf(), 0, iss1, err, l); |
| 83 |
VERIFY( err == ios_base::eofbit ); |
| 84 |
VERIFY( l == l2 ); |
| 85 |
|
| 86 |
iss1.str(L"0xa"); |
| 87 |
iss1.clear(); |
| 88 |
err = ios_base::goodbit; |
| 89 |
end = ng1.get(iss1.rdbuf(), 0, iss1, err, l); |
| 90 |
VERIFY( err == ios_base::goodbit ); |
| 91 |
VERIFY( *end == L'x' ); |
| 92 |
VERIFY( l == l1 ); |
| 93 |
|
| 94 |
iss1.str(L"+5"); |
| 95 |
iss1.clear(); |
| 96 |
err = ios_base::goodbit; |
| 97 |
end = ng1.get(iss1.rdbuf(), 0, iss1, err, d); |
| 98 |
VERIFY( err == ios_base::failbit ); |
| 99 |
VERIFY( *end == L'+' ); |
| 100 |
|
| 101 |
iss1.str(L"x4"); |
| 102 |
iss1.clear(); |
| 103 |
err = ios_base::goodbit; |
| 104 |
end = ng1.get(iss1.rdbuf(), 0, iss1, err, d); |
| 105 |
VERIFY( err == ios_base::eofbit ); |
| 106 |
VERIFY( d == d1 ); |
| 107 |
|
| 108 |
iss2.str(L"0001-"); |
| 109 |
err = ios_base::goodbit; |
| 110 |
end = ng2.get(iss2.rdbuf(), 0, iss2, err, l); |
| 111 |
VERIFY( err == ios_base::goodbit ); |
| 112 |
VERIFY( *end == L'-' ); |
| 113 |
VERIFY( l == l3 ); |
| 114 |
|
| 115 |
iss2.str(L"-2"); |
| 116 |
iss2.clear(); |
| 117 |
err = ios_base::goodbit; |
| 118 |
end = ng2.get(iss2.rdbuf(), 0, iss2, err, l); |
| 119 |
VERIFY( err == ios_base::failbit ); |
| 120 |
VERIFY( *end == L'-' ); |
| 121 |
|
| 122 |
iss2.str(L"0X1"); |
| 123 |
iss2.clear(); |
| 124 |
iss2.unsetf(ios::basefield); |
| 125 |
err = ios_base::goodbit; |
| 126 |
end = ng2.get(iss2.rdbuf(), 0, iss2, err, l); |
| 127 |
VERIFY( err == ios_base::failbit ); |
| 128 |
VERIFY( *end == L'X' ); |
| 129 |
VERIFY( l == 0 ); |
| 130 |
|
| 131 |
iss2.str(L"000778"); |
| 132 |
iss2.clear(); |
| 133 |
err = ios_base::goodbit; |
| 134 |
end = ng2.get(iss2.rdbuf(), 0, iss2, err, l); |
| 135 |
VERIFY( err == ios_base::goodbit ); |
| 136 |
VERIFY( *end == L'8' ); |
| 137 |
VERIFY( l == l4 ); |
| 138 |
|
| 139 |
iss2.str(L"00X"); |
| 140 |
iss2.clear(); |
| 141 |
err = ios_base::goodbit; |
| 142 |
end = ng2.get(iss2.rdbuf(), 0, iss2, err, d); |
| 143 |
VERIFY( err == (ios_base::eofbit | ios_base::failbit) ); |
| 144 |
VERIFY( d == d2 ); |
| 145 |
|
| 146 |
iss2.str(L"-1"); |
| 147 |
iss2.clear(); |
| 148 |
err = ios_base::goodbit; |
| 149 |
end = ng2.get(iss2.rdbuf(), 0, iss2, err, d); |
| 150 |
VERIFY( err == ios_base::eofbit ); |
| 151 |
VERIFY( d == d3 ); |
| 152 |
} |
| 153 |
|
| 154 |
int main() |
| 155 |
{ |
| 156 |
test01(); |
| 157 |
return 0; |
| 158 |
} |