def test_sequence_less_than_dice_sides(self, capsys, monkeypatch): # Test to see whether we can use a n-sided die to choose from # a sequence with less than n items src = RealDiceRandomSource(None) src.dice_sides = 6 # A length of 1 requires no rolls self.fake_input_values(["1"], monkeypatch) picked = src.choice([1]) out, err = capsys.readouterr() assert "roll" not in out assert picked == 1 # A length of 2,3 only requires 1 roll for choice_length in (2, 3): self.fake_input_values(["1"], monkeypatch) picked = src.choice(range(1, choice_length + 1)) out, err = capsys.readouterr() assert "roll 1 dice" in out assert picked == 1 # A length of 4,5 requires 2 rolls for choice_length in (4, 5): self.fake_input_values(["1", "1"], monkeypatch) picked = src.choice(range(1, choice_length + 1)) out, err = capsys.readouterr() assert "roll 2 dice" in out assert picked == 1
def test_pre_check_warn_if_not_all_seq_items_used(self, capsys): # we issue a warning if not all sequence items will be used src = RealDiceRandomSource(None) src.dice_sides = 10 src.pre_check(1, list(range(10))) out, err = capsys.readouterr() assert "entropy is reduced" not in out src.pre_check(1, list(range(11))) out, err = capsys.readouterr() assert "entropy is reduced" in out
def test_choice_distributes_equally_on_long_seq(self, fake_input): # we distribute nearly equally over sequences longer than # dice_sides**n src = RealDiceRandomSource(None) src.dice_sides = 2 dist = [0, 0, 0] fake_input(["1", "1", "1", "2", "2", "1", "2", "2"]) for x in range(8): picked = src.choice([1, 2, 3]) dist[picked - 1] += 1 assert dist == [4, 4, 0]
def test_choice_distributes_equally(self, fake_input): # we distribute nearly equally over sequences sized # dice_sides**n src = RealDiceRandomSource(None) src.dice_sides = 3 dist = [0, 0, 0] fake_input(["1", "2", "3"]) for x in range(3): picked = src.choice([1, 2, 3]) dist[picked - 1] += 1 assert dist == [1, 1, 1]
def test_choice_prints_hint_on_repeated_rolls(self, capsys, fake_input): # on short sequences (shorter than number of dice sides) # we give users hints to repeat dice rolls src = RealDiceRandomSource(None) src.dice_sides = 4 fake_input(["2", "3"]) # no value out of bounds (> 3) picked = src.choice([1, 2, 3]) out, err = capsys.readouterr() assert picked == 2 assert out.count("Please roll dice again") == 0 fake_input(["4", "4", "1"]) picked = src.choice([1, 2, 3]) out, err = capsys.readouterr() assert picked == 1 assert out.count("Please roll dice again") == 2
def test_choice_distributes_equally_on_short_seq(self, fake_input): # we distribute equally over sequences shorter than # dice_sides**n src = RealDiceRandomSource(None) src.dice_sides = 4 dist = [0, 0, 0] # a list of pairs in a row: 4-4 - 4-3 - 4-2 - ... - 1-3 - 1-2 - 1-1 rolled_values = list( chain.from_iterable(product(["4", "3", "2", "1"], repeat=2))) # 4 is not a valid roll value, must do a new roll then num_valid = len(rolled_values) - rolled_values.count("4") fake_input(rolled_values) for x in range(num_valid): picked = src.choice([1, 2, 3]) dist[picked - 1] += 1 assert dist == [8, 8, 8]
def test_choice_distributes_equally_on_short_seq(self, fake_input): # we distribute equally over sequences shorter than # dice_sides**n src = RealDiceRandomSource(None) src.dice_sides = 4 dist = [0, 0, 0] # a list of pairs in a row: 4-4 - 4-3 - 4-2 - ... - 1-3 - 1-2 - 1-1 rolled_values = list(chain.from_iterable( product(["4", "3", "2", "1"], repeat=2))) # 4 is not a valid roll value, must do a new roll then num_valid = len(rolled_values) - rolled_values.count("4") fake_input(rolled_values) for x in range(num_valid): picked = src.choice([1, 2, 3]) dist[picked - 1] += 1 assert dist == [8, 8, 8]
def test_choice_copes_with_small_sequences(self, capsys, fake_input): # We handle sequences correctly, that have less elements than the used # dice sides. src = RealDiceRandomSource(None) src.dice_sides = 6 # A length of 2,3 only requires 1 roll for choice_length in (2, 3): fake_input(["1"]) picked = src.choice(range(1, choice_length + 1)) out, err = capsys.readouterr() assert "roll 1 dice" in out assert picked == 1 # A length of 4,5 requires 2 rolls for choice_length in (4, 5): fake_input(["1", "2"]) picked = src.choice(range(1, choice_length + 1)) out, err = capsys.readouterr() assert "roll 1 dice" in out assert picked == 1