--- a/src/core/model/hash-fnv.cc Tue Nov 13 16:31:48 2012 -0800
+++ b/src/core/model/hash-fnv.cc Tue Nov 13 16:32:31 2012 -0800
@@ -41,7 +41,7 @@
namespace HashImplNS {
- namespace FnvNS {
+ namespace Fnv1aNS {
/*************************************************
** class FnvHashImplementation
@@ -152,7 +152,7 @@
*
* Use fully qualified type so this define works outside this scope //PDB
*/
-#define FNV0_32_INIT ((HashImplNS::FnvNS::Fnv32_t)0)
+#define FNV0_32_INIT ((HashImplNS::Fnv1aNS::Fnv32_t)0)
/*
@@ -169,7 +169,7 @@
*
* Use fully qualified type so this define works outside this scope //PDB
*/
-#define FNV1_32_INIT ((HashImplNS::FnvNS::Fnv32_t)0x811c9dc5)
+#define FNV1_32_INIT ((HashImplNS::Fnv1aNS::Fnv32_t)0x811c9dc5)
#define FNV1_32A_INIT FNV1_32_INIT
@@ -202,10 +202,10 @@
* Use fully qualified type so this define works outside this scope //PDB
*/
#if defined(HAVE_64BIT_LONG_LONG)
-#define FNV0_64_INIT ((HashImplNS::FnvNS::Fnv64_t)0)
+#define FNV0_64_INIT ((HashImplNS::Fnv1aNS::Fnv64_t)0)
#else /* HAVE_64BIT_LONG_LONG */
extern const Fnv64_t fnv0_64_init;
-#define FNV0_64_INIT (HashImplNS::FnvNS::fnv0_64_init)
+#define FNV0_64_INIT (HashImplNS::Fnv1aNS::fnv0_64_init)
#endif /* HAVE_64BIT_LONG_LONG */
@@ -222,7 +222,7 @@
* NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
*/
#if defined(HAVE_64BIT_LONG_LONG)
-#define FNV1_64_INIT ((HashImplNS::FnvNS::Fnv64_t)0xcbf29ce484222325ULL)
+#define FNV1_64_INIT ((HashImplNS::Fnv1aNS::Fnv64_t)0xcbf29ce484222325ULL)
#define FNV1A_64_INIT FNV1_64_INIT
#else /* HAVE_64BIT_LONG_LONG */
extern const fnv1_64_init;
@@ -338,7 +338,7 @@
/*
* 32 bit magic FNV-1a prime
*/
-#define FNV_32_PRIME ((HashImplNS::FnvNS::Fnv32_t)0x01000193)
+#define FNV_32_PRIME ((HashImplNS::Fnv1aNS::Fnv32_t)0x01000193)
/*
@@ -497,7 +497,7 @@
* 64 bit magic FNV-1a prime
*/
#if defined(HAVE_64BIT_LONG_LONG)
-#define FNV_64_PRIME ((HashImplNS::FnvNS::Fnv64_t)0x100000001b3ULL)
+#define FNV_64_PRIME ((HashImplNS::Fnv1aNS::Fnv64_t)0x100000001b3ULL)
#else /* HAVE_64BIT_LONG_LONG */
#define FNV_64_PRIME_LOW ((unsigned long)0x1b3) /* lower bits of FNV prime */
#define FNV_64_PRIME_SHIFT (8) /* top FNV prime shift above 2^32 */
@@ -723,21 +723,21 @@
//-----------------------------------------------------------------------------
-} // namespace FnvNS
+} // namespace Fnv1aNS
Hash::Hash32_t
-Fnv::GetHash32 (const char * buffer, const size_t size)
+Fnv1a::GetHash32 (const char * buffer, const size_t size)
{
- uint32_t result = FnvNS::fnv_32a_buf ((void *)buffer, size, FNV1_32A_INIT);
+ uint32_t result = Fnv1aNS::fnv_32a_buf ((void *)buffer, size, FNV1_32A_INIT);
return result;
}
Hash::Hash64_t
-Fnv::GetHash64 (const char * buffer, const size_t size)
+Fnv1a::GetHash64 (const char * buffer, const size_t size)
{
- uint64_t result = FnvNS::fnv_64a_buf ((void *)buffer, size, FNV1A_64_INIT);
+ uint64_t result = Fnv1aNS::fnv_64a_buf ((void *)buffer, size, FNV1A_64_INIT);
return result;
}
--- a/src/core/model/hash-fnv.h Tue Nov 13 16:31:48 2012 -0800
+++ b/src/core/model/hash-fnv.h Tue Nov 13 16:32:31 2012 -0800
@@ -30,10 +30,10 @@
/**
* \ingroup hash
*
- * \brief Fnv hash function implementation
+ * \brief Fnv1a hash function implementation
*
*/
-class Fnv : public HashImplementation
+class Fnv1a : public HashImplementation
{
public:
/**
@@ -65,7 +65,7 @@
SEED = 0x8BADF00D // Ate bad food
};
-}; // class Fnv
+}; // class Fnv1a
} // namespace HashImplNS
--- a/src/core/model/hash.cc Tue Nov 13 16:31:48 2012 -0800
+++ b/src/core/model/hash.cc Tue Nov 13 16:32:31 2012 -0800
@@ -33,11 +33,13 @@
Hash::Hash ()
{
m_impl = Create <HashImplNS::Murmur3> ();
+ NS_ASSERT (m_impl != 0);
}
Hash::Hash (Ptr<HashImplementation> hp)
: m_impl(hp)
{
+ NS_ASSERT (m_impl != 0);
}
--- a/src/core/model/hash.h Tue Nov 13 16:31:48 2012 -0800
+++ b/src/core/model/hash.h Tue Nov 13 16:32:31 2012 -0800
@@ -23,6 +23,7 @@
#include <string>
+#include "ns3/assert.h"
#include "ns3/ptr.h"
#include "ns3/simple-ref-count.h"
@@ -263,6 +264,7 @@
Hash::Hash32_t
Hash::GetHash32 (const char * buffer, const size_t size)
{
+ NS_ASSERT (m_impl != 0);
return m_impl->GetHash32 (buffer, size);
}
@@ -270,6 +272,7 @@
Hash::Hash64_t
Hash::GetHash64 (const char * buffer, const size_t size)
{
+ NS_ASSERT (m_impl != 0);
return m_impl->GetHash64 (buffer, size);
}
@@ -277,6 +280,7 @@
Hash::Hash32_t
Hash::GetHash32 (const std::string s)
{
+ NS_ASSERT (m_impl != 0);
return m_impl->GetHash32 (s.c_str (), s.size ());
}
@@ -284,6 +288,7 @@
Hash::Hash64_t
Hash::GetHash64 (const std::string s)
{
+ NS_ASSERT (m_impl != 0);
return m_impl->GetHash64 (s.c_str (), s.size ());
}
--- a/src/core/test/hash-test-suite.cc Tue Nov 13 16:31:48 2012 -0800
+++ b/src/core/test/hash-test-suite.cc Tue Nov 13 16:32:31 2012 -0800
@@ -41,7 +41,7 @@
};
HashFoxTestCase::HashFoxTestCase ()
- : TestCase ("Check Hash on a known string")
+ : TestCase ("Check default Hash on a known string")
{
}
@@ -69,6 +69,93 @@
);
}
+//----------------------------
+//
+// Test FNV hash on fixed string
+
+class HashFnv1aTestCase : public TestCase
+{
+public:
+ HashFnv1aTestCase ();
+ virtual ~HashFnv1aTestCase ();
+private:
+ virtual void DoRun (void);
+};
+
+HashFnv1aTestCase::HashFnv1aTestCase ()
+ : TestCase ("Check FNV1A Hash on a known string")
+{
+}
+
+HashFnv1aTestCase::~HashFnv1aTestCase ()
+{
+}
+
+void
+HashFnv1aTestCase::DoRun (void)
+{
+ std::string key("The quick brown fnv1a.");
+ Hash hasher = Hash ( Create<HashImplNS::Fnv1a> () );
+
+ uint32_t h32r = 0x5735855b; // FNV1A(key)
+ uint32_t h32 = hasher.GetHash32 (key);
+ NS_TEST_ASSERT_MSG_EQ (h32, h32r,
+ "Hash32 produced " << std::hex << std::setw ( 8) << h32
+ << ", expected " << std::hex << std::setw ( 8) << h32r
+ );
+
+ uint64_t h64r = 0x6fb0aea4ad83c27b;
+ uint64_t h64 = hasher.GetHash64 (key);
+ NS_TEST_ASSERT_MSG_EQ (h64, h64r,
+ "Hash64 produced " << std::hex << std::setw (16) << h64
+ << ", expected " << std::hex << std::setw (16) << h64r
+ );
+}
+
+
+//----------------------------
+//
+// Test Murmur3 hash on fixed string
+
+class HashMurmur3TestCase : public TestCase
+{
+public:
+ HashMurmur3TestCase ();
+ virtual ~HashMurmur3TestCase ();
+private:
+ virtual void DoRun (void);
+};
+
+HashMurmur3TestCase::HashMurmur3TestCase ()
+ : TestCase ("Check Murmur3 Hash on a known string")
+{
+}
+
+HashMurmur3TestCase::~HashMurmur3TestCase ()
+{
+}
+
+void
+HashMurmur3TestCase::DoRun (void)
+{
+ std::string key("The quick brown murmur3.");
+ Hash hasher = Hash ( Create<HashImplNS::Murmur3> () );
+
+ uint32_t h32r = 0xe8a2d100; // Murmur3(key)
+ uint32_t h32 = hasher.GetHash32 (key);
+ NS_TEST_ASSERT_MSG_EQ (h32, h32r,
+ "Hash32 produced " << std::hex << std::setw ( 8) << h32
+ << ", expected " << std::hex << std::setw ( 8) << h32r
+ );
+
+ uint64_t h64r = 0x95373d091a691071;
+ uint64_t h64 = hasher.GetHash64 (key);
+ NS_TEST_ASSERT_MSG_EQ (h64, h64r,
+ "Hash64 produced " << std::hex << std::setw (16) << h64
+ << ", expected " << std::hex << std::setw (16) << h64r
+ );
+}
+
//----------------------------
//
@@ -84,6 +171,8 @@
: TestSuite ("hash", UNIT)
{
AddTestCase (new HashFoxTestCase);
+ AddTestCase (new HashFnv1aTestCase);
+ AddTestCase (new HashMurmur3TestCase);
}
static HashTestSuite g_hashTestSuite;