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