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(
20 static_cast<xxhasher::result_type>(hasher) ==
21 16042857369214894119ULL);
22 }
23
24 void
26 {
27 testcase("With seed");
28
29 xxhasher hasher{static_cast<std::uint32_t>(102)};
30
31 std::string objectToHash{"Hello, xxHash!"};
32 hasher(objectToHash.data(), objectToHash.size());
33
34 BEAST_EXPECT(
35 static_cast<xxhasher::result_type>(hasher) ==
36 14440132435660934800ULL);
37 }
38
39 void
41 {
42 testcase("With two seeds");
43 xxhasher hasher{
44 static_cast<std::uint32_t>(102), static_cast<std::uint32_t>(103)};
45
46 std::string objectToHash{"Hello, xxHash!"};
47 hasher(objectToHash.data(), objectToHash.size());
48
49 BEAST_EXPECT(
50 static_cast<xxhasher::result_type>(hasher) ==
51 14440132435660934800ULL);
52 }
53
54 void
56 {
57 testcase("Big object with multiple small updates without seed");
58 xxhasher hasher{};
59
60 std::string objectToHash{"Hello, xxHash!"};
61 for (int i = 0; i < 100; i++)
62 {
63 hasher(objectToHash.data(), objectToHash.size());
64 }
65
66 BEAST_EXPECT(
67 static_cast<xxhasher::result_type>(hasher) ==
68 15296278154063476002ULL);
69 }
70
71 void
73 {
74 testcase("Big object with multiple small updates with seed");
75 xxhasher hasher{static_cast<std::uint32_t>(103)};
76
77 std::string objectToHash{"Hello, xxHash!"};
78 for (int i = 0; i < 100; i++)
79 {
80 hasher(objectToHash.data(), objectToHash.size());
81 }
82
83 BEAST_EXPECT(
84 static_cast<xxhasher::result_type>(hasher) ==
85 17285302196561698791ULL);
86 }
87
88 void
90 {
91 testcase("Big object with small and big updates without seed");
92 xxhasher hasher{};
93
94 std::string objectToHash{"Hello, xxHash!"};
95 std::string bigObject;
96 for (int i = 0; i < 20; i++)
97 {
98 bigObject += "Hello, xxHash!";
99 }
100 hasher(objectToHash.data(), objectToHash.size());
101 hasher(bigObject.data(), bigObject.size());
102 hasher(objectToHash.data(), objectToHash.size());
103
104 BEAST_EXPECT(
105 static_cast<xxhasher::result_type>(hasher) ==
106 1865045178324729219ULL);
107 }
108
109 void
111 {
112 testcase("Big object with small and big updates with seed");
113 xxhasher hasher{static_cast<std::uint32_t>(103)};
114
115 std::string objectToHash{"Hello, xxHash!"};
116 std::string bigObject;
117 for (int i = 0; i < 20; i++)
118 {
119 bigObject += "Hello, xxHash!";
120 }
121 hasher(objectToHash.data(), objectToHash.size());
122 hasher(bigObject.data(), bigObject.size());
123 hasher(objectToHash.data(), objectToHash.size());
124
125 BEAST_EXPECT(
126 static_cast<xxhasher::result_type>(hasher) ==
127 16189862915636005281ULL);
128 }
129
130 void
132 {
133 testcase("Big object with one update without seed");
134 xxhasher hasher{};
135
136 std::string objectToHash;
137 for (int i = 0; i < 100; i++)
138 {
139 objectToHash += "Hello, xxHash!";
140 }
141 hasher(objectToHash.data(), objectToHash.size());
142
143 BEAST_EXPECT(
144 static_cast<xxhasher::result_type>(hasher) ==
145 15296278154063476002ULL);
146 }
147
148 void
150 {
151 testcase("Big object with one update with seed");
152 xxhasher hasher{static_cast<std::uint32_t>(103)};
153
154 std::string objectToHash;
155 for (int i = 0; i < 100; i++)
156 {
157 objectToHash += "Hello, xxHash!";
158 }
159 hasher(objectToHash.data(), objectToHash.size());
160
161 BEAST_EXPECT(
162 static_cast<xxhasher::result_type>(hasher) ==
163 17285302196561698791ULL);
164 }
165
166 void
168 {
169 testcase("Operator result type doesn't change the internal state");
170 {
171 xxhasher hasher;
172
173 std::string object{"Hello xxhash"};
174 hasher(object.data(), object.size());
175 auto xxhashResult1 = static_cast<xxhasher::result_type>(hasher);
176 auto xxhashResult2 = static_cast<xxhasher::result_type>(hasher);
177
178 BEAST_EXPECT(xxhashResult1 == xxhashResult2);
179 }
180 {
181 xxhasher hasher;
182
183 std::string object;
184 for (int i = 0; i < 100; i++)
185 {
186 object += "Hello, xxHash!";
187 }
188 hasher(object.data(), object.size());
189 auto xxhashResult1 = hasher.operator xxhasher::result_type();
190 auto xxhashResult2 = hasher.operator xxhasher::result_type();
191
192 BEAST_EXPECT(xxhashResult1 == xxhashResult2);
193 }
194 }
195
196 void
210};
211
212BEAST_DEFINE_TESTSUITE(XXHasher, beast_core, beast);
213} // namespace beast
void testBigObjectWithSmallAndBigUpdatesWithoutSeed()
void testBigObjectWithOneUpdateWithSeed()
void testBigObjectWithMultiupleSmallUpdatesWithSeed()
void testBigObjectWithOneUpdateWithoutSeed()
void testBigObjectWithSmallAndBigUpdatesWithSeed()
void testOperatorResultTypeDoesNotChangeInternalState()
void testBigObjectWithMultiupleSmallUpdatesWithoutSeed()
void run() override
Runs the suite.
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
std::size_t result_type
Definition xxhasher.h:19
T data(T... args)
T size(T... args)