summaryrefslogtreecommitdiff
path: root/prototype/fw/test/crc32_ref.py
blob: 7ac058243559b7a7fb857f0eedbb3e6392bfcffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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))