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(streambuf_type* 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_stringbuf<CharT>;
34 
35     static_assert(!std::convertible_to<std::basic_syncbuf<CharT>*, OS>);
36     static_assert(std::constructible_from<OS, std::basic_syncbuf<CharT>*>);
37 
38     {
39       OS os{nullptr};
40       assert(os.get_wrapped() == nullptr);
41       assert(os.rdbuf()->get_wrapped() == nullptr);
42       assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());
43     }
44     {
45       W w;
46 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
47       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
48 #endif
49       {
50         OS os{&w};
51 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
52         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
53 #endif
54         assert(os.get_wrapped() == &w);
55         assert(os.rdbuf()->get_wrapped() == &w);
56         assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());
57       }
58 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
59       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
60 #endif
61     }
62   }
63 
64   {
65     using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>>;
66     using W  = std::basic_stringbuf<CharT, constexpr_char_traits<CharT>>;
67 
68     static_assert(!std::convertible_to<std::basic_syncbuf<CharT, constexpr_char_traits<CharT>>*, OS>);
69     static_assert(std::constructible_from<OS, std::basic_syncbuf<CharT, constexpr_char_traits<CharT>>*>);
70 
71     {
72       OS os{nullptr};
73       assert(os.get_wrapped() == nullptr);
74       assert(os.rdbuf()->get_wrapped() == nullptr);
75       assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());
76     }
77     {
78       W w;
79 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
80       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
81 #endif
82       {
83         OS os{&w};
84 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
85         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
86 #endif
87         assert(os.get_wrapped() == &w);
88         assert(os.rdbuf()->get_wrapped() == &w);
89         assert(os.rdbuf()->get_allocator() == std::allocator<CharT>());
90       }
91 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
92       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
93 #endif
94     }
95   }
96 
97   {
98     using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
99     using W  = std::basic_stringbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
100 
101     static_assert(
102         !std::convertible_to<std::basic_syncbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>*, OS>);
103     static_assert(
104         std::constructible_from<OS, std::basic_syncbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>*>);
105 
106     {
107       OS os{nullptr};
108       assert(os.get_wrapped() == nullptr);
109       assert(os.rdbuf()->get_wrapped() == nullptr);
110       assert(os.rdbuf()->get_allocator() == test_allocator<CharT>());
111     }
112     {
113       W w;
114 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
115       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
116 #endif
117       {
118         OS os{&w};
119 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
120         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
121 #endif
122         assert(os.get_wrapped() == &w);
123         assert(os.rdbuf()->get_wrapped() == &w);
124         assert(os.rdbuf()->get_allocator() == test_allocator<CharT>());
125       }
126 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
127       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
128 #endif
129     }
130   }
131 }
132 
main(int,char **)133 int main(int, char**) {
134   test<char>();
135 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
136   test<wchar_t>();
137 #endif
138 
139   return 0;
140 }
141