| 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 | // <istream> |
| 10 | |
| 11 | // template <class charT, class traits = char_traits<charT> > |
| 12 | // class basic_istream::sentry; |
| 13 | |
| 14 | // explicit sentry(basic_istream<charT,traits>& is, bool noskipws = false); |
| 15 | |
| 16 | #include <istream> |
| 17 | #include <cassert> |
| 18 | #include <streambuf> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | int sync_called = 0; |
| 23 | |
| 24 | template <class CharT> |
| 25 | struct testbuf |
| 26 | : public std::basic_streambuf<CharT> |
| 27 | { |
| 28 | typedef std::basic_string<CharT> string_type; |
| 29 | typedef std::basic_streambuf<CharT> base; |
| 30 | private: |
| 31 | string_type str_; |
| 32 | public: |
| 33 | |
| 34 | testbuf() {} |
| 35 | testbuf(const string_type& str) |
| 36 | : str_(str) |
| 37 | { |
| 38 | base::setg(const_cast<CharT*>(str_.data()), |
| 39 | const_cast<CharT*>(str_.data()), |
| 40 | const_cast<CharT*>(str_.data()) + str_.size()); |
| 41 | } |
| 42 | |
| 43 | CharT* eback() const {return base::eback();} |
| 44 | CharT* gptr() const {return base::gptr();} |
| 45 | CharT* egptr() const {return base::egptr();} |
| 46 | protected: |
| 47 | |
| 48 | int virtual sync() |
| 49 | { |
| 50 | ++sync_called; |
| 51 | return 1; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | int main(int, char**) |
| 56 | { |
| 57 | { |
| 58 | std::istream is((testbuf<char>*)0); |
| 59 | std::istream::sentry sen(is, true); |
| 60 | assert(!(bool)sen); |
| 61 | assert(!is.good()); |
| 62 | assert(is.gcount() == 0); |
| 63 | assert(sync_called == 0); |
| 64 | } |
| 65 | { |
| 66 | testbuf<char> sb(" 123" ); |
| 67 | std::istream is(&sb); |
| 68 | std::istream::sentry sen(is, true); |
| 69 | assert((bool)sen); |
| 70 | assert(is.good()); |
| 71 | assert(is.gcount() == 0); |
| 72 | assert(sync_called == 0); |
| 73 | assert(sb.gptr() == sb.eback()); |
| 74 | } |
| 75 | { |
| 76 | testbuf<char> sb(" 123" ); |
| 77 | std::istream is(&sb); |
| 78 | std::istream::sentry sen(is); |
| 79 | assert((bool)sen); |
| 80 | assert(is.good()); |
| 81 | assert(sync_called == 0); |
| 82 | assert(sb.gptr() == sb.eback() + 3); |
| 83 | } |
| 84 | { |
| 85 | testbuf<char> sb(" " ); |
| 86 | std::istream is(&sb); |
| 87 | std::istream::sentry sen(is); |
| 88 | assert(!(bool)sen); |
| 89 | assert(is.fail()); |
| 90 | assert(is.eof()); |
| 91 | assert(sync_called == 0); |
| 92 | assert(sb.gptr() == sb.eback() + 6); |
| 93 | } |
| 94 | { |
| 95 | testbuf<char> sb(" " ); |
| 96 | std::istream is(&sb); |
| 97 | std::istream::sentry sen(is, true); |
| 98 | assert((bool)sen); |
| 99 | assert(is.good()); |
| 100 | assert(sync_called == 0); |
| 101 | assert(sb.gptr() == sb.eback()); |
| 102 | } |
| 103 | |
| 104 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 105 | { |
| 106 | std::wistream is((testbuf<wchar_t>*)0); |
| 107 | std::wistream::sentry sen(is, true); |
| 108 | assert(!(bool)sen); |
| 109 | assert(!is.good()); |
| 110 | assert(is.gcount() == 0); |
| 111 | assert(sync_called == 0); |
| 112 | } |
| 113 | { |
| 114 | testbuf<wchar_t> sb(L" 123" ); |
| 115 | std::wistream is(&sb); |
| 116 | std::wistream::sentry sen(is, true); |
| 117 | assert((bool)sen); |
| 118 | assert(is.good()); |
| 119 | assert(is.gcount() == 0); |
| 120 | assert(sync_called == 0); |
| 121 | assert(sb.gptr() == sb.eback()); |
| 122 | } |
| 123 | { |
| 124 | testbuf<wchar_t> sb(L" 123" ); |
| 125 | std::wistream is(&sb); |
| 126 | std::wistream::sentry sen(is); |
| 127 | assert((bool)sen); |
| 128 | assert(is.good()); |
| 129 | assert(sync_called == 0); |
| 130 | assert(sb.gptr() == sb.eback() + 3); |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |