#!/usr/bin/python3
#
# this script is based on the upstream usage example found on README.rst
#

import sys

from crccheck.crc import Crc32, CrcXmodem
from crccheck.checksum import Checksum32

CRC_OK = 2090640218
CHECKSUM_OK = 3735928559
CRCBYTES_OK = bytes(b"\x9e\x8c")
CRCHEX_OK = "9e8c"
CRCINT_OK = 40588

def check(name, value, value_ok):
    if value != value_ok:
        print("ERROR: %s do not match with %s" % (name, str(value_ok)))
        print("%s = %s" % (name, str(value)))
        sys.exit(1)

# quick calculation
data = bytearray.fromhex("DEADBEEF")

crc = Crc32.calc(data)
check("crc", crc, CRC_OK)

checksum = Checksum32.calc(data)
check("checksum", checksum, CHECKSUM_OK)

# process multiple data buffers
data1 = b"Binary string"
data2 = bytes.fromhex("1234567890")
data3 = (0x0, 255, 12, 99)
crcinst = CrcXmodem()
crcinst.process(data1)
crcinst.process(data2)
crcinst.process(data3[1:-1])

crcbytes = crcinst.finalbytes()
check("crcbytes", crcbytes, CRCBYTES_OK)

crchex = crcinst.finalhex()
check("crchex", crchex, CRCHEX_OK)

crcint = crcinst.final()
check("crcint", crcint, CRCINT_OK)

sys.exit(0)
