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>& unget();
     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(" 123456789");
     45         std::istream is(&sb);
     46         is.get();
     47         is.get();
     48         is.get();
     49         is.unget();
     50         assert(is.good());
     51         assert(is.gcount() == 0);
     52         is.unget();
     53         assert(is.good());
     54         assert(is.gcount() == 0);
     55         is.unget();
     56         assert(is.good());
     57         assert(is.gcount() == 0);
     58         is.unget();
     59         assert(is.bad());
     60         assert(is.gcount() == 0);
     61     }
     62     {
     63         testbuf<wchar_t> sb(L" 123456789");
     64         std::wistream is(&sb);
     65         is.get();
     66         is.get();
     67         is.get();
     68         is.unget();
     69         assert(is.good());
     70         assert(is.gcount() == 0);
     71         is.unget();
     72         assert(is.good());
     73         assert(is.gcount() == 0);
     74         is.unget();
     75         assert(is.good());
     76         assert(is.gcount() == 0);
     77         is.unget();
     78         assert(is.bad());
     79         assert(is.gcount() == 0);
     80     }
     81 }
     82