27 #define TEST_SIZE 1024*1024 + 7 34 auto seed = std::chrono::system_clock::now().time_since_epoch().count();
35 std::default_random_engine generator(seed);
36 std::uniform_int_distribution<uint8_t> dist;
38 for(
auto & it : buffer)
44 const std::vector<uint8_t> & testdata,
45 size_t offset,
size_t blocksize,
47 int hashsize,
int bitness)
49 std::cout <<
"Testing " << hashsize <<
"-bit x" << bitness <<
" hash," 50 <<
" offset " << offset
51 <<
" blocksize " << blocksize <<
" ... ";
56 hasher.
update(testdata.data(), offset);
60 blocksize = testdata.size() - offset;
67 if( (done + todo) > testdata.size() )
68 todo = testdata.size() - done;
70 hasher.
update(testdata.data() + done, todo);
79 std::cout <<
"FAILED\n";
82 ss <<
"Mismatch: " << hashsize <<
"-bit hash," 83 <<
" offset " << offset
84 <<
" blocksize " << blocksize;
86 throw std::runtime_error(ss.str());
104 const void * testdata_ptr = testdata.data();
105 const size_t testdata_size = testdata.size();
106 const int testdata_size_int =
static_cast<int>(testdata_size);
119 mh32_x32.
update(testdata_ptr, testdata_size);
120 mh32_x64.
update(testdata_ptr, testdata_size);
121 mh64_x64.
update(testdata_ptr, testdata_size);
122 mh128_x64.
update(testdata_ptr, testdata_size);
124 auto bph_32_x32 = mh32_x32.
finalize();
125 auto bph_32_x64 = mh32_x64.
finalize();
126 auto bph_64_x64 = mh64_x64.
finalize();
127 auto bph_128_x64 = mh128_x64.
finalize();
143 std::cout <<
"32-bit hash, x32\n";
144 std::cout <<
"Reference: " << ref_32_x32_str <<
"\n";
145 std::cout <<
" BPHash: " << bph_32_x32_str <<
"\n";
147 if(ref_32_x32 != bph_32_x32)
148 throw std::runtime_error(
"Mismatch on 32-bit x64 hash");
150 std::cout <<
"32-bit hash, x64\n";
151 std::cout <<
"Reference: " << ref_32_x64_str <<
"\n";
152 std::cout <<
" BPHash: " << bph_32_x64_str <<
"\n";
154 if(ref_32_x64 != bph_32_x64)
155 throw std::runtime_error(
"Mismatch on 32-bit x64 hash");
157 std::cout <<
"64-bit hash, x64\n";
158 std::cout <<
"Reference: " << ref_64_x64_str <<
"\n";
159 std::cout <<
" BPHash: " << bph_64_x64_str <<
"\n";
161 if(ref_64_x64 != bph_64_x64)
162 throw std::runtime_error(
"Mismatch on 64-bit x64 hash");
164 std::cout <<
"128-bit hash, x64\n";
165 std::cout <<
"Reference: " << ref_128_x64_str <<
"\n";
166 std::cout <<
" BPHash: " << bph_128_x64_str <<
"\n";
168 if(ref_128_x64 != bph_128_x64)
169 throw std::runtime_error(
"Mismatch on 128-bit x64 hash");
173 for(
size_t i = 0; i <= 18; i++)
174 for(
size_t j = 0; j <= 1150; j += 23)
176 test_offset(mh32_x32, testdata, i, j, ref_32_x32, 32, 32);
177 test_offset(mh32_x64, testdata, i, j, ref_32_x64, 32, 64);
178 test_offset(mh64_x64, testdata, i, j, ref_64_x64, 64, 64);
179 test_offset(mh128_x64, testdata, i, j, ref_128_x64, 128, 64);
185 catch(
const std::exception & ex)
187 std::cout <<
"!!! Failed test: " << ex.what() <<
"\n\n";
MurmurHash reference implementation (header)
static void test_offset(detail::HashImpl &hasher, const std::vector< uint8_t > &testdata, size_t offset, size_t blocksize, const HashValue &reference, int hashsize, int bitness)
Implementation of MurmurHash3 32-bit x64 hash.
virtual void update(void const *data, size_t nbytes)
Add some data to the hash.
Implementation of MurmurHash3 64-bit x64 hash.
HashValue truncate_hash(const HashValue &hash, size_t nbytes)
Truncate the hash to a given number of bytes.
std::string hash_to_string(const HashValue &hash)
Return a string representation of a hash.
A class that hashes objects (header)
MurmurHash3 32-bit x32 hash (header)
virtual HashValue finalize(void)
Finish hashing and report the hash.
virtual HashValue finalize(void)=0
Finish hashing and report the hash.
void MurmurHash3_x86_32(const void *key, int len, uint32_t seed, void *out)
std::vector< uint8_t > HashValue
Stores the value of a hash.
MurmurHash3 128-bit x64 hash (header)
virtual HashValue finalize(void)
Finish hashing and report the hash.
MurmurHash3 32-bit x64 hash (header)
virtual void update(void const *data, size_t nbytes)=0
Add some data to the hash.
virtual void reset(void)=0
Zero out the hash.
virtual HashValue finalize(void)
Finish hashing and report the hash.
Implementation of MurmurHash3 32-bit x32 hash.
Implementation of MurmurHash3 128-bit x64 hash.
Base class for a hash implementation.
void MurmurHash3_x64_128(const void *key, const int len, const uint32_t seed, void *out)
virtual void update(void const *data, size_t nbytes)
Add some data to the hash.
MurmurHash3 64-bit x64 hash (header)
virtual HashValue finalize(void)
Finish hashing and report the hash.
static void random_fill(std::vector< uint8_t > &buffer)