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_osyncstream;
17 
18 // explicit basic_osyncstream(basic_ostream&  os);
19 
20 #include <cassert>
21 #include <concepts>
22 #include <syncstream>
23 #include <sstream>
24 
25 #include "test_macros.h"
26 #include "constexpr_char_traits.h"
27 #include "test_allocator.h"
28 
29 template <class CharT>
test()30 void test() {
31   {
32     using OS = std::basic_osyncstream<CharT>;
33     using W  = std::basic_ostringstream<CharT>;
34 
35     static_assert(!std::convertible_to<std::basic_ostream<CharT>&, OS>);
36     static_assert(std::constructible_from<OS, std::basic_ostream<CharT>&>);
37 
38     {
39       W w;
40 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
41       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
42 #endif
43       {
44         OS os{w};
45 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
46         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 1);
47 #endif
48         assert(os.get_wrapped() == w.rdbuf());
49         assert(os.rdbuf()->get_wrapped() == w.rdbuf());
50         assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());
51       }
52 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
53       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
54 #endif
55     }
56   }
57 
58   {
59     using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>>;
60     using W  = std::basic_ostringstream<CharT, constexpr_char_traits<CharT>>;
61 
62     static_assert(!std::convertible_to<std::basic_ostream<CharT, constexpr_char_traits<CharT>>&, OS>);
63     static_assert(std::constructible_from<OS, std::basic_ostream<CharT, constexpr_char_traits<CharT>>&>);
64 
65     {
66       W w;
67 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
68       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
69 #endif
70       {
71         OS os{w};
72 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
73         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 1);
74 #endif
75         assert(os.get_wrapped() == w.rdbuf());
76         assert(os.rdbuf()->get_wrapped() == w.rdbuf());
77         assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());
78       }
79 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
80       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
81 #endif
82     }
83   }
84 
85   {
86     using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
87     using W  = std::basic_ostringstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
88 
89     static_assert(!std::convertible_to<std::basic_ostream<CharT, constexpr_char_traits<CharT>>&, OS>);
90     static_assert(std::constructible_from<OS, std::basic_ostream<CharT, constexpr_char_traits<CharT>>&>);
91 
92     {
93       W w;
94 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
95       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
96 #endif
97       {
98         OS os{w};
99 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
100         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 1);
101 #endif
102         assert(os.get_wrapped() == w.rdbuf());
103         assert(os.rdbuf()->get_wrapped() == w.rdbuf());
104         assert(os.rdbuf()->get_allocator() == test_allocator<CharT>());
105       }
106 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
107       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
108 #endif
109     }
110   }
111 }
112 
main(int,char **)113 int main(int, char**) {
114   test<char>();
115 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
116   test<wchar_t>();
117 #endif
118 
119   return 0;
120 }
121