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