A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hash-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#include "ns3/hash.h"
10#include "ns3/test.h"
11
12#include <iomanip>
13#include <string>
14
15/**
16 * \file
17 * \ingroup core-tests
18 * \ingroup hash
19 * \ingroup hash-tests
20 * Hash test suite
21 */
22
23/**
24 * \ingroup core-tests
25 * \ingroup hash
26 * \defgroup hash-tests Hash test suite
27 */
28
29namespace ns3
30{
31
32namespace tests
33{
34
35/**
36 * \ingroup hash-tests
37 * Base class for hash tests
38 */
39class HashTestCase : public TestCase
40{
41 public:
42 /**
43 * Constructor
44 *
45 * \param [in] name reference name
46 */
47 HashTestCase(const std::string name);
48 /** Destructor. */
49 ~HashTestCase() override;
50
51 protected:
52 /**
53 * Check function
54 * \param [in] hashName the name of the hash
55 * \param [in] hash the hash value
56 */
57 void Check(const std::string hashName, const uint32_t hash);
58 /**
59 * Check function
60 * \param [in] hashName the name of the hash
61 * \param [in] hash the hash value
62 */
63 void Check(const std::string hashName, const uint64_t hash);
64
65 std::string key; //!< The reference value to hash.
66 uint32_t hash32Reference; //!< The 32-bit hash of the reference.
67 uint64_t hash64Reference; //!< The 64-bit hash of the reference.
68
69 private:
70 /**
71 * Check function
72 * \param [in] hashName the name of the hash
73 * \param [in] bits the number of bits
74 * \param [in] hash the hash value
75 */
76 void Check(const std::string hashName, const int bits, const uint64_t hash);
77 void DoRun() override;
78
79}; // class HashTestCase
80
81HashTestCase::HashTestCase(const std::string name)
82 : TestCase(name),
83 key("The quick brown fox jumped over the lazy dogs.")
84{
85}
86
90
91void
92HashTestCase::Check(const std::string hashName, const uint32_t hash)
93{
94 Check(hashName, 32, hash);
95}
96
97void
98HashTestCase::Check(const std::string hashName, const uint64_t hash)
99{
100 Check(hashName, 64, hash);
101}
102
103void
104HashTestCase::Check(std::string hashName, int bits, uint64_t hash)
105{
106 int w;
107 std::string type;
108 uint64_t hashRef;
109
110 if (bits == 32)
111 {
112 w = 8;
113 type = "Hash32";
114 hashRef = hash32Reference;
115 }
116 else
117 {
118 w = 16;
119 type = "Hash64";
120 hashRef = hash64Reference;
121 }
122
123 std::cout << GetName() << "checking " << hashName << " " << bits << "-bit result...";
125 hashRef,
126 hashName << " " << type << " produced " << std::hex << std::setw(w)
127 << hash << ", expected " << std::hex << std::setw(w) << hashRef
128 << std::dec);
129 std::cout << std::hex << std::setw(w) << hash << ", ok" << std::dec << std::endl;
130}
131
132void
136
137/**
138 * \ingroup hash-tests
139 * Test default hash on fixed string
140 */
142{
143 public:
144 /** Constructor. */
146 /** Destructor. */
147 ~DefaultHashTestCase() override;
148
149 private:
150 void DoRun() override;
151};
152
157
161
162void
164{
165 std::cout << GetName() << "checking with key: \"" << key << "\"" << std::endl;
166
167 hash32Reference = 0x463d70e2; // murmur3(key)
168 Check("default", Hash32(key));
169
170 hash64Reference = 0xa750412079d53e04ULL;
171 Check("default", Hash64(key));
172}
173
174/**
175 * \ingroup hash-tests
176 * FNV hash on fixed string
177 */
179{
180 public:
181 /** Constructor. */
183 /** Destructor. */
184 ~Fnv1aTestCase() override;
185
186 private:
187 void DoRun() override;
188};
189
191 : HashTestCase("Fnv1a: ")
192{
193}
194
198
199void
201{
203 hash32Reference = 0xa3fc0d6d; // Fnv1a(key)
204 Check("FNV1a", hasher.clear().GetHash32(key));
205
206 hash64Reference = 0x88f6cdbe0a31098dULL;
207 Check("FNV1a", hasher.clear().GetHash64(key));
208}
209
210/**
211 * \ingroup hash-tests
212 * Test Murmur3 hash on fixed string
213 */
215{
216 public:
217 /** Constructor. */
219 /** Destructor. */
220 ~Murmur3TestCase() override;
221
222 private:
223 void DoRun() override;
224};
225
227 : HashTestCase("Murmur3: ")
228{
229}
230
234
235void
237{
239 hash32Reference = 0x463d70e2; // Murmur3(key)
240 Check("murmur3", hasher.clear().GetHash32(key));
241
242 hash64Reference = 0xa750412079d53e04ULL;
243 Check("murmur3", hasher.clear().GetHash64(key));
244}
245
246/**
247 * \ingroup hash-tests
248 * Simple hash function based on the GNU sum program.
249 *
250 * 16-bit checksum algorithm. See
251 * http://svnweb.freebsd.org/base/stable/9/usr.bin/cksum/sum1.c?view=markup
252 *
253 * Used to test Hash32Function_ptr/Hash64Function_ptr
254 *
255 * \param [in,out] buffer The data to hash.
256 * \param [in] size The buffer size.
257 * \returns The checksum of the buffer contents.
258 */
259uint16_t
260gnu_sum(const char* buffer, const std::size_t size)
261{
262 const char* p = buffer;
263 const char* const pend = p + size;
264
265 uint16_t checksum = 0; /* The checksum mod 2^16. */
266
267 while (p != pend)
268 {
269 checksum = (checksum >> 1) + ((checksum & 1) << 15); // barrel shift
270 checksum += *p++;
271 }
272 return checksum;
273}
274
275/**
276 * \ingroup hash-tests
277 * A 32-bit hash function, based on gnu_sum().
278 * \copydetails gnu_sum()
279 */
281gnu_sum32(const char* buffer, const std::size_t size)
282{
283 uint32_t h = gnu_sum(buffer, size);
284 return (uint32_t)((h << 16) + h);
285}
286
287/**
288 * \ingroup hash-tests
289 * A 64-bit hash function, base on gnu_sum().
290 * \copydetails gnu_sum()
291 */
292uint64_t
293gnu_sum64(const char* buffer, const std::size_t size)
294{
295 uint64_t h = gnu_sum32(buffer, size);
296 return (uint64_t)((h << 32) + h);
297}
298
299/**
300 * \ingroup hash-tests
301 * Test 32-bit function pointer
302 */
304{
305 public:
306 /** Constructor. */
308 /** Destructor. */
310
311 private:
312 void DoRun() override;
313};
314
319
323
324void
326{
328 hash32Reference = 0x41264126; // Hash32FunctionPtr(key)
329 Check("gnu_sum32", hasher.clear().GetHash32(key));
330}
331
332/**
333 * \ingroup hash-tests
334 * Test 64-bit function pointer
335 */
337{
338 public:
339 /** Constructor. */
341 /** Destructor. */
343
344 private:
345 void DoRun() override;
346};
347
352
356
357void
359{
361 hash64Reference = 0x4126412641264126ULL; // Hash64FunctionPtr(key)
362 Check("gnu_sum64", hasher.clear().GetHash64(key));
363}
364
365/**
366 * \ingroup hash-tests
367 * Test incremental hashing
368 */
370{
371 public:
372 /** Constructor. */
374 /** Destructor. */
375 ~IncrementalTestCase() override;
376
377 private:
378 void DoRun() override;
379 /**
380 * Complute the hash test function
381 * \param name the hash name
382 * \param hasher the hash function
383 */
384 void DoHash(const std::string name, Hasher hasher);
385 std::string key1; //!< test string
386 std::string key2; //!< test string
387 std::string key12; //!< test string
388};
389
394
398
399void
400IncrementalTestCase::DoHash(const std::string name, Hasher hasher)
401{
403 hasher.clear().GetHash32(key1);
404 Check(name, hasher.GetHash32(key2));
405
407 hasher.clear().GetHash64(key1);
408 Check(name, hasher.GetHash64(key2));
409}
410
411void
413{
414 key1 = "The quick brown ";
415 key2 = "Incremental.";
416 key12 = key1 + key2;
417
418 std::cout << GetName() << "checking with key: "
419 << "\"" << key1 << "\"[" << key1.size() << "] + "
420 << "\"" << key2 << "\"[" << key2.size() << "]" << std::endl;
421 std::cout << GetName() << "equivalent to: "
422 << "\"" << key12 << "\"[" << key12.size() << "]" << std::endl;
423
424 DoHash("default", Hasher());
427}
428
429/**
430 * \ingroup hash-tests
431 * Hash functions test suite
432 */
434{
435 public:
436 /** Constructor. */
438};
439
450
451/**
452 * \ingroup hash-tests
453 * HashTestSuite instance variable.
454 */
456
457} // namespace tests
458
459} // namespace ns3
Generic Hash function interface.
Definition hash.h:76
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition hash.h:225
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition hash.h:232
Hasher & clear()
Restore initial state.
Definition hash.cc:45
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
std::string GetName() const
Definition test.cc:367
A suite of tests to run.
Definition test.h:1267
Test default hash on fixed string.
void DoRun() override
Implementation to actually run this TestCase.
~DefaultHashTestCase() override
Destructor.
FNV hash on fixed string.
void DoRun() override
Implementation to actually run this TestCase.
~Fnv1aTestCase() override
Destructor.
Test 32-bit function pointer.
void DoRun() override
Implementation to actually run this TestCase.
~Hash32FunctionPtrTestCase() override
Destructor.
Test 64-bit function pointer.
~Hash64FunctionPtrTestCase() override
Destructor.
void DoRun() override
Implementation to actually run this TestCase.
Base class for hash tests.
void DoRun() override
Implementation to actually run this TestCase.
uint64_t hash64Reference
The 64-bit hash of the reference.
uint32_t hash32Reference
The 32-bit hash of the reference.
~HashTestCase() override
Destructor.
HashTestCase(const std::string name)
Constructor.
std::string key
The reference value to hash.
void Check(const std::string hashName, const uint32_t hash)
Check function.
Hash functions test suite.
Test incremental hashing.
void DoRun() override
Implementation to actually run this TestCase.
~IncrementalTestCase() override
Destructor.
void DoHash(const std::string name, Hasher hasher)
Complute the hash test function.
Test Murmur3 hash on fixed string.
void DoRun() override
Implementation to actually run this TestCase.
~Murmur3TestCase() override
Destructor.
uint32_t gnu_sum32(const char *buffer, const std::size_t size)
A 32-bit hash function, based on gnu_sum().
uint64_t gnu_sum64(const char *buffer, const std::size_t size)
A 64-bit hash function, base on gnu_sum().
static HashTestSuite g_hashTestSuite
HashTestSuite instance variable.
uint16_t gnu_sum(const char *buffer, const std::size_t size)
Simple hash function based on the GNU sum program.
uint64_t Hash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer, using the default hash function.
Definition hash.h:269
uint32_t Hash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer, using the default hash function.
Definition hash.h:263
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.