• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // XFAIL: with_system_cxx_lib=macosx10.7
11 // XFAIL: with_system_cxx_lib=macosx10.8
12 
13 // <istream>
14 
15 // streamsize readsome(char_type* s, streamsize n);
16 
17 #include <istream>
18 #include <cassert>
19 
20 template <class CharT>
21 struct testbuf
22     : public std::basic_streambuf<CharT>
23 {
24     typedef std::basic_string<CharT> string_type;
25     typedef std::basic_streambuf<CharT> base;
26 private:
27     string_type str_;
28 public:
29 
testbuftestbuf30     testbuf() {}
testbuftestbuf31     testbuf(const string_type& str)
32         : str_(str)
33     {
34         base::setg(const_cast<CharT*>(str_.data()),
35                    const_cast<CharT*>(str_.data()),
36                    const_cast<CharT*>(str_.data()) + str_.size());
37     }
38 
ebacktestbuf39     CharT* eback() const {return base::eback();}
gptrtestbuf40     CharT* gptr() const {return base::gptr();}
egptrtestbuf41     CharT* egptr() const {return base::egptr();}
42 };
43 
main()44 int main()
45 {
46     {
47         testbuf<char> sb(" 1234567890");
48         std::istream is(&sb);
49         char s[5];
50         assert(is.readsome(s, 5) == 5);
51         assert(!is.eof());
52         assert(!is.fail());
53         assert(std::string(s, 5) == " 1234");
54         assert(is.gcount() == 5);
55         is.readsome(s, 5);
56         assert(!is.eof());
57         assert(!is.fail());
58         assert(std::string(s, 5) == "56789");
59         assert(is.gcount() == 5);
60         is.readsome(s, 5);
61         assert(!is.eof());
62         assert(!is.fail());
63         assert(is.gcount() == 1);
64         assert(std::string(s, 1) == "0");
65         assert(is.readsome(s, 5) == 0);
66     }
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, 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, 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 }
89