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
|
// Range v3 library
//
// Copyright Eric Niebler 2014-present
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <vector>
#include <iterator>
#include <range/v3/core.hpp>
#include <range/v3/algorithm/copy.hpp>
#include <range/v3/algorithm/equal_range.hpp>
#include <range/v3/functional/identity.hpp>
#include <range/v3/functional/invoke.hpp>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/iterator/insert_iterators.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/repeat_n.hpp>
#include <range/v3/view/take.hpp>
#include <range/v3/view/transform.hpp>
#include "../array.hpp"
#include "../simple_test.hpp"
#include "../test_iterators.hpp"
struct my_int
{
int value;
};
bool compare(my_int lhs, my_int rhs)
{
return lhs.value < rhs.value;
}
void not_totally_ordered()
{
// This better compile!
std::vector<my_int> vec;
ranges::equal_range(vec, my_int{10}, compare);
}
template<class Iter, class Sent, class T, class Proj = ranges::identity>
void
test_it(Iter first, Sent last, const T& value, Proj proj = Proj{})
{
ranges::subrange<Iter, Iter> i =
ranges::equal_range(first, last, value, ranges::less{}, proj);
for (Iter j = first; j != i.begin(); ++j)
CHECK(ranges::invoke(proj, *j) < value);
for (Iter j = i.begin(); j != last; ++j)
CHECK(!(ranges::invoke(proj, *j) < value));
for (Iter j = first; j != i.end(); ++j)
CHECK(!(value < ranges::invoke(proj, *j)));
for (Iter j = i.end(); j != last; ++j)
CHECK(value < ranges::invoke(proj, *j));
auto res = ranges::equal_range(
ranges::make_subrange(first, last), value, ranges::less{}, proj);
for (Iter j = first; j != res.begin(); ++j)
CHECK(ranges::invoke(proj, *j) < value);
for (Iter j = res.begin(); j != last; ++j)
CHECK(!(ranges::invoke(proj, *j) < value));
for (Iter j = first; j != res.end(); ++j)
CHECK(!(value < ranges::invoke(proj, *j)));
for (Iter j = res.end(); j != last; ++j)
CHECK(value < ranges::invoke(proj, *j));
}
template<class Iter, class Sent = Iter>
void
test_it()
{
using namespace ranges::views;
static constexpr unsigned M = 10;
std::vector<int> v;
auto input = ints | take(100) | transform([](int i){return repeat_n(i,M);}) | join;
ranges::copy(input, ranges::back_inserter(v));
for (int x = 0; x <= (int)M; ++x)
test_it(Iter(v.data()), Sent(v.data()+v.size()), x);
}
template<class Iter, class Sent, class T>
constexpr bool test_constexpr(Iter first, Sent last, const T & value)
{
bool result = true;
const auto i = ranges::equal_range(first, last, value);
for(Iter j = first; j != i.begin(); ++j)
{
STATIC_CHECK_RETURN(*j < value);
}
for(Iter j = i.begin(); j != last; ++j)
{
STATIC_CHECK_RETURN(!(*j < value));
}
for(Iter j = first; j != i.end(); ++j)
{
STATIC_CHECK_RETURN(!(value < *j));
}
for(Iter j = i.end(); j != last; ++j)
{
STATIC_CHECK_RETURN(value < *j);
}
const auto res = ranges::equal_range(ranges::make_subrange(first, last), value);
for(Iter j = first; j != res.begin(); ++j)
{
STATIC_CHECK_RETURN(*j < value);
}
for(Iter j = res.begin(); j != last; ++j)
{
STATIC_CHECK_RETURN(!(*j < value));
}
for(Iter j = first; j != res.end(); ++j)
{
STATIC_CHECK_RETURN(!(value < *j));
}
for(Iter j = res.end(); j != last; ++j)
{
STATIC_CHECK_RETURN(value < *j);
}
return result;
}
template<class Iter, class Sent = Iter>
constexpr bool test_constexpr()
{
constexpr unsigned M = 10;
constexpr unsigned N = 10;
test::array<int, N * M> v{{0}};
for(unsigned i = 0; i < N; ++i)
{
for(unsigned j = 0; j < M; ++j)
{
v[i * M + j] = (int)i;
}
}
for(int x = 0; x <= (int)M; ++x)
{
STATIC_CHECK_RETURN(test_constexpr(Iter(v.data()), Sent(v.data() + v.size()), x));
}
return true;
}
constexpr bool test_constexpr_some()
{
constexpr int d[] = {0, 1, 2, 3};
for(auto e = ranges::begin(d); e < ranges::end(d); ++e)
{
for(int x = -1; x <= 4; ++x)
{
STATIC_CHECK_RETURN(test_constexpr(d, e, x));
}
}
return true;
}
int main()
{
int d[] = {0, 1, 2, 3};
for (int* e = d; e <= d+4; ++e)
for (int x = -1; x <= 4; ++x)
test_it(d, e, x);
test_it<ForwardIterator<const int*> >();
test_it<BidirectionalIterator<const int*> >();
test_it<RandomAccessIterator<const int*> >();
test_it<const int*>();
test_it<ForwardIterator<const int*>, Sentinel<const int*> >();
test_it<BidirectionalIterator<const int*>, Sentinel<const int*> >();
test_it<RandomAccessIterator<const int*>, Sentinel<const int*> >();
{
struct foo { int i; };
foo some_foos[] = {{1}, {2}, {4}};
test_it(some_foos, some_foos + 3, 2, &foo::i);
}
{
STATIC_CHECK(test_constexpr_some());
STATIC_CHECK(test_constexpr<ForwardIterator<const int *>>());
STATIC_CHECK(test_constexpr<BidirectionalIterator<const int *>>());
STATIC_CHECK(test_constexpr<RandomAccessIterator<const int *>>());
STATIC_CHECK(test_constexpr<const int *>());
STATIC_CHECK(test_constexpr<ForwardIterator<const int *>, Sentinel<const int *>>());
STATIC_CHECK(test_constexpr<BidirectionalIterator<const int *>, Sentinel<const int *>>());
STATIC_CHECK(test_constexpr<RandomAccessIterator<const int *>, Sentinel<const int *>>());
}
return ::test_result();
}
|