#include "testing/testing.hpp" #include "routing/coding.hpp" #include #include #include using namespace routing; namespace { template void ForEachNumber(ToDo && toDo) { for (T number = std::numeric_limits::min();; ++number) { toDo(number); if (number == std::numeric_limits::max()) break; } } template void TestZigZag(T prev, T current) { auto const delta = EncodeZigZagDelta(prev, current); auto const decodedCurrent = DecodeZigZagDelta(prev, delta); TEST_EQUAL(decodedCurrent, current, ("prev:", prev, "delta:", delta)); } template void TestZigZagUnsigned() { static_assert(std::is_unsigned::value, "T should be an unsigned type"); constexpr auto max = std::numeric_limits::max(); constexpr T values[] = {0, 1, 7, max / 2, max - 1, max}; for (T prev : values) { for (T current : values) TestZigZag(prev, current); } } template void TestZigZagSigned() { static_assert(std::is_signed::value, "T should be a signed type"); constexpr auto min = std::numeric_limits::min(); constexpr auto max = std::numeric_limits::max(); constexpr T values[] = {min, min + 1, min / 2, -7, -1, 0, 1, 7, max / 2, max - 1, max}; for (T prev : values) { for (T current : values) TestZigZag(prev, current); } } } // namespace namespace routing_test { UNIT_TEST(ModuleCastTest) { ForEachNumber([](uint8_t number) { auto signedNumber = ModularCast(number); static_assert(std::is_same::value, "int8_t expected"); TEST_EQUAL(static_cast(signedNumber), number, ("signedNumber:", signedNumber)); }); } UNIT_TEST(ZigZagUint8) { ForEachNumber([](uint8_t prev) { ForEachNumber([&](uint8_t current) { TestZigZag(prev, current); }); }); } UNIT_TEST(ZigZagInt8) { ForEachNumber([](int8_t prev) { ForEachNumber([&](int8_t current) { TestZigZag(prev, current); }); }); } UNIT_TEST(ZigZagUint16) { TestZigZagUnsigned(); } UNIT_TEST(ZigZagInt16) { TestZigZagSigned(); } UNIT_TEST(ZigZagUint32) { TestZigZagUnsigned(); } UNIT_TEST(ZigZagInt32) { TestZigZagSigned(); } UNIT_TEST(ZigZagUint64) { TestZigZagUnsigned(); } UNIT_TEST(ZigZagInt64) { TestZigZagSigned(); } } // namespace routing_test