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 // basic_syncbuf(basic_syncbuf&& other);
19 
20 #include <syncstream>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "constexpr_char_traits.h"
25 #include "test_allocator.h"
26 
27 template <class CharT>
test()28 void test() {
29   {
30     using Buf = std::basic_syncbuf<CharT>;
31     const std::allocator<CharT> alloc;
32     {
33       Buf b1{nullptr, alloc};
34       Buf b2{std::move(b1)};
35 
36       assert(b2.get_wrapped() == nullptr);
37       assert(b2.get_allocator() == alloc);
38     }
39     {
40       Buf w;
41 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
42       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
43 #endif
44       {
45         Buf b1{&w, alloc};
46 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
47         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
48 #endif
49         Buf b2{std::move(b1)};
50 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
51         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
52 #endif
53         assert(b2.get_wrapped() == &w);
54         assert(b2.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 Buf = std::basic_syncbuf<CharT, constexpr_char_traits<CharT>>;
64     const std::allocator<CharT> alloc;
65     {
66       Buf b1{nullptr, alloc};
67       Buf b2{std::move(b1)};
68 
69       assert(b2.get_wrapped() == nullptr);
70       assert(b2.get_allocator() == alloc);
71     }
72     {
73       Buf w;
74 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
75       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
76 #endif
77       {
78         Buf b1{&w, alloc};
79 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
80         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
81 #endif
82         Buf b2{std::move(b1)};
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(b2.get_wrapped() == &w);
87         assert(b2.get_allocator() == alloc);
88       }
89 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
90       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
91 #endif
92     }
93   }
94 
95   {
96     using Buf = std::basic_syncbuf<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
97     test_allocator<CharT> alloc{42};
98     {
99       Buf b1{nullptr, alloc};
100       Buf b2{std::move(b1)};
101 
102       assert(b2.get_wrapped() == nullptr);
103       assert(b2.get_allocator() == alloc);
104     }
105     {
106       Buf w;
107 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
108       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
109 #endif
110       {
111         Buf b1{&w, alloc};
112 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
113         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
114 #endif
115         Buf b2{std::move(b1)};
116 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
117         assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 1);
118 #endif
119         assert(b2.get_wrapped() == &w);
120         assert(b2.get_allocator() == alloc);
121       }
122 #if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
123       assert(std::__wrapped_streambuf_mutex::__instance().__get_count(&w) == 0);
124 #endif
125     }
126   }
127 }
128 
main(int,char **)129 int main(int, char**) {
130   test<char>();
131 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
132   test<wchar_t>();
133 #endif
134 
135   return 0;
136 }
137