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