Difference between revisions of "CSC111 testHw6.py 2015"
(Created page with "--~~~~ ---- =testHw6.py= <br /> <source lang="python"> # testHw6.py # D. Thiebaut # This program tests all the functions for Homework 6. # To use this program, simply write a...") |
|||
Line 48: | Line 48: | ||
return noMatching/len( userList ) | return noMatching/len( userList ) | ||
+ | |||
+ | # --------------------------------------------------------------------- | ||
+ | def testFunction0(): | ||
+ | try: | ||
+ | score, total = testFunction0b() | ||
+ | return score, total | ||
+ | except: | ||
+ | print( "Function0 does not exist or crashed. 0 out of 4 points." ) | ||
+ | return 0, 4 | ||
+ | |||
+ | def testFunction0b(): | ||
+ | names = [ " HELLO there!", " !", " ! ", " :-) " ] | ||
+ | noTests = len( names ) | ||
+ | |||
+ | numberGood = 0 | ||
+ | for i in range( noTests ): | ||
+ | name = names[i] | ||
+ | expected = "**{0:1}**".format( name.title().strip() ) | ||
+ | |||
+ | userOut = hw6.function0( name ) | ||
+ | |||
+ | if type( userOut ) != type( expected ): | ||
+ | print( "function0 does not return a string. 0 out of 4." ) | ||
+ | return 0, 4 | ||
+ | if expected == userOut: | ||
+ | numberGood = numberGood + 1 | ||
+ | else: | ||
+ | print( "function0 returns", userOut, "instead of", expected + "." ) | ||
+ | |||
+ | print( "Function 0 passes", numberGood, "out of", noTests, "tests." ) | ||
+ | return numberGood, noTests | ||
# --------------------------------------------------------------------- | # --------------------------------------------------------------------- | ||
Line 55: | Line 86: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function1 crashed. 0 out of 4 points." ) | + | print( "Function1 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 72: | Line 103: | ||
print( "Function 1 passes", numberGood, "out of", noTests, "tests." ) | print( "Function 1 passes", numberGood, "out of", noTests, "tests." ) | ||
return numberGood, noTests | return numberGood, noTests | ||
− | + | ||
# --------------------------------------------------------------------- | # --------------------------------------------------------------------- | ||
def testFunction2(): | def testFunction2(): | ||
Line 79: | Line 110: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function2 crashed. 0 out of 4 points." ) | + | print( "Function2 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 109: | Line 140: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function3 crashed. 0 out of 3 points." ) | + | print( "Function3 does not exist or crashed. 0 out of 3 points." ) |
return 0, 3 | return 0, 3 | ||
Line 139: | Line 170: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function4 crashed. 0 out of 3 points." ) | + | print( "Function4 does not exist or crashed. 0 out of 3 points." ) |
return 0, 3 | return 0, 3 | ||
Line 170: | Line 201: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function5 crashed. 0 out of 3 points." ) | + | print( "Function5 does not exist or crashed. 0 out of 3 points." ) |
return 0, 3 | return 0, 3 | ||
Line 208: | Line 239: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function6 crashed. 0 out of 3 points." ) | + | print( "Function6 does not exist or crashed. 0 out of 3 points." ) |
return 0, 3 | return 0, 3 | ||
Line 237: | Line 268: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function7 crashed. 0 out of 3 points." ) | + | print( "Function7 does not exist or crashed. 0 out of 3 points." ) |
return 0, 3 | return 0, 3 | ||
Line 278: | Line 309: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function8 crashed. 0 out of 4 points." ) | + | print( "Function8 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 337: | Line 368: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function9 crashed. 0 out of 4 points." ) | + | print( "Function9 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 367: | Line 398: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function10 crashed. 0 out of 4 points." ) | + | print( "Function10 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 395: | Line 426: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function11 crashed. 0 out of 4 points." ) | + | print( "Function11 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 424: | Line 455: | ||
return score, total | return score, total | ||
except: | except: | ||
− | print( "Function12 crashed. 0 out of 4 points." ) | + | print( "Function12 does not exist or crashed. 0 out of 4 points." ) |
return 0, 4 | return 0, 4 | ||
Line 460: | Line 491: | ||
score = 0 | score = 0 | ||
total = 0 | total = 0 | ||
+ | |||
+ | numberGood, maxGood = testFunction0() | ||
+ | score = score + numberGood | ||
+ | total = total + maxGood | ||
+ | |||
numberGood, maxGood = testFunction1() | numberGood, maxGood = testFunction1() | ||
score = score + numberGood | score = score + numberGood |
Latest revision as of 16:43, 1 March 2015
--D. Thiebaut (talk) 16:19, 1 March 2015 (EST)
testHw6.py
# testHw6.py
# D. Thiebaut
# This program tests all the functions for Homework 6.
# To use this program, simply write a program called hw6.py and
# store your functions in it. The functions are expected to be
# named function1(), function2(), etc.
# To test the the function, simply make sure that test6.py and
# hw6.py are in the same directory, and run test6.py. It will
# automatically import hw6.py and call the different functions,
# passing them parameters and checking the values returned by
# the functions.
import random # that's the module that allows us to pick random quantities
import hw6 # that's your hw6.py module. We don't add ".py" to modules,
# but the file containing the program must have ".py" at the end.
# ---------------------------------------------------------------------
# compares 2 lists an indicate if they are identical. Prints
# information about differences that may exist.
def compare2Lists( functionName, userList, expectedList, listType ):
# same type?
if type( userList ) != type( expectedList ):
print( functionName, "does not return a list of", listType )
return 0
# same length?
if len( userList ) != len( expectedList ):
print( functionName, "returns the wrong number of items in the list" )
print( len( expectedList ), "items are expected, but",
len( userList ), "are returned." )
return 0
# same items in the list?
noMatching = 0
for i in range( len( userList ) ):
if userList[i] == expectedList[i]:
noMatching = noMatching + 1
else:
print( functionName + ": mismatch at Index", i,
userList[i], "found instead of", expectedList[i] )
return noMatching/len( userList )
# ---------------------------------------------------------------------
def testFunction0():
try:
score, total = testFunction0b()
return score, total
except:
print( "Function0 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction0b():
names = [ " HELLO there!", " !", " ! ", " :-) " ]
noTests = len( names )
numberGood = 0
for i in range( noTests ):
name = names[i]
expected = "**{0:1}**".format( name.title().strip() )
userOut = hw6.function0( name )
if type( userOut ) != type( expected ):
print( "function0 does not return a string. 0 out of 4." )
return 0, 4
if expected == userOut:
numberGood = numberGood + 1
else:
print( "function0 returns", userOut, "instead of", expected + "." )
print( "Function 0 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction1():
try:
score, total = testFunction1b()
return score, total
except:
print( "Function1 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction1b():
celsiusTemps = [ -10, 0, 11, 40 ]
expected = [14.0, 32.0, 51.8, 104.0]
numberGood = 0
noTests = len( expected )
for i in range( noTests ):
celsius = celsiusTemps[ i ]
fahr = hw6.function1( celsius )
if ( fahr == expected[ i ] ):
numberGood = numberGood + 1
print( "Function 1 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction2():
try:
score, total = testFunction2b()
return score, total
except:
print( "Function2 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction2b():
celsiusTemps = [ -10, 0, 9, 40 ]
expected = [ 14, 32, 48, 104 ]
numberGood = 0
noTests = len( expected )
for i in range( noTests ):
celsius = celsiusTemps[ i ]
fahr = hw6.function2( celsius )
#print( fahr )
if type( fahr ) != type( 3 ):
print( "Function 2 does not return an int" )
elif fahr != expected[ i ]:
print( "Function 2's output is incorrect for", celsius )
else:
numberGood = numberGood + 1
print( "Function 2 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction3():
try:
score, total = testFunction3b()
return score, total
except:
print( "Function3 does not exist or crashed. 0 out of 3 points." )
return 0, 3
def testFunction3b():
phoneStrings = [ "(413) 555 1212", "(617) 111 2233",
"(919) 777 1234" ]
expected = [ "555 1212", "111 2233", "777 1234" ]
numberGood = 0
noTests = len( expected )
for i in range( noTests ):
longPhone = phoneStrings[ i ]
phone = hw6.function3( longPhone )
if type( phone ) != type( "111 2233" ):
print( "Function 3 does not return a string" )
elif phone != expected[ i ]:
print( "Function 3's output is incorrect for", longPhone )
else:
numberGood = numberGood + 1
print( "Function 3 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction4():
try:
score, total = testFunction4b()
return score, total
except:
print( "Function4 does not exist or crashed. 0 out of 3 points." )
return 0, 3
def testFunction4b():
phoneStrings = [ "The phone is (413) 555 1212.", "+1 (617) 111 2233 is my phone",
"Dial (413) 585 2700 for Smith College" ]
expected = [ "The phone is (XXX) 555 1212.", "+1 (XXX) 111 2233 is my phone",
"Dial (XXX) 585 2700 for Smith College" ]
numberGood = 0
noTests = len( expected )
for i in range( noTests ):
longPhone = phoneStrings[ i ]
phone = hw6.function4( longPhone )
if type( phone ) != type( "111 2233" ):
print( "Function 4 does not return a string" )
elif phone != expected[ i ]:
print( "Function 4's output is incorrect for", longPhone )
else:
numberGood = numberGood + 1
print( "Function 4 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction5():
try:
score, total = testFunction5b()
return score, total
except:
print( "Function5 does not exist or crashed. 0 out of 3 points." )
return 0, 3
def testFunction5b():
lines = [ "My Id is 990111222. Yours?",
"990999999 is the ID.",
"The best student in the race has Id 990112233" ]
expected= [ "My Id is 990123456. Yours?",
"990123456 is the ID.",
"The best student in the race has Id 990123456" ]
numberGood = 0
noTests = len( expected )
for i in range( noTests ):
line = lines[ i ]
anonym = hw6.function5( line )
#print( line )
#print( anonym )
if type( anonym ) != type( "111 2233" ):
print( "Function 5 does not return a string" )
elif anonym != expected[ i ]:
print( "Function 5's output is incorrect for", line )
else:
numberGood = numberGood + 1
print( "Function 5 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction6():
try:
score, total = testFunction6b()
return score, total
except:
print( "Function6 does not exist or crashed. 0 out of 3 points." )
return 0, 3
def testFunction6b():
numberGood = 0
noTests = 3
for i in range( noTests ):
expected1 = random.choice( [ "first line", "premiere ligne", "01 LINE 1" ] )
file = open( "file1.txt", "w" )
file.write( expected1 + "\nsecond line\nthird line." )
file.close()
line1 = hw6.function6( "file1.txt" )
if type( line1 ) != type( "hello" ):
print( "Function 6 does not return a string" )
elif line1 != expected1:
print( "Function 6 returns the wrong line: " + line1 )
else:
numberGood = numberGood + 1
print( "Function 6 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction7():
try:
score, total = testFunction7b()
return score, total
except:
print( "Function7 does not exist or crashed. 0 out of 3 points." )
return 0, 3
def testFunction7b():
numberGood = 0
noTests = 3
# test 3 times
for i in range( noTests ):
# pick a random name for the file
fileName = random.choice( [ "lines.txt", "dataFile.txt", "file.data" ] )
# pick a last line to put at the end of the file
expected1 = random.choice( [ " IT IS THE LAST LINE!!",
"Last in file!", "DERNIERE LIGNE" ] )
# write the test file
file = open( fileName, "w" )
file.write( "\nSecond line of file\nSecond line\n" + expected1 )
file.close()
# get the line returned by function 7 in hw6.py
line1 = hw6.function7( fileName )
# test if correct
if type( line1 ) != type( "hello" ):
print( "Function 7 does not return a string" )
elif line1 != expected1:
print( "Function 7 returns the wrong line: " + line1 )
else:
numberGood = numberGood + 1
print( "Function 7 passes", numberGood, "out of", noTests, "tests." )
return numberGood, noTests
# ---------------------------------------------------------------------
def testFunction8():
try:
score, total = testFunction8b()
return score, total
except:
print( "Function8 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction8b():
numberGood = 0
noTests = 3
# pick a random name for the file
fileName = random.choice( [ "lines.txt", "dataFile.txt", "file.data" ] )
#
text = ""
for i in range( 4 ):
text = text + random.choice( [ "Good job!\n", "Great game of go today!\n",
"George gest going in the fog\n",
"Simple line\n", "g\n" ] )
# write the test file
file = open( fileName, "w" )
file.write( text )
file.close()
# get the line returned by function 7 in hw6.py
userText = hw6.function8( fileName )
#print( "userText = ", userText )
expected = text.replace( 'g', 'gremlin' )
expected = expected.replace( 'G', 'Gremlin' )
#print( "expected = ", expected )
text = userText
if expected.count( "\n" ) != text.count( "\n" ):
print( "Function8 does not return the right number of lines." )
print( "Function8 returns a string with", text.count( "\n" ), "lines, but" )
print( "the right number should be", expected.count( "\n" ) + "." )
return 0, 4
if type( expected ) != type( text ):
print( "Function8 does not return a string." )
return 0, 4
noExpectedGremlins = expected.lower().count( "gremlin" )
noReturnedGremlins = text.lower().count( "gremlin" )
if ( noExpectedGremlins != noReturnedGremlins ):
print( "Function 8 returns", noReturnedGremlins, "while",
noExpectedGremlins, "are expected." )
return int( 4.0*(noExpectedGremlins -
abs(noExpectedGremlins-noReturnedGremlins))/noExpectedGremlins), 4
print( "Function 8 passes the tests. 4 out of 4." )
return 4, 4
# ---------------------------------------------------------------------
def testFunction9():
try:
score, total = testFunction9b()
return score, total
except:
print( "Function9 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction9b():
farm = [ "hen", "duck", "chicken", "mouse", "cat", "dog", "horse", "pig", "iguana",
"dragon", "unicorn" ]
# remove one animal at random
index = random.randrange( 1, len( farm )-2 )
farm = farm[0:index] + farm[index+1: ]
expected = [ animal for i, animal in enumerate( farm ) if i%2==0 ]
userFarm = hw6.function9( farm )
score = compare2Lists( "Function 9", userFarm, expected, "string" )
if score == 1:
print( "Function 9 passes the tests. 4 out of 4." )
return 4, 4
else:
print( "Function 9 does not return the correct result. ", int(score*4),
"points out of 4." )
return int( score*4 ), 4
# ---------------------------------------------------------------------
def testFunction10():
try:
score, total = testFunction10b()
return score, total
except:
print( "Function10 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction10b():
temps = [ 31, 32, 33, 11, 40 ]
for temp in range( 15 ):
temps.append( random.choice( [100, 0, -5, -30, -32, 34 ] ) )
expected = len( [ k for k in temps if k >= 32 ] )
user = hw6.function10 ( temps )
if user == expected:
print( "Function 10 passes the tests. 4 out of 4." )
return 4, 4
else:
print( "Function 10 returns", user, "but",
expected, "temperatures above 32 are in the list.",
" 0 points out of 4." )
return 0, 4
# ---------------------------------------------------------------------
def testFunction11():
try:
score, total = testFunction11b()
return score, total
except:
print( "Function11 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction11b():
temps = [ 31, 32, 33, 11, 40 ]
for temp in range( 15 ):
temps.append( random.choice( [100, 0, -5, -30, -32, 34 ] ) )
expected = [ k for k in temps if k >= 32 ]
user = hw6.function11 ( temps )
score = compare2Lists( "Function 11", user, expected, "ints" )
if score == 1:
print( "Function 11 passes the tests. 4 out of 4." )
return 4, 4
else:
print( "Function 11 does not return the correct result. ", int(score*4),
"points out of 4." )
return int( score*4 ), 4
# ---------------------------------------------------------------------
def testFunction12():
try:
score, total = testFunction12b()
return score, total
except:
print( "Function12 does not exist or crashed. 0 out of 4 points." )
return 0, 4
def testFunction12b():
q = [ "Problematic", "Below average", "Average", "Very good", "Excellent", "Fail" ]
grades = [ 32, 100, 90, 74, 75, 60, 59, 61, 40, 11, 0 ]
expected = [ q[5], q[4], q[4], q[2], q[3], q[2], q[1], q[2], q[0], q[5], q[5] ]
noMatches = 0
for i, grade in enumerate( grades ):
expectedString = expected[ i ]
userString = hw6.function12( grade )
if type( expectedString ) != type( userString ):
print( "Function 12 does not return a string. 0 out of 4." )
return 0, 4
if expectedString != userString:
print( "Function 12 error: returns", userString, "for", grade, "instead of",
expectedString )
else:
noMatches = noMatches + 1
if noMatches != len( grades ):
print( "Function 12 passes", noMatches, "tests out of", len( grades ), end ="." )
score = int( 4 * noMatches / len( grades ) )
print( score, "out of 4." )
return score, 4
print( "Function 12 passes the tests. 4 out of 4." )
return 4, 4
def main():
score = 0
total = 0
numberGood, maxGood = testFunction0()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction1()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction2()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction3()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction4()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction5()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction6()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction7()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction8()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction9()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction10()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction11()
score = score + numberGood
total = total + maxGood
numberGood, maxGood = testFunction12()
score = score + numberGood
total = total + maxGood
print( "\nYour functions pass {0:1} tests out of {1:1}."
.format( score, total ) )
main()