Home | History | Annotate | Download | only in istream.unformatted
      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 // XFAIL: with_system_lib=x86_64-apple-darwin12
     12 
     13 // <istream>
     14 
     15 // basic_istream<charT,traits>& read(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 
     30     testbuf() {}
     31     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 
     39     CharT* eback() const {return base::eback();}
     40     CharT* gptr() const {return base::gptr();}
     41     CharT* egptr() const {return base::egptr();}
     42 };
     43 
     44 int main()
     45 {
     46     {
     47         testbuf<char> sb(" 123456789");
     48         std::istream is(&sb);
     49         char s[5];
     50         is.read(s, 5);
     51         assert(!is.eof());
     52         assert(!is.fail());
     53         assert(std::string(s, 5) == " 1234");
     54         assert(is.gcount() == 5);
     55         is.read(s, 5);
     56         assert(!is.eof());
     57         assert(!is.fail());
     58         assert(std::string(s, 5) == "56789");
     59         assert(is.gcount() == 5);
     60         is.read(s, 5);
     61         assert( is.eof());
     62         assert( is.fail());
     63         assert(is.gcount() == 0);
     64     }
     65     {
     66         testbuf<wchar_t> sb(L" 123456789");
     67         std::wistream is(&sb);
     68         wchar_t s[5];
     69         is.read(s, 5);
     70         assert(!is.eof());
     71         assert(!is.fail());
     72         assert(std::wstring(s, 5) == L" 1234");
     73         assert(is.gcount() == 5);
     74         is.read(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.read(s, 5);
     80         assert( is.eof());
     81         assert( is.fail());
     82         assert(is.gcount() == 0);
     83     }
     84 }
     85