| 1 | // 2003-07-09 Benjamin Kosnik <[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 | #include <locale> |
| 21 | #include <sstream> |
| 22 | #include <ostream> |
| 23 | #include <stdexcept> |
| 24 | #include <typeinfo> |
| 25 | #include <testsuite_hooks.h> |
| 26 | #include <testsuite_character.h> |
| 27 | |
| 28 | // Check for numpunct and ctype dependencies. Make sure that numpunct |
| 29 | // can be created without ctype. |
| 30 | void test01() |
| 31 | { |
| 32 | using namespace std; |
| 33 | using __gnu_test::pod_ushort; |
| 34 | |
| 35 | typedef numpunct<pod_ushort>::string_type string_type; |
| 36 | typedef basic_ostringstream<pod_ushort> ostream_type; |
| 37 | |
| 38 | bool test = true; |
| 39 | |
| 40 | // Test formatted output. |
| 41 | ostream_type os; |
| 42 | const locale loc = locale::classic(); |
| 43 | os.imbue(loc); |
| 44 | os.setf(ios_base::boolalpha); |
| 45 | os.exceptions(ios_base::badbit); |
| 46 | |
| 47 | // 1: fail, no num_put. |
| 48 | try |
| 49 | { |
| 50 | // Calls to num_put.put will fail, as there's no num_put facet. |
| 51 | os << true; |
| 52 | test = false; |
| 53 | } |
| 54 | catch(const bad_cast& obj) |
| 55 | { } |
| 56 | catch(...) |
| 57 | { test = false; } |
| 58 | VERIFY( test ); |
| 59 | |
| 60 | // 2: fail, no ctype |
| 61 | const locale loc2(loc, new num_put<pod_ushort>); |
| 62 | os.clear(); |
| 63 | os.imbue(loc2); |
| 64 | try |
| 65 | { |
| 66 | // Calls to ctype.widen will fail, as there's no ctype facet. |
| 67 | os << true; |
| 68 | test = false; |
| 69 | } |
| 70 | catch(const bad_cast& obj) |
| 71 | { } |
| 72 | catch(...) |
| 73 | { test = false; } |
| 74 | VERIFY( test ); |
| 75 | |
| 76 | // 3: fail, no numpunct |
| 77 | const locale loc3(loc2, new ctype<pod_ushort>); |
| 78 | os.clear(); |
| 79 | os.imbue(loc3); |
| 80 | try |
| 81 | { |
| 82 | // Formatted output fails as no numpunct. |
| 83 | os << true; |
| 84 | test = false; |
| 85 | } |
| 86 | catch(const bad_cast& obj) |
| 87 | { } |
| 88 | catch(...) |
| 89 | { test = false; } |
| 90 | VERIFY( test ); |
| 91 | |
| 92 | // 4: works. |
| 93 | const locale loc4(loc3, new numpunct<pod_ushort>); |
| 94 | os.clear(); |
| 95 | os.imbue(loc4); |
| 96 | try |
| 97 | { |
| 98 | os << long(500); |
| 99 | string_type s = os.str(); |
| 100 | VERIFY( s.length() == 3 ); |
| 101 | |
| 102 | VERIFY( os.narrow(s[0], char()) == '5' ); |
| 103 | VERIFY( os.narrow(s[1], char()) == '0' ); |
| 104 | VERIFY( os.narrow(s[2], char()) == '0' ); |
| 105 | } |
| 106 | catch(const bad_cast& obj) |
| 107 | { test = false; } |
| 108 | catch(...) |
| 109 | { test = false; } |
| 110 | VERIFY( test ); |
| 111 | } |
| 112 | |
| 113 | int main() |
| 114 | { |
| 115 | test01(); |
| 116 | return 0; |
| 117 | } |