mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 03:51:18 +00:00
Add Murmur3 hash
This commit is contained in:
44
test/unit/stdlib/hash/murmur.c3
Normal file
44
test/unit/stdlib/hash/murmur.c3
Normal file
@@ -0,0 +1,44 @@
|
||||
module std::hash::murmur3_test @test;
|
||||
import std::hash::murmur3;
|
||||
|
||||
fn void hash32()
|
||||
{
|
||||
test::eq(0, murmur3::hash32("", 0));
|
||||
test::eq(0x514E28B7, murmur3::hash32("", 1));
|
||||
test::eq(0x81F16F39, murmur3::hash32("", 0xffffffff));
|
||||
test::eq(0x2362F9DE, murmur3::hash32("\0\0\0\0", 0));
|
||||
test::eq(0x5A97808A, murmur3::hash32("aaaa", 0x9747b28c));
|
||||
test::eq(0x283E0130, murmur3::hash32("aaa", 0x9747b28c));
|
||||
test::eq(0x5D211726, murmur3::hash32("aa", 0x9747b28c));
|
||||
test::eq(0x7FA09EA6, murmur3::hash32("a", 0x9747b28c));
|
||||
test::eq(0xF0478627, murmur3::hash32("abcd", 0x9747b28c));
|
||||
test::eq(0xC84A62DD, murmur3::hash32("abc", 0x9747b28c));
|
||||
test::eq(0x74875592, murmur3::hash32("ab", 0x9747b28c));
|
||||
test::eq(0x7FA09EA6, murmur3::hash32("a", 0x9747b28c));
|
||||
test::eq(0x24884CBA, murmur3::hash32("Hello, world!", 0x9747b28c));
|
||||
test::eq(0xD58063C1, murmur3::hash32("ππππππππ", 0x9747b28c));
|
||||
char[256] test = { [0..255] = 'a' };
|
||||
test::eq(0x37405BDC, murmur3::hash32(&test, 0x9747b28c));
|
||||
}
|
||||
|
||||
fn void hash128_64()
|
||||
{
|
||||
test::eq(0, murmur3::hash128_64("", 0));
|
||||
test::eq(0x51622daa78f835834610abe56eff5cb5, murmur3::hash128_64("", 1));
|
||||
test::eq(0x857421121ee6446b6af1df4d9d3bc9ec, murmur3::hash128_64("", 0xffffffff));
|
||||
test::eq(0x589623161cf526f1cfa0f7ddd84c76bc, murmur3::hash128_64("\0\0\0\0", 0));
|
||||
test::eq(0xf66e73e07751664edbcf7463becf7e04, murmur3::hash128_64("xxxxxxxxxxxxxxxxxxxxxxxxxxxx", 123));
|
||||
test::eq(0xf19732fdd373c3f5421c8c738743acad, murmur3::hash128_64("Hello, world!", 123));
|
||||
test::eq(0x79200aeeb9546c79ca47f42bf86d4004, murmur3::hash128_64("Hello, world!", 321));
|
||||
}
|
||||
|
||||
fn void hash128_32()
|
||||
{
|
||||
test::eq(0, murmur3::hash128_32("", 0));
|
||||
test::eq(0x26f3e79926f3e79926f3e799fedc5245, murmur3::hash128_32("", 123));
|
||||
test::eq(0x989d49f7989d49f7989d49f7051e08a9, murmur3::hash128_32("", 0xFFFFFFFF));
|
||||
test::eq(0x9e5178409e5178409e517840cc066f1f, murmur3::hash128_32("\0\0\0\0", 0));
|
||||
test::eq(0x1fec60474cf929d378825a165e40bab2, murmur3::hash128_32("xxxxxxxxxxxxxxxxxxxxxxxxxxxx", 123));
|
||||
test::eq(0x9e37c886a41621625a1aacd761c9129e, murmur3::hash128_32("Hello, world!", 123));
|
||||
test::eq(0xa7170f0f045880c5c26c4193d5fbdcb3, murmur3::hash128_32("Hello, world!", 321));
|
||||
}
|
||||
@@ -6,8 +6,7 @@ fn void test_sha256_empty()
|
||||
Sha256 sha;
|
||||
sha.init();
|
||||
sha.update("");
|
||||
|
||||
test::@check(sha.final() == x"E3B0C442 98FC1C14 9AFBF4C8 996FB924 27AE41E4 649B934C A495991B 7852B855");
|
||||
test::eq(sha.final(), x"E3B0C442 98FC1C14 9AFBF4C8 996FB924 27AE41E4 649B934C A495991B 7852B855");
|
||||
}
|
||||
|
||||
fn void test_sha256_abc()
|
||||
@@ -16,7 +15,7 @@ fn void test_sha256_abc()
|
||||
sha.init();
|
||||
sha.update("abc");
|
||||
|
||||
test::@check(sha.final() == x"BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD");
|
||||
test::eq(sha.final(), x"BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD");
|
||||
}
|
||||
|
||||
fn void test_sha256_longer()
|
||||
@@ -24,7 +23,7 @@ fn void test_sha256_longer()
|
||||
Sha256 sha;
|
||||
sha.init();
|
||||
sha.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
|
||||
test::@check(sha.final() == x"59F109D9 533B2B70 E7C3B814 A2BD218F 78EA5D37 14455BC6 7987CF0D 664399CF");
|
||||
test::eq(sha.final(), x"59F109D9 533B2B70 E7C3B814 A2BD218F 78EA5D37 14455BC6 7987CF0D 664399CF");
|
||||
}
|
||||
|
||||
fn void test_sha256_multi_update_permute()
|
||||
@@ -37,7 +36,7 @@ fn void test_sha256_multi_update_permute()
|
||||
usz i = 0;
|
||||
for (; i < input.len / step; i++) sha.update(input[i * step : step]);
|
||||
if (i * step < input.len) sha.update(input[i * step..]);
|
||||
test::@check(sha.final() == x"b527293dfb70dcce37e593f4c43e1b81909615722bad041b90b8df22bebd00a0", "Mismatch for step %d", step);
|
||||
test::eq(sha.final(), x"b527293dfb70dcce37e593f4c43e1b81909615722bad041b90b8df22bebd00a0");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user