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 overflow(int_type c = traits::eof());
     16 
     17 #include <sstream>
     18 #include <cassert>
     19 
     20 int overflow_called = 0;
     21 
     22 template <class CharT>
     23 struct testbuf
     24     : public std::basic_stringbuf<CharT>
     25 {
     26     typedef std::basic_stringbuf<CharT> base;
     27     explicit testbuf(const std::basic_string<CharT>& str,
     28                      std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
     29         : base(str, which) {}
     30 
     31     typename base::int_type
     32         overflow(typename base::int_type c = base::type_traits::eof())
     33         {++overflow_called; return base::overflow(c);}
     34 
     35     void pbump(int n) {base::pbump(n);}
     36 };
     37 
     38 int main()
     39 {
     40     {
     41         testbuf<char> sb("abc");
     42         assert(sb.sputc('1') == '1');
     43         assert(sb.str() == "1bc");
     44         assert(sb.sputc('2') == '2');
     45         assert(sb.str() == "12c");
     46         assert(sb.sputc('3') == '3');
     47         assert(sb.str() == "123");
     48         assert(sb.sputc('4') == '4');
     49         assert(sb.str() == "1234");
     50         assert(sb.sputc('5') == '5');
     51         assert(sb.str() == "12345");
     52         assert(sb.sputc('6') == '6');
     53         assert(sb.str() == "123456");
     54         assert(sb.sputc('7') == '7');
     55         assert(sb.str() == "1234567");
     56         assert(sb.sputc('8') == '8');
     57         assert(sb.str() == "12345678");
     58         assert(sb.sputc('9') == '9');
     59         assert(sb.str() == "123456789");
     60         assert(sb.sputc('0') == '0');
     61         assert(sb.str() == "1234567890");
     62         assert(sb.sputc('1') == '1');
     63         assert(sb.str() == "12345678901");
     64     }
     65     {
     66         testbuf<wchar_t> sb(L"abc");
     67         assert(sb.sputc(L'1') == L'1');
     68         assert(sb.str() == L"1bc");
     69         assert(sb.sputc(L'2') == L'2');
     70         assert(sb.str() == L"12c");
     71         assert(sb.sputc(L'3') == L'3');
     72         assert(sb.str() == L"123");
     73         assert(sb.sputc(L'4') == L'4');
     74         assert(sb.str() == L"1234");
     75         assert(sb.sputc(L'5') == L'5');
     76         assert(sb.str() == L"12345");
     77         assert(sb.sputc(L'6') == L'6');
     78         assert(sb.str() == L"123456");
     79         assert(sb.sputc(L'7') == L'7');
     80         assert(sb.str() == L"1234567");
     81         assert(sb.sputc(L'8') == L'8');
     82         assert(sb.str() == L"12345678");
     83         assert(sb.sputc(L'9') == L'9');
     84         assert(sb.str() == L"123456789");
     85         assert(sb.sputc(L'0') == L'0');
     86         assert(sb.str() == L"1234567890");
     87         assert(sb.sputc(L'1') == L'1');
     88         assert(sb.str() == L"12345678901");
     89     }
     90     {
     91         testbuf<char> sb("abc", std::ios_base::app | std::ios_base::out);
     92         assert(sb.sputc('1') == '1');
     93         assert(sb.str() == "abc1");
     94         assert(sb.sputc('2') == '2');
     95         assert(sb.str() == "abc12");
     96     }
     97 }
     98