xref: /llvm-project/libcxx/test/std/input.output/syncstream/syncbuf/sync.pass.cpp (revision 7cc72a0a2ec22855572d96411febd4f2c4ac5a49)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: libcpp-has-no-experimental-syncstream
12 
13 // <syncstream>
14 
15 // template <class charT, class traits, class Allocator>
16 // class basic_syncbuf;
17 
18 // protected:
19 // [syncstream.syncbuf.virtuals], overridden virtual functions
20 // int sync() override;
21 
22 #include <syncstream>
23 #include <sstream>
24 #include <ostream>
25 #include <cassert>
26 
27 #include "test_macros.h"
28 
29 template <class CharT>
test_sync(bool emit_on_sync)30 void test_sync(bool emit_on_sync) {
31   std::basic_stringbuf<CharT> base;
32   std::basic_syncbuf<CharT> buff(&base);
33   std::basic_ostream<CharT> out(&buff);
34 
35   buff.set_emit_on_sync(emit_on_sync);
36 
37   out << 'a';
38   out.flush(); // This is an indirect call to sync.
39 
40   if (emit_on_sync) {
41     assert(base.str().size() == 1);
42     assert(base.str()[0] == CharT('a'));
43   } else
44     assert(base.str().empty());
45 }
46 
main(int,char **)47 int main(int, char**) {
48   test_sync<char>(true);
49   test_sync<char>(false);
50 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
51   test_sync<wchar_t>(true);
52   test_sync<wchar_t>(false);
53 #endif
54 
55   return 0;
56 }
57