def test_compact(self): ''' Test that the bit array is compacted correctly ''' array = BitArray() array.flip(127) self.assertEqual(128, array.length_of_bits) array.flip(127) self.assertEqual(0, array.length_of_bits)
def test_get(self): ''' Test that the get bit operation is correct ''' barray = BitArray(array=[0xEC]) for pos, val in {0:False, 1:False, 4:True}.items(): self.assertEqual(val, barray.get(pos)) barray = BitArray(array=[0x00]) for pos in [0, 1, 4]: self.assertEqual(False, barray.get(pos))
def test_flip_all(self): ''' Test that the flip_all bit operation is correct ''' cases = { 0xEC: [0xFFFFFFFFFFFFFF13], 0xFFFFFFFFFFFFFF13: [0xEC], } for array, expected in cases.items(): barray = BitArray(array=[array]) barray.flip_all() self.assertEqual(expected, barray.to_byte_list())
def test_set_all(self): ''' Test that the set_all bit operation is correct ''' cases = { 0x00: [0xFFFFFFFFFFFFFFFF], 0x12: [0xFFFFFFFFFFFFFFFF], 0x34: [0xFFFFFFFFFFFFFFFF], } for array, expected in cases.items(): barray = BitArray(array=[array]) barray.set_all() self.assertEqual(expected, barray.to_byte_list())
def test_get_range(self): ''' Test that the get_range bit operation is correct ''' cases = { #( 0, 7): 0x38, #( 4, 12): 0x23, #(15, 20): 0x0F, ( 0, 18): 0x7E238, } barray = BitArray(block=8, array=[0xC7, 0xE2, 0x38]) for (sidx, eidx), expected in cases.items(): #print "expectd: %s %s" %( hex(expected), hex(barray.get_range(sidx, eidx))) self.assertEqual(expected, barray.get_range(sidx, eidx))
def test_set(self): ''' Test that the set bit operation is correct ''' barray = BitArray(array=[0xEC]) for pos in [0, 1, 4]: barray.set(pos) self.assertEqual([0xFF], barray.to_byte_list()) barray = BitArray(array=[0x00]) for pos in [0, 1, 4]: barray.set(pos) self.assertEqual([0x13], barray.to_byte_list())
def test_clear(self): ''' Test that the clear bit operation is correct ''' barray = BitArray(array=[0xFF]) for pos in [0, 1, 4]: barray.clear(pos) self.assertEqual([0xEC], barray.to_byte_list()) barray = BitArray(array=[0x13]) for pos in [0, 1, 4]: barray.clear(pos) self.assertEqual([0x00], barray.to_byte_list())
def test_flip(self): ''' Test that the flip bit operation is correct ''' barray = BitArray(array=[0xEC]) for pos in [0, 1, 4]: barray.flip(pos) self.assertEqual([0xFF], barray.to_byte_list()) barray = BitArray(array=[0xFF]) for pos in [0, 1, 4]: barray.flip(pos) self.assertEqual([0xEC], barray.to_byte_list())
def test_magic_methods(self): ''' Test that the magic methods work correctly ''' barray = BitArray(array=[0x12, 0x34]) narray = BitArray(array=[0x34, 0x12]) self.assertTrue(0x12 in barray) self.assertTrue(0x56 not in barray) self.assertEqual(-8993115002363113022, hash(barray)) self.assertEqual(69, len(barray)) self.assertEqual(list(iter(barray)), barray.to_bit_list()) self.assertEqual(list(reversed(barray)), list(reversed(barray.to_bit_list()))) self.assertTrue(barray == barray) self.assertFalse(barray == narray) self.assertTrue(barray != narray) self.assertFalse(barray != barray) self.assertTrue(barray >= barray) self.assertTrue(barray <= barray) self.assertTrue(barray < narray) self.assertTrue(narray > barray) self.assertEqual(narray & barray, BitArray(array=[0x10, 0x10])) self.assertEqual(narray ^ barray, BitArray(array=[0x26, 0x26])) self.assertEqual(narray | barray, BitArray(array=[0x36, 0x36])) self.assertEqual(-barray, BitArray(array=[0xFFFFFFFFFFFFFFCC, 0xFFFFFFFFFFFFFFEE])) self.assertEqual(~barray, BitArray(array=[0xFFFFFFFFFFFFFFCB, 0xFFFFFFFFFFFFFFED])) self.assertEqual(+barray, BitArray(array=[0x12, 0x34]))