| 1 | // 1999-10-14 bkoz |
| 2 | |
| 3 | // Copyright (C) 1999-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 | |
| 21 | // 27.6.1.1.2 class basic_istream::sentry |
| 22 | |
| 23 | #include <istream> |
| 24 | #include <sstream> |
| 25 | #include <typeinfo> |
| 26 | #include <ext/pod_char_traits.h> |
| 27 | #include <testsuite_hooks.h> |
| 28 | #include <testsuite_character.h> |
| 29 | |
| 30 | void test01() |
| 31 | { |
| 32 | using namespace std; |
| 33 | using __gnu_test::pod_ushort; |
| 34 | typedef basic_string<pod_ushort> string_type; |
| 35 | typedef basic_stringbuf<pod_ushort> stringbuf_type; |
| 36 | typedef basic_istream<pod_ushort> istream_type; |
| 37 | |
| 38 | const string_type str01; |
| 39 | stringbuf_type strbuf01; |
| 40 | stringbuf_type strbuf02(str01); |
| 41 | istream_type istr01(&strbuf01); |
| 42 | istream_type istr02(&strbuf02); |
| 43 | |
| 44 | // test negatives |
| 45 | try |
| 46 | { |
| 47 | istream_type::sentry sentry01(istr01); |
| 48 | } |
| 49 | catch (std::bad_cast& obj) |
| 50 | { |
| 51 | // Ok, throws bad_cast because locale has no ctype facet. |
| 52 | } |
| 53 | catch (...) |
| 54 | { |
| 55 | VERIFY( false ); |
| 56 | } |
| 57 | |
| 58 | try |
| 59 | { |
| 60 | istream_type::sentry sentry02(istr01, true); |
| 61 | } |
| 62 | catch (std::bad_cast& obj) |
| 63 | { |
| 64 | // Ok, throws bad_cast because locale has no ctype facet. |
| 65 | } |
| 66 | catch (...) |
| 67 | { |
| 68 | VERIFY( false ); |
| 69 | } |
| 70 | |
| 71 | // imbued. |
| 72 | const std::locale loc(std::locale::classic(), new std::ctype<pod_ushort>); |
| 73 | istr01.imbue(loc); |
| 74 | try |
| 75 | { |
| 76 | istream_type::sentry sentry01(istr01); |
| 77 | VERIFY( bool(sentry01) == false ); |
| 78 | } |
| 79 | catch (...) |
| 80 | { |
| 81 | VERIFY( false ); |
| 82 | } |
| 83 | |
| 84 | try |
| 85 | { |
| 86 | istream_type::sentry sentry02(istr01, true); |
| 87 | VERIFY( bool(sentry02) == false ); |
| 88 | } |
| 89 | catch (...) |
| 90 | { |
| 91 | VERIFY( false ); |
| 92 | } |
| 93 | |
| 94 | // test positive |
| 95 | try |
| 96 | { |
| 97 | istream_type::sentry sentry03(istr02); |
| 98 | } |
| 99 | catch (std::bad_cast& obj) |
| 100 | { |
| 101 | // Ok, throws bad_cast because locale has no ctype facet. |
| 102 | } |
| 103 | catch (...) |
| 104 | { |
| 105 | VERIFY( false ); |
| 106 | } |
| 107 | |
| 108 | try |
| 109 | { |
| 110 | istream_type::sentry sentry04(istr02, true); |
| 111 | } |
| 112 | catch (std::bad_cast& obj) |
| 113 | { |
| 114 | // Ok, throws bad_cast because locale has no ctype facet. |
| 115 | } |
| 116 | catch (...) |
| 117 | { |
| 118 | VERIFY( false ); |
| 119 | } |
| 120 | |
| 121 | // imbued. |
| 122 | istr02.imbue(loc); |
| 123 | try |
| 124 | { |
| 125 | istr02.clear(); |
| 126 | istream_type::sentry sentry03(istr02); |
| 127 | // ... as eofbit set. |
| 128 | VERIFY( bool(sentry03) == false ); |
| 129 | } |
| 130 | catch (...) |
| 131 | { |
| 132 | VERIFY( false ); |
| 133 | } |
| 134 | |
| 135 | try |
| 136 | { |
| 137 | istr02.clear(); |
| 138 | istream_type::sentry sentry04(istr02, true); |
| 139 | VERIFY( bool(sentry04) == true ); |
| 140 | } |
| 141 | catch (...) |
| 142 | { |
| 143 | VERIFY( false ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #if !__GXX_WEAK__ |
| 148 | // Explicitly instantiate for systems with no COMDAT or weak support. |
| 149 | template |
| 150 | const std::basic_string<__gnu_test::pod_ushort>::size_type |
| 151 | std::basic_string<__gnu_test::pod_ushort>::_Rep::_S_max_size; |
| 152 | |
| 153 | template |
| 154 | const __gnu_test::pod_ushort |
| 155 | std::basic_string<__gnu_test::pod_ushort>::_Rep::_S_terminal; |
| 156 | #endif |
| 157 | |
| 158 | int main() |
| 159 | { |
| 160 | test01(); |
| 161 | return 0; |
| 162 | } |