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 // <istream>
     11 
     12 // basic_istream<charT,traits>& get(char_type* s, streamsize n);
     13 
     14 #include <istream>
     15 #include <cassert>
     16 
     17 template <class CharT>
     18 struct testbuf
     19     : public std::basic_streambuf<CharT>
     20 {
     21     typedef std::basic_string<CharT> string_type;
     22     typedef std::basic_streambuf<CharT> base;
     23 private:
     24     string_type str_;
     25 public:
     26 
     27     testbuf() {}
     28     testbuf(const string_type& str)
     29         : str_(str)
     30     {
     31         base::setg(const_cast<CharT*>(str_.data()),
     32                    const_cast<CharT*>(str_.data()),
     33                    const_cast<CharT*>(str_.data()) + str_.size());
     34     }
     35 
     36     CharT* eback() const {return base::eback();}
     37     CharT* gptr() const {return base::gptr();}
     38     CharT* egptr() const {return base::egptr();}
     39 };
     40 
     41 int main()
     42 {
     43     {
     44         testbuf<char> sb("  \n    \n ");
     45         std::istream is(&sb);
     46         char s[5];
     47         is.get(s, 5);
     48         assert(!is.eof());
     49         assert(!is.fail());
     50         assert(std::string(s) == "  ");
     51         assert(is.gcount() == 2);
     52         is.get(s, 5);
     53         assert(!is.eof());
     54         assert( is.fail());
     55         assert(std::string(s) == "");
     56         assert(is.gcount() == 0);
     57         is.clear();
     58         assert(is.get() == '\n');
     59         is.get(s, 5);
     60         assert(!is.eof());
     61         assert(!is.fail());
     62         assert(std::string(s) == "    ");
     63         assert(is.gcount() == 4);
     64         assert(is.get() == '\n');
     65         is.get(s, 5);
     66         assert( is.eof());
     67         assert(!is.fail());
     68         assert(std::string(s) == " ");
     69         assert(is.gcount() == 1);
     70     }
     71     {
     72         testbuf<wchar_t> sb(L"  \n    \n ");
     73         std::wistream is(&sb);
     74         wchar_t s[5];
     75         is.get(s, 5);
     76         assert(!is.eof());
     77         assert(!is.fail());
     78         assert(std::wstring(s) == L"  ");
     79         assert(is.gcount() == 2);
     80         is.get(s, 5);
     81         assert(!is.eof());
     82         assert( is.fail());
     83         assert(std::wstring(s) == L"");
     84         assert(is.gcount() == 0);
     85         is.clear();
     86         assert(is.get() == L'\n');
     87         is.get(s, 5);
     88         assert(!is.eof());
     89         assert(!is.fail());
     90         assert(std::wstring(s) == L"    ");
     91         assert(is.gcount() == 4);
     92         assert(is.get() == L'\n');
     93         is.get(s, 5);
     94         assert( is.eof());
     95         assert(!is.fail());
     96         assert(std::wstring(s) == L" ");
     97         assert(is.gcount() == 1);
     98     }
     99 }
    100