RangeAdapterTest.cpp
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//===- RangeAdapterTest.cpp - Unit tests for range adapters --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/iterator_range.h"
#include "gtest/gtest.h"
#include <iterator>
#include <list>
#include <vector>
using namespace llvm;
namespace {
// A wrapper around vector which exposes rbegin(), rend().
class ReverseOnlyVector {
std::vector<int> Vec;
public:
ReverseOnlyVector(std::initializer_list<int> list) : Vec(list) {}
typedef std::vector<int>::reverse_iterator reverse_iterator;
typedef std::vector<int>::const_reverse_iterator const_reverse_iterator;
reverse_iterator rbegin() { return Vec.rbegin(); }
reverse_iterator rend() { return Vec.rend(); }
const_reverse_iterator rbegin() const { return Vec.rbegin(); }
const_reverse_iterator rend() const { return Vec.rend(); }
};
// A wrapper around vector which exposes begin(), end(), rbegin() and rend().
// begin() and end() don't have implementations as this ensures that we will
// get a linker error if reverse() chooses begin()/end() over rbegin(), rend().
class BidirectionalVector {
mutable std::vector<int> Vec;
public:
BidirectionalVector(std::initializer_list<int> list) : Vec(list) {}
typedef std::vector<int>::iterator iterator;
iterator begin() const;
iterator end() const;
typedef std::vector<int>::reverse_iterator reverse_iterator;
reverse_iterator rbegin() const { return Vec.rbegin(); }
reverse_iterator rend() const { return Vec.rend(); }
};
/// This is the same as BidirectionalVector but with the addition of const
/// begin/rbegin methods to ensure that the type traits for has_rbegin works.
class BidirectionalVectorConsts {
std::vector<int> Vec;
public:
BidirectionalVectorConsts(std::initializer_list<int> list) : Vec(list) {}
typedef std::vector<int>::iterator iterator;
typedef std::vector<int>::const_iterator const_iterator;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
typedef std::vector<int>::reverse_iterator reverse_iterator;
typedef std::vector<int>::const_reverse_iterator const_reverse_iterator;
reverse_iterator rbegin() { return Vec.rbegin(); }
reverse_iterator rend() { return Vec.rend(); }
const_reverse_iterator rbegin() const { return Vec.rbegin(); }
const_reverse_iterator rend() const { return Vec.rend(); }
};
/// Check that types with custom iterators work.
class CustomIteratorVector {
mutable std::vector<int> V;
public:
CustomIteratorVector(std::initializer_list<int> list) : V(list) {}
typedef std::vector<int>::iterator iterator;
class reverse_iterator {
std::vector<int>::iterator I;
public:
reverse_iterator() = default;
reverse_iterator(const reverse_iterator &) = default;
reverse_iterator &operator=(const reverse_iterator &) = default;
explicit reverse_iterator(std::vector<int>::iterator I) : I(I) {}
reverse_iterator &operator++() {
--I;
return *this;
}
reverse_iterator &operator--() {
++I;
return *this;
}
int &operator*() const { return *std::prev(I); }
int *operator->() const { return &*std::prev(I); }
friend bool operator==(const reverse_iterator &L,
const reverse_iterator &R) {
return L.I == R.I;
}
friend bool operator!=(const reverse_iterator &L,
const reverse_iterator &R) {
return !(L == R);
}
};
iterator begin() const { return V.begin(); }
iterator end() const { return V.end(); }
reverse_iterator rbegin() const { return reverse_iterator(V.end()); }
reverse_iterator rend() const { return reverse_iterator(V.begin()); }
};
template <typename R> void TestRev(const R &r) {
int counter = 3;
for (int i : r)
EXPECT_EQ(i, counter--);
}
// Test fixture
template <typename T> class RangeAdapterLValueTest : public ::testing::Test {};
typedef ::testing::Types<std::vector<int>, std::list<int>, int[4]>
RangeAdapterLValueTestTypes;
TYPED_TEST_CASE(RangeAdapterLValueTest, RangeAdapterLValueTestTypes);
TYPED_TEST(RangeAdapterLValueTest, TrivialOperation) {
TypeParam v = {0, 1, 2, 3};
TestRev(reverse(v));
const TypeParam c = {0, 1, 2, 3};
TestRev(reverse(c));
}
template <typename T> struct RangeAdapterRValueTest : testing::Test {};
typedef ::testing::Types<std::vector<int>, std::list<int>, CustomIteratorVector,
ReverseOnlyVector, BidirectionalVector,
BidirectionalVectorConsts>
RangeAdapterRValueTestTypes;
TYPED_TEST_CASE(RangeAdapterRValueTest, RangeAdapterRValueTestTypes);
TYPED_TEST(RangeAdapterRValueTest, TrivialOperation) {
TestRev(reverse(TypeParam({0, 1, 2, 3})));
}
TYPED_TEST(RangeAdapterRValueTest, HasRbegin) {
static_assert(has_rbegin<TypeParam>::value, "rbegin() should be defined");
}
TYPED_TEST(RangeAdapterRValueTest, RangeType) {
static_assert(
std::is_same<
decltype(reverse(*static_cast<TypeParam *>(nullptr)).begin()),
decltype(static_cast<TypeParam *>(nullptr)->rbegin())>::value,
"reverse().begin() should have the same type as rbegin()");
static_assert(
std::is_same<
decltype(reverse(*static_cast<const TypeParam *>(nullptr)).begin()),
decltype(static_cast<const TypeParam *>(nullptr)->rbegin())>::value,
"reverse().begin() should have the same type as rbegin() [const]");
}
} // anonymous namespace