]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/24_iterators/range_operations/distance.cc
Add iterator concepts and range access customization points for C++20
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 24_iterators / range_operations / distance.cc
1 // Copyright (C) 2019 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-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <iterator>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24
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;
32
33 void
34 test01()
35 {
36 int a[10] = { };
37 VERIFY( std::ranges::distance(a) == 10 );
38
39 test_range<int, random_access_iterator_wrapper> c(a);
40 VERIFY( std::ranges::distance(c) == 10 );
41
42 auto b = c.begin(), e = c.end();
43 VERIFY( std::ranges::distance(b, e) == 10 );
44 VERIFY( std::ranges::distance(e, b) == -10 );
45
46 const auto cb = b, ce = e;
47 VERIFY( std::ranges::distance(cb, ce) == 10 );
48 VERIFY( std::ranges::distance(ce, cb) == -10 );
49
50 test_sized_range<int, random_access_iterator_wrapper> c2(a);
51 VERIFY( std::ranges::distance(c2) == 10 );
52 }
53
54 void
55 test02()
56 {
57 int a[2] = { };
58 VERIFY( std::ranges::distance(a) == 2 );
59
60 test_range<int, bidirectional_iterator_wrapper> c(a);
61 VERIFY( std::ranges::distance(c) == 2 );
62
63 auto b = c.begin(), e = c.end();
64 VERIFY( std::ranges::distance(b, e) == 2 );
65
66 const auto cb = b, ce = e;
67 VERIFY( std::ranges::distance(cb, ce) == 2 );
68
69 test_sized_range<int, bidirectional_iterator_wrapper> c2(a);
70 VERIFY( std::ranges::distance(c2) == 2 );
71 }
72
73 void
74 test03()
75 {
76 int a[3] = { };
77 test_range<int, forward_iterator_wrapper> c(a);
78 VERIFY( std::ranges::distance(c) == 3 );
79
80 auto b = c.begin(), e = c.end();
81 VERIFY( std::ranges::distance(b, e) == 3 );
82
83 const auto cb = b, ce = e;
84 VERIFY( std::ranges::distance(cb, ce) == 3 );
85
86 test_sized_range<int, forward_iterator_wrapper> c2(a);
87 VERIFY( std::ranges::distance(c2) == 3 );
88 }
89
90 void
91 test04()
92 {
93 int a[4] = { };
94 test_range<int, input_iterator_wrapper> c(a);
95 static_assert( std::ranges::range<decltype(c)> );
96
97 VERIFY( std::ranges::distance(c) == 4 );
98 // first call to distance has traversed the range:
99 VERIFY( std::ranges::distance(c) == 0 );
100
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 );
104
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 );
108
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 );
113 }
114
115 void
116 test05()
117 {
118 int a[5] = { };
119 test_range<int, output_iterator_wrapper> c(a);
120 VERIFY( std::ranges::distance(c) == 5 );
121
122 test_range<int, output_iterator_wrapper> c2(a);
123 auto b = c2.begin();
124 auto e = c2.end();
125 VERIFY( std::ranges::distance(b, e) == 5 );
126
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 );
131
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 );
136 }
137
138 int
139 main()
140 {
141 test01();
142 test02();
143 test03();
144 test04();
145 test05();
146 }