Пример #1
0
    def testSetSlice(self):
        f1 = Bytes(2, value="x2345")
        with self.assertRaises(InvalidFieldDataException):
            f1[15:8] = "x23"

        f2 = Bytes(2)
        f2.value = "x2345"
        f2[7:0] = "x23"
        self.assertTrue(f2 == "x2323")
        f2[0:7] = "x21"
        self.assertTrue(f2 == "x2384")
        f2[0:0] = "b1"
        self.assertTrue(f2 == "x2385")

        with self.assertRaises(IndexError):
            f2[-1:0] = "x1"
        with self.assertRaises(IndexError):
            f2[0:-1] = "x1"
        with self.assertRaises(IndexError):
            f2[0:0:-1] = "x1"
        with self.assertRaises(IndexError):
            f2[0:] = "x1"
        with self.assertRaises(IndexError):
            f2[16:0] = "x1"
        with self.assertRaises(InvalidFieldDataException):
            f2[7:0] = "x1234"
Пример #2
0
 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)
Пример #3
0
    def testSetItem(self):
        f1 = Bytes(2, value="x2345")
        with self.assertRaises(InvalidFieldDataException):
            f1[0] = "b0"

        f2 = Bytes(2)
        f2.value = "x2345"
        f2[0] = "b0"
        self.assertEqual(f2, "x2344")
        f2[15] = "b1"
        self.assertEqual(f2, "xA344")

        with self.assertRaises(IndexError):
            f2[-1] = "b1"
        with self.assertRaises(IndexError):
            f2[16] = "b1"
Пример #4
0
    def testGetItem(self):
        f1 = Bytes(2, value="x2345")

        # test getting single bits
        self.assertEqual(f1[0], "b1")
        self.assertEqual(f1[1], "b0")
        self.assertEqual(f1[2], "b1")
        self.assertEqual(f1[3], "b0")
        self.assertEqual(f1[4], "b0")
        self.assertEqual(f1[5], "b0")
        self.assertEqual(f1[6], "b1")
        self.assertEqual(f1[7], "b0")
        self.assertEqual(f1[8], "b1")
        self.assertEqual(f1[9], "b1")
        self.assertEqual(f1[10], "b0")
        self.assertEqual(f1[11], "b0")
        self.assertEqual(f1[12], "b0")
        self.assertEqual(f1[13], "b1")
        self.assertEqual(f1[14], "b0")
        self.assertEqual(f1[15], "b0")

        # test the bounds
        with self.assertRaises(IndexError):
            f1[-1]
        with self.assertRaises(IndexError):
            f1[16]
Пример #5
0
 def testContains(self):
     """
     Determine if a string or another field is contained in this field.
     If comparing with a string, the data format of the string will be used for the search.
     If comparing with another Field, a binary search will be performed.
     """
     f1 = Bytes(2, value="x1234")
     self.assertTrue("b10010" in f1)
     self.assertTrue("x8" not in f1)
     self.assertTrue("b1000" in f1)
     self.assertTrue("b11111" not in f1)
     self.assertTrue(Byte(value="x23") in f1)
     self.assertTrue(Byte(value="x8") not in f1)
Пример #6
0
 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")
Пример #7
0
 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")
Пример #8
0
 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")
Пример #9
0
    def testGetSlice(self):
        f1 = Bytes(2, value="x2345")

        self.assertTrue(f1[15:0] == "x2345")
        self.assertTrue(f1[15:8] == "x23")
        self.assertTrue(f1[7:0] == "x45")
        self.assertTrue(f1[0:0] == "b1")

        self.assertTrue(f1[0:15] == "xA2C4")
        self.assertTrue(f1[0:7] == "xA2")
        self.assertTrue(f1[8:15] == "xC4")

        self.assertTrue(f1[0:0] == "b1")

        with self.assertRaises(IndexError):
            f1[16:0]
        with self.assertRaises(IndexError):
            f1[15:-1]
        with self.assertRaises(IndexError):
            f1[16:-1]
        with self.assertRaises(IndexError):
            f1[0:0:-1]
        with self.assertRaises(IndexError):
            f1[:]
Пример #10
0
 def testRightShift(self):
     f1 = Bytes(2, value="x0002")
     self.assertTrue((f1 >> 1) == "x0001")
     self.assertTrue((f1 >> 2) == "x0000")
Пример #11
0
 def testLeftShift(self):
     f1 = Bytes(2, value="x0002")
     self.assertTrue((f1 << 5) == "x0040")
     self.assertTrue((f1 << 32) == "x0000")
Пример #12
0
 def testAdd_Concatenation(self):
     f1 = Bytes(2, value="x0002")
     f2 = Bytes(2, value="x0010")
     self.assertEqual((f1 + "x10"), "x000210")
     self.assertEqual((f1 + f2), "x00020010")
Пример #13
0
 def testAdd_WithNoOverflow(self):
     self.assertEqual(Bytes(2, value="x0002") + 16, "x12")
Пример #14
0
        val |= int(
            arg.render(fmt=Field.Format.Bin, pad_to_length=len(arg) - 1)[1:],
            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": {