]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/std/ranges/istream_view.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / istream_view.cc
1 // Copyright (C) 2020-2022 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 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <algorithm>
22 #include <ranges>
23 #include <sstream>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
26 #include <testsuite_rvalref.h>
27
28 namespace ranges = std::ranges;
29 namespace views = std::views;
30
31 struct X : __gnu_test::rvalstruct
32 {
33 char c;
34
35 friend std::istream&
36 operator>>(std::istream& is, X& m)
37 {
38 is >> m.c;
39 return is;
40 }
41 };
42
43
44 void
45 test01()
46 {
47 std::string s = "0123456789";
48 auto ss = std::istringstream{s};
49 auto v = ranges::istream_view<X>(ss);
50 VERIFY( ranges::equal(v, s, {}, &X::c) );
51 }
52
53 void
54 test02()
55 {
56 auto ints = std::istringstream{"0 1 2 3 4"};
57 int x[5];
58 ranges::copy(ranges::istream_view<int>(ints), x);
59 VERIFY( ranges::equal(x, (int[]){0,1,2,3,4}) );
60 }
61
62 void
63 test03()
64 {
65 auto input = std::istringstream{"0 1 2 3 4 5 6 7 8 9"};
66 auto small = [](const auto x) noexcept { return x < 5; };
67 auto v = ranges::istream_view<int>(input) | views::take_while(small);
68 VERIFY( ranges::equal(v, (int[]){0,1,2,3,4}) );
69 }
70
71 template<typename T>
72 concept has_iterator_category = requires { typename T::iterator_category; };
73
74 void
75 test04()
76 {
77 std::istringstream s("12345");
78 auto v = ranges::istream_view<char>(s);
79 // LWG 3397
80 using It = ranges::iterator_t<decltype(v)>;
81 static_assert(!has_iterator_category<It>);
82 static_assert(std::input_iterator<It>);
83 static_assert(!std::forward_iterator<It>);
84 }
85
86 void
87 test05()
88 {
89 // PR libstdc++/101231
90 auto words = std::istringstream{"42"};
91 auto is = ranges::istream_view<int>(words);
92 auto r = is | views::filter([](auto) { return true; });
93 for (auto x : r)
94 ;
95 }
96
97 void
98 test06()
99 {
100 // Default template argument
101 using V = std::ranges::basic_istream_view<int, char>;
102 using W = std::ranges::basic_istream_view<int, char, std::char_traits<char>>;
103 static_assert( std::is_same_v<V, W> );
104 }
105
106 void
107 test07()
108 {
109 // P2432R1, views::istream
110 auto nums = std::istringstream("0 1 2 3 4");
111 ranges::istream_view<int> v(nums);
112 int sum = 0;
113 for (int val : views::istream<int>(nums))
114 sum += val;
115 VERIFY( sum == 10 );
116 }
117
118 int
119 main()
120 {
121 test01();
122 test02();
123 test03();
124 test04();
125 test05();
126 test06();
127 test07();
128 }