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 
28     testbuf() {}
29     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 
37     CharT* eback() const {return base::eback();}
38     CharT* gptr() const {return base::gptr();}
39     CharT* egptr() const {return base::egptr();}
40 };
41 
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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
66     {
67         testbuf<wchar_t> sb(L" 1234567890");
68         std::wistream is(&sb);
69         wchar_t s[5];
70         assert(is.readsome(s, 5) == 5);
71         assert(!is.eof());
72         assert(!is.fail());
73         assert(std::wstring(s, 5) == L" 1234");
74         assert(is.gcount() == 5);
75         is.readsome(s, 5);
76         assert(!is.eof());
77         assert(!is.fail());
78         assert(std::wstring(s, 5) == L"56789");
79         assert(is.gcount() == 5);
80         is.readsome(s, 5);
81         assert(!is.eof());
82         assert(!is.fail());
83         assert(is.gcount() == 1);
84         assert(std::wstring(s, 1) == L"0");
85         assert(is.readsome(s, 5) == 0);
86     }
87 #endif
88 
89   return 0;
90 }
91