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 protected:
67  void Check ( const std::string hashName, const uint32_t hash);
73  void Check ( const std::string hashName, const uint64_t hash);
74 
75  std::string key;
76  uint32_t hash32Reference;
77  uint64_t hash64Reference;
78 
79 private:
86  void Check ( const std::string hashName, const int bits, const uint64_t hash);
87  virtual void DoRun (void);
88 
89 }; // class HashTestCase
90 
91 HashTestCase::HashTestCase (const std::string name)
92  : TestCase (name),
93  key ("The quick brown fox jumped over the lazy dogs.")
94 {
95 }
96 
98 {
99 }
100 
101 void
102 HashTestCase::Check ( const std::string hashName, const uint32_t hash)
103 {
104  Check (hashName, 32, hash);
105 }
106 
107 void
108 HashTestCase::Check ( const std::string hashName, const uint64_t hash)
109 {
110  Check (hashName, 64, hash);
111 }
112 
113 void
114 HashTestCase::Check ( std::string hashName, int bits, uint64_t hash)
115 {
116  int w;
117  std::string type;
118  uint64_t hashRef;
119 
120  if (bits == 32)
121  {
122  w = 8;
123  type = "Hash32";
124  hashRef = hash32Reference;
125  }
126  else
127  {
128  w = 16;
129  type = "Hash64";
130  hashRef = hash64Reference;
131  }
132 
133  std::cout << GetName () << "checking "
134  << hashName << " "
135  << bits << "-bit result...";
136  NS_TEST_EXPECT_MSG_EQ (hash, hashRef,
137  hashName << " " << type
138  << " produced " << std::hex << std::setw (w) << hash
139  << ", expected " << std::hex << std::setw (w) << hashRef
140  << std::dec
141  );
142  std::cout << std::hex << std::setw (w) << hash << ", ok"
143  << std::dec << std::endl;
144 }
145 
146 void
148 {
149 }
150 
151 
157 {
158 public:
162  virtual ~DefaultHashTestCase ();
163 private:
164  virtual void DoRun (void);
165 };
166 
168  : HashTestCase ("DefaultHash: ")
169 {
170 }
171 
173 {
174 }
175 
176 void
178 {
179  std::cout << GetName () << "checking with key: \""
180  << key << "\"" << std::endl;
181 
182  hash32Reference = 0x463d70e2; // murmur3(key)
183  Check ( "default", Hash32 (key));
184 
185  hash64Reference = 0xa750412079d53e04ULL;
186  Check ( "default", Hash64 (key));
187 }
188 
194 {
195 public:
197  Fnv1aTestCase ();
199  virtual ~Fnv1aTestCase ();
200 private:
201  virtual void DoRun (void);
202 };
203 
205  : HashTestCase ("Fnv1a: ")
206 {
207 }
208 
210 {
211 }
212 
213 void
215 {
216  Hasher hasher = Hasher ( Create<Hash::Function::Fnv1a> () );
217  hash32Reference = 0xa3fc0d6d; // Fnv1a(key)
218  Check ("FNV1a", hasher.clear ().GetHash32 (key));
219 
220  hash64Reference = 0x88f6cdbe0a31098dULL;
221  Check ( "FNV1a", hasher.clear ().GetHash64 (key));
222 }
223 
224 
230 {
231 public:
233  Murmur3TestCase ();
235  virtual ~Murmur3TestCase ();
236 private:
237  virtual void DoRun (void);
238 };
239 
241  : HashTestCase ("Murmur3: ")
242 {
243 }
244 
246 {
247 }
248 
249 void
251 {
252  Hasher hasher = Hasher ( Create<Hash::Function::Murmur3> () );
253  hash32Reference = 0x463d70e2; // Murmur3(key)
254  Check ( "murmur3", hasher.clear ().GetHash32 (key));
255 
256  hash64Reference = 0xa750412079d53e04ULL;
257  Check ( "murmur3", hasher.clear ().GetHash64 (key));
258 }
259 
260 
274 uint16_t
275 gnu_sum (const char * buffer, const std::size_t size)
276 {
277  const char * p = buffer;
278  const char * const pend = p + size;
279 
280  uint16_t checksum = 0; /* The checksum mod 2^16. */
281 
282  while (p != pend)
283  {
284  checksum = (checksum >> 1) + ((checksum & 1) << 15); // barrel shift
285  checksum += *p++;
286  }
287  return checksum;
288 }
289 
295 uint32_t
296 gnu_sum32 (const char * buffer, const std::size_t size)
297 {
298  uint32_t h = gnu_sum (buffer, size);
299  return (uint32_t)( (h << 16) + h);
300 }
301 
307 uint64_t
308 gnu_sum64 (const char * buffer, const std::size_t size)
309 {
310  uint64_t h = gnu_sum32 (buffer, size);
311  return (uint64_t)( (h << 32) + h);
312 }
313 
319 {
320 public:
324  virtual ~Hash32FunctionPtrTestCase ();
325 private:
326  virtual void DoRun (void);
327 };
328 
330  : HashTestCase ("Hash32FunctionPtr: ")
331 {
332 }
333 
335 {
336 }
337 
338 void
340 {
341  Hasher hasher = Hasher ( Create<Hash::Function::Hash32> (&gnu_sum32) );
342  hash32Reference = 0x41264126; // Hash32FunctionPtr(key)
343  Check ( "gnu_sum32", hasher.clear ().GetHash32 (key));
344 }
345 
351 {
352 public:
356  virtual ~Hash64FunctionPtrTestCase ();
357 private:
358  virtual void DoRun (void);
359 };
360 
362  : HashTestCase ("Hash64FunctionPtr: ")
363 {
364 }
365 
367 {
368 }
369 
370 void
372 {
373  Hasher hasher = Hasher ( Create<Hash::Function::Hash64> (&gnu_sum64) );
374  hash64Reference = 0x4126412641264126ULL; // Hash64FunctionPtr(key)
375  Check ( "gnu_sum64", hasher.clear ().GetHash64 (key));
376 }
377 
383 {
384 public:
388  virtual ~IncrementalTestCase ();
389 private:
390  virtual void DoRun (void);
396  void DoHash (const std::string name, Hasher hasher);
397  std::string key1;
398  std::string key2;
399  std::string key12;
400 };
401 
403  : HashTestCase ("Incremental: ")
404 {
405 }
406 
408 {
409 }
410 
411 void
412 IncrementalTestCase::DoHash (const std::string name, Hasher hasher)
413 {
414  hash32Reference = hasher.clear ().GetHash32 (key12);
415  hasher.clear ().GetHash32 (key1);
416  Check ( name, hasher.GetHash32 (key2));
417 
418  hash64Reference = hasher.clear ().GetHash64 (key12);
419  hasher.clear ().GetHash64 (key1);
420  Check ( name, hasher.GetHash64 (key2));
421 }
422 
423 void
425 {
426  key1 = "The quick brown ";
427  key2 = "Incremental.";
428  key12 = key1 + key2;
429 
430  std::cout << GetName () << "checking with key: "
431  << "\"" << key1 << "\"[" << key1.size () << "] + "
432  << "\"" << key2 << "\"[" << key2.size () << "]" << std::endl;
433  std::cout << GetName () << "equivalent to: "
434  << "\"" << key12 << "\"[" << key12.size () << "]" << std::endl;
435 
436  DoHash ( "default", Hasher ( ) );
437  DoHash ( "murmur3", Hasher ( Create<Hash::Function::Murmur3> () ) );
438  DoHash ( "FNV1a", Hasher ( Create<Hash::Function::Fnv1a> () ) );
439 }
440 
441 
446 class HashTestSuite : public TestSuite
447 {
448 public:
450  HashTestSuite ();
451 };
452 
454  : TestSuite ("hash")
455 {
462 }
463 
469 
470 
471  } // namespace tests
472 
473 } // namespace ns3
virtual ~Hash64FunctionPtrTestCase()
Destructor.
Test incremental hashing.
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:239
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual ~Hash32FunctionPtrTestCase()
Destructor.
A suite of tests to run.
Definition: test.h:1342
#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:285
Base class for hash tests.
encapsulates test code
Definition: test.h:1155
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().
virtual ~HashTestCase()
Destructor.
Test 64-bit function pointer.
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::string key1
test string
virtual ~IncrementalTestCase()
Destructor.
uint64_t hash64Reference
The 64-bit hash of the reference.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
FNV hash on fixed string.
std::string key2
test string
static HashTestSuite g_hashTestSuite
HashTestSuite instance variable.
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:283
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::string key12
test string
virtual ~DefaultHashTestCase()
Destructor.
Hasher & clear(void)
Restore initial state.
Definition: hash.cc:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoRun(void)
Implementation to actually run this TestCase.
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:276
virtual ~Murmur3TestCase()
Destructor.
uint16_t gnu_sum(const char *buffer, const std::size_t size)
Simple hash function based on the GNU sum program.
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash.h:247
HashTestCase(const std::string name)
Constructor.
std::string key
The reference value to hash.
std::string GetName(void) const
Definition: test.cc:370
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t hash32Reference
The 32-bit hash of the reference.
Test default hash on fixed string.
Test 32-bit function pointer.
Test Murmur3 hash on fixed string.
void DoHash(const std::string name, Hasher hasher)
Complute the hash test function.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Generic Hash function interface.
Definition: hash.h:87
virtual ~Fnv1aTestCase()
Destructor.
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.