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