]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/24_iterators/istream_iterator/1.cc
Fix non-standard behaviour of std::istream_iterator
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 24_iterators / istream_iterator / 1.cc
1 // Copyright (C) 2019 Free Software Foundation, Inc.
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
6 // Free Software Foundation; either version 3, or (at your option)
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
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // C++98 24.5.1 Template class istream_iterator
19
20 #include <iterator>
21 #include <sstream>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
25 void test01()
26 {
27 using namespace std;
28
29 const int N = 6;
30 int arr[N] = { };
31 using __gnu_test::test_container;
32 using __gnu_test::output_iterator_wrapper;
33 test_container<int, output_iterator_wrapper> con(arr, arr+N);
34 output_iterator_wrapper<int> out = con.begin();
35
36 istringstream ss("1 2 3 4 5");
37 std::istream_iterator<int> iter(ss), end;
38 while (iter != end)
39 *out++ = *iter++;
40 VERIFY( iter == end );
41 for (int i = 0; i < N; ++i)
42 VERIFY( arr[i] == (i + 1) % N );
43
44 std::istream_iterator<int> iter2(ss);
45 VERIFY( iter2 == end );
46
47 ss.clear();
48 ss.str("-1 -2 -3");
49 VERIFY( iter == end );
50
51 #ifndef _GLIBCXX_DEBUG
52 // This is undefined, so aborts under debug mode.
53 // Without debug mode, it should not extract anything from the stream,
54 // and the iterator should remain at end-of-stream.
55 ++iter;
56 VERIFY( iter == end );
57 #endif
58
59 std::istream_iterator<int> iter3(ss);
60 VERIFY( iter3 != end );
61 VERIFY( iter3 != iter );
62 VERIFY( *iter3 == -1 );
63 ++iter3;
64 VERIFY( *iter3 == -2 );
65
66 iter2 = iter3;
67 VERIFY( *iter2 == -2 );
68 ++iter2;
69 VERIFY( *iter2 == -3 );
70 ++iter2;
71 VERIFY( iter2 == end );
72 }
73
74 int main()
75 {
76 test01();
77 }