| 1 |
// Copyright (C) 2021-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 |
// PR libstdc++/100479 |
| 21 |
|
| 22 |
#include <ranges> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
#include <testsuite_iterators.h> |
| 25 |
|
| 26 |
using __gnu_test::test_forward_range; |
| 27 |
using __gnu_test::test_random_access_range; |
| 28 |
|
| 29 |
namespace ranges = std::ranges; |
| 30 |
namespace views = std::views; |
| 31 |
|
| 32 |
void |
| 33 |
test01() |
| 34 |
{ |
| 35 |
// Verify we don't propagate cached iterators when copying/moving an adapted |
| 36 |
// forward range that memoizes its begin(). |
| 37 |
static int pred_counter; |
| 38 |
int x[] = {1,2,3,4,5}; |
| 39 |
test_forward_range rx(x); |
| 40 |
auto is_odd = [](auto n) { ++pred_counter; return n%2 != 0; }; |
| 41 |
|
| 42 |
auto v = rx | views::filter(is_odd); |
| 43 |
v.begin(); v.begin(); |
| 44 |
VERIFY( pred_counter == 1 ); // filter_view caches its begin() iterator |
| 45 |
|
| 46 |
auto w = v; |
| 47 |
v.begin(); v.begin(); |
| 48 |
VERIFY( pred_counter == 1 ); // cached iterator not invalidated on copy |
| 49 |
w.begin(); w.begin(); |
| 50 |
VERIFY( pred_counter == 2 ); // cached iterator not propagated on copy |
| 51 |
|
| 52 |
auto z = std::move(w); |
| 53 |
w.begin(); w.begin(); |
| 54 |
VERIFY( pred_counter == 3 ); // cached iterator invalidated on move |
| 55 |
z.begin(); z.begin(); |
| 56 |
VERIFY( pred_counter == 4 ); // cached iterator not propagated on move |
| 57 |
} |
| 58 |
|
| 59 |
void |
| 60 |
test02() |
| 61 |
{ |
| 62 |
// Verify we invalidate the cached offset when moving an adapted |
| 63 |
// random access range that memoizes its begin(). |
| 64 |
static int pred_counter; |
| 65 |
int x[] = {1,2,3,4,5}; |
| 66 |
test_random_access_range rx(x); |
| 67 |
auto is_odd = [](auto n) { ++pred_counter; return n%2 != 0; }; |
| 68 |
|
| 69 |
auto v = rx | views::filter(is_odd); |
| 70 |
v.begin(); v.begin(); |
| 71 |
VERIFY( pred_counter == 1 ); // filter_view caches its begin() iterator |
| 72 |
|
| 73 |
auto w = v; |
| 74 |
v.begin(); v.begin(); |
| 75 |
VERIFY( pred_counter == 1 ); // cached offset not invalidated on copy |
| 76 |
w.begin(); w.begin(); |
| 77 |
VERIFY( pred_counter == 1 ); // cached offset propagated on copy |
| 78 |
|
| 79 |
auto z = std::move(w); |
| 80 |
w.begin(); w.begin(); |
| 81 |
VERIFY( pred_counter == 2 ); // cached offset invalidated on move |
| 82 |
z.begin(); z.begin(); |
| 83 |
VERIFY( pred_counter == 2 ); // cached offset propagated on move |
| 84 |
} |
| 85 |
|
| 86 |
constexpr bool |
| 87 |
test03() |
| 88 |
{ |
| 89 |
// Propagating cached iterators during copy/move would cause these asserts |
| 90 |
// to fail here. |
| 91 |
auto v = views::single(1) |
| 92 |
| views::lazy_split(1) |
| 93 |
| views::drop(0) |
| 94 |
| views::drop_while([](auto) { return false; }) |
| 95 |
| views::filter([](auto) { return true; }); |
| 96 |
static_assert(ranges::forward_range<decltype(v)>); |
| 97 |
VERIFY( ranges::distance(v) == 2 ); |
| 98 |
auto w = v; |
| 99 |
VERIFY( ranges::distance(v) == 2 ); |
| 100 |
auto z = std::move(w); |
| 101 |
VERIFY( ranges::distance(v) == 2 ); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
int |
| 106 |
main() |
| 107 |
{ |
| 108 |
test01(); |
| 109 |
test02(); |
| 110 |
test03(); |
| 111 |
static_assert(test03()); |
| 112 |
} |