libcruft-util/test/hash/xxhash.cpp

61 lines
1.8 KiB
C++
Raw Normal View History

2016-11-17 18:35:06 +11:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2016-11-17 18:35:06 +11:00
*
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
2016-11-17 18:35:06 +11:00
*/
#include <cruft/util/tap.hpp>
2016-11-17 18:35:06 +11:00
#include <cruft/util/hash/xxhash.hpp>
2016-11-17 18:35:06 +11:00
///////////////////////////////////////////////////////////////////////////////
std::vector<uint8_t>
operator""_u8s (const char *str, size_t len)
{
std::vector<uint8_t> res;
res.resize (len);
std::copy_n (str, len, std::begin (res));
return res;
}
///////////////////////////////////////////////////////////////////////////////
2016-11-17 18:35:06 +11:00
int
main (int, char **)
{
cruft::TAP::logger tap;
2016-11-17 18:35:06 +11:00
static const struct {
uint32_t hash32;
uint64_t hash64;
unsigned seed;
std::vector<uint8_t> data;
const char *msg;
2016-11-17 18:35:06 +11:00
} TESTS[] = {
{ 0x02cc5d05, 0xef46db3751d8e999, 0, ""_u8s, "empty string" },
{ 0x0b2cb792, 0xd5afba1336a3be4b, 1, ""_u8s, "empty string" },
{ 0x550d7456, 0xd24ec4f1a98c6e5b, 0, "a"_u8s, "single a" },
{ 0xf514706f, 0xdec2bc81c3cd46c6, 1, "a"_u8s, "single a" },
{ 0x32d153ff, 0x44bc2cf5ad770999, 0, "abc"_u8s, "abc" },
{ 0xaa3da8ff, 0xbea9ca8199328908, 1, "abc"_u8s, "abc" },
2016-11-17 18:35:06 +11:00
{ 0x54ca7e46, 0x892a0760a6343391, 0x1234,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+"_u8s,
2016-11-17 18:35:06 +11:00
"long alphabet" }
};
for (const auto &t: TESTS) {
cruft::hash::xxhash32 h32 (t.seed);
cruft::hash::xxhash64 h64 (t.seed);
2016-11-17 18:35:06 +11:00
2021-04-13 16:05:08 +10:00
tap.expect_eq (h32 (t.data), t.hash32, "xxhash32 {:s}", t.msg);
tap.expect_eq (h64 (t.data), t.hash64, "xxhash64 {:s}, seed {}", t.msg, t.seed);
2016-11-17 18:35:06 +11:00
}
return tap.status ();
}