diff --git a/tests/hash_test.cpp b/tests/hash_test.cpp index ca7cf30..03aea25 100644 --- a/tests/hash_test.cpp +++ b/tests/hash_test.cpp @@ -31,17 +31,18 @@ struct HashTest { uint32_t crc32; uint32_t adler32; + uint32_t murmur2a; const char* input; }; const HashTest s_hashTest[] = { - { 0, 1, "" }, - { 0xe8b7be43, 0x00620062, "a" }, - { 0x9e83486d, 0x012600c4, "ab" }, - { 0xc340daab, 0x06060205, "abvgd" }, - { 0x07642fe2, 0x020a00d6, "1389" }, - { 0x26d75737, 0x04530139, "555333" }, + { 0, 1, 0, "" }, + { 0xe8b7be43, 0x00620062, 0x803888b, "a" }, + { 0x9e83486d, 0x012600c4, 0x618515af, "ab" }, + { 0xc340daab, 0x06060205, 0x94e3dc4d, "abvgd" }, + { 0x07642fe2, 0x020a00d6, 0xe602fc07, "1389" }, + { 0x26d75737, 0x04530139, 0x58d37863, "555333" }, }; TEST_CASE("HashCrc32", "") @@ -77,3 +78,73 @@ TEST_CASE("HashAdler32", "") REQUIRE(test.adler32 == hash.end() ); } } + +/*----------------------------------------------------------------------------- +// MurmurHash2A, by Austin Appleby +// +// This is a variant of MurmurHash2 modified to use the Merkle-Damgard +// construction. Bulk speed should be identical to Murmur2, small-key speed +// will be 10%-20% slower due to the added overhead at the end of the hash. +// +// This variant fixes a minor issue where null keys were more likely to +// collide with each other than expected, and also makes the function +// more amenable to incremental implementations. +*/ + +#define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } + +uint32_t MurmurHash2A ( const void * key, int len, uint32_t seed ) +{ + const uint32_t m = 0x5bd1e995; + const int r = 24; + uint32_t l = len; + + const unsigned char * data = (const unsigned char *)key; + + uint32_t h = seed; + + while(len >= 4) + { + uint32_t k = *(uint32_t*)data; + + mmix(h,k); + + data += 4; + len -= 4; + } + + uint32_t t = 0; + + switch(len) + { + case 3: t ^= data[2] << 16; + case 2: t ^= data[1] << 8; + case 1: t ^= data[0]; + }; + + mmix(h,t); + mmix(h,l); + + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; +} + +TEST_CASE("HashMurmur2A", "") +{ + uint32_t seed = 0; + + for (uint32_t ii = 0; ii < BX_COUNTOF(s_hashTest); ++ii) + { + const HashTest& test = s_hashTest[ii]; + + bx::HashMurmur2A hash; + hash.begin(seed); + hash.add(test.input, bx::strLen(test.input) ); + REQUIRE(test.murmur2a == hash.end() ); + + REQUIRE(test.murmur2a == MurmurHash2A(test.input, bx::strLen(test.input), seed) ); + } +}