diff crc32-test.c @ 0:422835bc1aca

*: checkin
author Paper <paper@tflc.us>
date Mon, 09 Feb 2026 01:15:00 -0500
parents
children 90cb48b87dcc
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc32-test.c	Mon Feb 09 01:15:00 2026 -0500
@@ -0,0 +1,37 @@
+#include "crc32i.h"
+#include <stdio.h>
+
+/* Test implementations and make sure they agree with each other */
+int crc32_test(void)
+{
+	/* Force alignment :) */
+	static const __attribute__((__aligned__(CRC32_MAX_ALIGNMENT))) unsigned char testdata[1024] =
+#define DOUBLE(x) x x
+DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE("\x01\x02\x04\x08\x10\x20\x40\x80")))))))
+#undef DOUBLE
+	;
+	static const crc32_r_spec crc[] = {
+		crc32c_r,
+		crc32qw_r,
+		crc32x86_vpclmulqdq_r
+	};
+	size_t i;
+
+	uint32_t crcc = crc32(testdata, sizeof(testdata));
+
+	for (i = 0; i < ARRAY_SIZE(crc); i++) {
+		uint32_t thiscrc = ~crc[i](0xFFFFFFFF, testdata, sizeof(testdata));
+
+		if (thiscrc != crcc) {
+			fprintf(stderr, "%zu, mismatch: %08x, %08x\n", i, crcc, thiscrc);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+int main(void)
+{
+	return -crc32_test();
+}