]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_istream/extractors_character/wchar_t/4.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_istream / extractors_character / wchar_t / 4.cc
1 // 2005-07-22 Paolo Carlini <pcarlini@suse.de>
2
3 // Copyright (C) 2005-2024 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 // { dg-do run { target { ! c++20 } } }
24 // { dg-require-fileio "" }
25
26 #ifndef MAX_SIZE
27 #define MAX_SIZE 666
28 #endif
29
30 #include <istream>
31 #include <string>
32 #include <fstream>
33 #include <cstdlib>
34 #include <testsuite_hooks.h>
35
36 using namespace std;
37
38 wstring prepare(wstring::size_type len, unsigned nchunks)
39 {
40 wstring ret;
41 for (unsigned i = 0; i < nchunks; ++i)
42 {
43 for (wstring::size_type j = 0; j < len; ++j)
44 ret.push_back(L'a' + rand() % 26);
45 len *= 2;
46 ret.push_back(L' ');
47 }
48 return ret;
49 }
50
51 void check(wistream& stream, const wstring& str, unsigned nchunks)
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*)
74 void 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
92 int main()
93 {
94 test01();
95 return 0;
96 }