xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/iterators.pass.cpp (revision 8d3252a8987818171878a26e4298b4b5dbf2a7e9)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <vector>
10 
11 // iterator       begin();
12 // iterator       end();
13 // const_iterator begin()  const;
14 // const_iterator end()    const;
15 // const_iterator cbegin() const;
16 // const_iterator cend()   const;
17 
18 #include <vector>
19 #include <cassert>
20 #include <iterator>
21 
22 #include "test_macros.h"
23 #include "min_allocator.h"
24 
25 TEST_CONSTEXPR_CXX20 bool tests()
26 {
27     using IterRefT = std::iterator_traits<std::vector<bool>::iterator>::reference;
28     ASSERT_SAME_TYPE(IterRefT, std::vector<bool>::reference);
29 
30     using ConstIterRefT = std::iterator_traits<std::vector<bool>::const_iterator>::reference;
31 #if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
32     ASSERT_SAME_TYPE(ConstIterRefT, bool);
33 #else
34     ASSERT_SAME_TYPE(ConstIterRefT, std::__bit_const_reference<std::vector<bool> >);
35 #endif
36     {
37         typedef bool T;
38         typedef std::vector<T> C;
39         C c;
40         C::iterator i = c.begin();
41         C::iterator j = c.end();
42         assert(std::distance(i, j) == 0);
43         assert(i == j);
44     }
45     {
46         typedef bool T;
47         typedef std::vector<T> C;
48         const C c;
49         C::const_iterator i = c.begin();
50         C::const_iterator j = c.end();
51         assert(std::distance(i, j) == 0);
52         assert(i == j);
53     }
54     {
55         typedef bool T;
56         typedef std::vector<T> C;
57         C c;
58         C::const_iterator i = c.cbegin();
59         C::const_iterator j = c.cend();
60         assert(std::distance(i, j) == 0);
61         assert(i == j);
62         assert(i == c.end());
63     }
64     {
65         typedef bool T;
66         typedef std::vector<T> C;
67         C::iterator i;
68         C::const_iterator j;
69         (void) i;
70         (void) j;
71     }
72 #if TEST_STD_VER >= 11
73     {
74         typedef bool T;
75         typedef std::vector<T, min_allocator<T>> C;
76         C c;
77         C::iterator i = c.begin();
78         C::iterator j = c.end();
79         assert(std::distance(i, j) == 0);
80 
81         assert(i == j);
82         assert(!(i != j));
83 
84         assert(!(i < j));
85         assert((i <= j));
86 
87         assert(!(i > j));
88         assert((i >= j));
89 
90 #  if TEST_STD_VER >= 20
91         // P1614 + LWG3352
92         std::same_as<std::strong_ordering> decltype(auto) r = i <=> j;
93         assert(r == std::strong_ordering::equal);
94 #  endif
95     }
96     {
97         typedef bool T;
98         typedef std::vector<T, min_allocator<T>> C;
99         const C c;
100         C::const_iterator i = c.begin();
101         C::const_iterator j = c.end();
102         assert(std::distance(i, j) == 0);
103 
104         assert(i == j);
105         assert(!(i != j));
106 
107         assert(!(i < j));
108         assert((i <= j));
109 
110         assert(!(i > j));
111         assert((i >= j));
112 
113 #  if TEST_STD_VER >= 20
114         // P1614 + LWG3352
115         std::same_as<std::strong_ordering> decltype(auto) r = i <=> j;
116         assert(r == std::strong_ordering::equal);
117 #  endif
118     }
119     {
120         typedef bool T;
121         typedef std::vector<T, min_allocator<T>> C;
122         C c;
123         C::const_iterator i = c.cbegin();
124         C::const_iterator j = c.cend();
125         assert(std::distance(i, j) == 0);
126         assert(i == j);
127         assert(i == c.end());
128     }
129     {
130         typedef bool T;
131         typedef std::vector<T, min_allocator<T>> C;
132         C::iterator i;
133         C::const_iterator j;
134         (void) i;
135         (void) j;
136     }
137 #endif
138 #if TEST_STD_VER > 11
139     { // N3644 testing
140         std::vector<bool>::iterator ii1{}, ii2{};
141         std::vector<bool>::iterator ii4 = ii1;
142         std::vector<bool>::const_iterator cii{};
143         assert ( ii1 == ii2 );
144         assert ( ii1 == ii4 );
145 
146         assert (!(ii1 != ii2 ));
147 
148         assert ( (ii1 == cii ));
149         assert ( (cii == ii1 ));
150         assert (!(ii1 != cii ));
151         assert (!(cii != ii1 ));
152         assert (!(ii1 <  cii ));
153         assert (!(cii <  ii1 ));
154         assert ( (ii1 <= cii ));
155         assert ( (cii <= ii1 ));
156         assert (!(ii1 >  cii ));
157         assert (!(cii >  ii1 ));
158         assert ( (ii1 >= cii ));
159         assert ( (cii >= ii1 ));
160         assert (cii - ii1 == 0);
161         assert (ii1 - cii == 0);
162 
163 #  if TEST_STD_VER >= 20
164         // P1614 + LWG3352
165         std::same_as<std::strong_ordering> decltype(auto) r1 = ii1 <=> ii2;
166         assert(r1 == std::strong_ordering::equal);
167 
168         std::same_as<std::strong_ordering> decltype(auto) r2 = cii <=> ii2;
169         assert(r2 == std::strong_ordering::equal);
170 #  endif // TEST_STD_VER > 20
171     }
172 #endif
173 
174     return true;
175 }
176 
177 int main(int, char**)
178 {
179     tests();
180 #if TEST_STD_VER > 17
181     static_assert(tests());
182 #endif
183     return 0;
184 }
185