# check_expect_tuple_within_tolerance.py P. Conrad for CS5NM, Fall 2008 # UC Santa Barbara # # This provides a testing function that will work on tuples of floating point numbers import math # needed for math.fabs function # check_expect_within_tolerance # checks whether a function produces the expected result # but allows some degree of inaccuracy (a "tolerance") # in cases where calculating the exact result may not be feasible # (e.g. quantities involving pi, square roots, etc.) # consumes: # test: a string describing the test # check: the value we are testing (typically result of a function call) # expect: the value we are expecting (typically a literal value) # toleranace: the largest inaccuracy that is permitted # produces: # nothing (there is no return value) # side effect: # prints a message indicating whether the test passed or failed def check_expect_within_tolerance(test,check,expect,tolerance): if (math.fabs(check-expect) <= tolerance): print "Test " + test + " passed (with tolerance " + \ str(tolerance) + ")" else: print "Test " + test + " failed: expected " + str(expect) + \ " but I got " + str(check) + " (tolerance of " + \ str(tolerance) + " exceeded)" # check_expect_tuple_within_tolerance # checks whether a function produces the expected result # but allows some degree of inaccuracy (a "tolerance") # in cases where calculating the exact result may not be feasible # (e.g. quantities involving pi, square roots, etc.) # consumes: # test: a string describing the test # check: the tuple we are testing (typically result of a function call) # expect: the tuple we are expecting (typically a literal value) # toleranace: the largest inaccuracy that is permitted # produces: # nothing (there is no return value) # side effect: # prints a message indicating whether the test passed or failed def check_expect_tuple_within_tolerance(test,check,expect,tolerance): # assume that the test will pass--we'll set fail to True if we find a problem fail = False; message = "" # This is the message we print if the test fails if (len(expect)!=len(check)): fail = True; message = "Lengths don't match... length of expect tuple = " + str(len(expect)) + \ ", but length of check tuple is " + str(len(check)) else: length = len(check) # I now know that that is also the length of expect for i in range(length): if (math.fabs(check[i]-expect[i]) > tolerance): # if that was true, then the test failed fail = True; message += " check[" + str(i) + "] doesn't match expect[" + str(i) + "]\n" if (not fail): print "Test " + test + " passed (with tolerance " + \ str(tolerance) + ")" else: print "Test " + test + " failed: " + " failed: expected " + str(expect) + \ " but I got " + str(check) + " (tolerance of " + \ str(tolerance) + " exceeded)\n" + message check_expect_tuple_within_tolerance("This should pass",(1,2,3),(1,2,3),0.001) check_expect_tuple_within_tolerance("This should fail, different lengths",(1,2,3),(1,2,3,4),0.001) check_expect_tuple_within_tolerance("This should fail, different lengths",(1,2,3,4),(1,2,3),0.001) check_expect_tuple_within_tolerance("This should pass",(1,2,3),(0.999,1.9999,3.0001),0.01) check_expect_tuple_within_tolerance("This should fail",(1,2,3),(1,2,4),0.01) check_expect_tuple_within_tolerance("This should fail",(1,2,3),(3,2,1),0.01) check_expect_tuple_within_tolerance("This should fail",(1,2,3),(5,2,3),0.01) check_expect_tuple_within_tolerance("This should pass",[1,2,3],(1,2,3),0.001) check_expect_tuple_within_tolerance("This should fail, different lengths",[1,2,3],(1,2,3,4),0.001) check_expect_tuple_within_tolerance("This should fail, different lengths",[1,2,3,4],(1,2,3),0.001) check_expect_tuple_within_tolerance("This should pass",[1,2,3],(0.999,1.9999,3.0001),0.01) check_expect_tuple_within_tolerance("This should fail",[1,2,3],(1,2,4),0.01) check_expect_tuple_within_tolerance("This should fail",[1,2,3],(3,2,1),0.01) check_expect_tuple_within_tolerance("This should fail",[1,2,3],(5,2,3),0.01)