1 // Copyright (C) 2019-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++20 } }
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
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
;
36 VERIFY( std::ranges::distance(a
) == 10 );
38 test_range
<int, random_access_iterator_wrapper
> c(a
);
39 VERIFY( std::ranges::distance(c
) == 10 );
43 auto ei
= std::ranges::next(b
, e
);
44 VERIFY( std::ranges::distance(b
, e
) == 10 );
45 VERIFY( std::ranges::distance(ei
, b
) == -10 );
50 VERIFY( std::ranges::distance(cb
, ce
) == 10 );
51 VERIFY( std::ranges::distance(cei
, cb
) == -10 );
53 test_sized_range
<int, random_access_iterator_wrapper
> c2(a
);
54 VERIFY( std::ranges::distance(c2
) == 10 );
61 VERIFY( std::ranges::distance(a
) == 2 );
63 test_range
<int, bidirectional_iterator_wrapper
> c(a
);
64 VERIFY( std::ranges::distance(c
) == 2 );
68 VERIFY( std::ranges::distance(b
, e
) == 2 );
72 VERIFY( std::ranges::distance(cb
, ce
) == 2 );
74 test_sized_range
<int, bidirectional_iterator_wrapper
> c2(a
);
75 VERIFY( std::ranges::distance(c2
) == 2 );
82 test_range
<int, forward_iterator_wrapper
> c(a
);
83 VERIFY( std::ranges::distance(c
) == 3 );
87 VERIFY( std::ranges::distance(b
, e
) == 3 );
91 VERIFY( std::ranges::distance(cb
, ce
) == 3 );
93 test_sized_range
<int, forward_iterator_wrapper
> c2(a
);
94 VERIFY( std::ranges::distance(c2
) == 3 );
101 test_range
<int, input_iterator_wrapper
> c(a
);
102 static_assert( std::ranges::range
<decltype(c
)> );
104 VERIFY( std::ranges::distance(c
) == 4 );
105 // first call to distance has traversed the range:
106 VERIFY( std::ranges::distance(c
) == 0 );
108 c
= test_range
<int, input_iterator_wrapper
>(a
);
111 VERIFY( std::ranges::distance(b
, e
) == 4 );
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 );
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 );
128 test_range
<int, output_iterator_wrapper
> c(a
);
129 VERIFY( std::ranges::distance(c
) == 5 );
131 test_range
<int, output_iterator_wrapper
> c2(a
);
134 VERIFY( std::ranges::distance(b
, e
) == 5 );
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 );
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 );