Home | History | Annotate | Download | only in istream_extractors
      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 traits>
     13 //   basic_istream<char,traits>& operator>>(basic_istream<char,traits>&& in, signed char& c);
     14 
     15 #include <istream>
     16 #include <cassert>
     17 
     18 template <class CharT>
     19 struct testbuf
     20     : public std::basic_streambuf<CharT>
     21 {
     22     typedef std::basic_string<CharT> string_type;
     23     typedef std::basic_streambuf<CharT> base;
     24 private:
     25     string_type str_;
     26 public:
     27 
     28     testbuf() {}
     29     testbuf(const string_type& str)
     30         : str_(str)
     31     {
     32         base::setg(const_cast<CharT*>(str_.data()),
     33                    const_cast<CharT*>(str_.data()),
     34                    const_cast<CharT*>(str_.data()) + str_.size());
     35     }
     36 
     37     CharT* eback() const {return base::eback();}
     38     CharT* gptr() const {return base::gptr();}
     39     CharT* egptr() const {return base::egptr();}
     40 };
     41 
     42 int main()
     43 {
     44     {
     45         testbuf<char> sb("          ");
     46         std::istream is(&sb);
     47         signed char c = 'z';
     48         is >> c;
     49         assert( is.eof());
     50         assert( is.fail());
     51         assert(c == 'z');
     52     }
     53     {
     54         testbuf<char> sb("   abcdefghijk    ");
     55         std::istream is(&sb);
     56         signed char c;
     57         is >> c;
     58         assert(!is.eof());
     59         assert(!is.fail());
     60         assert(c == 'a');
     61         is >> c;
     62         assert(!is.eof());
     63         assert(!is.fail());
     64         assert(c == 'b');
     65         is >> c;
     66         assert(!is.eof());
     67         assert(!is.fail());
     68         assert(c == 'c');
     69     }
     70 }
     71