A Discrete-Event Network Simulator
API
hash-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
21 #include <iomanip>
22 #include <string>
23 
24 #include "ns3/test.h"
25 #include "ns3/hash.h"
26 
41 namespace ns3 {
42 
43 namespace tests {
44 
45 
50 class HashTestCase : public TestCase
51 {
52 public:
58  HashTestCase (const std::string name);
60  virtual ~HashTestCase ();
61 
62 protected:
68  void Check ( const std::string hashName, const uint32_t hash);
74  void Check ( const std::string hashName, const uint64_t hash);
75 
76  std::string key;
77  uint32_t hash32Reference;
78  uint64_t hash64Reference;
79 
80 private:
87  void Check ( const std::string hashName, const int bits, const uint64_t hash);
88  virtual void DoRun (void);
89 
90 }; // class HashTestCase
91 
92 HashTestCase::HashTestCase (const std::string name)
93  : TestCase (name),
94  key ("The quick brown fox jumped over the lazy dogs.")
95 {}
96 
98 {}
99 
100 void
101 HashTestCase::Check ( const std::string hashName, const uint32_t hash)
102 {
103  Check (hashName, 32, hash);
104 }
105 
106 void
107 HashTestCase::Check ( const std::string hashName, const uint64_t hash)
108 {
109  Check (hashName, 64, hash);
110 }
111 
112 void
113 HashTestCase::Check ( std::string hashName, int bits, uint64_t hash)
114 {
115  int w;
116  std::string type;
117  uint64_t hashRef;
118 
119  if (bits == 32)
120  {
121  w = 8;
122  type = "Hash32";
123  hashRef = hash32Reference;
124  }
125  else
126  {
127  w = 16;
128  type = "Hash64";
129  hashRef = hash64Reference;
130  }
131 
132  std::cout << GetName () << "checking "
133  << hashName << " "
134  << bits << "-bit result...";
135  NS_TEST_EXPECT_MSG_EQ (hash, hashRef,
136  hashName << " " << type
137  << " produced " << std::hex << std::setw (w) << hash
138  << ", expected " << std::hex << std::setw (w) << hashRef
139  << std::dec
140  );
141  std::cout << std::hex << std::setw (w) << hash << ", ok"
142  << std::dec << std::endl;
143 }
144 
145 void
147 {}
148 
149 
155 {
156 public:
160  virtual ~DefaultHashTestCase ();
161 
162 private:
163  virtual void DoRun (void);
164 };
165 
167  : HashTestCase ("DefaultHash: ")
168 {}
169 
171 {}
172 
173 void
175 {
176  std::cout << GetName () << "checking with key: \""
177  << key << "\"" << std::endl;
178 
179  hash32Reference = 0x463d70e2; // murmur3(key)
180  Check ( "default", Hash32 (key));
181 
182  hash64Reference = 0xa750412079d53e04ULL;
183  Check ( "default", Hash64 (key));
184 }
185 
191 {
192 public:
194  Fnv1aTestCase ();
196  virtual ~Fnv1aTestCase ();
197 
198 private:
199  virtual void DoRun (void);
200 };
201 
203  : HashTestCase ("Fnv1a: ")
204 {}
205 
207 {}
208 
209 void
211 {
212  Hasher hasher = Hasher ( Create<Hash::Function::Fnv1a> () );
213  hash32Reference = 0xa3fc0d6d; // Fnv1a(key)
214  Check ("FNV1a", hasher.clear ().GetHash32 (key));
215 
216  hash64Reference = 0x88f6cdbe0a31098dULL;
217  Check ( "FNV1a", hasher.clear ().GetHash64 (key));
218 }
219 
220 
226 {
227 public:
229  Murmur3TestCase ();
231  virtual ~Murmur3TestCase ();
232 
233 private:
234  virtual void DoRun (void);
235 };
236 
238  : HashTestCase ("Murmur3: ")
239 {}
240 
242 {}
243 
244 void
246 {
247  Hasher hasher = Hasher ( Create<Hash::Function::Murmur3> () );
248  hash32Reference = 0x463d70e2; // Murmur3(key)
249  Check ( "murmur3", hasher.clear ().GetHash32 (key));
250 
251  hash64Reference = 0xa750412079d53e04ULL;
252  Check ( "murmur3", hasher.clear ().GetHash64 (key));
253 }
254 
255 
269 uint16_t
270 gnu_sum (const char * buffer, const std::size_t size)
271 {
272  const char * p = buffer;
273  const char * const pend = p + size;
274 
275  uint16_t checksum = 0; /* The checksum mod 2^16. */
276 
277  while (p != pend)
278  {
279  checksum = (checksum >> 1) + ((checksum & 1) << 15); // barrel shift
280  checksum += *p++;
281  }
282  return checksum;
283 }
284 
290 uint32_t
291 gnu_sum32 (const char * buffer, const std::size_t size)
292 {
293  uint32_t h = gnu_sum (buffer, size);
294  return (uint32_t)( (h << 16) + h);
295 }
296 
302 uint64_t
303 gnu_sum64 (const char * buffer, const std::size_t size)
304 {
305  uint64_t h = gnu_sum32 (buffer, size);
306  return (uint64_t)( (h << 32) + h);
307 }
308 
314 {
315 public:
319  virtual ~Hash32FunctionPtrTestCase ();
320 
321 private:
322  virtual void DoRun (void);
323 };
324 
326  : HashTestCase ("Hash32FunctionPtr: ")
327 {}
328 
330 {}
331 
332 void
334 {
335  Hasher hasher = Hasher ( Create<Hash::Function::Hash32> (&gnu_sum32) );
336  hash32Reference = 0x41264126; // Hash32FunctionPtr(key)
337  Check ( "gnu_sum32", hasher.clear ().GetHash32 (key));
338 }
339 
345 {
346 public:
350  virtual ~Hash64FunctionPtrTestCase ();
351 
352 private:
353  virtual void DoRun (void);
354 };
355 
357  : HashTestCase ("Hash64FunctionPtr: ")
358 {}
359 
361 {}
362 
363 void
365 {
366  Hasher hasher = Hasher ( Create<Hash::Function::Hash64> (&gnu_sum64) );
367  hash64Reference = 0x4126412641264126ULL; // Hash64FunctionPtr(key)
368  Check ( "gnu_sum64", hasher.clear ().GetHash64 (key));
369 }
370 
376 {
377 public:
381  virtual ~IncrementalTestCase ();
382 
383 private:
384  virtual void DoRun (void);
390  void DoHash (const std::string name, Hasher hasher);
391  std::string key1;
392  std::string key2;
393  std::string key12;
394 };
395 
397  : HashTestCase ("Incremental: ")
398 {}
399 
401 {}
402 
403 void
404 IncrementalTestCase::DoHash (const std::string name, Hasher hasher)
405 {
406  hash32Reference = hasher.clear ().GetHash32 (key12);
407  hasher.clear ().GetHash32 (key1);
408  Check ( name, hasher.GetHash32 (key2));
409 
410  hash64Reference = hasher.clear ().GetHash64 (key12);
411  hasher.clear ().GetHash64 (key1);
412  Check ( name, hasher.GetHash64 (key2));
413 }
414 
415 void
417 {
418  key1 = "The quick brown ";
419  key2 = "Incremental.";
420  key12 = key1 + key2;
421 
422  std::cout << GetName () << "checking with key: "
423  << "\"" << key1 << "\"[" << key1.size () << "] + "
424  << "\"" << key2 << "\"[" << key2.size () << "]" << std::endl;
425  std::cout << GetName () << "equivalent to: "
426  << "\"" << key12 << "\"[" << key12.size () << "]" << std::endl;
427 
428  DoHash ( "default", Hasher ( ) );
429  DoHash ( "murmur3", Hasher ( Create<Hash::Function::Murmur3> () ) );
430  DoHash ( "FNV1a", Hasher ( Create<Hash::Function::Fnv1a> () ) );
431 }
432 
433 
438 class HashTestSuite : public TestSuite
439 {
440 public:
442  HashTestSuite ();
443 };
444 
446  : TestSuite ("hash")
447 {
454 }
455 
461 
462 
463 } // namespace tests
464 
465 } // namespace ns3
Generic Hash function interface.
Definition: hash.h:88
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:239
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash.h:247
Hasher & clear(void)
Restore initial state.
Definition: hash.cc:55
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
std::string GetName(void) const
Definition: test.cc:370
A suite of tests to run.
Definition: test.h:1188
Test default hash on fixed string.
virtual ~DefaultHashTestCase()
Destructor.
virtual void DoRun(void)
Implementation to actually run this TestCase.
FNV hash on fixed string.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual ~Fnv1aTestCase()
Destructor.
Test 32-bit function pointer.
virtual ~Hash32FunctionPtrTestCase()
Destructor.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Test 64-bit function pointer.
virtual ~Hash64FunctionPtrTestCase()
Destructor.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Base class for hash tests.
uint64_t hash64Reference
The 64-bit hash of the reference.
virtual ~HashTestCase()
Destructor.
uint32_t hash32Reference
The 32-bit hash of the reference.
HashTestCase(const std::string name)
Constructor.
std::string key
The reference value to hash.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void Check(const std::string hashName, const uint32_t hash)
Check function.
Hash functions test suite.
Test incremental hashing.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual ~IncrementalTestCase()
Destructor.
void DoHash(const std::string name, Hasher hasher)
Complute the hash test function.
std::string key12
test string
Test Murmur3 hash on fixed string.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual ~Murmur3TestCase()
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:289
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:282
#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:240
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.h:4680
Every class exported by the ns3 library is enclosed in the ns3 namespace.