From cf03215564f1ec9b584bd6b99071fd30a5e8c1c6 Mon Sep 17 00:00:00 2001 From: soerlemans <35037988+soerlemans@users.noreply.github.com> Date: Sun, 25 Jan 2026 13:16:44 +0100 Subject: [PATCH] Added unit tests for `Crc32` hashing. (#2833) * Adding crc32 unit tests. * Fix formatting in crc32 test cases --------- Co-authored-by: soerlemans Co-authored-by: Christoffer Lerno --- test/unit/stdlib/hash/crc32.c3 | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/unit/stdlib/hash/crc32.c3 diff --git a/test/unit/stdlib/hash/crc32.c3 b/test/unit/stdlib/hash/crc32.c3 new file mode 100644 index 000000000..3bef5fa07 --- /dev/null +++ b/test/unit/stdlib/hash/crc32.c3 @@ -0,0 +1,64 @@ +module crc32_test @test; + +import std::hash::crc32; +import std::io; + +fn void test_crc32_empty() +{ + const uint EXPECTED = 0x00000000; + + Crc32 crc; + crc.init(); + crc.update(""); + + uint final = crc.final(); + test::@check(final == EXPECTED, "Actual crc32: 0x%x == 0x%x", final, EXPECTED); +} + +fn void test_crc32_a() +{ + const uint EXPECTED = 0xe8b7be43; + + Crc32 crc; + crc.init(); + crc.updatec('a'); + + uint final = crc.final(); + test::@check(final == EXPECTED, "Actual crc32: 0x%x == 0x%x", final, EXPECTED); +} + +fn void test_crc32_abc() +{ + const uint EXPECTED = 0x352441c2; + + Crc32 crc; + crc.init(); + crc.update("abc"); + + uint final = crc.final(); + test::@check(final == EXPECTED, "Actual crc32: 0x%x == 0x%x", final, EXPECTED); +} + +fn void test_crc32_longer() +{ + const uint EXPECTED = 0x656e49ed; + + Crc32 crc; + crc.init(); + crc.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); + + uint final = crc.final(); + test::@check(final == EXPECTED, "Actual crc32: 0x%x == 0x%x", final, EXPECTED); +} + +fn void test_crc32_alphabet() +{ + const uint EXPECTED = 0x4c2750bd; + + Crc32 crc; + crc.init(); + crc.update("abcdefghijklmnopqrstuvwxyz"); + + uint final = crc.final(); + test::@check(final == EXPECTED, "Actual crc32: 0x%x == 0x%x", final, EXPECTED); +}