Skip to content

Instantly share code, notes, and snippets.

@vladimirgamalyan
Created October 3, 2021 04:13
Show Gist options
  • Save vladimirgamalyan/defb2482feefbf5c3ea25b14c557753b to your computer and use it in GitHub Desktop.
Save vladimirgamalyan/defb2482feefbf5c3ea25b14c557753b to your computer and use it in GitHub Desktop.
Test vectors for MurmurHash3
#include "MurmurHash3.h"
#include "doctest.h"
#include <string>
#include <vector>
static void TestString(const std::string& s, uint32_t seed, uint32_t expected)
{
uint32_t hash;
MurmurHash3_x86_32(s.c_str(), s.length(), seed, &hash);
CHECK(hash == expected);
}
static void TestArray(const std::vector<uint8_t>& s, uint32_t seed, uint32_t expected)
{
uint32_t hash;
MurmurHash3_x86_32(s.data(), s.size(), seed, &hash);
CHECK(hash == expected);
}
TEST_CASE("MurmurHash3")
{
TestArray({}, 0, 0); // with zero data and zero seed, everything becomes zero
TestArray({}, 1, 0x514E28B7); // ignores nearly all the math
TestArray({}, 0xffffffff, 0x81F16F39); // make sure your seed uses unsigned 32-bit math
TestArray({ 0xff, 0xff, 0xff, 0xff }, 0, 0x76293B50); // make sure 4-byte chunks use unsigned math
TestArray({ 0x21, 0x43, 0x65, 0x87 }, 0, 0xF55B516B); // Endian order. UInt32 should end up as 0x87654321
TestArray({ 0x21, 0x43, 0x65, 0x87 }, 0x5082EDEE, 0x2362F9DE); // Special seed value eliminates initial key with xor
TestArray({ 0x21, 0x43, 0x65 }, 0, 0x7E4A8634); // Only three bytes. Should end up as 0x654321
TestArray({ 0x21, 0x43 }, 0, 0xA0F7B07A); // Only two bytes. Should end up as 0x4321
TestArray({ 0x21 }, 0, 0x72661CF4); // Only one byte. Should end up as 0x21
TestArray({ 0x00, 0x00, 0x00, 0x00 }, 0, 0x2362F9DE); // Make sure compiler doesn't see zero and convert to null
TestArray({ 0x00, 0x00, 0x00 }, 0, 0x85F0B427);
TestArray({ 0x00, 0x00 }, 0, 0x30F4C306);
TestArray({ 0x00 }, 0, 0x514E28B7);
TestString("", 0, 0); // empty string with zero seed should give zero
TestString("", 1, 0x514E28B7);
TestString("", 0xffffffff, 0x81F16F39); // make sure seed value is handled unsigned
TestString("aaaa", 0x9747b28c, 0x5A97808A); // one full chunk
TestString("aaa", 0x9747b28c, 0x283E0130); // three characters
TestString("aa", 0x9747b28c, 0x5D211726); // two characters
TestString("a", 0x9747b28c, 0x7FA09EA6); // one character
// Endian order within the chunks
TestString("abcd", 0x9747b28c, 0xF0478627); // one full chunk
TestString("abc", 0x9747b28c, 0xC84A62DD);
TestString("ab", 0x9747b28c, 0x74875592);
TestString("a", 0x9747b28c, 0x7FA09EA6);
TestString("Hello, world!", 0x9747b28c, 0x24884CBA);
TestString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0x9747b28c, 0x37405BDC);
TestString("abc", 0, 0xB3DD93FA);
TestString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 0xEE925B90);
TestString("The quick brown fox jumps over the lazy dog", 0x9747b28c, 0x2FA826CD);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment