| 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 <iterator> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
#include <testsuite_iterators.h> |
| 23 |
|
| 24 |
using __gnu_test::test_range; |
| 25 |
using __gnu_test::test_sized_range; |
| 26 |
using __gnu_test::random_access_iterator_wrapper; |
| 27 |
using __gnu_test::bidirectional_iterator_wrapper; |
| 28 |
using __gnu_test::forward_iterator_wrapper; |
| 29 |
using __gnu_test::input_iterator_wrapper; |
| 30 |
using __gnu_test::output_iterator_wrapper; |
| 31 |
|
| 32 |
void |
| 33 |
test01() |
| 34 |
{ |
| 35 |
int a[10] = { }; |
| 36 |
VERIFY( std::ranges::distance(a) == 10 ); |
| 37 |
|
| 38 |
test_range<int, random_access_iterator_wrapper> c(a); |
| 39 |
VERIFY( std::ranges::distance(c) == 10 ); |
| 40 |
|
| 41 |
auto b = c.begin(); |
| 42 |
auto e = c.end(); |
| 43 |
auto ei = std::ranges::next(b, e); |
| 44 |
VERIFY( std::ranges::distance(b, e) == 10 ); |
| 45 |
VERIFY( std::ranges::distance(ei, b) == -10 ); |
| 46 |
|
| 47 |
const auto cb = b; |
| 48 |
const auto ce = e; |
| 49 |
const auto cei = ei; |
| 50 |
VERIFY( std::ranges::distance(cb, ce) == 10 ); |
| 51 |
VERIFY( std::ranges::distance(cei, cb) == -10 ); |
| 52 |
|
| 53 |
test_sized_range<int, random_access_iterator_wrapper> c2(a); |
| 54 |
VERIFY( std::ranges::distance(c2) == 10 ); |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
test02() |
| 59 |
{ |
| 60 |
int a[2] = { }; |
| 61 |
VERIFY( std::ranges::distance(a) == 2 ); |
| 62 |
|
| 63 |
test_range<int, bidirectional_iterator_wrapper> c(a); |
| 64 |
VERIFY( std::ranges::distance(c) == 2 ); |
| 65 |
|
| 66 |
auto b = c.begin(); |
| 67 |
auto e = c.end(); |
| 68 |
VERIFY( std::ranges::distance(b, e) == 2 ); |
| 69 |
|
| 70 |
const auto cb = b; |
| 71 |
const auto ce = e; |
| 72 |
VERIFY( std::ranges::distance(cb, ce) == 2 ); |
| 73 |
|
| 74 |
test_sized_range<int, bidirectional_iterator_wrapper> c2(a); |
| 75 |
VERIFY( std::ranges::distance(c2) == 2 ); |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
test03() |
| 80 |
{ |
| 81 |
int a[3] = { }; |
| 82 |
test_range<int, forward_iterator_wrapper> c(a); |
| 83 |
VERIFY( std::ranges::distance(c) == 3 ); |
| 84 |
|
| 85 |
auto b = c.begin(); |
| 86 |
auto e = c.end(); |
| 87 |
VERIFY( std::ranges::distance(b, e) == 3 ); |
| 88 |
|
| 89 |
const auto cb = b; |
| 90 |
const auto ce = e; |
| 91 |
VERIFY( std::ranges::distance(cb, ce) == 3 ); |
| 92 |
|
| 93 |
test_sized_range<int, forward_iterator_wrapper> c2(a); |
| 94 |
VERIFY( std::ranges::distance(c2) == 3 ); |
| 95 |
} |
| 96 |
|
| 97 |
void |
| 98 |
test04() |
| 99 |
{ |
| 100 |
int a[4] = { }; |
| 101 |
test_range<int, input_iterator_wrapper> c(a); |
| 102 |
static_assert( std::ranges::range<decltype(c)> ); |
| 103 |
|
| 104 |
VERIFY( std::ranges::distance(c) == 4 ); |
| 105 |
// first call to distance has traversed the range: |
| 106 |
VERIFY( std::ranges::distance(c) == 0 ); |
| 107 |
|
| 108 |
c = test_range<int, input_iterator_wrapper>(a); |
| 109 |
auto b = c.begin(); |
| 110 |
auto e = c.end(); |
| 111 |
VERIFY( std::ranges::distance(b, e) == 4 ); |
| 112 |
|
| 113 |
test_range<int, input_iterator_wrapper> c2(a); |
| 114 |
const auto cb = c2.begin(); |
| 115 |
const auto ce = c2.end(); |
| 116 |
VERIFY( std::ranges::distance(cb, ce) == 4 ); |
| 117 |
|
| 118 |
test_sized_range<int, input_iterator_wrapper> c3(a); |
| 119 |
VERIFY( std::ranges::distance(c3) == 4 ); |
| 120 |
// first call to distance just called size() without affecting the range: |
| 121 |
VERIFY( std::ranges::distance(c3) == 4 ); |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
test05() |
| 126 |
{ |
| 127 |
int a[5] = { }; |
| 128 |
test_range<int, output_iterator_wrapper> c(a); |
| 129 |
VERIFY( std::ranges::distance(c) == 5 ); |
| 130 |
|
| 131 |
test_range<int, output_iterator_wrapper> c2(a); |
| 132 |
auto b = c2.begin(); |
| 133 |
auto e = c2.end(); |
| 134 |
VERIFY( std::ranges::distance(b, e) == 5 ); |
| 135 |
|
| 136 |
test_range<int, output_iterator_wrapper> c3(a); |
| 137 |
const auto cb = c3.begin(); |
| 138 |
const auto ce = c3.end(); |
| 139 |
VERIFY( std::ranges::distance(cb, ce) == 5 ); |
| 140 |
|
| 141 |
test_sized_range<int, output_iterator_wrapper> c4(a); |
| 142 |
VERIFY( std::ranges::distance(c4) == 5 ); |
| 143 |
// first call to distance just called size() without affecting the range: |
| 144 |
VERIFY( std::ranges::distance(c4) == 5 ); |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
test06() |
| 149 |
{ |
| 150 |
// LWG 3664 - LWG 3392 broke std::ranges::distance(a, a+3) |
| 151 |
int a[] = {1, 2, 3}; |
| 152 |
VERIFY( std::ranges::distance(a, a+3) == 3 ); |
| 153 |
VERIFY( std::ranges::distance(a, a) == 0 ); |
| 154 |
VERIFY( std::ranges::distance(a+3, a) == -3 ); |
| 155 |
} |
| 156 |
|
| 157 |
int |
| 158 |
main() |
| 159 |
{ |
| 160 |
test01(); |
| 161 |
test02(); |
| 162 |
test03(); |
| 163 |
test04(); |
| 164 |
test05(); |
| 165 |
test06(); |
| 166 |
} |