def testPluralField(self): field = Bits(length=2) with self.assertRaises(InvalidFormatException): field = Bits() with self.assertRaises(InvalidFormatException): field = Bits(0) with self.assertRaises(InvalidFormatException): field = Bits(1)
def testInt(self): field = Bits(10, value="b0000000000") self.assertTrue(int(field) == 0) field = Byte(value="x0F") self.assertTrue(int(field) == 15) field = Bytes(10, value="o12") self.assertTrue(int(field) == 10)
def testLessThanGreaterThan(self): f1 = Bits(10, value="b0000000000") f2 = Byte(value="xF0") f3 = Nibbles(3, value="x100") self.assertTrue(f3 > f1) self.assertTrue(f2 < f3) self.assertTrue(f1 < 1) self.assertTrue(f1 < "b1") self.assertTrue("b1" > f1) self.assertTrue("b0" < f2) self.assertTrue("b0" >= f1) self.assertTrue("b0" <= f1)
def testXor(self): f1 = Bytes(2, value="b000011000010") f2 = Bits(3, value="b100") f3 = f1 ^ f2 self.assertEqual(f3, "b000011000110") f4 = f2 ^ f1 self.assertEqual(f4, "b000011000110") f5 = f1 ^ f1 self.assertEqual(f5, "b000000000000") f6 = f2 ^ f2 self.assertEqual(f6, "b000") self.assertEqual(f1 ^ "b10000011", "b01000001") self.assertEqual("b10000011" ^ f1, "b01000001") self.assertEqual(f1 ^ 15, "b11001101") self.assertEqual(15 ^ f1, "b11001101")
def testOr(self): f1 = Bytes(2, value="b000011000010") f2 = Bits(3, value="b100") f3 = f1 | f2 self.assertEqual(f3, "b000011000110") f4 = f2 | f1 self.assertEqual(f4, "b000011000110") f5 = f1 | f1 self.assertEqual(f5, "b000011000010") f6 = f2 | f2 self.assertEqual(f6, "b100") self.assertEqual(f1 | "b10000011", "b11000011") self.assertEqual("b10000011" | f1, "b11000011") self.assertEqual(f1 | 15, "b11001111") self.assertEqual(15 | f1, "b11001111")
def testAnd(self): f1 = Bytes(2, value="b000011000010") f2 = Bits(3, value="b100") f3 = f1 & f2 self.assertEqual(f3, "b0") f4 = f2 & f1 self.assertEqual(f4, "b0") f5 = f1 & f1 self.assertEqual(f5, "b11000010") f6 = f2 & f2 self.assertEqual(f6, "b100") self.assertEqual(f1 & Byte(value="x2"), "x2") self.assertEqual(f1 & "b10000010", "b10000010") self.assertEqual("b10000010" & f1, "b10000010") self.assertEqual(f1 & 2, "b10") self.assertEqual(2 & f1, "b10")
def testNonDWordAlignedField(self): field = Bits(7) self.assertTrue(field.length_as_format(Field.Format.Hex), 2) self.assertTrue(field.length_as_format(Field.Format.Dec), 1) self.assertTrue(field.length_as_format(Field.Format.Oct), 3) self.assertTrue(field.length_as_format(Field.Format.Bin), 7)
def testNeg(self): field = Bits(7, value="b0011011") self.assertEqual(-field, "b1100100")
def testInvert(self): field = Bits(7, value="b0011011") self.assertEqual(~field, "b1100100")
def testFormat(self): field = Bits(6, "b101100") self.assertEqual(format(field, "x"), "x2c") self.assertEqual(format(field, "d"), "d44") self.assertEqual(format(field, "o"), "o54") self.assertEqual(format(field, "b"), "b101100")
def testHex(self): self.assertEqual(hex(Bits(10, value="b100")), "0x4")
def testBool_False(self): field = Bits(10, value="b0000000000") self.assertFalse(field)
def testBool_True(self): field = Bits(10, value="b0000000001") self.assertTrue(field)
2) return f"x{val:X}" msg_fmts = { "GET_ADDR": { "id": Nibbles(4, value="x14"), "length": Nibbles( 4, value=lambda id: id.render(fmt=Field.Format.Hex, pad_to_length=4)), "ptr": Bytes(4), "addr": Bits(11), "pad": Bits(3, value="b000"), "crc": Bytes(2, value=lambda ptr: ptr.render(fmt=Field.Format.Hex)[:5]), }, "FILL_KEY": { "id": Nibbles(4, value="x0015"), "ptr": Bytes(3), "addr": Bits(2), "pad": Bits(4, value="b0000"), "crc": Bytes(4, value=lambda pad: pad.render()), }, "WRITE_REGISTER_REQUEST": { "mid": Nibbles(4, value="x0016"), "length": Bytes(2, value=lambda addr, data: calcLength(addr, data)),