# My name: # Date: # Assignment: Extra Credit 1 # My Collaboration Statement: #My functions go here: #This function takes two parameters. The first is a function, and the second #is a list of test cases. If the function returns the correct values for #each test case, it passes. Otherwise, it fails! This function is a helper #function for the main test() function below. def testFunction(func,cases): allPassed = True for test in cases: if (test[0] == 0): result = func() elif (test[0] == 1): result = func(test[1]) elif (test[0] == 2): result = func(test[1], test[2]) if result != test[-1]: print "FAIL: Input:", for x in range(1,test[0]+1): print test[x] print "gives output:", result, "and I was expecting:",test[-1] allPassed = False if (allPassed): print "PASSED all tests, Congrats!\n" #A test function that can be used to test student code: def test(): #Try testing the countJs() function: print "1. countJs()" try: testCases = [ (1,"Jay Summet",1), (1,"No big js",0), (1,"JJJJJJ",6) ] testFunction(countJs,testCases) except: print "Error: Unable to test the countJ's function!" raise #Try testing the maximum() function: print "2. maximum()" try: testCases = [ (1, [5,6,7,8], 8), (1, [7,5,4,1], 7), (1, [], None), (1, [True, False,5,"boo!"],"boo!")] testFunction(maximum,testCases) except: print "Error: Unable to test the maximum function!" raise #Try testing the changeGrade() function. print "3. changeGrade()" try: testCases = [ (1, "Jay","Jay"), (1, "Grade is B", "Grade is A"), (1, "ABCDEF","AABCED"), (1, "abcdef","abcdef"),(1, "","")] testFunction(changeGrade,testCases) except: print "Error: Unable to test the changeGrade() function!" raise #Try testing the rotateText() function: print "4. rotateText()" try: testCases = [ (2,'Guvf vf n 923. grfg.',13, 'This is a 923. test.'), (2,'Iwxh xh p 923. ithi.',11, 'This is a 923. test.') ] testFunction(rotateText, testCases) except: print "Error: unable to test the rotateText function!" raise print "5. blocksInPyramidHeight()" try: testCases = [ (1, 1, 1), (1, 2, 3), (1, 3, 6), (1, 4, 10), ] testFunction(blocksInPyramidHeight, testCases) except: print "Error: unable to test the blocksInPyramidHeight function!" raise print "6. binaryToDecimal()" try: testCases = [ (1, "0000", 0), (1, "11", 3), (1, "0001", 1), (1, "1001", 9), (1, "1111111111", 1023) ] testFunction(binaryToDecimal, testCases) except: print "Error: unable to test the binaryToDecimal function!" raise print "7. blocksInPyramidWidth()" try: testCases = [ (1, 1, 1), (1, 2, 3), (1, 3, 6), (1, 4, 10), ] testFunction(blocksInPyramidWidth, testCases) except: print "Error: Unable to test the blocksInPyramidWidth function!" raise print "8. everyOtherLetter()" try: testCases = [ (1, "This is a test.", "Ti sats."), (1, "CS 1301 rocks!", "C 31rcs"), (1, "USSRE7R==ridde1e0tt", "USER=id10t"), ] testFunction(everyOtherLetter, testCases) except: print "Error: Unable to test the everyOtherLetter function!" raise print "9. sumNestedList()" try: testCases = [ (1, [4,5, [2,3, [1.5,2] ] ], 17.5), (1, [1,2,3], 6), (1, [4.5, True, "Not a Num!", [2,3,"a"],44.5], 54.0), (1, [2.7, 44, [10, 15, [0,0,5,-10],44.5],8], 119.2) ] testFunction(sumNestedList,testCases) except: print "Error: Unable to test the sumNestedList() function!" raise print "10. tupleUnpacking()" try: testCases = [ (1, ("Jay", 7), 'Jay likes the number 7'), (1, ("Sam", 5), 'Sam likes the number 5') ] testFunction(tupleUnpacking,testCases) except: print "Error: Unable to test the tupleUnpacking() function!" raise