]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/basic_istream/getline/wchar_t/5.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_istream / getline / wchar_t / 5.cc
CommitLineData
818ab71a 1// Copyright (C) 2004-2016 Free Software Foundation, Inc.
75a25e3f
PC
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
748086b7 6// Free Software Foundation; either version 3, or (at your option)
75a25e3f
PC
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
748086b7
JJ
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
75a25e3f
PC
17
18// 27.6.1.3 unformatted input functions
19
5a0727d9
SE
20// { dg-options "-DMAX_LENGTH=7" { target simulator } }
21
22#ifndef MAX_LENGTH
23#define MAX_LENGTH 777
24#endif
25
75a25e3f
PC
26#include <istream>
27#include <string>
28#include <fstream>
debac9f4 29#include <cstdlib>
75a25e3f
PC
30#include <testsuite_hooks.h>
31
32using namespace std;
33
34wstring
35prepare(wstring::size_type len, unsigned nchunks, wchar_t delim)
36{
37 wstring ret;
38 for (unsigned i = 0; i < nchunks; ++i)
39 {
40 for (wstring::size_type j = 0; j < len; ++j)
41 ret.push_back(L'a' + rand() % 26);
42 len *= 2;
43 ret.push_back(delim);
44 }
45 return ret;
46}
47
48void
49check(wistream& stream, const wstring& str, unsigned nchunks, wchar_t delim)
50{
51 bool test __attribute__((unused)) = true;
52
a47add83 53 static wchar_t buf[1000000];
75a25e3f
PC
54 wstring::size_type index = 0, index_new = 0;
55 unsigned n = 0;
56
57 while (stream.getline(buf, sizeof(buf) / sizeof(wchar_t), delim))
58 {
59 index_new = str.find(delim, index);
8b5bc374
CJ
60 VERIFY( static_cast<string::size_type>(stream.gcount()) ==
61 index_new - index + 1 );
75a25e3f
PC
62 VERIFY( !str.compare(index, index_new - index, buf) );
63 index = index_new + 1;
64 ++n;
65 }
66 VERIFY( stream.gcount() == 0 );
67 VERIFY( stream.eof() );
68 VERIFY( n == nchunks );
69}
70
71void test01()
72{
73 const char filename[] = "wistream_getline.txt";
74
75 const wchar_t delim = L'|';
76 const unsigned nchunks = 10;
5a0727d9 77 const wstring data = prepare(MAX_LENGTH, nchunks, delim);
75a25e3f
PC
78
79 wofstream ofstrm;
80 ofstrm.open(filename);
81 ofstrm.write(data.data(), data.size());
82 ofstrm.close();
83
84 wifstream ifstrm;
85 ifstrm.open(filename);
86 check(ifstrm, data, nchunks, delim);
87 ifstrm.close();
88}
89
90int main()
91{
92 test01();
93 return 0;
94}