| 1 |
#include "d_range.h" |
| 2 |
#include <ranges> |
| 3 |
#include <vector> |
| 4 |
|
| 5 |
#define BOOST_TEST_DYN_LINK |
| 6 |
#define BOOST_TEST_MODULE Rebirth xrange |
| 7 |
#include <boost/test/unit_test.hpp> |
| 8 |
|
| 9 |
/* Test that an xrange is empty when the ending bound is 0. |
| 10 |
*/ |
| 11 |
BOOST_AUTO_TEST_CASE(xrange_empty_0) |
| 12 |
{ |
| 13 |
bool empty = true; |
| 14 |
for (auto &&v : xrange(0u)) |
| 15 |
{ |
| 16 |
(void)v; |
| 17 |
empty = false; |
| 18 |
} |
| 19 |
BOOST_TEST(empty); |
| 20 |
} |
| 21 |
|
| 22 |
/* Test that an xrange is empty when the start is higher than the end. |
| 23 |
*/ |
| 24 |
BOOST_AUTO_TEST_CASE(xrange_empty_transposed) |
| 25 |
{ |
| 26 |
bool empty = true; |
| 27 |
for (auto &&v : xrange(2u, 1u)) |
| 28 |
{ |
| 29 |
(void)v; |
| 30 |
empty = false; |
| 31 |
} |
| 32 |
BOOST_TEST(empty); |
| 33 |
} |
| 34 |
|
| 35 |
/* Test that an xrange produces the correct number of entries. |
| 36 |
*/ |
| 37 |
BOOST_AUTO_TEST_CASE(xrange_length) |
| 38 |
{ |
| 39 |
unsigned count{0}; |
| 40 |
constexpr unsigned length = 4u; |
| 41 |
for (auto &&v : xrange(length)) |
| 42 |
{ |
| 43 |
(void)v; |
| 44 |
++ count; |
| 45 |
} |
| 46 |
BOOST_TEST(count == length); |
| 47 |
} |
| 48 |
|
| 49 |
/* Test that an xrange produces the correct values when using an implied |
| 50 |
* start of 0. |
| 51 |
*/ |
| 52 |
BOOST_AUTO_TEST_CASE(xrange_contents) |
| 53 |
{ |
| 54 |
std::vector<unsigned> out; |
| 55 |
for (auto &&v : xrange(4u)) |
| 56 |
out.emplace_back(v); |
| 57 |
std::vector<unsigned> expected{0, 1, 2, 3}; |
| 58 |
BOOST_TEST(out == expected); |
| 59 |
} |
| 60 |
|
| 61 |
/* Test that an xrange produces the correct values when using an |
| 62 |
* explicit start. |
| 63 |
*/ |
| 64 |
BOOST_AUTO_TEST_CASE(xrange_contents_start) |
| 65 |
{ |
| 66 |
std::vector<unsigned> out; |
| 67 |
for (auto &&v : xrange(2u, 4u)) |
| 68 |
out.emplace_back(v); |
| 69 |
std::vector<unsigned> expected{2, 3}; |
| 70 |
BOOST_TEST(out == expected); |
| 71 |
} |
| 72 |
|
| 73 |
/* Test that an xrange produces the correct values when stepping up |
| 74 |
* with skipped values. |
| 75 |
*/ |
| 76 |
BOOST_AUTO_TEST_CASE(xrange_contents_start_up_2) |
| 77 |
{ |
| 78 |
std::vector<unsigned> out; |
| 79 |
for (auto &&v : xrange(std::integral_constant<unsigned, 2u>(), std::integral_constant<unsigned, 6u>(), std::integral_constant<int, 2>())) |
| 80 |
out.emplace_back(v); |
| 81 |
std::vector<unsigned> expected{2, 4}; |
| 82 |
BOOST_TEST(out == expected); |
| 83 |
} |
| 84 |
|
| 85 |
BOOST_AUTO_TEST_CASE(xrange_contents_descending) |
| 86 |
{ |
| 87 |
std::vector<unsigned> out; |
| 88 |
for (auto &&v : xrange(std::integral_constant<unsigned, 4u>(), std::integral_constant<unsigned, 2u>(), xrange_descending())) |
| 89 |
out.emplace_back(v); |
| 90 |
std::vector<unsigned> expected{4, 3}; |
| 91 |
BOOST_TEST(out == expected); |
| 92 |
} |
| 93 |
|
| 94 |
/* Test that an xrange produces the correct values when stepping down |
| 95 |
* with skipped values. |
| 96 |
*/ |
| 97 |
BOOST_AUTO_TEST_CASE(xrange_contents_start_down_2_constant) |
| 98 |
{ |
| 99 |
std::vector<unsigned> out; |
| 100 |
for (auto &&v : xrange(std::integral_constant<unsigned, 5u>(), std::integral_constant<unsigned, 1u>(), std::integral_constant<int, -2>())) |
| 101 |
out.emplace_back(v); |
| 102 |
std::vector<unsigned> expected{5, 3}; |
| 103 |
BOOST_TEST(out == expected); |
| 104 |
} |
| 105 |
|
| 106 |
BOOST_AUTO_TEST_CASE(xrange_contents_start_down_variable) |
| 107 |
{ |
| 108 |
std::vector<unsigned> out; |
| 109 |
for (auto &&v : xrange(5u, std::integral_constant<unsigned, 2u>(), xrange_descending())) |
| 110 |
out.emplace_back(v); |
| 111 |
std::vector<unsigned> expected{5, 4, 3}; |
| 112 |
BOOST_TEST(out == expected); |
| 113 |
} |
| 114 |
|
| 115 |
/* Test that pairs of iterators pulled from a shared xrange are |
| 116 |
* independent. If traversing an xrange modified the state of the |
| 117 |
* xrange object, this test would either hang due to the inner loop |
| 118 |
* resetting state, or produce incorrect output. |
| 119 |
*/ |
| 120 |
BOOST_AUTO_TEST_CASE(xrange_self_nest) |
| 121 |
{ |
| 122 |
std::vector<unsigned> out; |
| 123 |
auto &&r = xrange(2u); |
| 124 |
for (auto &&ov : r) |
| 125 |
for (auto &&iv : r) |
| 126 |
out.emplace_back((ov << 8) | iv); |
| 127 |
std::vector<unsigned> expected{0, 1, (1 << 8), (1 << 8) + 1}; |
| 128 |
BOOST_TEST(out == expected); |
| 129 |
} |
| 130 |
|
| 131 |
BOOST_AUTO_TEST_CASE(xrange_iter_values) |
| 132 |
{ |
| 133 |
auto &&r = xrange(2u); |
| 134 |
BOOST_TEST(*r.begin() == 0); |
| 135 |
BOOST_TEST(*std::next(r.begin()) == 1); |
| 136 |
BOOST_TEST(*std::next(r.begin(), 2) == 2); |
| 137 |
} |
| 138 |
|
| 139 |
static_assert(std::ranges::borrowed_range<decltype(xrange(2u))>); |