summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Judd <ben@judd.blue>2021-04-17 13:19:35 -0700
committerBen Judd <ben@judd.blue>2021-04-17 13:19:35 -0700
commit161ca6d4d158ec91b1efeacd6bcf73aab7b41b31 (patch)
treea1a7f95e9cded60020ae82daf3618884de4a8b4f /src
parent88e6fd64adc8f179449074f023ce8900e36d5238 (diff)
hash unit tests.
Diffstat (limited to 'src')
-rw-r--r--src/hash.cc23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/hash.cc b/src/hash.cc
index 342e37a7..058985b3 100644
--- a/src/hash.cc
+++ b/src/hash.cc
@@ -3,6 +3,9 @@
#include <cstdint>
#include <cstring>
+#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);
+}};
+
}