예제 #1
0
    def testPackifyUnpackify(self):
        """
        Test the packbits
        """
        console.terse("{0}\n".format(self.testPackifyUnpackify.__doc__))

        fmt = u"3 2 1 1"
        fields = [6, 2, True, False]
        size = 1
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([212]))
        self.assertEqual(aiding.binize(aiding.unbytify(packed), size * 8), "11010100")

        fmt = u""
        fields = []
        size = 0
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([]))

        fmt = u"3 1"
        fields = [5, True]
        size = 1
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([0xB0]))
        self.assertEqual(aiding.binize(aiding.unbytify(packed), size * 8), "10110000")

        fmt = u"8 6 7 3"
        fields = [0xA5, 0x38, 0x08, 0x01]
        size = 3
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([0xA5, 0xE0, 0x41]))
        self.assertEqual(aiding.binize(aiding.unbytify(packed), size * 8), "101001011110000001000001")
        # 0xa5e040

        fmt = u"3 2 1 1"
        packed = bytearray([212])
        fields = aiding.unpackify(fmt=fmt, b=packed, boolean=True)
        self.assertEqual(fields, (6, 2, True, False, False))
        fields = aiding.unpackify(fmt=fmt, b=packed, boolean=False)
        self.assertEqual(fields, (6, 2, 1, 0, 0))

        fmt = u""
        packed = bytearray([])
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, tuple())

        fmt = u"3 1"
        packed = [0xB0]
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, (5, 1, 0))

        fmt = u"4 3 1"
        packed = [0x0B]
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, (0, 5, 1))

        fmt = u"8 6 7 3"
        packed = bytearray([0xA5, 0xE0, 0x41])
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, (0xA5, 0x38, 0x08, 0x01))
예제 #2
0
    def testIntifyBytify(self):
        """
        Test the intify bytify functions
        """
        console.terse("{0}\n".format(self.testIntifyBytify.__doc__))

        b = bytearray([1, 2, 3])
        n = aiding.unbytify(b)
        self.assertEqual(n, 0x010203)
        n = aiding.unbytify([1, 2, 3])
        self.assertEqual(n, 0x010203)
        n = aiding.unbytify(b"\x01\x02\x03")
        self.assertEqual(n, 0x010203)

        b = aiding.bytify(n)
        self.assertEqual(b, bytearray([1, 2, 3]))
        b = aiding.bytify(n, 4)
        self.assertEqual(b, bytearray([0, 1, 2, 3]))
        b = aiding.bytify(n, 2)
        self.assertEqual(b, bytearray([1, 2, 3]))
예제 #3
0
    def testIntifyBytify(self):
        """
        Test the intify bytify functions
        """
        console.terse("{0}\n".format(self.testIntifyBytify.__doc__))

        b = bytearray([1, 2, 3])
        n = aiding.unbytify(b)
        self.assertEqual(n, 0x010203)
        n = aiding.unbytify([1, 2, 3])
        self.assertEqual(n, 0x010203)
        n = aiding.unbytify(b'\x01\x02\x03')
        self.assertEqual(n, 0x010203)

        b = aiding.bytify(n)
        self.assertEqual(b, bytearray([1, 2, 3]))
        b = aiding.bytify(n, 4)
        self.assertEqual(b, bytearray([0, 1, 2, 3]))
        b = aiding.bytify(n, 2)
        self.assertEqual(b, bytearray([1, 2, 3]))
예제 #4
0
    def testPackifyUnpackify(self):
        """
        Test the packbits
        """
        console.terse("{0}\n".format(self.testPackifyUnpackify.__doc__))

        fmt = u'3 2 1 1'
        fields = [6, 2, True, False]
        size = 1
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([212]))
        self.assertEqual(aiding.binize(aiding.unbytify(packed), size * 8),
                         '11010100')

        fmt = u''
        fields = []
        size = 0
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([]))

        fmt = u'3 1'
        fields = [5, True]
        size = 1
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([0xb0]))
        self.assertEqual(aiding.binize(aiding.unbytify(packed), size * 8),
                         '10110000')

        fmt = u'8 6 7 3'
        fields = [0xA5, 0x38, 0x08, 0x01]
        size = 3
        packed = aiding.packify(fmt=fmt, fields=fields, size=size)
        self.assertEqual(packed, bytearray([0xa5, 0xe0, 0x41]))
        self.assertEqual(aiding.binize(aiding.unbytify(packed), size * 8),
                         '101001011110000001000001')
        # 0xa5e040

        fmt = u'3 2 1 1'
        packed = bytearray([212])
        fields = aiding.unpackify(fmt=fmt, b=packed, boolean=True)
        self.assertEqual(fields, (6, 2, True, False, False))
        fields = aiding.unpackify(fmt=fmt, b=packed, boolean=False)
        self.assertEqual(fields, (6, 2, 1, 0, 0))

        fmt = u''
        packed = bytearray([])
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, tuple())

        fmt = u'3 1'
        packed = [0xb0]
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, (5, 1, 0))

        fmt = u'4 3 1'
        packed = [0x0b]
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, (0, 5, 1))

        fmt = u'8 6 7 3'
        packed = bytearray([0xa5, 0xe0, 0x41])
        fields = aiding.unpackify(fmt=fmt, b=packed)
        self.assertEqual(fields, (0xA5, 0x38, 0x08, 0x01))