#!/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))

