diff --git a/rolldice/rolldice.py b/rolldice/rolldice.py index 95f7a7f..c18f932 100644 --- a/rolldice/rolldice.py +++ b/rolldice/rolldice.py @@ -812,7 +812,7 @@ def roll_dice(roll, *, functions=True, floats=True): num_to_drop = int(drop[3] if drop[3] != '' else 1) assert 1 <= num_to_drop < len(group_result) - results.append(sum(group_result[:num_to_drop])) + results.append(sum(group_result[num_to_drop:])) roll = ','.join([str(i) for i in group_result[num_to_drop:]]) + ' ~~ ' # Same as above. roll += ','.join([str(i) for i in group_result[:num_to_drop]]) string.append('[%s]' % roll) diff --git a/tests/test_drop.py b/tests/test_drop.py new file mode 100644 index 0000000..05e1939 --- /dev/null +++ b/tests/test_drop.py @@ -0,0 +1,36 @@ +from rolldice import roll_dice +from hypothesis import given, note +from hypothesis import strategies as st + + +# This is a basic sanity check that avoids the need to parse the explanation string. +# If you roll N dice, and drop N-1, then the result has to be in the range of the die size. +@given( + roll_count=st.integers(min_value=2, max_value=20), + die_size=st.integers(min_value=2, max_value=100), + op=st.sampled_from("Xx"), +) +def test_result_in_bounds(roll_count, die_size, op): + drop_count = roll_count - 1 + expr = f"{roll_count}d{die_size}{op}{drop_count}" + result, explanation = roll_dice(expr) + note(f'expr={expr}, explanation={explanation}') + assert 1 <= result <= die_size + +@given( + roll_count=st.integers(min_value=2, max_value=20), + die_size=st.integers(min_value=2, max_value=100), + op=st.sampled_from("Xx"), + data=st.data() +) +def test_sum(roll_count, die_size, op, data): + drop_count = data.draw(st.integers(min_value=1, max_value=roll_count - 1)) + expr = f"{roll_count}d{die_size}{op}{drop_count}" + result, explanation = roll_dice(expr) + included, dropped = explanation.strip('[]').split(' ~~ ') + included_rolls = [int(x) for x in included.split(',')] + dropped_rolls = [int(x) for x in dropped.split(',')] + assert(len(dropped_rolls) == drop_count) + assert(len(included_rolls) + len(dropped_rolls) == roll_count) + included_sum = sum(included_rolls) + assert(result == included_sum)