| 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 | |
| 19 | #include <sstream> |
| 20 | #include <locale> |
| 21 | #include <iomanip> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | struct MyNP : std::numpunct<wchar_t> |
| 25 | { |
| 26 | std::string do_grouping() const; |
| 27 | wchar_t do_thousands_sep() const; |
| 28 | }; |
| 29 | |
| 30 | std::string |
| 31 | MyNP::do_grouping() const |
| 32 | { |
| 33 | std::string s("\3"); |
| 34 | return s; |
| 35 | } |
| 36 | |
| 37 | wchar_t |
| 38 | MyNP::do_thousands_sep() const |
| 39 | { return L' '; } |
| 40 | |
| 41 | void test01() |
| 42 | { |
| 43 | const wchar_t lit[] = L"0123 456\n" |
| 44 | L": 01 234 567:\n" |
| 45 | L":0123 456 :\n" |
| 46 | L": 012 345:\n" |
| 47 | L": 01 234:\n" |
| 48 | L":0726 746 425:\n" |
| 49 | L":04 553 207 :\n" |
| 50 | L": 0361 100:\n" |
| 51 | L": 0173:\n" |
| 52 | L"0x12 345 678\n" |
| 53 | L"|0x000012 345 678|\n" |
| 54 | L"|0x12 345 6780000|\n" |
| 55 | L"|00000x12 345 678|\n" |
| 56 | L"|0x000012 345 678|\n"; |
| 57 | |
| 58 | std::wostringstream oss; |
| 59 | oss.imbue(std::locale(std::locale(), new MyNP)); |
| 60 | |
| 61 | // Octals |
| 62 | oss << std::oct << std::showbase; |
| 63 | oss << 0123456l << std::endl; |
| 64 | |
| 65 | oss << L":" << std::setw(11); |
| 66 | oss << 01234567l << L":" << std::endl; |
| 67 | |
| 68 | oss << L":" << std::setw(11) << std::left; |
| 69 | oss << 0123456l << L":" << std::endl; |
| 70 | |
| 71 | oss << L":" << std::setw(11) << std::right; |
| 72 | oss << 012345l << L":" << std::endl; |
| 73 | |
| 74 | oss << L":" << std::setw(11) << std::internal; |
| 75 | oss << 01234l << L":" << std::endl; |
| 76 | |
| 77 | oss << L":" << std::setw(11); |
| 78 | oss << 123456789l << L":" << std::endl; |
| 79 | |
| 80 | oss << L":" << std::setw(11) << std::left; |
| 81 | oss << 1234567l << L":" << std::endl; |
| 82 | |
| 83 | oss << L":" << std::setw(11) << std::right; |
| 84 | oss << 123456l << L":" << std::endl; |
| 85 | |
| 86 | oss << L":" << std::setw(11) << std::internal; |
| 87 | oss << 123l << L":" << std::endl; |
| 88 | |
| 89 | // Hexadecimals |
| 90 | oss << std::hex << std::setfill(L'0'); |
| 91 | oss << 0x12345678l << std::endl; |
| 92 | |
| 93 | oss << L"|" << std::setw(16); |
| 94 | oss << 0x12345678l << L"|" << std::endl; |
| 95 | |
| 96 | oss << L"|" << std::setw(16) << std::left; |
| 97 | oss << 0x12345678l << L"|" << std::endl; |
| 98 | |
| 99 | oss << L"|" << std::setw(16) << std::right; |
| 100 | oss << 0x12345678l << L"|" << std::endl; |
| 101 | |
| 102 | oss << L"|" << std::setw(16) << std::internal; |
| 103 | oss << 0x12345678l << L"|" << std::endl; |
| 104 | |
| 105 | VERIFY( oss.good() ); |
| 106 | VERIFY( oss.str() == lit ); |
| 107 | } |
| 108 | |
| 109 | int |
| 110 | main() |
| 111 | { |
| 112 | test01(); |
| 113 | return 0; |
| 114 | } |