Home | History | Annotate | Download | only in ostream_sentry
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <ostream>
     11 
     12 // template <class charT, class traits = char_traits<charT> >
     13 // class basic_ostream::sentry;
     14 
     15 // ~sentry();
     16 
     17 #include <ostream>
     18 #include <cassert>
     19 
     20 #include "test_macros.h"
     21 
     22 int sync_called = 0;
     23 
     24 template <class CharT>
     25 struct testbuf1
     26     : public std::basic_streambuf<CharT>
     27 {
     28     testbuf1() {}
     29 
     30 protected:
     31 
     32     int virtual sync()
     33     {
     34         ++sync_called;
     35         return 1;
     36     }
     37 };
     38 
     39 int main()
     40 {
     41     {
     42         std::ostream os((std::streambuf*)0);
     43         std::ostream::sentry s(os);
     44         assert(!bool(s));
     45     }
     46     assert(sync_called == 0);
     47     {
     48         testbuf1<char> sb;
     49         std::ostream os(&sb);
     50         std::ostream::sentry s(os);
     51         assert(bool(s));
     52     }
     53     assert(sync_called == 0);
     54     {
     55         testbuf1<char> sb;
     56         std::ostream os(&sb);
     57         std::ostream::sentry s(os);
     58         assert(bool(s));
     59         unitbuf(os);
     60     }
     61     assert(sync_called == 1);
     62 #ifndef TEST_HAS_NO_EXCEPTIONS
     63     {
     64         testbuf1<char> sb;
     65         std::ostream os(&sb);
     66         try
     67         {
     68             std::ostream::sentry s(os);
     69             assert(bool(s));
     70             unitbuf(os);
     71             throw 1;
     72         }
     73         catch (...)
     74         {
     75         }
     76         assert(sync_called == 1);
     77     }
     78 #endif
     79 }
     80