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::traits_type::eof())
     33         {++overflow_called; return base::overflow(c);}
     34 
     35     void pbump(int n) {base::pbump(n);}
     36 };
     37 
     38 int main()
     39 {
     40     {  // sanity check
     41     testbuf<char> tb("");
     42     tb.overflow();
     43     }
     44     {
     45         testbuf<char> sb("abc");
     46         assert(sb.sputc('1') == '1');
     47         assert(sb.str() == "1bc");
     48         assert(sb.sputc('2') == '2');
     49         assert(sb.str() == "12c");
     50         assert(sb.sputc('3') == '3');
     51         assert(sb.str() == "123");
     52         assert(sb.sputc('4') == '4');
     53         assert(sb.str() == "1234");
     54         assert(sb.sputc('5') == '5');
     55         assert(sb.str() == "12345");
     56         assert(sb.sputc('6') == '6');
     57         assert(sb.str() == "123456");
     58         assert(sb.sputc('7') == '7');
     59         assert(sb.str() == "1234567");
     60         assert(sb.sputc('8') == '8');
     61         assert(sb.str() == "12345678");
     62         assert(sb.sputc('9') == '9');
     63         assert(sb.str() == "123456789");
     64         assert(sb.sputc('0') == '0');
     65         assert(sb.str() == "1234567890");
     66         assert(sb.sputc('1') == '1');
     67         assert(sb.str() == "12345678901");
     68     }
     69     {
     70         testbuf<wchar_t> sb(L"abc");
     71         assert(sb.sputc(L'1') == L'1');
     72         assert(sb.str() == L"1bc");
     73         assert(sb.sputc(L'2') == L'2');
     74         assert(sb.str() == L"12c");
     75         assert(sb.sputc(L'3') == L'3');
     76         assert(sb.str() == L"123");
     77         assert(sb.sputc(L'4') == L'4');
     78         assert(sb.str() == L"1234");
     79         assert(sb.sputc(L'5') == L'5');
     80         assert(sb.str() == L"12345");
     81         assert(sb.sputc(L'6') == L'6');
     82         assert(sb.str() == L"123456");
     83         assert(sb.sputc(L'7') == L'7');
     84         assert(sb.str() == L"1234567");
     85         assert(sb.sputc(L'8') == L'8');
     86         assert(sb.str() == L"12345678");
     87         assert(sb.sputc(L'9') == L'9');
     88         assert(sb.str() == L"123456789");
     89         assert(sb.sputc(L'0') == L'0');
     90         assert(sb.str() == L"1234567890");
     91         assert(sb.sputc(L'1') == L'1');
     92         assert(sb.str() == L"12345678901");
     93     }
     94     {
     95         testbuf<char> sb("abc", std::ios_base::app | std::ios_base::out);
     96         assert(sb.sputc('1') == '1');
     97         assert(sb.str() == "abc1");
     98         assert(sb.sputc('2') == '2');
     99         assert(sb.str() == "abc12");
    100     }
    101 }
    102