Ejemplo n.º 1
0
 def test_bitview_getitem__single_byte_bitslice_with_bits(self):
     for i in range(0, 256):
         for j in range(0, 8):
             bv = BitView(bytearray([i]))
             bv_slice = bv[float(j) / 8:]
             ba = bitarray(endian='little')
             ba.frombytes(chr(i) if PY2 else bytes([i]))
             ba_slice = ba[j:]
             self.assertEqualBitArrayBitView(ba_slice, bv_slice)
Ejemplo n.º 2
0
 def test_bitview_getitem__single_byte_bitslice_with_bits(self):
     for i in range(0, 256):
         for j in range(0, 8):
             bv = BitView(bytearray([i]))
             bv_slice = bv[float(j) / 8:]
             ba = bitarray(endian='little')
             ba.frombytes(chr(i) if PY2 else bytes([i]))
             ba_slice = ba[j:]
             self.assertEqualBitArrayBitView(ba_slice, bv_slice)
Ejemplo n.º 3
0
    def test_bitview__positive_slicing(self):
        for i in range(0, 100):
            ba = self._create_random_bit_array()
            bv = BitView(self._bitarray_to_bytes(ba), stop=float(ba.length()) / 8)
            self.assertEqualBitArrayBitView(ba, bv)

            slice_start_in_bits = random.choice(range(0, ba.length() + 10))
            slice_end_in_bits = random.choice(range(slice_start_in_bits, ba.length() + 10))

            ba_slice = ba[slice_start_in_bits:slice_end_in_bits]
            bv_slice = bv[float(slice_start_in_bits) / 8:float(slice_end_in_bits) / 8]
            self.assertEqualBitArrayBitView(ba_slice, bv_slice)
Ejemplo n.º 4
0
    def test_bitview__positive_slicing(self):
        for i in range(0, 100):
            ba = self._create_random_bit_array()
            bv = BitView(self._bitarray_to_bytes(ba),
                         stop=float(ba.length()) / 8)
            self.assertEqualBitArrayBitView(ba, bv)

            slice_start_in_bits = random.choice(range(0, ba.length() + 10))
            slice_end_in_bits = random.choice(
                range(slice_start_in_bits,
                      ba.length() + 10))

            ba_slice = ba[slice_start_in_bits:slice_end_in_bits]
            bv_slice = bv[float(slice_start_in_bits) /
                          8:float(slice_end_in_bits) / 8]
            self.assertEqualBitArrayBitView(ba_slice, bv_slice)
Ejemplo n.º 5
0
    def test_buffer_bits__simple(self):
        class Foo(Buffer):
            f_int = int_field(sign='unsigned',
                              where=(bytes_ref[7].bits[0:4] +
                                     bytes_ref[6].bits[0:4] +
                                     bytes_ref[5].bits[0:4] +
                                     bytes_ref[4].bits[0:4] +
                                     bytes_ref[3].bits[0:4] +
                                     bytes_ref[2].bits[0:4] +
                                     bytes_ref[1].bits[0:4] +
                                     bytes_ref[0].bits[0:4]))

        self.assertEqual(7.5, Foo.byte_size)
        for n in (0x12345678, 0x87654321, 0, 1, 0x10000000, 0xFF000000):
            foo = Foo()
            foo.f_int = n
            packed_value = foo.pack()
            self.assertEqual(8, len(packed_value))
            packed_result = bytearray(8)
            for i in range(8):
                packed_result[7 - i] = (foo.f_int >> (i * 4)) & 0x0F
            self.assertEqual(packed_result, packed_value)
            foo = Foo()
            foo.unpack(packed_result)
            self.assertEqual(foo.f_int, n)
