-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashes_test.cc
105 lines (90 loc) · 2.73 KB
/
hashes_test.cc
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 2021 Simon (psyomn) Symeonidis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "ds/hashes.h"
#include <vector>
std::vector<std::byte> sample_bytes_1(void);
std::vector<std::byte> sample_bytes_2(void);
std::vector<std::byte> sample_bytes_1(void) {
return std::vector<std::byte>{
std::byte(0x01),
std::byte(0x02),
std::byte(0x03),
std::byte(0x04),
std::byte(0x05),
};
}
std::vector<std::byte> sample_bytes_2(void) {
return std::vector<std::byte>{
std::byte(0x01),
std::byte(0x10),
std::byte(0xaa),
std::byte(0xbb),
std::byte(0xcc),
std::byte(0xff),
};
}
TEST(hashes, djb2) {
const auto bytes_1 = sample_bytes_1();
const auto result_1 = psy::ds::djb2(bytes_1);
EXPECT_NE(result_1, 0);
EXPECT_EQ(result_1, 210588810932);
const auto bytes_2 = sample_bytes_2();
const auto result_2 = psy::ds::djb2(bytes_2);
EXPECT_NE(result_2, result_1);
EXPECT_EQ(result_2, 6949453571238);
}
TEST(hashes, sdbm) {
const auto bytes_1 = sample_bytes_1();
const auto result_1 = psy::ds::sdbm(bytes_1);
const auto bytes_2 = sample_bytes_2();
const auto result_2 = psy::ds::sdbm(bytes_2);
EXPECT_NE(result_1, 0);
EXPECT_EQ(result_1, 71598627968528387U);
EXPECT_EQ(result_2, 12367020746941822611U);
EXPECT_NE(result_1, result_2);
}
TEST(hashes, loselose) {
const auto bytes_1 = sample_bytes_1();
const auto result_1 = psy::ds::loselose(bytes_1);
EXPECT_NE(result_1, 0);
EXPECT_EQ(result_1, 15);
}
TEST(hashes, fletcher16) {
{
const auto bytes = std::vector<std::byte> { std::byte(1), std::byte(2) };
const auto result = psy::ds::fletcher16(bytes);
EXPECT_EQ(result, 0x0403);
}
{
const auto bytes = std::vector<std::byte> {
std::byte(10), std::byte(25), std::byte(32),
std::byte(99), std::byte(100), std::byte(44),
};
const auto result = psy::ds::fletcher16(bytes);
EXPECT_EQ(result, 0x5636);
}
}
TEST(hashes, fletcher32) {
{
const auto bytes = sample_bytes_1();
const auto result = psy::ds::fletcher32(bytes);
EXPECT_EQ(result, 0x23000f);
}
{
const auto bytes = sample_bytes_2();
const auto result = psy::ds::fletcher32(bytes);
EXPECT_EQ(result, 0x7c60341);
}
}