Пример #1
0
    def test_encode_int_field(self):
        """ Test for Field.int.encode """
        self.assertEqual(bytearray([0, 0]), Field.integer('test').encode(0))
        self.assertEqual(bytearray([0, 1]), Field.integer('test').encode(1))
        self.assertEqual(bytearray([1, 17]),
                         Field.integer('test').encode(1 * 256 + 17))
        self.assertEqual(bytearray([255, 255]),
                         Field.integer('test').encode(255 * 256 + 255))

        with self.assertRaises(ValueError):
            Field.integer('test').encode(-1)
        with self.assertRaises(ValueError):
            Field.integer('test').encode(102400)
Пример #2
0
    def test_decode_int_field(self):
        """ Test for Field.int.decode """
        self.assertEqual(0, Field.integer('test').decode(bytearray([0, 0])))
        self.assertEqual(1, Field.integer('test').decode(bytearray([0, 1])))
        self.assertEqual(256 + 17,
                         Field.integer('test').decode(bytearray([1, 17])))
        self.assertEqual(255 * 256 + 255,
                         Field.integer('test').decode(bytearray([255, 255])))

        with self.assertRaises(ValueError):
            Field.integer('test').decode(bytearray(b'123'))