Ejemplo n.º 6
0
    def evaluate(self, ctx):
        range_list = self.parent_range_ref.deref(ctx)
        assert len(range_list) >= 1

        bit_range = SequentialRange(float(self.start.deref(ctx)) / 8, float(self.stop.deref(ctx)) / 8)

        i, sum_length = range_list.find_relative_container_index(bit_range.start)
        if i is None:
            raise ValueError("Bit offset {0} is out of range for range sequence {1!r}".format(
                int(bit_range.start * 8), range_list))

        if bit_range.is_open():
            subrange_start = range_list[i].start + bit_range.start - sum_length
            return SequentialRangeList([SequentialRange(subrange_start, range_list[i].stop)] + range_list[i + 1:])

        bit_range_remaining_len = bit_range.byte_length()
        result = []
        for i in range(i, len(range_list)):
            r = range_list[i]
            subrange_start = r.start + bit_range.start - sum_length
            if r.is_open() or (r.stop - subrange_start) >= bit_range_remaining_len:
                subrange_stop = subrange_start + bit_range_remaining_len
            else:
                subrange_stop = r.stop

            result.append(SequentialRange(subrange_start, subrange_stop))

            bit_range_remaining_len -= (subrange_stop - subrange_start)
            if bit_range_remaining_len == 0:
                break

        if bit_range_remaining_len > 0:
            raise ValueError("Bit range {0!r} is out of range for parent range {1!r}".format(bit_range, range_list))

        return SequentialRangeList(result)
Ejemplo n.º 7
0
    def test_iadd_1(self):
        a = bytearray(b'\xd3\x94Q`\xb1\x93\x17\xed\xb2W\xa5\x00')
        b = bytearray(b'MK\xa3Li\xf9>\x039')
        bv1 = BitAwareByteArray(bytearray(a), start=0, stop=11.125)
        bv2 = BitView(bytearray(b), start=0, stop=8.75)
        bv1 += bv2

        a[-1] &= 0x01
        a[-1] |= (b[0] & 0x7F) << 1

        for i in range(len(b) - 1):
            a.append((b[i] >> 7) + ((b[i + 1] & 0x7F) << 1))

        self.assertEquals(list(bv1), list(a))
Ejemplo n.º 8
0
    def test_iadd_1(self):
        a = bytearray(b'\xd3\x94Q`\xb1\x93\x17\xed\xb2W\xa5\x00')
        b = bytearray(b'MK\xa3Li\xf9>\x039')
        bv1 = BitAwareByteArray(bytearray(a), start=0, stop=11.125)
        bv2 = BitView(bytearray(b), start=0, stop=8.75)
        bv1 += bv2

        a[-1] &= 0x01
        a[-1] |= (b[0] & 0x7F) << 1

        for i in range(len(b) - 1):
            a.append((b[i] >> 7) + ((b[i + 1] & 0x7F) << 1))

        self.assertEquals(list(bv1), list(a))
Ejemplo n.º 9
0
    def evaluate(self, ctx):
        range_list = self.parent_range_ref.deref(ctx)
        assert len(range_list) >= 1

        bit_range = SequentialRange(
            float(self.start.deref(ctx)) / 8,
            float(self.stop.deref(ctx)) / 8)

        i, sum_length = range_list.find_relative_container_index(
            bit_range.start)
        if i is None:
            raise ValueError(
                "Bit offset {0} is out of range for range sequence {1!r}".
                format(int(bit_range.start * 8), range_list))

        if bit_range.is_open():
            subrange_start = range_list[i].start + bit_range.start - sum_length
            return SequentialRangeList(
                [SequentialRange(subrange_start, range_list[i].stop)] +
                range_list[i + 1:])

        bit_range_remaining_len = bit_range.byte_length()
        result = []
        for i in range(i, len(range_list)):
            r = range_list[i]
            subrange_start = r.start + bit_range.start - sum_length
            if r.is_open() or (r.stop -
                               subrange_start) >= bit_range_remaining_len:
                subrange_stop = subrange_start + bit_range_remaining_len
            else:
                subrange_stop = r.stop

            result.append(SequentialRange(subrange_start, subrange_stop))

            bit_range_remaining_len -= (subrange_stop - subrange_start)
            if bit_range_remaining_len == 0:
                break

        if bit_range_remaining_len > 0:
            raise ValueError(
                "Bit range {0!r} is out of range for parent range {1!r}".
                format(bit_range, range_list))

        return SequentialRangeList(result)
