From 161ca6d4d158ec91b1efeacd6bcf73aab7b41b31 Mon Sep 17 00:00:00 2001 From: Ben Judd Date: Sat, 17 Apr 2021 13:19:35 -0700 Subject: [PATCH] hash unit tests. --- src/hash.cc | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/hash.cc b/src/hash.cc index 342e37a77..058985b38 100644 --- a/src/hash.cc +++ b/src/hash.cc @@ -3,6 +3,9 @@ #include #include +#include "unit_tests.hh" +#include "assert.hh" + namespace Kakoune { @@ -50,10 +53,12 @@ size_t hash_data(const char* input, size_t len) const uint8_t* tail = data + nblocks * 4; uint32_t key = 0; - switch (len & 3) + switch (len & 0b11) { - case 3: key ^= tail[2] << 16; /* fallthrough */ - case 2: key ^= tail[1] << 8; /* fallthrough */ + case 3: key ^= tail[2] << 16; + [[fallthrough]]; + case 2: key ^= tail[1] << 8; + [[fallthrough]]; case 1: key ^= tail[0]; key *= c1; key = rotl(key,15); @@ -67,4 +72,16 @@ size_t hash_data(const char* input, size_t len) return hash; } +UnitTest test_murmer_hash{[] { + { + constexpr char data[] = "Hello, World!"; + kak_assert(hash_data(data, strlen(data)) == 0xf816f95b); + } + { + constexpr char data[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; + kak_assert(hash_data(data, strlen(data)) == 3551113186); + } + kak_assert(hash_data("", 0) == 2572747774); +}}; + }