mirror of
https://github.com/mawww/kakoune.git
synced 2024-11-30 23:37:17 +03:00
hash unit tests.
This commit is contained in:
parent
88e6fd64ad
commit
161ca6d4d1
23
src/hash.cc
23
src/hash.cc
@ -3,6 +3,9 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "unit_tests.hh"
|
||||||
|
#include "assert.hh"
|
||||||
|
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -50,10 +53,12 @@ size_t hash_data(const char* input, size_t len)
|
|||||||
|
|
||||||
const uint8_t* tail = data + nblocks * 4;
|
const uint8_t* tail = data + nblocks * 4;
|
||||||
uint32_t key = 0;
|
uint32_t key = 0;
|
||||||
switch (len & 3)
|
switch (len & 0b11)
|
||||||
{
|
{
|
||||||
case 3: key ^= tail[2] << 16; /* fallthrough */
|
case 3: key ^= tail[2] << 16;
|
||||||
case 2: key ^= tail[1] << 8; /* fallthrough */
|
[[fallthrough]];
|
||||||
|
case 2: key ^= tail[1] << 8;
|
||||||
|
[[fallthrough]];
|
||||||
case 1: key ^= tail[0];
|
case 1: key ^= tail[0];
|
||||||
key *= c1;
|
key *= c1;
|
||||||
key = rotl(key,15);
|
key = rotl(key,15);
|
||||||
@ -67,4 +72,16 @@ size_t hash_data(const char* input, size_t len)
|
|||||||
return hash;
|
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);
|
||||||
|
}};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user