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