Ejemplo n.º 10
0
    def test_buffer_bits__complex(self):
        class Foo(Buffer):
            f_int = int_field(sign='unsigned',
                              where=(bytes_ref[0:2].bits[4:12] + bytes_ref[2:4].bits[4:12] +
                                     bytes_ref[4:6].bits[4:12] + bytes_ref[6:8].bits[4:12]))

        self.assertEqual(7.5, Foo.byte_size)
        for n in (0xFF000000, 0x12345678, 0x87654321, 0, 1, 0x10000000, 0xFF000000):
            foo = Foo()
            foo.f_int = n
            packed_value = foo.pack()
            self.assertEqual(8, len(packed_value))

            ba = bitarray('0' * (8 * 8), endian='little')
            int_pack = struct.pack("<L", foo.f_int)
            for i in range(4):
                b = bitarray(endian='little')
                b.frombytes(bytes([int_pack[i]]) if not PY2 else int_pack[i])
                ba[i * 2 * 8 + 4:i * 2 * 8 + 4 + 8] = b

            self.assertEqual(ba.tobytes(), packed_value)
Ejemplo n.º 11
0
    def test_buffer_bits__simple(self):
        class Foo(Buffer):
            f_int = int_field(
                sign='unsigned',
                where=(bytes_ref[7].bits[0:4] + bytes_ref[6].bits[0:4] +
                       bytes_ref[5].bits[0:4] + bytes_ref[4].bits[0:4] +
                       bytes_ref[3].bits[0:4] + bytes_ref[2].bits[0:4] +
                       bytes_ref[1].bits[0:4] + bytes_ref[0].bits[0:4]))

        self.assertEqual(7.5, Foo.byte_size)
        for n in (0x12345678, 0x87654321, 0, 1, 0x10000000, 0xFF000000):
            foo = Foo()
            foo.f_int = n
            packed_value = foo.pack()
            self.assertEqual(8, len(packed_value))
            packed_result = bytearray(8)
            for i in range(8):
                packed_result[7 - i] = (foo.f_int >> (i * 4)) & 0x0F
            self.assertEqual(packed_result, packed_value)
            foo = Foo()
            foo.unpack(packed_result)
            self.assertEqual(foo.f_int, n)
Ejemplo n.º 12
0
    def test_buffer_bits__complex(self):
        class Foo(Buffer):
            f_int = int_field(
                sign='unsigned',
                where=(bytes_ref[0:2].bits[4:12] + bytes_ref[2:4].bits[4:12] +
                       bytes_ref[4:6].bits[4:12] + bytes_ref[6:8].bits[4:12]))

        self.assertEqual(7.5, Foo.byte_size)
        for n in (0xFF000000, 0x12345678, 0x87654321, 0, 1, 0x10000000,
                  0xFF000000):
            foo = Foo()
            foo.f_int = n
            packed_value = foo.pack()
            self.assertEqual(8, len(packed_value))

            ba = bitarray('0' * (8 * 8), endian='little')
            int_pack = struct.pack("<L", foo.f_int)
            for i in range(4):
                b = bitarray(endian='little')
                b.frombytes(bytes([int_pack[i]]) if not PY2 else int_pack[i])
                ba[i * 2 * 8 + 4:i * 2 * 8 + 4 + 8] = b

            self.assertEqual(ba.tobytes(), packed_value)
Ejemplo n.º 13
0
 def test_bitview_getitem__single_byte_bitslice(self):
     for i in range(0, 256):
         for j in range(0, 8):
             bv = BitView(bytearray([i]))
             self.assertEqual(list(bv[float(j) / 8:])[0], i >> j)
Ejemplo n.º 14
0
def junk_generator(size):
    import string
    import random
    chars = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(chars) for x in range(size)).encode('ASCII')
Ejemplo n.º 15
0
 def _create_random_bit_array(self):
     length_in_bits = random.randint(0, 8 * 16)
     return bitarray("".join(
         random.choice(('0', '1')) for i in range(length_in_bits)),
                     endian='little')
Ejemplo n.º 16
0
 def _create_random_bit_array(self):
     length_in_bits = random.randint(0, 8 * 16)
     return bitarray("".join(random.choice(('0', '1')) for i in range(length_in_bits)), endian='little')
Ejemplo n.º 17
0
def junk_generator(size):
    import string
    import random
    chars = string.ascii_letters + string.digits + string.punctuation
    return ''.join(random.choice(chars) for x in range(size)).encode('ASCII')
Ejemplo n.º 18
0
 def test_bitview_getitem__single_byte_bitslice(self):
     for i in range(0, 256):
         for j in range(0, 8):
             bv = BitView(bytearray([i]))
             self.assertEqual(list(bv[float(j) / 8:])[0], i >> j)