rippled
Loading...
Searching...
No Matches
xxhasher_test.cpp
1#include <xrpl/beast/hash/xxhasher.h>
2#include <xrpl/beast/unit_test.h>
3
4namespace beast {
5
7{
8public:
9 void
11 {
12 testcase("Without seed");
13
14 xxhasher hasher{};
15
16 std::string objectToHash{"Hello, xxHash!"};
17 hasher(objectToHash.data(), objectToHash.size());
18
19 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 16042857369214894119ULL);
20 }
21
22 void
24 {
25 testcase("With seed");
26
27 xxhasher hasher{static_cast<std::uint32_t>(102)};
28
29 std::string objectToHash{"Hello, xxHash!"};
30 hasher(objectToHash.data(), objectToHash.size());
31
32 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 14440132435660934800ULL);
33 }
34
35 void
37 {
38 testcase("With two seeds");
39 xxhasher hasher{static_cast<std::uint32_t>(102), static_cast<std::uint32_t>(103)};
40
41 std::string objectToHash{"Hello, xxHash!"};
42 hasher(objectToHash.data(), objectToHash.size());
43
44 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 14440132435660934800ULL);
45 }
46
47 void
49 {
50 testcase("Big object with multiple small updates without seed");
51 xxhasher hasher{};
52
53 std::string objectToHash{"Hello, xxHash!"};
54 for (int i = 0; i < 100; i++)
55 {
56 hasher(objectToHash.data(), objectToHash.size());
57 }
58
59 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 15296278154063476002ULL);
60 }
61
62 void
64 {
65 testcase("Big object with multiple small updates with seed");
66 xxhasher hasher{static_cast<std::uint32_t>(103)};
67
68 std::string objectToHash{"Hello, xxHash!"};
69 for (int i = 0; i < 100; i++)
70 {
71 hasher(objectToHash.data(), objectToHash.size());
72 }
73
74 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 17285302196561698791ULL);
75 }
76
77 void
79 {
80 testcase("Big object with small and big updates without seed");
81 xxhasher hasher{};
82
83 std::string objectToHash{"Hello, xxHash!"};
84 std::string bigObject;
85 for (int i = 0; i < 20; i++)
86 {
87 bigObject += "Hello, xxHash!";
88 }
89 hasher(objectToHash.data(), objectToHash.size());
90 hasher(bigObject.data(), bigObject.size());
91 hasher(objectToHash.data(), objectToHash.size());
92
93 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 1865045178324729219ULL);
94 }
95
96 void
98 {
99 testcase("Big object with small and big updates with seed");
100 xxhasher hasher{static_cast<std::uint32_t>(103)};
101
102 std::string objectToHash{"Hello, xxHash!"};
103 std::string bigObject;
104 for (int i = 0; i < 20; i++)
105 {
106 bigObject += "Hello, xxHash!";
107 }
108 hasher(objectToHash.data(), objectToHash.size());
109 hasher(bigObject.data(), bigObject.size());
110 hasher(objectToHash.data(), objectToHash.size());
111
112 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 16189862915636005281ULL);
113 }
114
115 void
117 {
118 testcase("Big object with one update without seed");
119 xxhasher hasher{};
120
121 std::string objectToHash;
122 for (int i = 0; i < 100; i++)
123 {
124 objectToHash += "Hello, xxHash!";
125 }
126 hasher(objectToHash.data(), objectToHash.size());
127
128 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 15296278154063476002ULL);
129 }
130
131 void
133 {
134 testcase("Big object with one update with seed");
135 xxhasher hasher{static_cast<std::uint32_t>(103)};
136
137 std::string objectToHash;
138 for (int i = 0; i < 100; i++)
139 {
140 objectToHash += "Hello, xxHash!";
141 }
142 hasher(objectToHash.data(), objectToHash.size());
143
144 BEAST_EXPECT(static_cast<xxhasher::result_type>(hasher) == 17285302196561698791ULL);
145 }
146
147 void
149 {
150 testcase("Operator result type doesn't change the internal state");
151 {
152 xxhasher hasher;
153
154 std::string object{"Hello xxhash"};
155 hasher(object.data(), object.size());
156 auto xxhashResult1 = static_cast<xxhasher::result_type>(hasher);
157 auto xxhashResult2 = static_cast<xxhasher::result_type>(hasher);
158
159 BEAST_EXPECT(xxhashResult1 == xxhashResult2);
160 }
161 {
162 xxhasher hasher;
163
164 std::string object;
165 for (int i = 0; i < 100; i++)
166 {
167 object += "Hello, xxHash!";
168 }
169 hasher(object.data(), object.size());
170 auto xxhashResult1 = hasher.operator xxhasher::result_type();
171 auto xxhashResult2 = hasher.operator xxhasher::result_type();
172
173 BEAST_EXPECT(xxhashResult1 == xxhashResult2);
174 }
175 }
176
177 void
191};
192
193BEAST_DEFINE_TESTSUITE(XXHasher, beast_core, beast);
194} // namespace beast
void testBigObjectWithSmallAndBigUpdatesWithoutSeed()
void testBigObjectWithOneUpdateWithSeed()
void testBigObjectWithMultipleSmallUpdatesWithoutSeed()
void testBigObjectWithOneUpdateWithoutSeed()
void testBigObjectWithSmallAndBigUpdatesWithSeed()
void testOperatorResultTypeDoesNotChangeInternalState()
void run() override
Runs the suite.
void testBigObjectWithMultipleSmallUpdatesWithSeed()
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
std::size_t result_type
Definition xxhasher.h:18
T data(T... args)
T size(T... args)