rippled
Loading...
Searching...
No Matches
xxhasher_test.cpp
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2025 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/hash/xxhasher.h>
21#include <xrpl/beast/unit_test.h>
22
23namespace beast {
24
26{
27public:
28 void
30 {
31 testcase("Without seed");
32
33 xxhasher hasher{};
34
35 std::string objectToHash{"Hello, xxHash!"};
36 hasher(objectToHash.data(), objectToHash.size());
37
38 BEAST_EXPECT(
39 static_cast<xxhasher::result_type>(hasher) ==
40 16042857369214894119ULL);
41 }
42
43 void
45 {
46 testcase("With seed");
47
48 xxhasher hasher{static_cast<std::uint32_t>(102)};
49
50 std::string objectToHash{"Hello, xxHash!"};
51 hasher(objectToHash.data(), objectToHash.size());
52
53 BEAST_EXPECT(
54 static_cast<xxhasher::result_type>(hasher) ==
55 14440132435660934800ULL);
56 }
57
58 void
60 {
61 testcase("With two seeds");
62 xxhasher hasher{
63 static_cast<std::uint32_t>(102), static_cast<std::uint32_t>(103)};
64
65 std::string objectToHash{"Hello, xxHash!"};
66 hasher(objectToHash.data(), objectToHash.size());
67
68 BEAST_EXPECT(
69 static_cast<xxhasher::result_type>(hasher) ==
70 14440132435660934800ULL);
71 }
72
73 void
75 {
76 testcase("Big object with multiple small updates without seed");
77 xxhasher hasher{};
78
79 std::string objectToHash{"Hello, xxHash!"};
80 for (int i = 0; i < 100; i++)
81 {
82 hasher(objectToHash.data(), objectToHash.size());
83 }
84
85 BEAST_EXPECT(
86 static_cast<xxhasher::result_type>(hasher) ==
87 15296278154063476002ULL);
88 }
89
90 void
92 {
93 testcase("Big object with multiple small updates with seed");
94 xxhasher hasher{static_cast<std::uint32_t>(103)};
95
96 std::string objectToHash{"Hello, xxHash!"};
97 for (int i = 0; i < 100; i++)
98 {
99 hasher(objectToHash.data(), objectToHash.size());
100 }
101
102 BEAST_EXPECT(
103 static_cast<xxhasher::result_type>(hasher) ==
104 17285302196561698791ULL);
105 }
106
107 void
109 {
110 testcase("Big object with small and big updates without seed");
111 xxhasher hasher{};
112
113 std::string objectToHash{"Hello, xxHash!"};
114 std::string bigObject;
115 for (int i = 0; i < 20; i++)
116 {
117 bigObject += "Hello, xxHash!";
118 }
119 hasher(objectToHash.data(), objectToHash.size());
120 hasher(bigObject.data(), bigObject.size());
121 hasher(objectToHash.data(), objectToHash.size());
122
123 BEAST_EXPECT(
124 static_cast<xxhasher::result_type>(hasher) ==
125 1865045178324729219ULL);
126 }
127
128 void
130 {
131 testcase("Big object with small and big updates with seed");
132 xxhasher hasher{static_cast<std::uint32_t>(103)};
133
134 std::string objectToHash{"Hello, xxHash!"};
135 std::string bigObject;
136 for (int i = 0; i < 20; i++)
137 {
138 bigObject += "Hello, xxHash!";
139 }
140 hasher(objectToHash.data(), objectToHash.size());
141 hasher(bigObject.data(), bigObject.size());
142 hasher(objectToHash.data(), objectToHash.size());
143
144 BEAST_EXPECT(
145 static_cast<xxhasher::result_type>(hasher) ==
146 16189862915636005281ULL);
147 }
148
149 void
151 {
152 testcase("Big object with one update without seed");
153 xxhasher hasher{};
154
155 std::string objectToHash;
156 for (int i = 0; i < 100; i++)
157 {
158 objectToHash += "Hello, xxHash!";
159 }
160 hasher(objectToHash.data(), objectToHash.size());
161
162 BEAST_EXPECT(
163 static_cast<xxhasher::result_type>(hasher) ==
164 15296278154063476002ULL);
165 }
166
167 void
169 {
170 testcase("Big object with one update with seed");
171 xxhasher hasher{static_cast<std::uint32_t>(103)};
172
173 std::string objectToHash;
174 for (int i = 0; i < 100; i++)
175 {
176 objectToHash += "Hello, xxHash!";
177 }
178 hasher(objectToHash.data(), objectToHash.size());
179
180 BEAST_EXPECT(
181 static_cast<xxhasher::result_type>(hasher) ==
182 17285302196561698791ULL);
183 }
184
185 void
187 {
188 testcase("Operator result type doesn't change the internal state");
189 {
190 xxhasher hasher;
191
192 std::string object{"Hello xxhash"};
193 hasher(object.data(), object.size());
194 auto xxhashResult1 = static_cast<xxhasher::result_type>(hasher);
195 auto xxhashResult2 = static_cast<xxhasher::result_type>(hasher);
196
197 BEAST_EXPECT(xxhashResult1 == xxhashResult2);
198 }
199 {
200 xxhasher hasher;
201
202 std::string object;
203 for (int i = 0; i < 100; i++)
204 {
205 object += "Hello, xxHash!";
206 }
207 hasher(object.data(), object.size());
208 auto xxhashResult1 = hasher.operator xxhasher::result_type();
209 auto xxhashResult2 = hasher.operator xxhasher::result_type();
210
211 BEAST_EXPECT(xxhashResult1 == xxhashResult2);
212 }
213 }
214
215 void
229};
230
231BEAST_DEFINE_TESTSUITE(XXHasher, beast_core, beast);
232} // 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:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
std::size_t result_type
Definition xxhasher.h:38
T data(T... args)
T size(T... args)