Home | History | Annotate | Download | only in istream.manip
      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 // template <class charT, class traits>
     13 //   basic_istream<charT,traits>&
     14 //   ws(basic_istream<charT,traits>& is);
     15 
     16 #include <istream>
     17 #include <cassert>
     18 
     19 template <class CharT>
     20 struct testbuf
     21     : public std::basic_streambuf<CharT>
     22 {
     23     typedef std::basic_string<CharT> string_type;
     24     typedef std::basic_streambuf<CharT> base;
     25 private:
     26     string_type str_;
     27 public:
     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 
     43 int main()
     44 {
     45     {
     46         testbuf<char> sb("   123");
     47         std::istream is(&sb);
     48         ws(is);
     49         assert(is.good());
     50         assert(is.peek() == '1');
     51     }
     52     {
     53         testbuf<wchar_t> sb(L"   123");
     54         std::wistream is(&sb);
     55         ws(is);
     56         assert(is.good());
     57         assert(is.peek() == L'1');
     58     }
     59     {
     60         testbuf<char> sb("  ");
     61         std::istream is(&sb);
     62         ws(is);
     63         assert(!is.fail());
     64         assert(is.eof());
     65         ws(is);
     66         assert(is.eof());
     67         assert(is.fail());
     68     }
     69     {
     70         testbuf<wchar_t> sb(L"  ");
     71         std::wistream is(&sb);
     72         ws(is);
     73         assert(!is.fail());
     74         assert(is.eof());
     75         ws(is);
     76         assert(is.eof());
     77         assert(is.fail());
     78     }
     79 }
     80