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; 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 39 int main(int, char**) 40 { 41 // sanity check 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 { 72 testbuf<wchar_t> sb(L"abc"); 73 assert(sb.sputc(L'1') == L'1'); 74 assert(sb.str() == L"1bc"); 75 assert(sb.sputc(L'2') == L'2'); 76 assert(sb.str() == L"12c"); 77 assert(sb.sputc(L'3') == L'3'); 78 assert(sb.str() == L"123"); 79 assert(sb.sputc(L'4') == L'4'); 80 assert(sb.str() == L"1234"); 81 assert(sb.sputc(L'5') == L'5'); 82 assert(sb.str() == L"12345"); 83 assert(sb.sputc(L'6') == L'6'); 84 assert(sb.str() == L"123456"); 85 assert(sb.sputc(L'7') == L'7'); 86 assert(sb.str() == L"1234567"); 87 assert(sb.sputc(L'8') == L'8'); 88 assert(sb.str() == L"12345678"); 89 assert(sb.sputc(L'9') == L'9'); 90 assert(sb.str() == L"123456789"); 91 assert(sb.sputc(L'0') == L'0'); 92 assert(sb.str() == L"1234567890"); 93 assert(sb.sputc(L'1') == L'1'); 94 assert(sb.str() == L"12345678901"); 95 } 96 { 97 testbuf<char> sb("abc", std::ios_base::app | std::ios_base::out); 98 assert(sb.sputc('1') == '1'); 99 assert(sb.str() == "abc1"); 100 assert(sb.sputc('2') == '2'); 101 assert(sb.str() == "abc12"); 102 } 103 104 return 0; 105 } 106