81 lines · cpp
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-exception6//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 21int sync_called = 0;22 23template <class CharT>24struct testbuf125 : public std::basic_streambuf<CharT>26{27 testbuf1() {}28 29protected:30 31 int virtual sync()32 {33 ++sync_called;34 return 1;35 }36};37 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_EXCEPTIONS62 {63 testbuf1<char> sb;64 std::ostream os(&sb);65 try66 {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#endif78 79 return 0;80}81