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
|
// 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
//
// Copyright 2005 - 2007 Adobe Systems Incorporated
// Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
// or a copy at http://stlab.adobe.com/licenses.html)
//===----------------------------------------------------------------------===//
//
// 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 <memory>
#include <random>
#include <algorithm>
#include <functional>
#include <range/v3/core.hpp>
#include <range/v3/algorithm/heap_algorithm.hpp>
#include <range/v3/algorithm/is_sorted.hpp>
#include "../array.hpp"
#include "../simple_test.hpp"
#include "../test_utils.hpp"
#include "../test_iterators.hpp"
RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
namespace
{
constexpr bool test_constexpr()
{
using namespace ranges;
bool r = true;
constexpr int N = 100;
test::array<int, N> ia{{0}};
for(int i = 0; i < N; ++i)
ia[i] = N - 1 - i;
make_heap(begin(ia), end(ia));
STATIC_CHECK_RETURN(sort_heap(begin(ia), end(ia), ranges::less{}) == end(ia));
STATIC_CHECK_RETURN(is_sorted(ia));
return r;
}
std::mt19937 gen;
void test_1(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N);
CHECK(ranges::sort_heap(ia, ia+N) == ia+N);
CHECK(std::is_sorted(ia, ia+N));
delete[] ia;
}
void test_2(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N);
CHECK(ranges::sort_heap(ia, Sentinel<int*>(ia+N)) == ia+N);
CHECK(std::is_sorted(ia, ia+N));
delete[] ia;
}
void test_3(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N);
CHECK(ranges::sort_heap(::as_lvalue(ranges::make_subrange(ia, ia+N))) == ia+N);
CHECK(std::is_sorted(ia, ia+N));
delete[] ia;
}
void test_4(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N);
CHECK(ranges::sort_heap(::as_lvalue(ranges::make_subrange(ia, Sentinel<int*>(ia+N)))) == ia+N);
CHECK(std::is_sorted(ia, ia+N));
delete[] ia;
}
void test_5(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N, std::greater<int>());
CHECK(ranges::sort_heap(ia, ia+N, std::greater<int>()) == ia+N);
CHECK(std::is_sorted(ia, ia+N, std::greater<int>()));
delete[] ia;
}
void test_6(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N, std::greater<int>());
CHECK(ranges::sort_heap(ia, Sentinel<int*>(ia+N), std::greater<int>()) == ia+N);
CHECK(std::is_sorted(ia, ia+N, std::greater<int>()));
delete[] ia;
}
void test_7(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N, std::greater<int>());
CHECK(ranges::sort_heap(::as_lvalue(ranges::make_subrange(ia, ia+N)), std::greater<int>()) == ia+N);
CHECK(std::is_sorted(ia, ia+N, std::greater<int>()));
delete[] ia;
}
void test_8(int N)
{
int* ia = new int[N];
for (int i = 0; i < N; ++i)
ia[i] = i;
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N, std::greater<int>());
CHECK(ranges::sort_heap(ranges::make_subrange(ia, Sentinel<int*>(ia+N)), std::greater<int>()) == ia+N);
CHECK(std::is_sorted(ia, ia+N, std::greater<int>()));
delete[] ia;
}
struct indirect_less
{
template<class P>
bool operator()(const P& x, const P& y)
{return *x < *y;}
};
void test_9(int N)
{
std::unique_ptr<int>* ia = new std::unique_ptr<int>[N];
for (int i = 0; i < N; ++i)
ia[i].reset(new int(i));
std::shuffle(ia, ia+N, gen);
std::make_heap(ia, ia+N, indirect_less());
CHECK(ranges::sort_heap(ia, ia+N, indirect_less()) == ia+N);
CHECK(std::is_sorted(ia, ia+N, indirect_less()));
delete[] ia;
}
struct S
{
int i;
};
void test_10(int N)
{
S* ia = new S[N];
int* ib = new int[N];
for (int i = 0; i < N; ++i)
ib[i] = i;
std::shuffle(ib, ib+N, gen);
std::make_heap(ib, ib+N);
std::transform(ib, ib+N, ia, [](int i){return S{i};});
CHECK(ranges::sort_heap(ia, ia+N, std::less<int>(), &S::i) == ia+N);
std::transform(ia, ia+N, ib, std::mem_fn(&S::i));
CHECK(std::is_sorted(ib, ib+N));
delete[] ia;
delete[] ib;
}
void test_all(int N)
{
test_1(N);
test_2(N);
test_3(N);
test_4(N);
test_5(N);
test_6(N);
test_7(N);
test_8(N);
}
}
int main()
{
test_all(0);
test_all(1);
test_all(2);
test_all(3);
test_all(10);
test_all(1000);
test_9(1000);
test_10(1000);
{
STATIC_CHECK(test_constexpr());
}
return test_result();
}
|