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 | // reverse_iterator rbegin(); |
12 | // reverse_iterator rend(); |
13 | // const_reverse_iterator rbegin() const; |
14 | // const_reverse_iterator rend() const; |
15 | // const_reverse_iterator crbegin() const; |
16 | // const_reverse_iterator crend() const; |
17 | |
18 | #include <vector> |
19 | #include <cassert> |
20 | #include <iterator> |
21 | |
22 | #include "min_allocator.h" |
23 | |
24 | template <class Vector> |
25 | TEST_CONSTEXPR_CXX20 void check_vector_reverse_iterators() { |
26 | { |
27 | Vector vec; |
28 | assert(vec.rbegin() == vec.rend()); |
29 | assert(vec.crbegin() == vec.crend()); |
30 | } |
31 | { |
32 | const int n = 10; |
33 | Vector vec; |
34 | const Vector& cvec = vec; |
35 | vec.reserve(n); |
36 | for (int i = 0; i < n; ++i) |
37 | vec.push_back(i); |
38 | { |
39 | int iterations = 0; |
40 | |
41 | for (typename Vector::const_reverse_iterator it = vec.crbegin(); it != vec.crend(); ++it) { |
42 | assert(*it == (n - iterations - 1)); |
43 | ++iterations; |
44 | } |
45 | assert(iterations == n); |
46 | } |
47 | { |
48 | assert(cvec.rbegin() == vec.crbegin()); |
49 | assert(cvec.rend() == vec.crend()); |
50 | } |
51 | { |
52 | int iterations = 0; |
53 | |
54 | for (typename Vector::reverse_iterator it = vec.rbegin(); it != vec.rend(); ++it) { |
55 | assert(*it == (n - iterations - 1)); |
56 | *it = 40; |
57 | assert(*it == 40); |
58 | ++iterations; |
59 | } |
60 | assert(iterations == n); |
61 | } |
62 | |
63 | assert(std::distance(vec.rbegin(), vec.rend()) == n); |
64 | assert(std::distance(cvec.rbegin(), cvec.rend()) == n); |
65 | assert(std::distance(vec.crbegin(), vec.crend()) == n); |
66 | assert(std::distance(cvec.crbegin(), cvec.crend()) == n); |
67 | } |
68 | } |
69 | |
70 | TEST_CONSTEXPR_CXX20 bool test() { |
71 | check_vector_reverse_iterators<std::vector<int> >(); |
72 | #if TEST_STD_VER >= 11 |
73 | check_vector_reverse_iterators<std::vector<int, min_allocator<int> > >(); |
74 | #endif |
75 | |
76 | return true; |
77 | } |
78 | |
79 | int main(int, char**) { |
80 | test(); |
81 | #if TEST_STD_VER > 17 |
82 | static_assert(test()); |
83 | #endif |
84 | |
85 | return 0; |
86 | } |
87 | |