| 1 | // 2001-11-19 Benjamin Kosnik <[email protected]> |
| 2 | |
| 3 | // Copyright (C) 2001-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.2.1 num_put members |
| 21 | |
| 22 | #include <locale> |
| 23 | #include <sstream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | void test02() |
| 27 | { |
| 28 | using namespace std; |
| 29 | typedef ostreambuf_iterator<char> iterator_type; |
| 30 | |
| 31 | // basic construction |
| 32 | locale loc_c = locale::classic(); |
| 33 | |
| 34 | // sanity check the data is correct. |
| 35 | const string empty; |
| 36 | string result1; |
| 37 | string result2; |
| 38 | |
| 39 | bool b1 = true; |
| 40 | bool b0 = false; |
| 41 | unsigned long ul1 = 1294967294; |
| 42 | unsigned long ul2 = 0; |
| 43 | |
| 44 | // cache the num_put facet |
| 45 | ostringstream oss; |
| 46 | oss.imbue(loc_c); |
| 47 | const num_put<char>& np = use_facet<num_put<char> >(oss.getloc()); |
| 48 | |
| 49 | // C |
| 50 | // bool, more twisted examples |
| 51 | oss.str(empty); |
| 52 | oss.width(20); |
| 53 | oss.setf(ios_base::right, ios_base::adjustfield); |
| 54 | np.put(oss.rdbuf(), oss, '+', b0); |
| 55 | result1 = oss.str(); |
| 56 | VERIFY( result1 == "+++++++++++++++++++0" ); |
| 57 | |
| 58 | oss.str(empty); |
| 59 | oss.width(20); |
| 60 | oss.setf(ios_base::left, ios_base::adjustfield); |
| 61 | oss.setf(ios_base::boolalpha); |
| 62 | np.put(oss.rdbuf(), oss, '+', b1); |
| 63 | result2 = oss.str(); |
| 64 | VERIFY( result2 == "true++++++++++++++++" ); |
| 65 | |
| 66 | // unsigned long, in a locale that does not group |
| 67 | oss.imbue(loc_c); |
| 68 | oss.str(empty); |
| 69 | oss.clear(); |
| 70 | np.put(oss.rdbuf(), oss, '+', ul1); |
| 71 | result1 = oss.str(); |
| 72 | VERIFY( result1 == "1294967294" ); |
| 73 | |
| 74 | oss.str(empty); |
| 75 | oss.clear(); |
| 76 | oss.width(20); |
| 77 | oss.setf(ios_base::left, ios_base::adjustfield); |
| 78 | np.put(oss.rdbuf(), oss, '+', ul2); |
| 79 | result1 = oss.str(); |
| 80 | VERIFY( result1 == "0+++++++++++++++++++" ); |
| 81 | } |
| 82 | |
| 83 | int main() |
| 84 | { |
| 85 | test02(); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 |