A generated input loop is useful only when each case checks a precise invariant and failures remain reproducible.
Python generated checks: name the invariant and keep rejected cases
Operation contract
The parser accepts signed decimal integer text and must round-trip every number in a bounded range. The loop checks that invariant for 95 inputs, then tests three invalid forms separately. This is a deterministic regression fixture, not exhaustive proof for every integer string and not a replacement for a property-testing engine that shrinks failures.
Failure and ownership boundary
Varying inputs without an assertion does not test anything. Random generation needs a recorded seed and a strategy that includes boundaries; a fixed range is easier to reproduce but narrower. Define whether whitespace, plus signs, leading zeroes, Unicode digits, and overflow are valid before broadening the input space.
Working program
def parse_signed_decimal(raw):
digits = raw[1:] if raw.startswith("-") else raw
if not digits or not all("0" <= character <= "9" for character in digits):
raise ValueError("not signed decimal")
return int(raw)
checked = 0
for quantity in range(-47, 48):
assert parse_signed_decimal(str(quantity)) == quantity
checked += 1
rejected = 0
for raw in ("", "47x", " 47"):
try:
parse_signed_decimal(raw)
except ValueError:
rejected += 1
print("round_trips", checked)
print("rejected", rejected)Output
round_trips 95
rejected 3Costs and limits
The shown loop runs O(n × digits) checks with constant state. A broader generated suite needs a runtime budget and retained failing seeds or examples.
Common Mistakes
- Generated values do not prove the invariant outside their domain.
- Keep rejected-input tests separate from success round trips.
- An unrecorded random seed makes an intermittent failure hard to reproduce.
