def test_bitarray_set_word(): # Non-inverted expected = 9876 | (123456 << (BitArray.num_bits_per_word * 3)) ba = BitArray(0) ba.set_word(0, 9876) ba.set_word(3, 123456) assert ba.__getstate__() == expected assert not ba.is_all_on() # Inverted ba = BitArray(0) ba.invert_in_place() ba.set_word(2, 1234) full_word = (1 << BitArray.num_bits_per_word) - 1 expected = ~((full_word & ~1234) << (BitArray.num_bits_per_word * 2)) assert ba.__getstate__() == expected assert not ba.is_all_on()
def test_bitarray_constructor_sparse(): # Create a BitArray from a SparseArray. ba = BitArray(SparseArray.all_on()) assert ba.is_all_on() ba = BitArray(SparseArray()) assert ba.is_zero() sa = SparseArray() sa.set_range(3, 64) sa.set_range(0, 1) sa.clear_range(60, 2) ba = BitArray(sa) exp = 0b1111100111111111111111111111111111111111111111111111111111111111001 assert ba.__getstate__() == exp sa.invert_in_place() ba = BitArray(sa) assert ba.__getstate__() == ~exp