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(basic_osyncstream&&) noexcept;
19 
20 // TODO Why is this noexcept?
21 // Does the reasoning for https://cplusplus.github.io/LWG/issue3867 not hold true here?
22 
23 #include <cassert>
24 #include <sstream>
25 #include <syncstream>
26 
27 #include "test_macros.h"
28 #include "constexpr_char_traits.h"
29 #include "test_allocator.h"
30 
31 template <class CharT>
test()32 void test() {
33   {
34     using OS = std::basic_osyncstream<CharT>;
35     using W  = std::basic_syncbuf<CharT>;
36 
37     const std::allocator<CharT> alloc;
38     {
39       OS os = {OS{nullptr, alloc}};
40       assert(os.get_wrapped() == nullptr);
41       assert(os.rdbuf()->get_wrapped() == nullptr);
42       assert(os.rdbuf()->get_allocator() == alloc);
43       ASSERT_NOEXCEPT(OS{std::move(os)});
44     }
45     {
46       W w;
47 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
48       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
49 #endif
50       {
51         OS os = {OS{&w, alloc}};
52 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
53         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
54 #endif
55         assert(os.get_wrapped() == &w);
56         assert(os.rdbuf()->get_wrapped() == &w);
57         assert(os.rdbuf()->get_allocator() == alloc);
58       }
59 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
60       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
61 #endif
62     }
63   }
64 
65   {
66     using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>>;
67     using W  = std::basic_stringbuf<CharT, constexpr_char_traits<CharT>>;
68 
69     const std::allocator<CharT> alloc;
70     {
71       OS os = {OS{nullptr, alloc}};
72       assert(os.get_wrapped() == nullptr);
73       assert(os.rdbuf()->get_wrapped() == nullptr);
74       assert(os.rdbuf()->get_allocator() == alloc);
75     }
76     {
77       W w;
78 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
79       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
80 #endif
81       {
82         OS os = {OS{&w, alloc}};
83 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
84         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
85 #endif
86         assert(os.get_wrapped() == &w);
87         assert(os.rdbuf()->get_wrapped() == &w);
88         assert(os.rdbuf()->get_allocator() == alloc);
89       }
90 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
91       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
92 #endif
93     }
94   }
95 
96   {
97     using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
98     using W  = std::basic_stringbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
99 
100     const test_allocator<CharT> alloc;
101     {
102       OS os = {OS{nullptr, alloc}};
103       assert(os.get_wrapped() == nullptr);
104       assert(os.rdbuf()->get_wrapped() == nullptr);
105       assert(os.rdbuf()->get_allocator() == alloc);
106     }
107     {
108       W w;
109 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
110       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
111 #endif
112       {
113         OS os = {OS{&w, alloc}};
114 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
115         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
116 #endif
117         assert(os.get_wrapped() == &w);
118         assert(os.rdbuf()->get_wrapped() == &w);
119         assert(os.rdbuf()->get_allocator() == alloc);
120       }
121 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
122       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
123 #endif
124     }
125   }
126 }
127 
main(int,char **)128 int main(int, char**) {
129   test<char>();
130 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
131   test<wchar_t>();
132 #endif
133 
134   return 0;
135 }
136