| 1 |
// Copyright (C) 2018-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-options "-Wno-deprecated" } |
| 19 |
// { dg-do run { target c++11 } } |
| 20 |
|
| 21 |
#include <strstream> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
|
| 24 |
void |
| 25 |
test01() |
| 26 |
{ |
| 27 |
std::istrstream is("15 16"); |
| 28 |
std::istrstream is2 = std::move(is); |
| 29 |
int a; |
| 30 |
is >> a; |
| 31 |
VERIFY( !is ); |
| 32 |
is2 >> a; |
| 33 |
VERIFY( is2 ); |
| 34 |
VERIFY( a == 15 ); |
| 35 |
std::istrstream is3 = std::move(is2); |
| 36 |
int b; |
| 37 |
is2 >> b; |
| 38 |
VERIFY( !is2 ); |
| 39 |
is3 >> b; |
| 40 |
VERIFY( is3 ); |
| 41 |
VERIFY( b == 16 ); |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
test02() |
| 46 |
{ |
| 47 |
std::istrstream is(""); |
| 48 |
int a; |
| 49 |
is >> a; |
| 50 |
VERIFY( !is ); |
| 51 |
is = std::istrstream("17 18"); |
| 52 |
is >> a; |
| 53 |
VERIFY( is ); |
| 54 |
VERIFY( a == 17 ); |
| 55 |
is = std::istrstream(""); |
| 56 |
int b; |
| 57 |
is >> b; |
| 58 |
VERIFY( !is ); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
test03() |
| 63 |
{ |
| 64 |
std::ostrstream os; |
| 65 |
os << "a few chars"; // fits in initial allocation |
| 66 |
char* s = os.str(); // os now frozen |
| 67 |
std::ostrstream os2 = std::move(os); |
| 68 |
VERIFY( os2.str() == s ); |
| 69 |
VERIFY( os.str() == nullptr ); |
| 70 |
os2.freeze(false); |
| 71 |
|
| 72 |
os2 << "enough additional chars to force a reallocation"; |
| 73 |
VERIFY( os2 ); |
| 74 |
s = os2.str(); // os2 now frozen |
| 75 |
std::ostrstream os3 = std::move(os2); |
| 76 |
VERIFY( os3.str() == s ); |
| 77 |
VERIFY( os2.str() == nullptr ); |
| 78 |
delete[] s; |
| 79 |
} |
| 80 |
|
| 81 |
void |
| 82 |
test04() |
| 83 |
{ |
| 84 |
char buf[16]; |
| 85 |
std::ostrstream os(buf, sizeof(buf)); |
| 86 |
os << "a few chars"; |
| 87 |
char* s = os.str(); // os now frozen |
| 88 |
VERIFY( s == buf ); |
| 89 |
std::ostrstream os2 = std::move(os); |
| 90 |
VERIFY( os2.str() == s ); |
| 91 |
VERIFY( os.str() == nullptr ); |
| 92 |
os2.freeze(false); |
| 93 |
|
| 94 |
os2 << "enough additional chars to force a reallocation"; |
| 95 |
VERIFY( !os2 ); |
| 96 |
s = os2.str(); // os2 now frozen |
| 97 |
VERIFY( s == buf ); |
| 98 |
std::ostrstream os3 = std::move(os2); |
| 99 |
VERIFY( os3.str() == s ); |
| 100 |
VERIFY( os2.str() == nullptr ); |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
test05() |
| 105 |
{ |
| 106 |
char buf[] = "0123456789"; |
| 107 |
std::ostrstream os(buf, 1); |
| 108 |
os << "aa"; |
| 109 |
VERIFY( !os ); |
| 110 |
os = std::ostrstream(buf, 10); |
| 111 |
os << "some chars"; |
| 112 |
VERIFY( os ); |
| 113 |
VERIFY( os.pcount() == 10 ); |
| 114 |
os << "a"; |
| 115 |
VERIFY( !os ); |
| 116 |
os = std::ostrstream(); |
| 117 |
os << "a"; |
| 118 |
VERIFY( os ); |
| 119 |
VERIFY( os.pcount() == 1 ); |
| 120 |
char* s = os.str(); // os now frozen |
| 121 |
os = std::ostrstream(); |
| 122 |
os.freeze(false); // no effect |
| 123 |
delete[] s; |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
test06() |
| 128 |
{ |
| 129 |
char buf[] = "15 16"; |
| 130 |
std::strstream ss(buf, 5, std::ios::in|std::ios::app); |
| 131 |
std::strstream ss2 = std::move(ss); |
| 132 |
int a; |
| 133 |
ss >> a; |
| 134 |
VERIFY( !ss ); |
| 135 |
ss2 >> a; |
| 136 |
VERIFY( ss2 ); |
| 137 |
VERIFY( a == 15 ); |
| 138 |
std::strstream ss3 = std::move(ss2); |
| 139 |
int b; |
| 140 |
ss2 >> b; |
| 141 |
VERIFY( !ss2 ); |
| 142 |
ss3 >> b; |
| 143 |
VERIFY( ss3 ); |
| 144 |
VERIFY( b == 16 ); |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
test07() |
| 149 |
{ |
| 150 |
std::strstream ss; |
| 151 |
int a; |
| 152 |
ss >> a; |
| 153 |
VERIFY( !ss ); |
| 154 |
char buf[] = "17 18"; |
| 155 |
ss = std::strstream(buf, 5, std::ios::in|std::ios::app); |
| 156 |
ss >> a; |
| 157 |
VERIFY( ss ); |
| 158 |
VERIFY( a == 17 ); |
| 159 |
ss = std::strstream(); |
| 160 |
int b; |
| 161 |
ss >> b; |
| 162 |
VERIFY( !ss ); |
| 163 |
} |
| 164 |
|
| 165 |
void |
| 166 |
test08() |
| 167 |
{ |
| 168 |
std::strstream ss; |
| 169 |
ss << "a few chars"; // fits in initial allocation |
| 170 |
char* s = ss.str(); // ss now frozen |
| 171 |
std::strstream ss2 = std::move(ss); |
| 172 |
VERIFY( ss2.str() == s ); |
| 173 |
VERIFY( ss.str() == nullptr ); |
| 174 |
ss2.freeze(false); |
| 175 |
|
| 176 |
ss2 << "enough additional chars to force a reallocation"; |
| 177 |
VERIFY( ss2 ); |
| 178 |
s = ss2.str(); // ss2 now frozen |
| 179 |
std::strstream ss3 = std::move(ss2); |
| 180 |
VERIFY( ss3.str() == s ); |
| 181 |
VERIFY( ss2.str() == nullptr ); |
| 182 |
delete[] s; |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
test09() |
| 187 |
{ |
| 188 |
char buf[16]; |
| 189 |
std::strstream ss(buf, sizeof(buf)); |
| 190 |
ss << "a few chars"; |
| 191 |
char* s = ss.str(); // ss now frozen |
| 192 |
VERIFY( s == buf ); |
| 193 |
std::strstream ss2 = std::move(ss); |
| 194 |
VERIFY( ss2.str() == s ); |
| 195 |
VERIFY( ss.str() == nullptr ); |
| 196 |
ss2.freeze(false); |
| 197 |
|
| 198 |
ss2 << "enough additional chars to force a reallocation"; |
| 199 |
VERIFY( !ss2 ); |
| 200 |
s = ss2.str(); // ss2 now frozen |
| 201 |
VERIFY( s == buf ); |
| 202 |
std::strstream ss3 = std::move(ss2); |
| 203 |
VERIFY( ss3.str() == s ); |
| 204 |
VERIFY( ss2.str() == nullptr ); |
| 205 |
} |
| 206 |
|
| 207 |
void |
| 208 |
test10() |
| 209 |
{ |
| 210 |
char buf[] = "0123456789"; |
| 211 |
std::strstream ss(buf, 1); |
| 212 |
ss << "aa"; |
| 213 |
VERIFY( !ss ); |
| 214 |
ss = std::strstream(buf, 10); |
| 215 |
ss << "some chars"; |
| 216 |
VERIFY( ss ); |
| 217 |
VERIFY( ss.pcount() == 10 ); |
| 218 |
ss << "a"; |
| 219 |
VERIFY( !ss ); |
| 220 |
ss = std::strstream(); |
| 221 |
ss << "a"; |
| 222 |
VERIFY( ss ); |
| 223 |
VERIFY( ss.pcount() == 1 ); |
| 224 |
char* s = ss.str(); // ss now frozen |
| 225 |
ss = std::strstream(); |
| 226 |
ss.freeze(false); // no effect |
| 227 |
delete[] s; |
| 228 |
} |
| 229 |
|
| 230 |
int |
| 231 |
main() |
| 232 |
{ |
| 233 |
test01(); |
| 234 |
test02(); |
| 235 |
test03(); |
| 236 |
test04(); |
| 237 |
test05(); |
| 238 |
test06(); |
| 239 |
test07(); |
| 240 |
test08(); |
| 241 |
test09(); |
| 242 |
test10(); |
| 243 |
} |