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