Welcome to mirror list, hosted at ThFree Co, Russian Federation.

coding_test.cpp « routing_tests « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0702a86673cc75f23f258fb5472c6eb883d9a1a6 (plain)
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
#include "testing/testing.hpp"

#include "routing/coding.hpp"

#include <cstdint>
#include <limits>
#include <type_traits>

using namespace routing;

namespace
{
template <typename T, typename ToDo>
void ForEachNumber(ToDo && toDo)
{
  for (T number = std::numeric_limits<T>::min();; ++number)
  {
    toDo(number);

    if (number == std::numeric_limits<T>::max())
      break;
  }
}

template <typename T>
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 <typename T>
void TestZigZagUnsigned()
{
  static_assert(std::is_unsigned<T>::value, "T should be an unsigned type");

  constexpr auto max = std::numeric_limits<T>::max();
  constexpr T values[] = {0, 1, 7, max / 2, max - 1, max};
  for (T prev : values)
  {
    for (T current : values)
      TestZigZag(prev, current);
  }
}

template <typename T>
void TestZigZagSigned()
{
  static_assert(std::is_signed<T>::value, "T should be a signed type");

  constexpr auto min = std::numeric_limits<T>::min();
  constexpr auto max = std::numeric_limits<T>::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>([](uint8_t number) {
    auto signedNumber = ModularCast(number);
    static_assert(std::is_same<decltype(signedNumber), int8_t>::value, "int8_t expected");
    TEST_EQUAL(static_cast<uint8_t>(signedNumber), number, ("signedNumber:", signedNumber));
  });
}

UNIT_TEST(ZigZagUint8)
{
  ForEachNumber<uint8_t>([](uint8_t prev) {
    ForEachNumber<uint8_t>([&](uint8_t current) { TestZigZag(prev, current); });
  });
}

UNIT_TEST(ZigZagInt8)
{
  ForEachNumber<int8_t>([](int8_t prev) {
    ForEachNumber<int8_t>([&](int8_t current) { TestZigZag(prev, current); });
  });
}

UNIT_TEST(ZigZagUint16) { TestZigZagUnsigned<uint16_t>(); }
UNIT_TEST(ZigZagInt16) { TestZigZagSigned<int16_t>(); }
UNIT_TEST(ZigZagUint32) { TestZigZagUnsigned<uint32_t>(); }
UNIT_TEST(ZigZagInt32) { TestZigZagSigned<int32_t>(); }
UNIT_TEST(ZigZagUint64) { TestZigZagUnsigned<uint64_t>(); }
UNIT_TEST(ZigZagInt64) { TestZigZagSigned<int64_t>(); }
}  // namespace routing_test