| 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 | // streamsize readsome(char_type* s, streamsize n); |
| 12 | |
| 13 | #include <istream> |
| 14 | #include <cassert> |
| 15 | #include <streambuf> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | template <class CharT> |
| 20 | struct testbuf |
| 21 | : public std::basic_streambuf<CharT> |
| 22 | { |
| 23 | typedef std::basic_string<CharT> string_type; |
| 24 | typedef std::basic_streambuf<CharT> base; |
| 25 | private: |
| 26 | string_type str_; |
| 27 | public: |
| 28 | |
| 29 | testbuf() {} |
| 30 | testbuf(const string_type& str) |
| 31 | : str_(str) |
| 32 | { |
| 33 | base::setg(const_cast<CharT*>(str_.data()), |
| 34 | const_cast<CharT*>(str_.data()), |
| 35 | const_cast<CharT*>(str_.data()) + str_.size()); |
| 36 | } |
| 37 | |
| 38 | CharT* eback() const {return base::eback();} |
| 39 | CharT* gptr() const {return base::gptr();} |
| 40 | CharT* egptr() const {return base::egptr();} |
| 41 | }; |
| 42 | |
| 43 | int main(int, char**) |
| 44 | { |
| 45 | { |
| 46 | testbuf<char> sb(" 1234567890" ); |
| 47 | std::istream is(&sb); |
| 48 | char s[5]; |
| 49 | assert(is.readsome(s, 5) == 5); |
| 50 | assert(!is.eof()); |
| 51 | assert(!is.fail()); |
| 52 | assert(std::string(s, 5) == " 1234" ); |
| 53 | assert(is.gcount() == 5); |
| 54 | is.readsome(s: s, n: 5); |
| 55 | assert(!is.eof()); |
| 56 | assert(!is.fail()); |
| 57 | assert(std::string(s, 5) == "56789" ); |
| 58 | assert(is.gcount() == 5); |
| 59 | is.readsome(s: s, n: 5); |
| 60 | assert(!is.eof()); |
| 61 | assert(!is.fail()); |
| 62 | assert(is.gcount() == 1); |
| 63 | assert(std::string(s, 1) == "0" ); |
| 64 | assert(is.readsome(s, 5) == 0); |
| 65 | } |
| 66 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 67 | { |
| 68 | testbuf<wchar_t> sb(L" 1234567890" ); |
| 69 | std::wistream is(&sb); |
| 70 | wchar_t s[5]; |
| 71 | assert(is.readsome(s, 5) == 5); |
| 72 | assert(!is.eof()); |
| 73 | assert(!is.fail()); |
| 74 | assert(std::wstring(s, 5) == L" 1234" ); |
| 75 | assert(is.gcount() == 5); |
| 76 | is.readsome(s: s, n: 5); |
| 77 | assert(!is.eof()); |
| 78 | assert(!is.fail()); |
| 79 | assert(std::wstring(s, 5) == L"56789" ); |
| 80 | assert(is.gcount() == 5); |
| 81 | is.readsome(s: s, n: 5); |
| 82 | assert(!is.eof()); |
| 83 | assert(!is.fail()); |
| 84 | assert(is.gcount() == 1); |
| 85 | assert(std::wstring(s, 1) == L"0" ); |
| 86 | assert(is.readsome(s, 5) == 0); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |