-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ps4a.py
executable file
·200 lines (152 loc) · 6.72 KB
/
test_ps4a.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from ps4a import *
#
# Test code
# You don't need to understand how this test code works (but feel free to look it over!)
# To run these tests, simply run this file (open up in IDLE, then run the file as normal)
def test_getWordScore():
"""
Unit test for getWordScore
"""
failure=False
# dictionary of words and scores
words = {("", 7):0, ("it", 7):4, ("was", 7):18, ("scored", 7):54, ("waybill", 7):155, ("outgnaw", 7):127, ("fork", 7):44, ("fork", 4):94}
for (word, n) in words.keys():
score = getWordScore(word, n)
if score != words[(word, n)]:
print "FAILURE: test_getWordScore()"
print "\tExpected", words[(word, n)], "points but got '" + str(score) + "' for word '" + word + "', n=" + str(n)
failure=True
if not failure:
print "SUCCESS: test_getWordScore()"
# end of test_getWordScore
def test_updateHand():
"""
Unit test for updateHand
"""
# test 1
handOrig = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
handCopy = handOrig.copy()
word = "quail"
hand2 = updateHand(handCopy, word)
expectedHand1 = {'l':1, 'm':1}
expectedHand2 = {'a':0, 'q':0, 'l':1, 'm':1, 'u':0, 'i':0}
if hand2 != expectedHand1 and hand2 != expectedHand2:
print "FAILURE: test_updateHand('"+ word +"', " + str(handOrig) + ")"
print "\tReturned: ", hand2, "\n\t-- but expected:", expectedHand1, "or", expectedHand2
return # exit function
if handCopy != handOrig:
print "FAILURE: test_updateHand('"+ word +"', " + str(handOrig) + ")"
print "\tOriginal hand was", handOrig
print "\tbut implementation of updateHand mutated the original hand!"
print "\tNow the hand looks like this:", handCopy
return # exit function
# test 2
handOrig = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2}
handCopy = handOrig.copy()
word = "evil"
hand2 = updateHand(handCopy, word)
expectedHand1 = {'v':1, 'n':1, 'l':1}
expectedHand2 = {'e':0, 'v':1, 'n':1, 'i':0, 'l':1}
if hand2 != expectedHand1 and hand2 != expectedHand2:
print "FAILURE: test_updateHand('"+ word +"', " + str(handOrig) + ")"
print "\tReturned: ", hand2, "\n\t-- but expected:", expectedHand1, "or", expectedHand2
return # exit function
if handCopy != handOrig:
print "FAILURE: test_updateHand('"+ word +"', " + str(handOrig) + ")"
print "\tOriginal hand was", handOrig
print "\tbut implementation of updateHand mutated the original hand!"
print "\tNow the hand looks like this:", handCopy
return # exit function
# test 3
handOrig = {'h': 1, 'e': 1, 'l': 2, 'o': 1}
handCopy = handOrig.copy()
word = "hello"
hand2 = updateHand(handCopy, word)
expectedHand1 = {}
expectedHand2 = {'h': 0, 'e': 0, 'l': 0, 'o': 0}
if hand2 != expectedHand1 and hand2 != expectedHand2:
print "FAILURE: test_updateHand('"+ word +"', " + str(handOrig) + ")"
print "\tReturned: ", hand2, "\n\t-- but expected:", expectedHand1, "or", expectedHand2
return # exit function
if handCopy != handOrig:
print "FAILURE: test_updateHand('"+ word +"', " + str(handOrig) + ")"
print "\tOriginal hand was", handOrig
print "\tbut implementation of updateHand mutated the original hand!"
print "\tNow the hand looks like this:", handCopy
return # exit function
print "SUCCESS: test_updateHand()"
# end of test_updateHand
def test_isValidWord(wordList):
"""
Unit test for isValidWord
"""
failure=False
# test 1
word = "hello"
handOrig = getFrequencyDict(word)
handCopy = handOrig.copy()
if not isValidWord(word, handCopy, wordList):
print "FAILURE: test_isValidWord()"
print "\tExpected True, but got False for word: '" + word + "' and hand:", handOrig
failure = True
# Test a second time to see if wordList or hand has been modified
if not isValidWord(word, handCopy, wordList):
print "FAILURE: test_isValidWord()"
if handCopy != handOrig:
print "\tTesting word", word, "for a second time - be sure you're not modifying hand."
print "\tAt this point, hand ought to be", handOrig, "but it is", handCopy
else:
print "\tTesting word", word, "for a second time - have you modified wordList?"
wordInWL = word in wordList
print "The word", word, "should be in wordList - is it?", wordInWL
print "\tExpected True, but got False for word: '" + word + "' and hand:", handCopy
failure = True
# test 2
hand = {'r': 1, 'a': 3, 'p': 2, 'e': 1, 't': 1, 'u':1}
word = "rapture"
if isValidWord(word, hand, wordList):
print "FAILURE: test_isValidWord()"
print "\tExpected False, but got True for word: '" + word + "' and hand:", hand
failure = True
# test 3
hand = {'n': 1, 'h': 1, 'o': 1, 'y': 1, 'd':1, 'w':1, 'e': 2}
word = "honey"
if not isValidWord(word, hand, wordList):
print "FAILURE: test_isValidWord()"
print "\tExpected True, but got False for word: '"+ word +"' and hand:", hand
failure = True
# test 4
hand = {'r': 1, 'a': 3, 'p': 2, 't': 1, 'u':2}
word = "honey"
if isValidWord(word, hand, wordList):
print "FAILURE: test_isValidWord()"
print "\tExpected False, but got True for word: '" + word + "' and hand:", hand
failure = True
# test 5
hand = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2}
word = "evil"
if not isValidWord(word, hand, wordList):
print "FAILURE: test_isValidWord()"
print "\tExpected True, but got False for word: '" + word + "' and hand:", hand
failure = True
# test 6
word = "even"
if isValidWord(word, hand, wordList):
print "FAILURE: test_isValidWord()"
print "\tExpected False, but got True for word: '" + word + "' and hand:", hand
print "\t(If this is the only failure, make sure isValidWord() isn't mutating its inputs)"
failure = True
if not failure:
print "SUCCESS: test_isValidWord()"
wordList = loadWords()
print "----------------------------------------------------------------------"
print "Testing getWordScore..."
test_getWordScore()
print "----------------------------------------------------------------------"
print "Testing updateHand..."
test_updateHand()
print "----------------------------------------------------------------------"
print "Testing isValidWord..."
test_isValidWord(wordList)
print "----------------------------------------------------------------------"
print "All done!"