def test_xor_hwcrc16_random(self): dataSize = 24 data1 = random.randint(1, 0xFFFFFF) data2 = random.randint(1, 0xFFFFFF) crcSize = 16 inputPoly = 0x10000 | random.randint(1, 0xFFFF) regInit = random.randint(1, 0xFFFF) xorOut = random.randint(1, 0xFFFF) crcFun = HwCRC() crcFun.setReversed(False) crcFun.setXorOutValue(xorOut) crcFun.setRegisterInitValue(regInit) crc1 = crcFun.calculate2(data1, dataSize, inputPoly, crcSize) crc2 = crcFun.calculate2(data2, dataSize, inputPoly, crcSize) xdata = data1 ^ data2 crcFunXor = HwCRC() crcFunXor.setReversed(False) xcrc = crcFunXor.calculate2(xdata, dataSize, inputPoly, crcSize) xorcrc = crc1 ^ crc2 # print "xor: d1:{:X} {:X} d2:{:X} {:X} r:{:X} xo:{:X} xor:{:X} {:X} {:X}".format( data1, crc1, data2, crc2, regInit, xorOut, xdata, xcrc, xorcrc ) self.assertEquals(xcrc, xorcrc)
def test_calculate_1(self): crcProc = HwCRC() crc = crcProc.calculate2(0b1, 1, 0b1, 1) self.assertEqual(crc, 0b1) crc = crcProc.calculate2(0b0, 1, 0b1, 1) self.assertEqual(crc, 0b0) crc = crcProc.calculate2(0b10, 2, 0b1, 1) self.assertEqual(crc, 0b1) crc = crcProc.calculate2(0b01, 2, 0b1, 1) self.assertEqual(crc, 0b1)
def test_calculate_2(self): crcProc = HwCRC() crc = crcProc.calculate2(0b00, 2, 0b11, 2) self.assertEqual(crc, 0b00) crc = crcProc.calculate2(0b01, 2, 0b11, 2) self.assertEqual(crc, 0b11) crc = crcProc.calculate2(0b10, 2, 0b11, 2) self.assertEqual(crc, 0b01) crc = crcProc.calculate2(0b11, 2, 0b11, 2) self.assertEqual(crc, 0b10)
def test_calculate3_8rev_init_xor(self): data = 0xF0 dataSize = 8 inputPoly = 0x107 crcSize = 8 regInit = 0x0F xorOut = 0xF0 crcProc = HwCRC() crcProc.setRegisterInitValue(regInit) crcProc.setXorOutValue(xorOut) crcProc.setReversed() crc = crcProc.calculate2(data, dataSize, inputPoly, crcSize) self.assertEqual(crc, 240)
def test_calculate_3(self): ## taken from https://en.wikipedia.org/wiki/Cyclic_redundancy_check crcProc = HwCRC() crc = crcProc.calculate2(0b11010011101100, 16, 0b011, 3) ## 0x34EC 0xB self.assertEqual(crc, 0b100) crc = crcProc.calculate2(0b11010011101101, 16, 0b011, 3) ## 0x34ED 0xB self.assertEqual(crc, 0b111) crc = crcProc.calculate2(0b11010011101110, 16, 0b011, 3) ## 0x34EE 0xB self.assertEqual(crc, 0b010) crc = crcProc.calculate2(0b11010011101111, 16, 0b011, 3) ## 0x34EF 0xB self.assertEqual(crc, 0b001) crc = crcProc.calculate2(0b11010011101111, 16, 0b011, 3) ## 0x34EF 0xB self.assertEqual(crc, 0b001) crc = crcProc.calculate2(0b11010111101111, 16, 0b011, 3) ## 0x35EF 0xB self.assertEqual(crc, 0b111) crc = crcProc.calculate2(0b11011011101111, 16, 0b011, 3) ## 0x36EF 0xB self.assertEqual(crc, 0b110) crc = crcProc.calculate2(0b11011111101111, 16, 0b011, 3) ## 0x37EF 0xB self.assertEqual(crc, 0b000)
def test_calculate2_8rev_check(self): ## input data generated from other tests crcProc = HwCRC() crcProc.setReversed() crc = crcProc.calculate2(0x000300, 24, 0x1BF, 8) self.assertEqual(crc, 0x50) ## 80
def test_calculate2_8rev_01(self): ## input data generated from other tests crcProc = HwCRC() crcProc.setReversed() crc = crcProc.calculate2(0x12, 8, 0x1BF, 8) self.assertEqual(crc, 0x3F) ## 63