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_ostream& 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
29template <class CharT>
30void test() {
31 {
32 using OS = std::basic_osyncstream<CharT>;
33 using W = std::basic_ostringstream<CharT>;
34
35 const std::allocator<CharT> alloc;
36
37 {
38 W w;
39#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
40 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
41#endif
42 {
43 OS os = {w, alloc};
44#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
45 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 1);
46#endif
47 assert(os.get_wrapped() == w.rdbuf());
48 assert(os.rdbuf()->get_wrapped() == w.rdbuf());
49 assert(os.rdbuf()->get_allocator() == alloc);
50 }
51#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
52 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
53#endif
54 }
55 }
56 {
57 using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>>;
58 using W = std::basic_ostringstream<CharT, constexpr_char_traits<CharT>>;
59
60 const std::allocator<CharT> alloc;
61 {
62 W w;
63#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
64 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
65#endif
66 {
67 OS os = {w.rdbuf(), alloc};
68#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
69 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 1);
70#endif
71 assert(os.get_wrapped() == w.rdbuf());
72 assert(os.rdbuf()->get_wrapped() == w.rdbuf());
73 assert(os.rdbuf()->get_allocator() == alloc);
74 }
75#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
76 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
77#endif
78 }
79 }
80
81 {
82 using OS = std::basic_osyncstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
83 using W = std::basic_ostringstream<CharT, constexpr_char_traits<CharT>, test_allocator<CharT>>;
84
85 const test_allocator<CharT> alloc;
86 {
87 W w;
88#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
89 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
90#endif
91 {
92 OS os = {w.rdbuf(), alloc};
93#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
94 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 1);
95#endif
96 assert(os.get_wrapped() == w.rdbuf());
97 assert(os.rdbuf()->get_wrapped() == w.rdbuf());
98 assert(os.rdbuf()->get_allocator() == alloc);
99 }
100#if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_THREADS)
101 assert(std::__wrapped_streambuf_mutex::__instance().__get_count(w.rdbuf()) == 0);
102#endif
103 }
104 }
105}
106
107int main(int, char**) {
108 test<char>();
109#ifndef TEST_HAS_NO_WIDE_CHARACTERS
110 test<wchar_t>();
111#endif
112
113 return 0;
114}
115

source code of libcxx/test/std/input.output/syncstream/osyncstream/syncstream.osyncstream.cons/cons.ostream.allocator.pass.cpp