1 // Copyright (C) 2019 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
25 using __gnu_test::test_range
;
26 using __gnu_test::test_sized_range
;
27 using __gnu_test::random_access_iterator_wrapper
;
28 using __gnu_test::bidirectional_iterator_wrapper
;
29 using __gnu_test::forward_iterator_wrapper
;
30 using __gnu_test::input_iterator_wrapper
;
31 using __gnu_test::output_iterator_wrapper
;
37 VERIFY( std::ranges::distance(a
) == 10 );
39 test_range
<int, random_access_iterator_wrapper
> c(a
);
40 VERIFY( std::ranges::distance(c
) == 10 );
42 auto b
= c
.begin(), e
= c
.end();
43 VERIFY( std::ranges::distance(b
, e
) == 10 );
44 VERIFY( std::ranges::distance(e
, b
) == -10 );
46 const auto cb
= b
, ce
= e
;
47 VERIFY( std::ranges::distance(cb
, ce
) == 10 );
48 VERIFY( std::ranges::distance(ce
, cb
) == -10 );
50 test_sized_range
<int, random_access_iterator_wrapper
> c2(a
);
51 VERIFY( std::ranges::distance(c2
) == 10 );
58 VERIFY( std::ranges::distance(a
) == 2 );
60 test_range
<int, bidirectional_iterator_wrapper
> c(a
);
61 VERIFY( std::ranges::distance(c
) == 2 );
63 auto b
= c
.begin(), e
= c
.end();
64 VERIFY( std::ranges::distance(b
, e
) == 2 );
66 const auto cb
= b
, ce
= e
;
67 VERIFY( std::ranges::distance(cb
, ce
) == 2 );
69 test_sized_range
<int, bidirectional_iterator_wrapper
> c2(a
);
70 VERIFY( std::ranges::distance(c2
) == 2 );
77 test_range
<int, forward_iterator_wrapper
> c(a
);
78 VERIFY( std::ranges::distance(c
) == 3 );
80 auto b
= c
.begin(), e
= c
.end();
81 VERIFY( std::ranges::distance(b
, e
) == 3 );
83 const auto cb
= b
, ce
= e
;
84 VERIFY( std::ranges::distance(cb
, ce
) == 3 );
86 test_sized_range
<int, forward_iterator_wrapper
> c2(a
);
87 VERIFY( std::ranges::distance(c2
) == 3 );
94 test_range
<int, input_iterator_wrapper
> c(a
);
95 static_assert( std::ranges::range
<decltype(c
)> );
97 VERIFY( std::ranges::distance(c
) == 4 );
98 // first call to distance has traversed the range:
99 VERIFY( std::ranges::distance(c
) == 0 );
101 c
= test_range
<int, input_iterator_wrapper
>(a
);
102 auto b
= c
.begin(), e
= c
.end();
103 VERIFY( std::ranges::distance(b
, e
) == 4 );
105 test_range
<int, input_iterator_wrapper
> c2(a
);
106 const auto cb
= c2
.begin(), ce
= c2
.end();
107 VERIFY( std::ranges::distance(cb
, ce
) == 4 );
109 test_sized_range
<int, input_iterator_wrapper
> c3(a
);
110 VERIFY( std::ranges::distance(c3
) == 4 );
111 // first call to distance just called size() without affecting the range:
112 VERIFY( std::ranges::distance(c3
) == 4 );
119 test_range
<int, output_iterator_wrapper
> c(a
);
120 VERIFY( std::ranges::distance(c
) == 5 );
122 test_range
<int, output_iterator_wrapper
> c2(a
);
125 VERIFY( std::ranges::distance(b
, e
) == 5 );
127 test_range
<int, output_iterator_wrapper
> c3(a
);
128 const auto cb
= c3
.begin();
129 const auto ce
= c3
.end();
130 VERIFY( std::ranges::distance(cb
, ce
) == 5 );
132 test_sized_range
<int, output_iterator_wrapper
> c4(a
);
133 VERIFY( std::ranges::distance(c4
) == 5 );
134 // first call to distance just called size() without affecting the range:
135 VERIFY( std::ranges::distance(c4
) == 5 );