# CSE 143, AU10, Alyssa Harding # # A basic testing program for sorted list. # import all the methods from your sortedlist file from sortedlist import * # These testing methods will work if you name your list field "list" # Compares the expected result to the result of calling the given function # on the given list, and prints out the results # # function - the name of the function to call # list - the list to call it on # input - any other parameters to pass the function # expected - the expected result # def assert_equals(expected, function, input = None): result = function() if input == None else function(input) print function.__name__ + '(' + ('' if input == None else str(input)) + ')', if result == expected: print '=', result, "\n\t(This is the expected result!)" else: print "!=", result, "\n\t(The expected result is:", str(expected) + ')' print def assert_result(expected, function, list, input = None): function() if input == None else function(input) print function.__name__ + '(' + ('' if input == None else str(input)) + ')', if list.list == expected: print '=', list.list, "\n\t(This is the expected result!)" else: print "!=", list.list, "\n\t(The expected result is:", str(expected) + ')' print list = SortedList(False) # tests on an empty list assert_equals(0, list.size) # This should raise an exception #assert_equals(None, max, []) assert_equals(False, list.getUnique) # add tests assert_result([1], list.add, list, 1) assert_result([1,2], list.add, list, 2) assert_result([1,2,3], list.add, list, 3) # Size tests assert_equals(3, list.size) # Max and min tests assert_equals(1, list.min) assert_equals(3, list.max) # indexOf tests assert_equals(0, list.indexOf, 1) assert_equals(1, list.indexOf, 2) assert_equals(-1, list.indexOf, 42) # contains tests assert_equals(True, list.contains, 1) assert_equals(True, list.contains, 3) assert_equals(False, list.contains, 42) # remove tests assert_result([2,3], list.remove, list, 0) assert_result([2], list.remove, list, 1) # setUnique tests list.add(1) list.add(1) list.add(2) list.add(3) list.add(4) assert_result([1,1,2,2,3,4], list.setUnique, list, False) assert_result([1,2,3,4], list.setUnique, list, True) assert_result([1,2,3,4], list.setUnique, list, False) # Add more of your own tests here! You can use the "assert_equals" method if # you like, or call methods and print out the results