Debian Codegraph @

libstdc++-v3/testsuite/27_io/basic_syncstream/basic_ops/1.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-additional-options "-pthread" { target pthread } }
19 // { dg-do run { target c++20 } }
20 // { dg-require-effective-target cxx11_abi }
21
22 #include <sstream>
23 #include <string_view>
24 #include <syncstream>
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
27
28 void
29 test01() // construction
30 {
31 {
32 std::stringbuf b;
33 std::osyncstream s(&b);
34 VERIFY( s.rdbuf() != nullptr );
35 VERIFY( s.get_wrapped() == &b );
36 }
37
38 {
39 std::ostringstream stm;
40 std::osyncstream s(stm);
41 VERIFY( s.get_wrapped() == stm.rdbuf() );
42 }
43
44 {
45 using alloc_type = __gnu_test::uneq_allocator<char>;
46 using sbuf_t = std::basic_syncbuf<char, std::char_traits<char>,
47 alloc_type>;
48 using stream_t = std::basic_osyncstream<char, std::char_traits<char>,
49 alloc_type>;
50 using str_t = std::basic_ostringstream<char, std::char_traits<char>,
51 alloc_type>;
52 sbuf_t b;
53
54 alloc_type aa;
55 stream_t s1(&b, aa);
56 VERIFY( aa == s1.rdbuf()->get_allocator() );
57
58 alloc_type aaa(42);
59 stream_t s2(&b, aaa);
60 VERIFY( aaa == s2.rdbuf()->get_allocator() );
61
62 VERIFY( s1.rdbuf()->get_allocator() != s2.rdbuf()->get_allocator() );
63
64 str_t stm;
65 stream_t s3(stm, aa);
66 VERIFY( s3.get_wrapped() == stm.rdbuf() );
67 VERIFY( aa == s1.rdbuf()->get_allocator() );
68 }
69 }
70
71 void
72 test02() // moving
73 {
74 {
75 std::stringbuf b;
76 std::osyncstream s1(&b);
77
78 std::osyncstream s2(std::move(s1));
79
80 VERIFY( s1.get_wrapped() == nullptr );
81 VERIFY( s2.get_wrapped() == &b );
82 }
83
84 {
85 std::stringbuf b1;
86 std::osyncstream s1(&b1);
87
88 std::stringbuf b2;
89 std::osyncstream s2(&b2);
90 s2 = std::move(s1);
91
92 VERIFY( s1.get_wrapped() == nullptr );
93 VERIFY( s2.get_wrapped() == &b1 );
94 }
95 }
96
97 void
98 test03() // swaping
99 {
100 std::stringbuf b1;
101 std::osyncstream s1(&b1);
102
103 std::stringbuf b2;
104 std::osyncstream s2(&b2);
105
106 std::swap(s1, s2);
107
108 VERIFY( s1.get_wrapped() == &b2 );
109 VERIFY( s2.get_wrapped() == &b1 );
110 }
111
112 void
113 test04() // emitting
114 {
115 {
116 std::stringbuf b;
117 std::osyncstream s(&b);
118
119 const std::string_view txt("This is a test");
120 s << txt;
121
122 s.emit();
123 VERIFY( b.str() == txt );
124 }
125
126 {
127 std::stringbuf b;
128 std::osyncstream s(&b);
129
130 s.put('a');
131 s.put('b');
132 s.put('c');
133
134 s.emit();
135 VERIFY( b.str() == "abc" );
136 }
137
138 {
139 std::stringbuf b;
140 std::osyncstream s(&b);
141
142 s << "abc";
143 s.put(' ');
144 s << "def";
145 s.emit();
146 VERIFY( b.str() == "abc def" );
147
148 s << "ghi";
149 s.put(' ');
150 s << "jkl";
151 s.emit();
152 VERIFY( b.str() == "abc defghi jkl" );
153 }
154 }
155
156 int main()
157 {
158 test01();
159 test02();
160 test03();
161 test04();
162 }