# A simple test of binary search algorithms def bs_test(bs): """ A test framework for a simple binary search procedure. """ errors = 0 for size in range(0,33): vals = range(0,2*(size-1),2) # Test something that precedes everything index = bs(-1, vals) if (index != -1): print("ERROR! For bs(-1,", list(vals), "), expected -1 and got", index) errors = errors + 1 # Test values in the array for i in vals: index = bs(i, vals) if (index != i/2): print("ERROR! For bs(", i, ",", list(vals), ") expected", int(i/2), "and got", index) errors = errors + 1 # Test values in the array for i in vals: # Test values not in the array index = bs(i+1, vals) if (index != -1): print("ERROR! For bs(-1,", list(vals), "), expected -1 and got", index) errors = errors + 1 # Report results if (errors == 1): print("One error encountered.") return False elif (errors > 1): print(errors, "errors encountered.") return False else: return True