-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_bits.cpp
92 lines (72 loc) · 1.82 KB
/
test_bits.cpp
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
#include <vector>
#include "bits.hpp"
#include "test.hpp"
using namespace morton;
using p32_64 = std::pair<uint32_t, uint64_t>;
const std::vector<p32_64> pdata = {
{0x00000000U, 0x0000000000000000UL},
{0x00000001U, 0x0000000000000001UL},
{0x00000002U, 0x0000000000000004UL},
{0x00000004U, 0x0000000000000010UL},
{0x00000008U, 0x0000000000000040UL},
{0x00000010U, 0x0000000000000100UL},
{0x0000000fU, 0x0000000000000055UL},
{0xffffffffU, 0x5555555555555555UL},
};
bool test_split() {
for(auto& item: pdata) {
auto res = split(item.first);
// All odd bits must be zero
// 0xa == 0b1010
auto mask = 0xaaaaaaaaaaaaaaaaUL;
if (mask & res) {
std::cerr << "FAIL! Have a non-zero odd bit in " << res << std::endl;
return false;
}
TEST_ASSERT_EQUAL(item.second, res);
}
return true;
}
bool test_pack() {
for(auto& item: pdata) {
auto res = pack(item.second);
TEST_ASSERT_EQUAL(item.first, res);
}
return true;
}
const std::vector<std::tuple<uint32_t, uint32_t, uint64_t>> enc_data = {
{0, 0, 0},
{1, 0, 1},
{0, 1, 2},
{1, 1, 3},
{42, 7, 1134},
{0x45812369U, 0xa7112504U, 0x983b42030c271461UL},
{0xffffffffU, 0xffffffffU, 0xffffffffffffffffUL}
};
bool test_encode() {
for (auto& item: enc_data) {
auto& x = std::get<0>(item);
auto& y = std::get<1>(item);
auto& z = std::get<2>(item);
auto res = encode(x, y);
TEST_ASSERT_EQUAL(z, res);
uint32_t rx, ry;
decode(z, rx, ry);
TEST_ASSERT_EQUAL(x, rx);
TEST_ASSERT_EQUAL(y, ry);
}
return true;
}
bool test_shift() {
uint64_t start = 0;
auto res = dec_y(dec_x(inc_y(inc_x(start))));
TEST_ASSERT_EQUAL(start, res);
return true;
}
int main() {
RUN_TEST(test_split);
RUN_TEST(test_pack);
RUN_TEST(test_encode);
RUN_TEST(test_shift);
return 0;
}