| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // Requires 396145d in the built library. |
| 10 | // XFAIL: using-built-library-before-llvm-9 |
| 11 | |
| 12 | // <istream> |
| 13 | |
| 14 | // template <class charT, class traits = char_traits<charT> > |
| 15 | // class basic_istream; |
| 16 | |
| 17 | // operator>>(short& val); |
| 18 | |
| 19 | #include <istream> |
| 20 | #include <limits> |
| 21 | #include <cassert> |
| 22 | #include <streambuf> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | template <class CharT> |
| 27 | struct testbuf |
| 28 | : public std::basic_streambuf<CharT> |
| 29 | { |
| 30 | typedef std::basic_string<CharT> string_type; |
| 31 | typedef std::basic_streambuf<CharT> base; |
| 32 | private: |
| 33 | string_type str_; |
| 34 | public: |
| 35 | |
| 36 | testbuf() {} |
| 37 | testbuf(const string_type& str) |
| 38 | : str_(str) |
| 39 | { |
| 40 | base::setg(const_cast<CharT*>(str_.data()), |
| 41 | const_cast<CharT*>(str_.data()), |
| 42 | const_cast<CharT*>(str_.data()) + str_.size()); |
| 43 | } |
| 44 | |
| 45 | CharT* eback() const {return base::eback();} |
| 46 | CharT* gptr() const {return base::gptr();} |
| 47 | CharT* egptr() const {return base::egptr();} |
| 48 | }; |
| 49 | |
| 50 | int main(int, char**) |
| 51 | { |
| 52 | { |
| 53 | std::istream is((std::streambuf*)0); |
| 54 | short n = 0; |
| 55 | is >> n; |
| 56 | assert(is.fail()); |
| 57 | } |
| 58 | { |
| 59 | testbuf<char> sb("0" ); |
| 60 | std::istream is(&sb); |
| 61 | short n = 10; |
| 62 | is >> n; |
| 63 | assert(n == 0); |
| 64 | assert( is.eof()); |
| 65 | assert(!is.fail()); |
| 66 | } |
| 67 | { |
| 68 | testbuf<char> sb(" 123 " ); |
| 69 | std::istream is(&sb); |
| 70 | short n = 10; |
| 71 | is >> n; |
| 72 | assert(n == 123); |
| 73 | assert(!is.eof()); |
| 74 | assert(!is.fail()); |
| 75 | } |
| 76 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 77 | { |
| 78 | testbuf<wchar_t> sb(L" -1234567890 " ); |
| 79 | std::wistream is(&sb); |
| 80 | short n = 10; |
| 81 | is >> n; |
| 82 | assert(n == std::numeric_limits<short>::min()); |
| 83 | assert(!is.eof()); |
| 84 | assert( is.fail()); |
| 85 | } |
| 86 | #endif |
| 87 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 88 | { |
| 89 | testbuf<char> sb; |
| 90 | std::basic_istream<char> is(&sb); |
| 91 | is.exceptions(std::ios_base::failbit); |
| 92 | |
| 93 | bool threw = false; |
| 94 | try { |
| 95 | short n = 0; |
| 96 | is >> n; |
| 97 | } catch (std::ios_base::failure const&) { |
| 98 | threw = true; |
| 99 | } |
| 100 | |
| 101 | assert(!is.bad()); |
| 102 | assert(is.fail()); |
| 103 | assert(is.eof()); |
| 104 | assert(threw); |
| 105 | } |
| 106 | { |
| 107 | testbuf<char> sb; |
| 108 | std::basic_istream<char> is(&sb); |
| 109 | is.exceptions(std::ios_base::eofbit); |
| 110 | |
| 111 | bool threw = false; |
| 112 | try { |
| 113 | short n = 0; |
| 114 | is >> n; |
| 115 | } catch (std::ios_base::failure const&) { |
| 116 | threw = true; |
| 117 | } |
| 118 | |
| 119 | assert(!is.bad()); |
| 120 | assert(is.fail()); |
| 121 | assert(is.eof()); |
| 122 | assert(threw); |
| 123 | } |
| 124 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |