1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <sstream>
10 
11 // template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
12 // class basic_stringbuf
13 
14 // int_type overflow(int_type c = traits::eof());
15 
16 #include <sstream>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 int overflow_called = 0;
22 
23 template <class CharT>
24 struct testbuf
25     : public std::basic_stringbuf<CharT>
26 {
27     typedef std::basic_stringbuf<CharT> base;
testbuftestbuf28     explicit testbuf(const std::basic_string<CharT>& str,
29                      std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
30         : base(str, which) {}
31 
32     typename base::int_type
overflowtestbuf33         overflow(typename base::int_type c = base::traits_type::eof())
34         {++overflow_called; return base::overflow(c);}
35 
pbumptestbuf36     void pbump(int n) {base::pbump(n);}
37 };
38 
main(int,char **)39 int main(int, char**)
40 {
41     {  // sanity check
42     testbuf<char> tb("");
43     tb.overflow();
44     }
45     {
46         testbuf<char> sb("abc");
47         assert(sb.sputc('1') == '1');
48         assert(sb.str() == "1bc");
49         assert(sb.sputc('2') == '2');
50         assert(sb.str() == "12c");
51         assert(sb.sputc('3') == '3');
52         assert(sb.str() == "123");
53         assert(sb.sputc('4') == '4');
54         assert(sb.str() == "1234");
55         assert(sb.sputc('5') == '5');
56         assert(sb.str() == "12345");
57         assert(sb.sputc('6') == '6');
58         assert(sb.str() == "123456");
59         assert(sb.sputc('7') == '7');
60         assert(sb.str() == "1234567");
61         assert(sb.sputc('8') == '8');
62         assert(sb.str() == "12345678");
63         assert(sb.sputc('9') == '9');
64         assert(sb.str() == "123456789");
65         assert(sb.sputc('0') == '0');
66         assert(sb.str() == "1234567890");
67         assert(sb.sputc('1') == '1');
68         assert(sb.str() == "12345678901");
69     }
70     {
71         testbuf<wchar_t> sb(L"abc");
72         assert(sb.sputc(L'1') == L'1');
73         assert(sb.str() == L"1bc");
74         assert(sb.sputc(L'2') == L'2');
75         assert(sb.str() == L"12c");
76         assert(sb.sputc(L'3') == L'3');
77         assert(sb.str() == L"123");
78         assert(sb.sputc(L'4') == L'4');
79         assert(sb.str() == L"1234");
80         assert(sb.sputc(L'5') == L'5');
81         assert(sb.str() == L"12345");
82         assert(sb.sputc(L'6') == L'6');
83         assert(sb.str() == L"123456");
84         assert(sb.sputc(L'7') == L'7');
85         assert(sb.str() == L"1234567");
86         assert(sb.sputc(L'8') == L'8');
87         assert(sb.str() == L"12345678");
88         assert(sb.sputc(L'9') == L'9');
89         assert(sb.str() == L"123456789");
90         assert(sb.sputc(L'0') == L'0');
91         assert(sb.str() == L"1234567890");
92         assert(sb.sputc(L'1') == L'1');
93         assert(sb.str() == L"12345678901");
94     }
95     {
96         testbuf<char> sb("abc", std::ios_base::app | std::ios_base::out);
97         assert(sb.sputc('1') == '1');
98         assert(sb.str() == "abc1");
99         assert(sb.sputc('2') == '2');
100         assert(sb.str() == "abc12");
101     }
102 
103   return 0;
104 }
105