]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_ostringstream/cons/char/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_ostringstream / cons / char / 1.cc
1 // Copyright (C) 2020-2023 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 // C++20 29.8.4.2 basic_ostringstream constructors [ostringstream.cons]
19
20 // { dg-options "-std=gnu++2a" }
21 // { dg-do run { target c++2a } }
22 // { dg-require-effective-target cxx11_abi }
23
24 #include <sstream>
25 #include <string>
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
28
29 void
30 test01()
31 {
32 std::ostringstream::allocator_type a;
33 std::ostringstream stm(std::ios_base::in, a);
34 }
35
36 auto const cstr = "This is a test string";
37
38 void
39 test02()
40 {
41 std::string s1(cstr);
42 std::ostringstream stm(std::move(s1));
43 VERIFY( s1.empty() );
44
45 std::string s2(cstr);
46 VERIFY( stm.str() == s2 );
47 }
48
49 void
50 test03()
51 {
52 using C = char;
53 using alloc_type = __gnu_test::uneq_allocator<C>;
54 using traits_type = std::char_traits<C>;
55 using string = std::basic_string<C, traits_type, alloc_type>;
56 using ostringstream = std::basic_ostringstream<C, traits_type, alloc_type>;
57
58 auto const mode = std::ios_base::in;
59 alloc_type a1(1);
60 const string s1(cstr, a1);
61
62 // basic_ostringstream()
63 {
64 alloc_type a0;
65 ostringstream ss;
66 VERIFY( ss.str().empty() );
67 VERIFY( ss.rdbuf()->get_allocator() == a0 );
68 VERIFY( ss.str().get_allocator() == a0 );
69 }
70
71 // basic_ostringstream(openmode)
72 {
73 alloc_type a0;
74 ostringstream ss(mode);
75 VERIFY( ss.str().empty() );
76 VERIFY( ss.rdbuf()->get_allocator() == a0 );
77 VERIFY( ss.str().get_allocator() == a0 );
78 }
79
80 // basic_ostringstream(const basic_string<C,T,A>&, openmode = in)
81 {
82 ostringstream ss(s1);
83 VERIFY( ss.str() == cstr );
84 VERIFY( ss.rdbuf()->get_allocator() == a1 );
85 VERIFY( ss.str().get_allocator() == a1 );
86 }
87
88 // basic_ostringstream(const basic_string<C,T,A>&, openmode = in)
89 {
90 ostringstream ss(s1, mode);
91 VERIFY( ss.str() == cstr );
92 VERIFY( ss.rdbuf()->get_allocator() == a1 );
93 VERIFY( ss.str().get_allocator() == a1 );
94 }
95
96 // basic_ostringstream(openmode, const A&)
97 {
98 ostringstream ss(mode, a1);
99 VERIFY( ss.str().empty() );
100 VERIFY( ss.rdbuf()->get_allocator() == a1 );
101 VERIFY( ss.str().get_allocator() == a1 );
102 }
103
104 // basic_ostringstream(basic_string<C,T,A>&&, openmode = in)
105 {
106 ostringstream ss(string{s1});
107 VERIFY( ss.str() == s1 );
108 VERIFY( ss.rdbuf()->get_allocator() == a1 );
109 VERIFY( ss.str().get_allocator() == a1 );
110 }
111
112 // basic_ostringstream(basic_string<C,T,A>&&, openmode = in)
113 {
114 ostringstream ss(string(s1), mode);
115 VERIFY( ss.str() == s1 );
116 VERIFY( ss.rdbuf()->get_allocator() == a1 );
117 VERIFY( ss.str().get_allocator() == a1 );
118 }
119
120 // basic_ostringstream(const basic_string<C,T,SA>&, const A&)
121 {
122 alloc_type a2(2);
123 ostringstream ss(s1, a2);
124 VERIFY( ss.str() == s1 );
125 VERIFY( ss.rdbuf()->get_allocator() == a2 );
126 VERIFY( ss.str().get_allocator() == a2 );
127 }
128
129 // basic_ostringstream(const basic_string<C,T,SA>&, const A&)
130 {
131 alloc_type a2(2);
132 const std::string s2 = cstr;
133 ostringstream ss(s2, a2);
134 VERIFY( ss.str() == cstr );
135 VERIFY( ss.rdbuf()->get_allocator() == a2 );
136 VERIFY( ss.str().get_allocator() == a2 );
137 }
138
139 // basic_ostringstream(const basic_string<C,T,SA>&, openmode, const A&)
140 {
141 alloc_type a2(2);
142 ostringstream ss(s1, mode, a2);
143 VERIFY( ss.str() == s1 );
144 VERIFY( ss.rdbuf()->get_allocator() == a2 );
145 VERIFY( ss.str().get_allocator() == a2 );
146 }
147
148 // basic_ostringstream(const basic_string<C,T,SA>&, openmode, const A&)
149 {
150 alloc_type a2(2);
151 const std::string s2 = cstr;
152 ostringstream ss(s2, mode, a2);
153 VERIFY( ss.str() == cstr );
154 VERIFY( ss.rdbuf()->get_allocator() == a2 );
155 VERIFY( ss.str().get_allocator() == a2 );
156 }
157
158 // basic_ostringstream(const basic_string<C,T,SA>&, openmode = in)
159 {
160 alloc_type a0;
161 const std::string s2 = cstr;
162 ostringstream ss(s2, mode);
163 VERIFY( ss.str() == cstr );
164 VERIFY( ss.rdbuf()->get_allocator() == a0 );
165 VERIFY( ss.str().get_allocator() == a0 );
166 }
167 }
168
169 // A minimal allocator with no default constructor
170 template<typename T>
171 struct NoDefaultCons : __gnu_test::SimpleAllocator<T>
172 {
173 using __gnu_test::SimpleAllocator<T>::SimpleAllocator;
174
175 NoDefaultCons() = delete;
176
177 NoDefaultCons(int) { }
178 };
179
180 void
181 test04()
182 {
183 using sstream = std::basic_ostringstream<char, std::char_traits<char>,
184 NoDefaultCons<char>>;
185
186 NoDefaultCons<char> a(1);
187 const std::string str(cstr);
188
189 sstream ss1(str, a);
190 VERIFY( ss1.str() == cstr );
191
192 sstream ss2(str, std::ios::in, a);
193 VERIFY( ss2.str() == cstr );
194 VERIFY( bool(ss2 << "That") );
195 VERIFY( ss2.str() == "That is a test string" );
196
197 sstream ss3(std::string(str), std::ios::ate, a);
198 VERIFY( ss3.str() == cstr );
199 VERIFY( bool(ss3 << "y thing") );
200 VERIFY( ss3.str() == "This is a test stringy thing" );
201 }
202
203 int
204 main()
205 {
206 test01();
207 test02();
208 test03();
209 test04();
210 }