| 1 |
// 2005-07-22 Paolo Carlini <[email protected]> |
| 2 |
|
| 3 |
// Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 27.6.1.2.3 basic_istream::operator>> |
| 21 |
|
| 22 |
// { dg-do run { target { ! c++20 } } } |
| 23 |
// { dg-require-fileio "" } |
| 24 |
|
| 25 |
#include <istream> |
| 26 |
#include <string> |
| 27 |
#include <fstream> |
| 28 |
#include <cstdlib> |
| 29 |
#include <cstring> |
| 30 |
#include <testsuite_hooks.h> |
| 31 |
|
| 32 |
using namespace std; |
| 33 |
|
| 34 |
string prepare(string::size_type len, unsigned nchunks) |
| 35 |
{ |
| 36 |
string ret; |
| 37 |
for (unsigned i = 0; i < nchunks; ++i) |
| 38 |
{ |
| 39 |
for (string::size_type j = 0; j < len; ++j) |
| 40 |
ret.push_back('a' + rand() % 26); |
| 41 |
len *= 2; |
| 42 |
ret.push_back(' '); |
| 43 |
} |
| 44 |
return ret; |
| 45 |
} |
| 46 |
|
| 47 |
void check(istream& stream, const string& str, unsigned nchunks) |
| 48 |
{ |
| 49 |
char* chunk = new char[str.size()]; |
| 50 |
memset(chunk, 'X', str.size()); |
| 51 |
|
| 52 |
string::size_type index = 0, index_new = 0; |
| 53 |
unsigned n = 0; |
| 54 |
|
| 55 |
while (stream >> chunk) |
| 56 |
{ |
| 57 |
index_new = str.find(' ', index); |
| 58 |
VERIFY( !str.compare(index, index_new - index, chunk) ); |
| 59 |
index = index_new + 1; |
| 60 |
++n; |
| 61 |
memset(chunk, 'X', str.size()); |
| 62 |
} |
| 63 |
VERIFY( stream.eof() ); |
| 64 |
VERIFY( n == nchunks ); |
| 65 |
|
| 66 |
delete[] chunk; |
| 67 |
} |
| 68 |
|
| 69 |
// istream& operator>>(istream&, charT*) |
| 70 |
void test01() |
| 71 |
{ |
| 72 |
const char filename[] = "inserters_extractors-4.txt"; |
| 73 |
|
| 74 |
const unsigned nchunks = 10; |
| 75 |
const string data = prepare(666, nchunks); |
| 76 |
|
| 77 |
ofstream ofstrm; |
| 78 |
ofstrm.open(filename); |
| 79 |
ofstrm.write(data.data(), data.size()); |
| 80 |
ofstrm.close(); |
| 81 |
|
| 82 |
ifstream ifstrm; |
| 83 |
ifstrm.open(filename); |
| 84 |
check(ifstrm, data, nchunks); |
| 85 |
ifstrm.close(); |
| 86 |
} |
| 87 |
|
| 88 |
int main() |
| 89 |
{ |
| 90 |
test01(); |
| 91 |
return 0; |
| 92 |
} |