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 // <ostream> 10 11 // template <class charT, class traits = char_traits<charT> > 12 // class basic_ostream::sentry; 13 14 // ~sentry(); 15 16 #include <ostream> 17 #include <cassert> 18 19 #include "test_macros.h" 20 21 int sync_called = 0; 22 23 template <class CharT> 24 struct testbuf1 25 : public std::basic_streambuf<CharT> 26 { testbuf1testbuf127 testbuf1() {} 28 29 protected: 30 synctestbuf131 int virtual sync() 32 { 33 ++sync_called; 34 return 1; 35 } 36 }; 37 main(int,char **)38int main(int, char**) 39 { 40 { 41 std::ostream os((std::streambuf*)0); 42 std::ostream::sentry s(os); 43 assert(!bool(s)); 44 } 45 assert(sync_called == 0); 46 { 47 testbuf1<char> sb; 48 std::ostream os(&sb); 49 std::ostream::sentry s(os); 50 assert(bool(s)); 51 } 52 assert(sync_called == 0); 53 { 54 testbuf1<char> sb; 55 std::ostream os(&sb); 56 std::ostream::sentry s(os); 57 assert(bool(s)); 58 std::unitbuf(os); 59 } 60 assert(sync_called == 1); 61 #ifndef TEST_HAS_NO_EXCEPTIONS 62 { 63 testbuf1<char> sb; 64 std::ostream os(&sb); 65 try 66 { 67 std::ostream::sentry s(os); 68 assert(bool(s)); 69 std::unitbuf(os); 70 throw 1; 71 } 72 catch (...) 73 { 74 } 75 assert(sync_called == 1); 76 } 77 #endif 78 79 return 0; 80 } 81