| 1 | // Copyright (C) 2020-2025 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-do run { target c++20 } } |
| 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 | |
| 27 | namespace ranges = std::ranges; |
| 28 | namespace views = std::views; |
| 29 | |
| 30 | struct 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 | |
| 43 | void |
| 44 | test01() |
| 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 | |
| 52 | void |
| 53 | test02() |
| 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 | |
| 61 | void |
| 62 | test03() |
| 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 | |
| 70 | template<typename T> |
| 71 | concept has_iterator_category = requires { typename T::iterator_category; }; |
| 72 | |
| 73 | void |
| 74 | test04() |
| 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 | |
| 85 | void |
| 86 | test05() |
| 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 | |
| 96 | void |
| 97 | test06() |
| 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 | |
| 105 | void |
| 106 | test07() |
| 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 | |
| 117 | template<class T, class U> |
| 118 | concept can_istream_view = requires (U u) { views::istream<T>(u); }; |
| 119 | |
| 120 | void |
| 121 | test08() |
| 122 | { |
| 123 | // Verify SFINAE behavior. |
| 124 | struct S { }; |
| 125 | static_assert(!can_istream_view<S, std::istringstream>); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | main() |
| 130 | { |
| 131 | test01(); |
| 132 | test02(); |
| 133 | test03(); |
| 134 | test04(); |
| 135 | test05(); |
| 136 | test06(); |
| 137 | test07(); |
| 138 | test08(); |
| 139 | } |