]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/char/4.cc
11.cc: Include <cstring>.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_istream / extractors_character / char / 4.cc
CommitLineData
ceed88b1
PC
1// 2005-07-22 Paolo Carlini <pcarlini@suse.de>
2
debac9f4 3// Copyright (C) 2005, 2006, 2007 Free Software Foundation
ceed88b1
PC
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 2, 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 COPYING. If not, write to the Free
83f51799 18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
ceed88b1
PC
19// USA.
20
21// 27.6.1.2.3 basic_istream::operator>>
22
0c20e4ec
NS
23// { dg-require-fileio "" }
24
ceed88b1
PC
25#include <istream>
26#include <string>
27#include <fstream>
debac9f4 28#include <cstdlib>
1b716e90 29#include <cstring>
ceed88b1
PC
30#include <testsuite_hooks.h>
31
32using namespace std;
33
34string 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
47void check(istream& stream, const string& str, unsigned nchunks)
48{
49 bool test __attribute__((unused)) = true;
50
51 char* chunk = new char[str.size()];
52 memset(chunk, 'X', str.size());
53
54 string::size_type index = 0, index_new = 0;
55 unsigned n = 0;
56
57 while (stream >> chunk)
58 {
59 index_new = str.find(' ', index);
60 VERIFY( !str.compare(index, index_new - index, chunk) );
61 index = index_new + 1;
62 ++n;
63 memset(chunk, 'X', str.size());
64 }
65 VERIFY( stream.eof() );
66 VERIFY( n == nchunks );
67
68 delete[] chunk;
69}
70
71// istream& operator>>(istream&, charT*)
72void test01()
73{
74 const char filename[] = "inserters_extractors-4.txt";
75
76 const unsigned nchunks = 10;
77 const string data = prepare(666, nchunks);
78
79 ofstream ofstrm;
80 ofstrm.open(filename);
81 ofstrm.write(data.data(), data.size());
82 ofstrm.close();
83
84 ifstream ifstrm;
85 ifstrm.open(filename);
86 check(ifstrm, data, nchunks);
87 ifstrm.close();
88}
89
90int main()
91{
92 test01();
93 return 0;
94}