| 1 | // Copyright (C) 2019-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 <vector> |
| 23 | #include <testsuite_hooks.h> |
| 24 | |
| 25 | void |
| 26 | test01() |
| 27 | { |
| 28 | int vals[5] = { }; |
| 29 | int* out = vals; |
| 30 | for (int i : std::ranges::iota_view{1, 4}) |
| 31 | *out++ = i; |
| 32 | VERIFY(out == vals + 3); |
| 33 | VERIFY(vals[0] == 1); |
| 34 | VERIFY(vals[1] == 2); |
| 35 | VERIFY(vals[2] == 3); |
| 36 | VERIFY(vals[3] == 0); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | test02() |
| 41 | { |
| 42 | auto v = std::ranges::views::iota(4); |
| 43 | auto it = v.begin(); |
| 44 | VERIFY( *it == 4 ); |
| 45 | ++it; |
| 46 | VERIFY( *it == 5 ); |
| 47 | it++; |
| 48 | VERIFY( *it == 6 ); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | test03() |
| 53 | { |
| 54 | auto v = std::ranges::views::iota(10, 15); |
| 55 | auto it = v.begin(); |
| 56 | VERIFY( *it == 10 ); |
| 57 | it += 2; |
| 58 | VERIFY( *it == 12 ); |
| 59 | it += 2; |
| 60 | VERIFY( *it == 14 ); |
| 61 | ++it; |
| 62 | VERIFY( it == v.end() ); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | test04() |
| 67 | { |
| 68 | int x[] = {1,2,3}; |
| 69 | auto v = std::ranges::views::iota(std::counted_iterator(x, 3), |
| 70 | std::default_sentinel); |
| 71 | auto it = v.begin(); |
| 72 | VERIFY( (*it).base() == x ); |
| 73 | ++it; |
| 74 | VERIFY( (*it).base() == x+1 ); |
| 75 | ++it; |
| 76 | VERIFY( (*it).base() == x+2 ); |
| 77 | ++it; |
| 78 | VERIFY( it == v.end() ); |
| 79 | } |
| 80 | |
| 81 | // Verify we optimize away the 'bound' data member of an unbounded iota_view. |
| 82 | static_assert(sizeof(std::ranges::iota_view<char>) == 1); |
| 83 | |
| 84 | void |
| 85 | test05() |
| 86 | { |
| 87 | // PR libstdc++/100690 |
| 88 | int x[] = {42, 42, 42}; |
| 89 | auto r = std::views::iota(std::ranges::begin(x), std::ranges::cbegin(x) + 3); |
| 90 | VERIFY( r.end() - r.begin() == 3 ); |
| 91 | VERIFY( r.begin() - r.end() == -3 ); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | test06() |
| 96 | { |
| 97 | // Verify LWG 3523 changes. |
| 98 | auto v1 = std::views::iota(0, 5); |
| 99 | auto w1 = decltype(v1)(v1.begin(), v1.end()); |
| 100 | VERIFY( std::ranges::equal(v1, w1) ); |
| 101 | |
| 102 | auto v2 = std::views::iota(0); |
| 103 | auto w2 = decltype(v2)(v2.begin(), v2.end()); |
| 104 | static_assert(std::same_as<decltype(w2.end()), std::unreachable_sentinel_t>); |
| 105 | VERIFY( *w2.begin() == 0 ); |
| 106 | |
| 107 | auto v3 = std::views::iota(0, 5l); |
| 108 | auto w3 = decltype(v3)(v3.begin(), v3.end()); |
| 109 | static_assert(!std::ranges::common_range<decltype(w3)>); |
| 110 | VERIFY( std::ranges::equal(v3, w3) ); |
| 111 | } |
| 112 | |
| 113 | template<auto iota = std::views::iota> |
| 114 | void |
| 115 | test07() |
| 116 | { |
| 117 | // Verify SFINAE behavior. |
| 118 | static_assert(!requires { iota(nullptr); }); |
| 119 | static_assert(!requires { iota(nullptr, nullptr); }); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | test08() |
| 124 | { |
| 125 | // LWC 4001 - iota_view should provide empty |
| 126 | std::vector<int> v; |
| 127 | auto it = std::back_inserter(v); |
| 128 | auto r = std::views::iota(it); |
| 129 | VERIFY( !r.empty() ); |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | main() |
| 134 | { |
| 135 | test01(); |
| 136 | test02(); |
| 137 | test03(); |
| 138 | test04(); |
| 139 | test05(); |
| 140 | test06(); |
| 141 | test07(); |
| 142 | test08(); |
| 143 | } |