1// 2005-07-22  Paolo Carlini  <pcarlini@suse.de>
2
3// Copyright (C) 2005-2015 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-require-fileio "" }
23
24#include <istream>
25#include <string>
26#include <fstream>
27#include <cstdlib>
28#include <cstring>
29#include <testsuite_hooks.h>
30
31using namespace std;
32
33string prepare(string::size_type len, unsigned nchunks)
34{
35  string ret;
36  for (unsigned i = 0; i < nchunks; ++i)
37    {
38      for (string::size_type j = 0; j < len; ++j)
39	ret.push_back('a' + rand() % 26);
40      len *= 2;
41      ret.push_back(' ');
42    }
43  return ret;
44}
45
46void check(istream& stream, const string& str, unsigned nchunks)
47{
48  bool test __attribute__((unused)) = true;
49
50  char* chunk = new char[str.size()];
51  memset(chunk, 'X', str.size());
52
53  string::size_type index = 0, index_new = 0;
54  unsigned n = 0;
55
56  while (stream >> chunk)
57    {
58      index_new = str.find(' ', index);
59      VERIFY( !str.compare(index, index_new - index, chunk) );
60      index = index_new + 1;
61      ++n;
62      memset(chunk, 'X', str.size());
63    }
64  VERIFY( stream.eof() );
65  VERIFY( n == nchunks );
66
67  delete[] chunk;
68}
69
70// istream& operator>>(istream&, charT*)
71void test01()
72{
73  const char filename[] = "inserters_extractors-4.txt";
74
75  const unsigned nchunks = 10;
76  const string data = prepare(666, nchunks);
77
78  ofstream ofstrm;
79  ofstrm.open(filename);
80  ofstrm.write(data.data(), data.size());
81  ofstrm.close();
82
83  ifstream ifstrm;
84  ifstrm.open(filename);
85  check(ifstrm, data, nchunks);
86  ifstrm.close();
87}
88
89int main()
90{
91  test01();
92  return 0;
93}
94