def main(): bb = BitBucket() testcase_a = [ (([0xFF,0],6+8), "|11111111|000000 [6]"), (([0xFF], 3), "|11111111|00000011|1 [1]"), (([0x00], 3), "|11111111|00000011|1000 [4]"), (([0xFF,0], 8+6), "|11111111|00000011|10001111|11110000|00 [2]"), (([0xFF], 4), "|11111111|00000011|10001111|11110000|001111 [6]"), (([0x0], 4), "|11111111|00000011|10001111|11110000|00111100|00 [2]"), ] RunTestCase(bb, testcase_a) testcase_b = [ (([0xF0], 5), "|11110 [5]"), (([0x0F], 5), "|11110000|01 [2]"), (([0xF0], 5), "|11110000|0111110 [7]"), (([0x0F], 5), "|11110000|01111100|0001 [4]"), (([0xF0], 5), "|11110000|01111100|00011111|0 [1]"), (([0x0F], 5), "|11110000|01111100|00011111|000001 [6]"), (([0xF0], 5), "|11110000|01111100|00011111|00000111|110 [3]"), (([0x0F], 5), "|11110000|01111100|00011111|00000111|11000001 [0]"), (([0xF0], 5), "|11110000|01111100|00011111|00000111|11000001|11110 [5]"), ] bb.Clear() RunTestCase(bb, testcase_b) testcase_c = [ (([0xF0], 1), "|1 [1]"), (([0x0F], 1), "|10 [2]"), (([0xF0], 1), "|101 [3]"), (([0x0F], 1), "|1010 [4]"), (([0xF0], 1), "|10101 [5]"), (([0x0F], 1), "|101010 [6]"), (([0xF0], 1), "|1010101 [7]"), (([0x0F], 1), "|10101010 [0]"), (([0xF0], 1), "|10101010|1 [1]"), (([0x00,0xFF], 8+7), "|10101010|10000000|01111111 [0]"), ] bb.Clear() RunTestCase(bb, testcase_c) testcase_d = [ (([0xF0], 8), "|11110000 [0]"), (([0xF0], 8), "|11110000|11110000 [0]"), (([0xF0], 1), "|11110000|11110000|1 [1]"), (([0x0F], 8), "|11110000|11110000|10000111|1 [1]"), ] bb.Clear() RunTestCase(bb, testcase_d) testcase_e = [ (([0,52], 8+6), "|00000000|001101 [6]"), (([185], 8), "|00000000|00110110|111001 [6]"), ] bb.Clear() RunTestCase(bb, testcase_e) print "Success!"
def test_GetBits4(self): bb = BitBucket() inp = [0xd, 0xd, 0xd, 0xd] for offset in xrange(16): bb.Clear() StoreBitsFromString(bb, "1" * offset + "|11011101|11011101 [0]") for i in xrange(offset): bb.GetBit() for i in xrange(len(inp)): assert bb.GetBits4() == inp[i]
def test_GetBits8(self): bb = BitBucket() inp = [0xdd, 0xee, 0xaa, 0xdd] bitstr = "|11011101|11101110|10101010|11011101 [0]" for offset in xrange(16): bb.Clear() StoreBitsFromString(bb, ("1" * offset) + bitstr) for i in xrange(offset): bb.GetBit() for i in xrange(len(inp)): assert bb.GetBits8() == inp[i]
def test_StoreBits8(self): bb = BitBucket() inp = [0xff, 0x00, 0x00, 0xdd] orig_expected_str = "|11111111|00000000|00000000|11011101 [0]" for offset in xrange(16): bb.Clear() for i in xrange(offset): bb.StoreBit(1) for n in inp: bb.StoreBits8(n) assert str(bb) == ReformatExpectedStr('1' * offset, orig_expected_str)
def test_Clear(self): bb = BitBucket() bb.StoreBits(([0xff, 0xff, 0xff], 23)) if str(bb) != "|11111111|11111111|1111111 [7]": self.fail("basic storing of bits didn't work!") bb.Clear() assert str(bb) == " [0]" assert bb.output == [] assert bb.out_byte == 0 assert bb.out_boff == 0 assert bb.idx_byte == 0 assert bb.idx_boff == 0
def test_StoreBits4(self): bb = BitBucket() inp = [0xff, 0xf0, 0xf0, 0xfd] for n in inp: bb.StoreBits4(n) assert str(bb) == "|11110000|00001101 [0]" bb.Clear() bb.StoreBit(0) for n in inp: bb.StoreBits4(n) assert str(bb) == "|01111000|00000110|1 [1]" bb.Clear() bb.StoreBit(0) bb.StoreBit(1) for n in inp: bb.StoreBits4(n) assert str(bb) == "|01111100|00000011|01 [2]" bb.Clear() bb.StoreBit(0) bb.StoreBit(1) bb.StoreBit(0) for n in inp: bb.StoreBits4(n) assert str(bb) == "|01011110|00000001|101 [3]" bb.Clear() bb.StoreBit(0) bb.StoreBit(1) bb.StoreBit(0) bb.StoreBit(1) for n in inp: bb.StoreBits4(n) assert str(bb) == "|01011111|00000000|1101 [4]"
def test_AdvanceReadPtrToByteBoundary(self): bb = BitBucket() for offset in xrange(16): bb.Clear() StoreBitsFromString(bb, "1" * 32) for i in xrange(offset): bb.GetBit() old_idx_byte = bb.idx_byte old_idx_boff = bb.idx_boff assert bb.idx_byte == offset / 8 assert bb.idx_boff == offset % 8 bb.AdvanceReadPtrToByteBoundary() if old_idx_boff == 0: assert bb.idx_boff == old_idx_boff assert bb.idx_byte == old_idx_byte else: assert bb.idx_boff == 0 assert bb.idx_byte == (old_idx_byte + 1)