def test_or_bitfields(): bitfield_1 = get_empty_bitfield(2) bitfield_1 = set_voted(bitfield_1, 0) assert get_vote_count(bitfield_1) == 1 # same size as bitfield_1 bitfield_2 = get_empty_bitfield(2) bitfield_2 = set_voted(bitfield_2, 1) assert get_vote_count(bitfield_2) == 1 bitfield = or_bitfields([bitfield_1, bitfield_2]) assert get_vote_count(bitfield) == 2 # different size from bitfield_1 bitfield_3 = get_empty_bitfield(100) bitfield_3 = set_voted(bitfield_3, 99) assert get_vote_count(bitfield_3) == 1 with pytest.raises(ValueError): or_bitfields([bitfield_1, bitfield_3])
def test_has_voted_random(votes_count): bit_count = 1000 bitfield = get_empty_bitfield(bit_count) random_votes = random.sample(range(bit_count), votes_count) for index in random_votes: bitfield = set_voted(bitfield, index) assert get_vote_count(bitfield) == votes_count for index in range(bit_count): if index in random_votes: assert has_voted(bitfield, index) else: assert not has_voted(bitfield, index)
def test_get_vote_count(): bitfield = get_empty_bitfield(5) bitfield = set_voted(bitfield, 0) bitfield = set_voted(bitfield, 3) assert get_vote_count(bitfield) == 2