Home | History | Annotate | Download | only in stringbuf.virtuals
      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 // <sstream>
     11 
     12 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
     13 // class basic_stringbuf
     14 
     15 // int_type pbackfail(int_type c = traits::eof());
     16 
     17 #include <sstream>
     18 #include <cassert>
     19 
     20 template <class CharT>
     21 struct testbuf
     22     : public std::basic_stringbuf<CharT>
     23 {
     24     typedef std::basic_stringbuf<CharT> base;
     25     explicit testbuf(const std::basic_string<CharT>& str,
     26                      std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
     27         : base(str, which) {}
     28 
     29     typename base::int_type
     30         pbackfail(typename base::int_type c = base::traits_type::eof())
     31         {return base::pbackfail(c);}
     32 
     33     void pbump(int n) {base::pbump(n);}
     34 };
     35 
     36 int main()
     37 {
     38     {  // sanity check
     39     testbuf<char> tb("");;
     40     tb.pbackfail();
     41     }
     42     {
     43         testbuf<char> sb("123", std::ios_base::in);
     44         assert(sb.sgetc() == '1');
     45         assert(sb.snextc() == '2');
     46         assert(sb.snextc() == '3');
     47         assert(sb.sgetc() == '3');
     48         assert(sb.snextc() == std::char_traits<char>::eof());
     49         assert(sb.pbackfail('3') == '3');
     50         assert(sb.pbackfail('3') == std::char_traits<char>::eof());
     51         assert(sb.pbackfail('2') == '2');
     52         assert(sb.pbackfail(std::char_traits<char>::eof()) != std::char_traits<char>::eof());
     53         assert(sb.pbackfail(std::char_traits<char>::eof()) == std::char_traits<char>::eof());
     54         assert(sb.str() == "123");
     55     }
     56     {
     57         testbuf<char> sb("123");
     58         assert(sb.sgetc() == '1');
     59         assert(sb.snextc() == '2');
     60         assert(sb.snextc() == '3');
     61         assert(sb.sgetc() == '3');
     62         assert(sb.snextc() == std::char_traits<char>::eof());
     63         assert(sb.pbackfail('3') == '3');
     64         assert(sb.pbackfail('3') == '3');
     65         assert(sb.pbackfail(std::char_traits<char>::eof()) != std::char_traits<char>::eof());
     66         assert(sb.pbackfail(std::char_traits<char>::eof()) == std::char_traits<char>::eof());
     67         assert(sb.str() == "133");
     68     }
     69     {
     70         testbuf<wchar_t> sb(L"123", std::ios_base::in);
     71         assert(sb.sgetc() == L'1');
     72         assert(sb.snextc() == L'2');
     73         assert(sb.snextc() == L'3');
     74         assert(sb.sgetc() == L'3');
     75         assert(sb.snextc() == std::char_traits<wchar_t>::eof());
     76         assert(sb.pbackfail(L'3') == L'3');
     77         assert(sb.pbackfail(L'3') == std::char_traits<wchar_t>::eof());
     78         assert(sb.pbackfail(L'2') == L'2');
     79         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) != std::char_traits<wchar_t>::eof());
     80         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) == std::char_traits<wchar_t>::eof());
     81         assert(sb.str() == L"123");
     82     }
     83     {
     84         testbuf<wchar_t> sb(L"123");
     85         assert(sb.sgetc() == L'1');
     86         assert(sb.snextc() == L'2');
     87         assert(sb.snextc() == L'3');
     88         assert(sb.sgetc() == L'3');
     89         assert(sb.snextc() == std::char_traits<wchar_t>::eof());
     90         assert(sb.pbackfail(L'3') == L'3');
     91         assert(sb.pbackfail(L'3') == L'3');
     92         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) != std::char_traits<wchar_t>::eof());
     93         assert(sb.pbackfail(std::char_traits<wchar_t>::eof()) == std::char_traits<wchar_t>::eof());
     94         assert(sb.str() == L"133");
     95     }
     96 }
     97