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