]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_ostringstream/cons/char/1.cc
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
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)
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.
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/>.
18 // C++20 29.8.4.2 basic_ostringstream constructors [ostringstream.cons]
20 // { dg-options "-std=gnu++2a" }
21 // { dg-do run { target c++2a } }
22 // { dg-require-effective-target cxx11_abi }
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
32 std::ostringstream::allocator_type a
;
33 std::ostringstream
stm(std::ios_base::in
, a
);
36 auto const cstr
= "This is a test string";
42 std::ostringstream
stm(std::move(s1
));
46 VERIFY( stm
.str() == s2
);
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
>;
58 auto const mode
= std::ios_base::in
;
60 const string
s1(cstr
, a1
);
62 // basic_ostringstream()
66 VERIFY( ss
.str().empty() );
67 VERIFY( ss
.rdbuf()->get_allocator() == a0
);
68 VERIFY( ss
.str().get_allocator() == a0
);
71 // basic_ostringstream(openmode)
74 ostringstream
ss(mode
);
75 VERIFY( ss
.str().empty() );
76 VERIFY( ss
.rdbuf()->get_allocator() == a0
);
77 VERIFY( ss
.str().get_allocator() == a0
);
80 // basic_ostringstream(const basic_string<C,T,A>&, openmode = in)
83 VERIFY( ss
.str() == cstr
);
84 VERIFY( ss
.rdbuf()->get_allocator() == a1
);
85 VERIFY( ss
.str().get_allocator() == a1
);
88 // basic_ostringstream(const basic_string<C,T,A>&, openmode = in)
90 ostringstream
ss(s1
, mode
);
91 VERIFY( ss
.str() == cstr
);
92 VERIFY( ss
.rdbuf()->get_allocator() == a1
);
93 VERIFY( ss
.str().get_allocator() == a1
);
96 // basic_ostringstream(openmode, const A&)
98 ostringstream
ss(mode
, a1
);
99 VERIFY( ss
.str().empty() );
100 VERIFY( ss
.rdbuf()->get_allocator() == a1
);
101 VERIFY( ss
.str().get_allocator() == a1
);
104 // basic_ostringstream(basic_string<C,T,A>&&, openmode = in)
106 ostringstream
ss(string
{s1
});
107 VERIFY( ss
.str() == s1
);
108 VERIFY( ss
.rdbuf()->get_allocator() == a1
);
109 VERIFY( ss
.str().get_allocator() == a1
);
112 // basic_ostringstream(basic_string<C,T,A>&&, openmode = in)
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
);
120 // basic_ostringstream(const basic_string<C,T,SA>&, const A&)
123 ostringstream
ss(s1
, a2
);
124 VERIFY( ss
.str() == s1
);
125 VERIFY( ss
.rdbuf()->get_allocator() == a2
);
126 VERIFY( ss
.str().get_allocator() == a2
);
129 // basic_ostringstream(const basic_string<C,T,SA>&, const A&)
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
);
139 // basic_ostringstream(const basic_string<C,T,SA>&, openmode, const A&)
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
);
148 // basic_ostringstream(const basic_string<C,T,SA>&, openmode, const A&)
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
);
158 // basic_ostringstream(const basic_string<C,T,SA>&, openmode = in)
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
);
169 // A minimal allocator with no default constructor
171 struct NoDefaultCons
: __gnu_test::SimpleAllocator
<T
>
173 using __gnu_test::SimpleAllocator
<T
>::SimpleAllocator
;
175 NoDefaultCons() = delete;
177 NoDefaultCons(int) { }
183 using sstream
= std::basic_ostringstream
<char, std::char_traits
<char>,
184 NoDefaultCons
<char>>;
186 NoDefaultCons
<char> a(1);
187 const std::string
str(cstr
);
190 VERIFY( ss1
.str() == cstr
);
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" );
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" );