-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest.cpp
90 lines (77 loc) · 2.54 KB
/
test.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
#include <gtest/gtest.h>
#include "hashids.h"
TEST(Encode, Saltless) {
hashidsxx::Hashids hash;
ASSERT_EQ(hash.encode({123}), "Mj3");
}
TEST(Decode, Saltless) {
hashidsxx::Hashids hash;
std::vector<uint64_t> output = hash.decode("Mj3");
ASSERT_EQ(output.size(), 1);
ASSERT_EQ(output[0], 123);
}
TEST(Encode, Salt) {
hashidsxx::Hashids hash("salt");
ASSERT_EQ(hash.encode({123}), "0lb");
}
TEST(Decode, Salt) {
hashidsxx::Hashids hash("salt");
std::vector<uint64_t> output = hash.decode("0lb");
ASSERT_EQ(output.size(), 1);
ASSERT_EQ(output[0], 123);
}
TEST(Encode, SaltMinLength) {
hashidsxx::Hashids hash("salt", 15);
std::string output = hash.encode(1,2,3,4);
ASSERT_GE(output.size(), 15);
ASSERT_EQ(output, "7P231QiYuXU53Y9");
}
TEST(Decode, SaltMinLength) {
hashidsxx::Hashids hash("salt", 15);
std::vector<uint64_t> output = hash.decode("7P231QiYuXU53Y9");
ASSERT_EQ(output.size(), 4);
ASSERT_EQ(output[0], 1);
ASSERT_EQ(output[1], 2);
ASSERT_EQ(output[2], 3);
ASSERT_EQ(output[3], 4);
// we should be able to decode shorter hashes with the same salt regardless of the length..
output = hash.decode("0lb");
ASSERT_EQ(output.size(), 1);
ASSERT_EQ(output[0], 123);
}
TEST(Encode, SaltMinLengthAlphabet) {
const std::string alphabet("abcdefghijklmnopqrstuvwxyz");
hashidsxx::Hashids hash("salt", 16, alphabet);
std::string output = hash.encode({123456789});
ASSERT_GE(output.size(), 16);
ASSERT_EQ(output, "oavlpogkzrxrkpxd");
// we should NOT find any characters other than our alphabet in the output
ASSERT_EQ(output.find_first_not_of(alphabet), std::string::npos);
}
TEST(Decode, SaltMinLengthAlphabet) {
const std::string alphabet("abcdefghijklmnopqrstuvwxyz");
hashidsxx::Hashids hash("salt", 16, alphabet);
std::vector<uint64_t> output = hash.decode("oavlpogkzrxrkpxd");
ASSERT_EQ(output.size(), 1);
ASSERT_EQ(output[0], 123456789);
}
TEST(Encode, Hex) {
hashidsxx::Hashids hash("this is my salt");
std::string output = hash.encodeHex("abc");
ASSERT_EQ(output, "5RzW");
output = hash.encodeHex("f000000000000000f");
ASSERT_EQ(output, "8OlB1X6RjYuzPWW");
}
TEST(Decode, Hex) {
hashidsxx::Hashids hash("this is my salt");
std::string output = hash.decodeHex("5RzW");
ASSERT_EQ(output, "abc");
output = hash.decodeHex("8OlB1X6RjYuzPWW");
ASSERT_EQ(output, "f000000000000000f");
}
int main(int argc, char** argv) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";
::testing::FLAGS_gtest_shuffle = true;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
};