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
|
// Copyright (C) 2016-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <type_safe/index.hpp>
#include <catch.hpp>
using namespace type_safe;
TEST_CASE("index_t")
{
index_t idx;
REQUIRE(idx == index_t(0u));
SECTION("operator+=")
{
difference_t a(5);
idx += a;
REQUIRE(idx == index_t(5u));
difference_t b(-3);
idx += b;
REQUIRE(idx == index_t(2u));
}
SECTION("operator-=")
{
idx = index_t(10u);
difference_t a(5);
idx -= a;
REQUIRE(idx == index_t(5u));
difference_t b(-3);
idx -= b;
REQUIRE(idx == index_t(8u));
}
SECTION("operator+")
{
auto c = idx + difference_t(5);
REQUIRE(c == index_t(5u));
auto d = difference_t(5) + idx;
REQUIRE(d == index_t(5u));
auto e = c + difference_t(-3);
REQUIRE(e == index_t(2u));
auto f = difference_t(-3) + d;
REQUIRE(f == index_t(2u));
}
SECTION("next")
{
auto a = next(idx, difference_t(5));
REQUIRE(a == index_t(5u));
auto b = next(a, difference_t(-3));
REQUIRE(b == index_t(2u));
}
SECTION("prev")
{
idx = index_t(10u);
auto a = prev(idx, difference_t(5));
REQUIRE(a == index_t(5u));
auto b = prev(a, difference_t(-3));
REQUIRE(b == index_t(8u));
}
SECTION("advance")
{
advance(idx, difference_t(5));
REQUIRE(idx == index_t(5u));
advance(idx, difference_t(-3));
REQUIRE(idx == index_t(2u));
}
SECTION("operator-")
{
idx = index_t(10u);
auto c = idx - difference_t(5);
REQUIRE(c == index_t(5u));
auto d = c - difference_t(-3);
REQUIRE(d == index_t(8u));
}
SECTION("distance")
{
auto a = index_t(5u) - idx;
REQUIRE(a == difference_t(5));
REQUIRE(a == distance(idx, index_t(5u)));
auto b = idx - index_t(5u);
REQUIRE(b == difference_t(-5));
REQUIRE(b == distance(index_t(5u), idx));
}
SECTION("at")
{
std::size_t array[] = {0, 1, 2, 3, 4, 5};
for (index_t i; i != 5u; ++i)
REQUIRE(at(array, i) == std::size_t(get(i)));
}
}
|