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

source code of libcxx/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/overflow.pass.cpp