-
Notifications
You must be signed in to change notification settings - Fork 3
/
ranges_concepts.cc
244 lines (204 loc) · 8.43 KB
/
ranges_concepts.cc
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "preview/ranges.h"
#include "gtest.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <deque>
#include <forward_list>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
#include "preview/algorithm.h"
#include "preview/concepts.h"
#include "preview/span.h"
#include "preview/string_view.h"
namespace ranges = preview::ranges;
TEST(VERSIONED(RangesConcepts), range) {
// A minimum range
struct SimpleRange
{
int* begin();
int* end();
};
static_assert(ranges::range<SimpleRange>::value, "");
// not a range: no begin/end
struct NotRange
{
int t {};
};
static_assert(!ranges::range<NotRange>::value, "");
// not a range: begin does not return an input_or_output_iterator
struct NotRange2
{
void* begin();
int* end();
};
static_assert(!ranges::range<NotRange2>::value, "");
}
template<typename T, std::size_t N>
struct MyRange : std::array<T, N> {};
template<typename T, std::size_t N>
PREVIEW_SPECIALIZE_ENABLE_BORROWED_RANGE(MyRange<T, N>) = false;
template<typename T, std::size_t N>
struct MyBorrowedRange : preview::span<T, N> {};
template<typename T, std::size_t N>
PREVIEW_INLINE_VARIABLE constexpr bool ::preview::ranges::enable_borrowed_range<MyBorrowedRange<T, N>> = true;
TEST(VERSIONED(RangesConcepts), borrowed_range) {
EXPECT_TRUE_TYPE (ranges::range<MyRange<int, 8>>);
EXPECT_FALSE_TYPE (ranges::borrowed_range<MyRange<int, 8>>);
EXPECT_TRUE_TYPE (ranges::range<MyBorrowedRange<int, 8>>);
EXPECT_TRUE_TYPE (ranges::borrowed_range<MyBorrowedRange<int, 8>>);
#if PREVIEW_CXX_VERSION >= 17
auto getMyRangeByValue = []{ return MyRange<int, 4>{{1, 2, 42, 3}}; };
auto dangling_iter = ranges::max_element(getMyRangeByValue());
EXPECT_TRUE_TYPE (std::is_same<ranges::dangling, decltype(dangling_iter)>);
EXPECT_FALSE_TYPE (preview::dereferenceable<decltype(dangling_iter)>);
// *dangling_iter; // compilation error (i.e. dangling protection works.)
auto my = MyRange<int, 4>{{1, 2, 42, 3}};
auto valid_iter = ranges::max_element(my);
EXPECT_EQ(*valid_iter, 42);
auto getMyBorrowedRangeByValue = []
{
static int sa[4]{1, 2, 42, 3};
return MyBorrowedRange<int, preview::size(sa)>{sa};
};
auto valid_iter2 = ranges::max_element(getMyBorrowedRangeByValue());
EXPECT_EQ(*valid_iter2, 42);
#endif
}
TEST(VERSIONED(RangesConcepts), sized_range) {
EXPECT_TRUE_TYPE(ranges::sized_range<int[4]>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::array<int, 4>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::deque<int>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::list<int>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::map<int, int>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::set<int>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::unordered_map<int, int>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::unordered_set<int>>);
EXPECT_TRUE_TYPE(ranges::sized_range<std::vector<int>>);
EXPECT_FALSE_TYPE(ranges::sized_range<int[]>);
EXPECT_FALSE_TYPE(ranges::sized_range<std::forward_list<int>>);
EXPECT_FALSE_TYPE(ranges::sized_range<std::queue<int>>);
}
struct ArchetypalView : ranges::view_interface<ArchetypalView>
{
int* begin();
int* end();
};
TEST(VERSIONED(RangesConcepts), view) {
EXPECT_TRUE_TYPE(ranges::view<ArchetypalView>);
EXPECT_TRUE_TYPE(ranges::view<ranges::subrange<typename std::vector<int>::iterator>>);
EXPECT_TRUE_TYPE(ranges::view<ranges::iota_view<int>>);
EXPECT_TRUE_TYPE(ranges::view<ranges::iota_view<int, int>>);
EXPECT_FALSE_TYPE(ranges::view<std::vector<int>>);
}
// TODO: Write test
TEST(VERSIONED(RangesConcepts), input_range) {}
// TODO: Write test
TEST(VERSIONED(RangesConcepts), output_range) {}
TEST(VERSIONED(RangesConcepts), forward_range) {
const char* str{"not a forward range"};
const char str2[] = "a forward range";
EXPECT_TRUE_TYPE(ranges::forward_range<decltype("a forward range")>);
EXPECT_TRUE_TYPE(ranges::forward_range<decltype(str2)>);
EXPECT_TRUE_TYPE(ranges::forward_range<std::forward_list<char>>);
EXPECT_TRUE_TYPE(ranges::forward_range<preview::span<char>>);
EXPECT_TRUE_TYPE(ranges::forward_range<std::unordered_map<int, int>>);
EXPECT_TRUE_TYPE(ranges::forward_range<std::unordered_set<int>>);
EXPECT_FALSE_TYPE(ranges::forward_range<decltype(str)>);
EXPECT_FALSE_TYPE(ranges::forward_range<std::stack<char>>);
EXPECT_FALSE_TYPE(ranges::forward_range<std::tuple<std::forward_list<char>>>);
EXPECT_FALSE_TYPE(ranges::forward_range<std::queue<char>>);
}
TEST(VERSIONED(RangesConcepts), bidirectional_range) {
EXPECT_TRUE_TYPE(ranges::bidirectional_range<std::set<int>>);
EXPECT_TRUE_TYPE(ranges::bidirectional_range<std::list<int>>);
// MSVC and Intel C++: hash container models bidirectional range (iterator is from std::list)
#if defined(_MSC_VER)
EXPECT_TRUE_TYPE(ranges::bidirectional_range<std::unordered_map<int, int>>);
EXPECT_TRUE_TYPE(ranges::bidirectional_range<std::unordered_set<int>>);
#else
EXPECT_FALSE_TYPE(ranges::bidirectional_range<std::unordered_map<int, int>>);
EXPECT_FALSE_TYPE(ranges::bidirectional_range<std::unordered_set<int>>);
#endif
EXPECT_FALSE_TYPE(ranges::bidirectional_range<std::forward_list<int>>);
}
TEST(VERSIONED(RangesConcepts), random_access_range) {
int a[4];
EXPECT_TRUE_TYPE(ranges::random_access_range<std::vector<int>>);
EXPECT_TRUE_TYPE(ranges::random_access_range<std::vector<bool>>);
EXPECT_TRUE_TYPE(ranges::random_access_range<std::deque<int>>);
EXPECT_TRUE_TYPE(ranges::random_access_range<std::valarray<int>>);
EXPECT_TRUE_TYPE(ranges::random_access_range<decltype(a)>);
EXPECT_TRUE_TYPE(ranges::random_access_range<std::array<std::list<int>, 42>>);
EXPECT_FALSE_TYPE(ranges::random_access_range<std::list<int>>);
EXPECT_FALSE_TYPE(ranges::random_access_range<std::set<int>>);
}
TEST(VERSIONED(RangesConcepts), contiguous_range) {
int a[4];
EXPECT_TRUE_TYPE(ranges::contiguous_range<std::vector<int>>);
EXPECT_TRUE_TYPE(ranges::contiguous_range<std::valarray<int>>);
EXPECT_TRUE_TYPE(ranges::contiguous_range<decltype(a)>);
EXPECT_TRUE_TYPE(ranges::contiguous_range<std::array<std::list<int>, 42>>);
EXPECT_FALSE_TYPE(ranges::contiguous_range<std::vector<bool>>);
EXPECT_FALSE_TYPE(ranges::contiguous_range<std::deque<int>>);
EXPECT_FALSE_TYPE(ranges::contiguous_range<std::list<int>>);
EXPECT_FALSE_TYPE(ranges::contiguous_range<std::set<int>>);
}
TEST(VERSIONED(RangesConcepts), common_range) {
struct A {
char* begin();
char* end();
};
EXPECT_TRUE_TYPE(ranges::common_range<A>);
struct B {
char* begin();
bool end();
}; // not a common_range: begin/end have different types
EXPECT_FALSE_TYPE(ranges::common_range<B>);
struct C {
char* begin();
}; // not a common_range, not even a range: has no end()
EXPECT_FALSE_TYPE(ranges::common_range<C>);
}
template<typename T>
auto test_viewable_range(T&&) -> ranges::viewable_range<T>;
TEST(VERSIONED(RangesConcepts), viewable_range) {
auto il = {1, 2, 3};
int arr []{1, 2, 3};
#if PREVIEW_CXX_VERSION < 17
std::vector<int> vec{1, 2, 3};
ranges::ref_view<int[3]> r{arr};
ranges::owning_view<std::string> o{std::string("Hello")};
#else
std::vector vec{1, 2, 3};
ranges::ref_view r{arr};
ranges::owning_view o{std::string("Hello")};
#endif
EXPECT_TRUE_TYPE(decltype(test_viewable_range( il )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( arr )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( vec )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( std::move(vec) )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( r )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( std::move(r) )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( std::move(o) )));
EXPECT_TRUE_TYPE(decltype(test_viewable_range( ranges::ref_view<ranges::owning_view<std::string>>(o) )));
EXPECT_FALSE_TYPE(decltype(test_viewable_range( std::move(il) )));
EXPECT_FALSE_TYPE(ranges::viewable_range<decltype(std::move(arr))>);
EXPECT_FALSE_TYPE(decltype(test_viewable_range( o )));
}
TEST(VERSIONED(RangesConcepts), constant_range) {
EXPECT_TRUE_TYPE(ranges::constant_range<const std::vector<int>>);
EXPECT_TRUE_TYPE(ranges::constant_range<preview::string_view>);
EXPECT_TRUE_TYPE(ranges::constant_range<preview::span<const int>>);
EXPECT_FALSE_TYPE(ranges::constant_range<std::vector<int>>);
EXPECT_FALSE_TYPE(ranges::constant_range<preview::span<int>>);
EXPECT_FALSE_TYPE(ranges::constant_range<const preview::span<int>>);
}