Created
July 12, 2012 03:26
-
-
Save jtratner/3095486 to your computer and use it in GitHub Desktop.
Fuzzer for CS258 Sudoku Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fuzzer_for_sudoku_checker(sudoku_checker): | |
""" given `sudoku_checker` a sudoku checker function, attempts to ferret out common issues with checking | |
for valid input. Raises AssertionError s if the function fails to conform to expectations""" | |
try: | |
illegal = (0, [], range(10), [range(10), range(10), 0, range(10), 1, range(10), range(10), range(10), range(10)]) | |
valid_row = range(1, 10) | |
invalid = ([[1]*9] * 9, [valid_row * 8] + [range(2, 11)]) | |
for s in illegal: | |
res = sudoku_checker(s) | |
assert res is None, "Failed to detect that {s} was illegal. Returned {res} instead of `None`".format(s=s, res=res) | |
for s in invalid: | |
res = sudoku_checker(s) | |
assert res is False, "Failed to return False for invalid sudoku {s}. Returned {res} instead of `False`".format(s=s, res=res) | |
except AssertionError: | |
raise | |
except Exception as e: | |
raise AssertionError("Raised error type {etype} with msg {emsg}.".format(etype=type(e), emsg=str(e))) |
Didn't feel like adding it right now, but I've got a really evil test: For invalid, add a grid containing 8 rows of 9 zeroes, plus 1 set containing 1..9. That should be invalid because it contains a set instead of list, but if your code tries to detect invalid elements by attempting to iterate over each row and seeing whether it fails, the code will break as sets are iterable.
I added it to the repo I made for this (put an argparse wrapper around it so you could run it more easily) jtratner/sudoku-fuzzer-udacity@9017adf . It's a neat case ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have additional test cases, definitely edit this gist and add them in!