From 451904a0c5fae3bd28d35c7128df3a9db1a04b26 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 26 Nov 2020 12:38:52 +0100 Subject: Add CRC32 implementation --- prototype/fw/test/crc32_ref.py | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 prototype/fw/test/crc32_ref.py (limited to 'prototype/fw/test') diff --git a/prototype/fw/test/crc32_ref.py b/prototype/fw/test/crc32_ref.py new file mode 100644 index 0000000..7ac0582 --- /dev/null +++ b/prototype/fw/test/crc32_ref.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import subprocess +import zlib +import os +import itertools +import unittest + +INTERESTING = bytes([0x00, 0x42, 0xff, 0x01, 0x80]) + +class RefdataTest(unittest.TestCase): + def run_test(self, test_data): + ref_val = zlib.crc32(test_data) + + binary = os.environ.get('CRC32_TEST_BINARY', 'build/crc32_test') + + output = subprocess.check_output(binary, input=test_data) + test_val = int(output, 16) & 0xffffffff + + self.assertEqual(test_val, ref_val, f'mismatched results checking input {test_data}') + + def test_empty(self): + self.run_test(b'') + + def test_one_byte(self): + for x in range(256): + self.run_test(bytes([x])) + + def test_two_byte(self): + for x, y in itertools.product(INTERESTING, repeat=2): + self.run_test(bytes([x, y])) + + def test_three_bytes(self): + for x, y, z in itertools.product(INTERESTING, repeat=3): + self.run_test(bytes([x, y, z])) + + def test_four_bytes(self): + for w, x, y, z in itertools.product(INTERESTING, repeat=4): + self.run_test(bytes([w, x, y, z])) + + def test_random_strings(self): + for length in range(3, 24): + for i in range(20): + self.run_test(os.urandom(length)) + -- cgit