read.pass.cpp revision aac8dd83317151da747c86e786df34e80872bf0c
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_lib=x86_64-apple-darwin11
11
12// <istream>
13
14// basic_istream<charT,traits>& read(char_type* s, streamsize n);
15
16#include <istream>
17#include <cassert>
18
19template <class CharT>
20struct testbuf
21    : public std::basic_streambuf<CharT>
22{
23    typedef std::basic_string<CharT> string_type;
24    typedef std::basic_streambuf<CharT> base;
25private:
26    string_type str_;
27public:
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
43int main()
44{
45    {
46        testbuf<char> sb(" 123456789");
47        std::istream is(&sb);
48        char s[5];
49        is.read(s, 5);
50        assert(!is.eof());
51        assert(!is.fail());
52        assert(std::string(s, 5) == " 1234");
53        assert(is.gcount() == 5);
54        is.read(s, 5);
55        assert(!is.eof());
56        assert(!is.fail());
57        assert(std::string(s, 5) == "56789");
58        assert(is.gcount() == 5);
59        is.read(s, 5);
60        assert( is.eof());
61        assert( is.fail());
62        assert(is.gcount() == 0);
63    }
64    {
65        testbuf<wchar_t> sb(L" 123456789");
66        std::wistream is(&sb);
67        wchar_t s[5];
68        is.read(s, 5);
69        assert(!is.eof());
70        assert(!is.fail());
71        assert(std::wstring(s, 5) == L" 1234");
72        assert(is.gcount() == 5);
73        is.read(s, 5);
74        assert(!is.eof());
75        assert(!is.fail());
76        assert(std::wstring(s, 5) == L"56789");
77        assert(is.gcount() == 5);
78        is.read(s, 5);
79        assert( is.eof());
80        assert( is.fail());
81        assert(is.gcount() == 0);
82    }
83}
84