Skip to content

Instantly share code, notes, and snippets.

@jtratner
Created July 12, 2012 03:26
Show Gist options
  • Save jtratner/3095486 to your computer and use it in GitHub Desktop.
Save jtratner/3095486 to your computer and use it in GitHub Desktop.
Fuzzer for CS258 Sudoku Checker
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)))
@jtratner
Copy link
Author

If you have additional test cases, definitely edit this gist and add them in!

@rbohrer
Copy link

rbohrer commented Jul 12, 2012

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.

@jtratner
Copy link
Author

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