/
tournament_test.py
178 lines (157 loc) · 5.71 KB
/
tournament_test.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
#!/usr/bin/env python
#
# Test cases for tournament.py
from tournament import *
def testDeleteMatches():
deleteMatches()
print "1. Old matches can be deleted."
def testDelete():
deleteMatches()
deletePlayers()
print "2. Player records can be deleted."
def testCount():
deleteMatches()
deletePlayers()
c = countPlayers()
if c == '0':
raise TypeError(
"countPlayers() should return numeric zero, not string '0'.")
# print c
if c != 0:
raise ValueError("After deleting, countPlayers should return zero.")
print "3. After deleting, countPlayers() returns zero."
def testRegister():
deleteMatches()
deletePlayers()
registerPlayer("Chandra Nalaar")
c = countPlayers()
if c != 1:
raise ValueError(
"After one player registers, countPlayers() should be 1.")
print "4. After registering a player, countPlayers() returns 1."
def testRegisterCountDelete():
deleteMatches()
deletePlayers()
registerPlayer("Markov Chaney")
registerPlayer("Joe Malik")
registerPlayer("Mao Tsu-hsi")
registerPlayer("Atlanta Hope")
c = countPlayers()
# print c
if c != 4:
raise ValueError(
"After registering four players, countPlayers should be 4.")
deletePlayers()
c = countPlayers()
if c != 0:
raise ValueError("After deleting, countPlayers should return zero.")
print "5. Players can be registered and deleted."
def testStandingsBeforeMatches():
deleteMatches()
deletePlayers()
registerPlayer("Melpomene Murray")
registerPlayer("Randy Schwartz")
standings = playerStandings()
if len(standings) < 2:
raise ValueError("Players should appear in playerStandings even before "
"they have played any matches.")
elif len(standings) > 2:
raise ValueError("Only registered players should appear in standings.")
if len(standings[0]) != 4:
raise ValueError("Each playerStandings row should have four columns.")
[(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings
# print standings
if matches1 != 0 or matches2 != 0 or wins1 != 0 or wins2 != 0:
raise ValueError(
"Newly registered players should have no matches or wins.")
if set([name1, name2]) != set(["Melpomene Murray", "Randy Schwartz"]):
raise ValueError("Registered players' names should appear in standings, "
"even if they have no matches played.")
print "6. Newly registered players appear in the standings with no matches."
def testReportMatches():
deleteMatches()
deletePlayers()
registerPlayer("Bruno Walton")
registerPlayer("Boots O'Neal")
registerPlayer("Cathy Burton")
registerPlayer("Diane Grant")
standings = playerStandings()
[id1, id2, id3, id4] = [row[0] for row in standings]
reportMatch(id1, id2)
reportMatch(id3, id4)
standings = playerStandings()
# print standings
for (i, n, w, m) in standings:
if m != 1:
raise ValueError("Each player should have one match recorded.")
if i in (id1, id3) and w != 1:
raise ValueError("Each match winner should have one win recorded.")
elif i in (id2, id4) and w != 0:
raise ValueError("Each match loser should have zero wins recorded.")
print "7. After a match, players have updated standings."
def testPairings():
deleteMatches()
deletePlayers()
registerPlayer("Twilight Sparkle")
registerPlayer("Fluttershy")
registerPlayer("Applejack")
registerPlayer("Pinkie Pie")
standings = playerStandings()
[id1, id2, id3, id4] = [row[0] for row in standings]
reportMatch(id1, id2)
reportMatch(id3, id4)
pairings = swissPairings()
# print pairings
if len(pairings) != 2:
raise ValueError(
"For four players, swissPairings should return two pairs.")
[(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4)] = pairings
correct_pairs = set([frozenset([id1, id3]), frozenset([id2, id4])])
actual_pairs = set([frozenset([pid1, pid2]), frozenset([pid3, pid4])])
if correct_pairs != actual_pairs:
raise ValueError(
"After one match, players with one win should be paired.")
print "8. After one match, players with one win are paired."
# Testing the code with 8 players
# deleteMatches()
# deletePlayers()
# registerPlayer("A")
# registerPlayer("B")
# registerPlayer("C")
# registerPlayer("D")
# registerPlayer("E")
# registerPlayer("F")
# registerPlayer("G")
# registerPlayer("H")
# standings = playerStandings()
# [id1, id2, id3, id4, id5, id6, id7, id8] = [row[0] for row in standings]
# reportMatch(id1, id2)
# reportMatch(id3, id4)
# reportMatch(id6, id5)
# reportMatch(id7, id8)
# pairings = swissPairings()
# print pairings
# [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4), (pid5, pname5, pid6, pname6), (pid7, pname7, pid8, pname8)] = pairings
# reportMatch(pid2,pid1)
# reportMatch(pid3,pid4)
# reportMatch(pid6,pid5)
# reportMatch(pid8,pid7)
# pairings = swissPairings()
# print pairings
# [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4), (pid5, pname5, pid6, pname6), (pid7, pname7, pid8, pname8)] = pairings
# reportMatch(pid1,pid2)
# reportMatch(pid4,pid3)
# reportMatch(pid6,pid5)
# reportMatch(pid8,pid7)
# pairings = swissPairings()
# print pairings
if __name__ == '__main__':
testDeleteMatches()
testDelete()
testCount()
testRegister()
testRegisterCountDelete()
testStandingsBeforeMatches()
testReportMatches()
testPairings()
print "Success! All tests pass!"