summaryrefslogtreecommitdiffstats
path: root/test/digest_test.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-09-10 20:11:43 -0400
committerDavid Robillard <d@drobilla.net>2021-09-10 20:54:28 -0400
commitb8461860b40f721fd9949ceb5c012c46f914445d (patch)
tree16544ca09a5aa373e205920e7fbb8835282e1b67 /test/digest_test.c
parent497b0a9fb716023b73bb5fc1c8c451da11ba32d8 (diff)
downloadzix-b8461860b40f721fd9949ceb5c012c46f914445d.tar.gz
zix-b8461860b40f721fd9949ceb5c012c46f914445d.tar.bz2
zix-b8461860b40f721fd9949ceb5c012c46f914445d.zip
Replace CRC32 digest with more modern and appropriate algorithms
This makes the hassle of platform-specific code go away, and instead uses portable implementations of relatively standard modern hash algorithms. CRC32 is not great as a hash function anyway, though it is very fast when hardware accelerated.
Diffstat (limited to 'test/digest_test.c')
-rw-r--r--test/digest_test.c131
1 files changed, 91 insertions, 40 deletions
diff --git a/test/digest_test.c b/test/digest_test.c
index 3ed5aff..75dc8ff 100644
--- a/test/digest_test.c
+++ b/test/digest_test.c
@@ -1,5 +1,5 @@
/*
- Copyright 2020 David Robillard <d@drobilla.net>
+ Copyright 2020-2021 David Robillard <d@drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -21,71 +21,122 @@
#include <assert.h>
#include <stddef.h>
+// Just basic smoke tests to ensure the hash functions are reacting to data
+
static void
-test_bytes(void)
+test_digest(void)
{
- static const uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ static const uint8_t data[] = {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
- for (size_t offset = 0; offset < 7; ++offset) {
- const size_t len = 8 - offset;
+ size_t last = 0u;
- uint32_t d = zix_digest_start();
+ for (size_t offset = 0; offset < 7; ++offset) {
+ const size_t len = 8u - offset;
for (size_t i = offset; i < 8; ++i) {
- const uint32_t new_d = zix_digest_add(d, &data[i], 1);
- assert(new_d != d);
- d = new_d;
+ const size_t h = zix_digest(0u, &data[i], len);
+ assert(h != last);
+ last = h;
}
+ }
+}
+
+static void
+test_digest32(void)
+{
+ static const uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+ uint32_t last = 0u;
- assert(zix_digest_add(zix_digest_start(), &data[offset], len) == d);
+ for (size_t offset = 0; offset < 3; ++offset) {
+ for (size_t i = offset; i < 4; ++i) {
+ const uint32_t h = zix_digest32(0u, &data[i], 4);
+ assert(h != last);
+ last = h;
+ }
}
}
static void
-test_64(void)
+test_digest64(void)
{
- static const uint64_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
+ static const uint8_t data[] = {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+
+ uint64_t last = 0u;
- uint32_t d = zix_digest_start();
- for (size_t i = 0; i < 8; ++i) {
- const uint32_t new_d = zix_digest_add_64(d, &data[i], sizeof(uint64_t));
- assert(new_d != d);
- d = new_d;
+ for (size_t offset = 0; offset < 7; ++offset) {
+ for (size_t i = offset; i < 8; ++i) {
+ const uint64_t h = zix_digest64(0u, &data[i], 8);
+ assert(h != last);
+ last = h;
+ }
}
+}
- assert(zix_digest_add(zix_digest_start(), data, 8 * sizeof(uint64_t)) == d);
+static void
+test_digest32_aligned(void)
+{
+ static const uint32_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+ uint32_t last = 0u;
+
+ for (size_t offset = 0; offset < 3; ++offset) {
+ const size_t len = 4u - offset;
+ for (size_t i = offset; i < 4; ++i) {
+ const uint32_t h =
+ zix_digest32_aligned(0u, &data[i], len * sizeof(uint32_t));
+ assert(h != last);
+ last = h;
+ }
+ }
}
static void
-test_ptr(void)
+test_digest64_aligned(void)
{
- static const uint64_t pointees[] = {1, 2, 3, 4, 5, 6, 7, 8};
-
- static const void* data[] = {&pointees[0],
- &pointees[1],
- &pointees[2],
- &pointees[3],
- &pointees[4],
- &pointees[5],
- &pointees[6],
- &pointees[7],
- &pointees[8]};
-
- uint32_t d = zix_digest_start();
- for (size_t i = 0; i < 8; ++i) {
- const uint32_t new_d = zix_digest_add_ptr(d, data[i]);
- assert(new_d != d);
- d = new_d;
+ static const uint64_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+ uint64_t last = 0u;
+
+ for (size_t offset = 0; offset < 3; ++offset) {
+ const size_t len = 4u - offset;
+ for (size_t i = offset; i < 4; ++i) {
+ const uint64_t h =
+ zix_digest64_aligned(0u, &data[i], len * sizeof(uint64_t));
+ assert(h != last);
+ last = h;
+ }
}
+}
+
+static void
+test_digest_aligned(void)
+{
+ static const size_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
- assert(zix_digest_add(zix_digest_start(), data, 8 * sizeof(void*)) == d);
+ size_t last = 0u;
+
+ for (size_t offset = 0; offset < 3; ++offset) {
+ const size_t len = 4u - offset;
+ for (size_t i = offset; i < 4; ++i) {
+ const size_t h = zix_digest_aligned(0u, &data[i], len * sizeof(size_t));
+ assert(h != last);
+ last = h;
+ }
+ }
}
ZIX_PURE_FUNC int
main(void)
{
- test_bytes();
- test_64();
- test_ptr();
+ test_digest32();
+ test_digest64();
+ test_digest();
+
+ test_digest32_aligned();
+ test_digest64_aligned();
+ test_digest_aligned();
return 0;
}