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 // end of class HashTestCase
80};
81
82HashTestCase::HashTestCase(const std::string name)
83 : TestCase(name),
84 key("The quick brown fox jumped over the lazy dogs.")
85{
86}
87
91
92void
93HashTestCase::Check(const std::string hashName, const uint32_t hash)
94{
95 Check(hashName, 32, hash);
96}
97
98void
99HashTestCase::Check(const std::string hashName, const uint64_t hash)
100{
101 Check(hashName, 64, hash);
102}
103
104void
105HashTestCase::Check(std::string hashName, int bits, uint64_t hash)
106{
107 int w;
108 std::string type;
109 uint64_t hashRef;
110
111 if (bits == 32)
112 {
113 w = 8;
114 type = "Hash32";
115 hashRef = hash32Reference;
116 }
117 else
118 {
119 w = 16;
120 type = "Hash64";
121 hashRef = hash64Reference;
122 }
123
124 std::cout << GetName() << "checking " << hashName << " " << bits << "-bit result...";
126 hashRef,
127 hashName << " " << type << " produced " << std::hex << std::setw(w)
128 << hash << ", expected " << std::hex << std::setw(w) << hashRef
129 << std::dec);
130 std::cout << std::hex << std::setw(w) << hash << ", ok" << std::dec << std::endl;
131}
132
133void
137
138/**
139 * @ingroup hash-tests
140 * Test default hash on fixed string
141 */
143{
144 public:
145 /** Constructor. */
147 /** Destructor. */
148 ~DefaultHashTestCase() override;
149
150 private:
151 void DoRun() override;
152};
153
158
162
163void
165{
166 std::cout << GetName() << "checking with key: \"" << key << "\"" << std::endl;
167
168 hash32Reference = 0x463d70e2; // murmur3(key)
169 Check("default", Hash32(key));
170
171 hash64Reference = 0xa750412079d53e04ULL;
172 Check("default", Hash64(key));
173}
174
175/**
176 * @ingroup hash-tests
177 * FNV hash on fixed string
178 */
180{
181 public:
182 /** Constructor. */
184 /** Destructor. */
185 ~Fnv1aTestCase() override;
186
187 private:
188 void DoRun() override;
189};
190
192 : HashTestCase("Fnv1a: ")
193{
194}
195
199
200void
202{
204 hash32Reference = 0xa3fc0d6d; // Fnv1a(key)
205 Check("FNV1a", hasher.clear().GetHash32(key));
206
207 hash64Reference = 0x88f6cdbe0a31098dULL;
208 Check("FNV1a", hasher.clear().GetHash64(key));
209}
210
211/**
212 * @ingroup hash-tests
213 * Test Murmur3 hash on fixed string
214 */
216{
217 public:
218 /** Constructor. */
220 /** Destructor. */
221 ~Murmur3TestCase() override;
222
223 private:
224 void DoRun() override;
225};
226
228 : HashTestCase("Murmur3: ")
229{
230}
231
235
236void
238{
240 hash32Reference = 0x463d70e2; // Murmur3(key)
241 Check("murmur3", hasher.clear().GetHash32(key));
242
243 hash64Reference = 0xa750412079d53e04ULL;
244 Check("murmur3", hasher.clear().GetHash64(key));
245}
246
247/**
248 * @ingroup hash-tests
249 * Simple hash function based on the GNU sum program.
250 *
251 * 16-bit checksum algorithm. See
252 * http://svnweb.freebsd.org/base/stable/9/usr.bin/cksum/sum1.c?view=markup
253 *
254 * Used to test Hash32Function_ptr/Hash64Function_ptr
255 *
256 * @param [in,out] buffer The data to hash.
257 * @param [in] size The buffer size.
258 * @returns The checksum of the buffer contents.
259 */
260uint16_t
261gnu_sum(const char* buffer, const std::size_t size)
262{
263 const char* p = buffer;
264 const char* const pend = p + size;
265
266 uint16_t checksum = 0; /* The checksum mod 2^16. */
267
268 while (p != pend)
269 {
270 checksum = (checksum >> 1) + ((checksum & 1) << 15); // barrel shift
271 checksum += *p++;
272 }
273 return checksum;
274}
275
276/**
277 * @ingroup hash-tests
278 * A 32-bit hash function, based on gnu_sum().
279 * @copydetails gnu_sum()
280 */
282gnu_sum32(const char* buffer, const std::size_t size)
283{
284 uint32_t h = gnu_sum(buffer, size);
285 return (uint32_t)((h << 16) + h);
286}
287
288/**
289 * @ingroup hash-tests
290 * A 64-bit hash function, base on gnu_sum().
291 * @copydetails gnu_sum()
292 */
293uint64_t
294gnu_sum64(const char* buffer, const std::size_t size)
295{
296 uint64_t h = gnu_sum32(buffer, size);
297 return (uint64_t)((h << 32) + h);
298}
299
300/**
301 * @ingroup hash-tests
302 * Test 32-bit function pointer
303 */
305{
306 public:
307 /** Constructor. */
309 /** Destructor. */
311
312 private:
313 void DoRun() override;
314};
315
320
324
325void
327{
329 hash32Reference = 0x41264126; // Hash32FunctionPtr(key)
330 Check("gnu_sum32", hasher.clear().GetHash32(key));
331}
332
333/**
334 * @ingroup hash-tests
335 * Test 64-bit function pointer
336 */
338{
339 public:
340 /** Constructor. */
342 /** Destructor. */
344
345 private:
346 void DoRun() override;
347};
348
353
357
358void
360{
362 hash64Reference = 0x4126412641264126ULL; // Hash64FunctionPtr(key)
363 Check("gnu_sum64", hasher.clear().GetHash64(key));
364}
365
366/**
367 * @ingroup hash-tests
368 * Test incremental hashing
369 */
371{
372 public:
373 /** Constructor. */
375 /** Destructor. */
376 ~IncrementalTestCase() override;
377
378 private:
379 void DoRun() override;
380 /**
381 * Complute the hash test function
382 * @param name the hash name
383 * @param hasher the hash function
384 */
385 void DoHash(const std::string name, Hasher hasher);
386 std::string key1; //!< test string
387 std::string key2; //!< test string
388 std::string key12; //!< test string
389};
390
395
399
400void
401IncrementalTestCase::DoHash(const std::string name, Hasher hasher)
402{
404 hasher.clear().GetHash32(key1);
405 Check(name, hasher.GetHash32(key2));
406
408 hasher.clear().GetHash64(key1);
409 Check(name, hasher.GetHash64(key2));
410}
411
412void
414{
415 key1 = "The quick brown ";
416 key2 = "Incremental.";
417 key12 = key1 + key2;
418
419 std::cout << GetName() << "checking with key: ";
420 std::cout << "\"" << key1 << "\"[" << key1.size() << "] + ";
421 std::cout << "\"" << key2 << "\"[" << key2.size() << "]" << std::endl;
422
423 std::cout << GetName() << "equivalent to: ";
424 std::cout << "\"" << key12 << "\"[" << key12.size() << "]" << std::endl;
425
426 DoHash("default", Hasher());
429}
430
431/**
432 * @ingroup hash-tests
433 * Hash functions test suite
434 */
436{
437 public:
438 /** Constructor. */
440};
441
452
453/**
454 * @ingroup hash-tests
455 * HashTestSuite instance variable.
456 */
458
459} // namespace tests
460
461} // 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:227
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition hash.h:234
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:271
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:265
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.