def test_operator_minus(self): mask_a = BitMask.from_bin("0b10111") mask_a_old = deepcopy(mask_a) mask_b = BitMask.from_bin("0b01100") mask_target = BitMask.from_bin("0b10011") diff = mask_a - mask_b self.assertEqual(diff, mask_target) self.assertEqual(mask_a_old, mask_a) # make sure the original mask is unchanged.
def test_get_and_set_outside_of_range(self): mask = BitMask.from_bin("0b11110000") self.assertFalse(mask.get_kth_bit(100)) mask.set_kth_bit(100) self.assertTrue(mask.get_kth_bit(100))
def test_get_and_set(self): mask = BitMask.from_bin("0b11110000") self.assertFalse(mask.get_kth_bit(2)) mask.set_kth_bit(2) self.assertTrue(mask.get_kth_bit(2)) self.assertTrue(mask.get_kth_bit(4)) mask.unset_kth_bit(4) self.assertFalse(mask.get_kth_bit(4))
def test_nonzero_bits(self): mask = BitMask.from_bin("0b0") indices = [100, 1000, 10_000, 100_000] self.assertEqual(mask.x, 0) for index in indices: mask.set_kth_bit(index) self.assertGreaterEqual(mask.x, 0) also_indices = mask.to_indices() for i, j in zip(indices, also_indices): self.assertEqual(i, j)
def test_invert(self): # get random bitstring length = 10 bitstring = self.random_bitstring(10) #get inverse mask = BitMask.from_bin(bitstring) mask.invert(length) inverted = mask.to_bin() # remove 0b inverted = inverted[2:] bitstring = bitstring[2:] for i in range(min(len(bitstring), len(inverted))): if bitstring[-i - 1] == '0': self.assertEqual(inverted[-i - 1], '1') else: self.assertEqual(inverted[-i - 1], '0')
def test_store_and_retrieve(self): x = int("0b01010100100100100100100010010100100100101001001010101010", 2) mask = BitMask(x) mask.set_kth_bit(11) mask.set_kth_bit(22) mask.set_kth_bit(33) mask.set_kth_bit(44) mask.set_kth_bit(55) mask.set_kth_bit(66) mask.set_kth_bit(77) mask.set_kth_bit(88) mask.set_kth_bit(99) somewhere = mask.to_hex() somewhere_else = mask.to_bin() mask_somewhere = BitMask.from_hex(somewhere) mask_somewhere_else = BitMask.from_bin(somewhere_else) self.assertEqual(mask.x, mask_somewhere.x) self.assertEqual(mask.x, mask_somewhere_else.x)
def assert_difference(self, bistring_1: str, bitstring_2: str, target: str): mask_a = BitMask.from_bin(bistring_1) mask_b = BitMask.from_bin(bitstring_2) mask_a.difference(mask_b) self.assertEqual(mask_a.x, int(target, 2))
def test_intersection(self): mask_a = BitMask.from_bin("0b101") mask_b = BitMask.from_bin("0b100") mask_a.intersection(mask_b) self.assertEqual(mask_a.x, int("0b100", 2))
def test_union(self): mask_a = BitMask.from_bin("0b001") mask_b = BitMask.from_bin("0b100") mask_a.union(mask_b) self.assertEqual(mask_a.x, int("0b101", 2))
def test_large_bitmasks(self): bitstring = "0b" + "1" * 5678 mask = BitMask.from_bin(bitstring) mask_as_bitstring = mask.to_bin() self.assertEqual(mask_as_bitstring, bitstring)
def test_masked_select_from_list_example(self): list_ = [1, 2, 3, 4, 5, 6] mask = BitMask.from_bin('0b001101') # expected result is [1, 3, 4] selected = mask.masked_select_from_list(list_) self.assertListEqual(selected, [1, 3, 4])
def test_equal(self): mask_a = BitMask.from_bin("0b101") mask_b = BitMask.from_bin("0b101") self.assertEqual(mask_a, mask_b)
def test_subset_a_list(self): list_ = [4, 7, 9, 1] mask = BitMask.from_bin("0b0101") target_masked_list = [7, 1] masked_list = mask.masked_select_from_list(list_) self.assertEqual(target_masked_list, masked_list)