|
| 1 | +// AGAIN the contents of iterbase are completely subject to change, do not rely |
| 2 | +// on any of this. Users of the library must consider all of this undocumented |
| 3 | +// |
| 4 | + |
| 5 | +#include <internal/iterbase.hpp> |
| 6 | +#include <type_traits> |
| 7 | +#include <vector> |
| 8 | +#include <iterator> |
| 9 | +#include <string> |
| 10 | +#include <list> |
| 11 | +#include <enumerate.hpp> |
| 12 | + |
| 13 | +#include "catch.hpp" |
| 14 | +#include "helpers.hpp" |
| 15 | + |
| 16 | +namespace it = iter::impl; |
| 17 | + |
| 18 | +using IVec = std::vector<int>; |
| 19 | + |
| 20 | +template <typename T> |
| 21 | +using hrai = it::has_random_access_iter<T>; |
| 22 | +TEST_CASE("Detects random access iterators correctly", "[iterbase]") { |
| 23 | + REQUIRE(hrai<std::vector<int>>::value); |
| 24 | + REQUIRE(hrai<std::string>::value); |
| 25 | + REQUIRE(hrai<int[10]>::value); |
| 26 | + |
| 27 | + REQUIRE_FALSE(hrai<std::list<int>>::value); |
| 28 | + REQUIRE_FALSE(hrai<decltype(iter::enumerate(std::list<int>{}))>::value); |
| 29 | + REQUIRE_FALSE(hrai<itertest::BasicIterable<int>>::value); |
| 30 | +} |
| 31 | + |
| 32 | +TEST_CASE("Detects correct iterator types", "[iterbase]") { |
| 33 | + REQUIRE((std::is_same<it::iterator_type<IVec>, IVec::iterator>::value)); |
| 34 | + REQUIRE((std::is_same<it::iterator_type<IVec>, IVec::iterator>::value)); |
| 35 | + REQUIRE((std::is_same<it::iterator_deref<IVec>, |
| 36 | + IVec::iterator::reference>::value)); |
| 37 | + REQUIRE((std::is_same<it::const_iterator_deref<IVec>, |
| 38 | + IVec::iterator::reference>::value)); |
| 39 | + REQUIRE((std::is_same<it::iterator_traits_deref<IVec>, |
| 40 | + IVec::iterator::value_type>::value)); |
| 41 | + |
| 42 | + REQUIRE((std::is_same<it::reverse_iterator_type<IVec>, |
| 43 | + IVec::reverse_iterator>::value)); |
| 44 | + REQUIRE((std::is_same<it::reverse_iterator_deref<IVec>, |
| 45 | + IVec::reverse_iterator::reference>::value)); |
| 46 | + REQUIRE( |
| 47 | + (std::is_same<it::iterator_arrow<IVec>, IVec::iterator::pointer>::value)); |
| 48 | + REQUIRE((std::is_same<it::iterator_arrow<int[10]>, int*>::value)); |
| 49 | + REQUIRE((std::is_same<it::reverse_iterator_arrow<IVec>, |
| 50 | + IVec::reverse_iterator::pointer>::value)); |
| 51 | +} |
| 52 | + |
| 53 | +TEST_CASE("advance, next, size", "[iterbase]") { |
| 54 | + IVec v = {2, 4, 6, 8, 10, 12, 14, 16, 18}; |
| 55 | + auto itr = std::begin(v); |
| 56 | + REQUIRE(it::apply_arrow(itr) == &v[0]); |
| 57 | + |
| 58 | + it::dumb_advance(itr, 3); |
| 59 | + REQUIRE(itr == (std::begin(v) + 3)); |
| 60 | + REQUIRE(it::dumb_next(std::begin(v), 3) == std::begin(v) + 3); |
| 61 | + REQUIRE(it::dumb_size(v) == v.size()); |
| 62 | +} |
| 63 | + |
| 64 | +TEST_CASE("are_same", "[iterbase]") { |
| 65 | + REQUIRE((it::are_same<int, int, int, int>::value)); |
| 66 | + REQUIRE_FALSE((it::are_same<double, int, int, int>::value)); |
| 67 | + REQUIRE_FALSE((it::are_same<int, int, int, double>::value)); |
| 68 | + REQUIRE_FALSE((it::are_same<int, double, int, int>::value)); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_CASE("DerefHolder lvalue reference", "[iterbase]") { |
| 72 | + it::DerefHolder<int&> dh; |
| 73 | + int a = 2; |
| 74 | + int b = 5; |
| 75 | + REQUIRE_FALSE(dh); |
| 76 | + dh.reset(a); |
| 77 | + REQUIRE(dh); |
| 78 | + |
| 79 | + REQUIRE(dh.get_ptr() == &a); |
| 80 | + REQUIRE(&dh.get() == &a); |
| 81 | + dh.reset(b); |
| 82 | + REQUIRE(dh.get_ptr() == &b); |
| 83 | + REQUIRE(&dh.get() == &b); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_CASE("DerefHolder non-reference", "[iterbase]") { |
| 87 | + it::DerefHolder<int> dh; |
| 88 | + int a = 2; |
| 89 | + int b = 5; |
| 90 | + REQUIRE_FALSE(dh); |
| 91 | + dh.reset(std::move(a)); |
| 92 | + REQUIRE(dh.get() == 2); |
| 93 | + REQUIRE(&dh.get() != &a); |
| 94 | + |
| 95 | + dh.reset(std::move(b)); |
| 96 | + REQUIRE(dh.get() == 5); |
| 97 | +} |
0 commit comments