| 15 |
jtkorb |
1 |
#!/p/python/python
|
|
|
2 |
|
|
|
3 |
import os
|
|
|
4 |
import random
|
|
|
5 |
|
|
|
6 |
import parseinput
|
|
|
7 |
import tally
|
|
|
8 |
|
|
|
9 |
def testfile(dirpath, filename):
|
|
|
10 |
print filename + ":",
|
|
|
11 |
filenameCSV = os.path.join(dirpath, filename)
|
|
|
12 |
filenameOut = os.path.splitext(filenameCSV)[0] + ".out"
|
|
|
13 |
result = tallyfile(filenameCSV)
|
|
|
14 |
handleresult(result, filename, filenameOut)
|
|
|
15 |
|
|
|
16 |
def tallyfile(filenameCSV):
|
|
|
17 |
nWinners, ballots = parseinput.parsefile(filenameCSV)
|
|
|
18 |
random.seed(1)
|
|
|
19 |
return str(tally.dotally(nWinners, ballots)) + "\n"
|
|
|
20 |
|
|
|
21 |
def handleresult(result, filename, filenameOut):
|
|
|
22 |
if os.path.isfile(filenameOut):
|
|
|
23 |
fOut = open(filenameOut)
|
|
|
24 |
correct = fOut.read()
|
|
|
25 |
if result == correct:
|
|
|
26 |
print "OK"
|
|
|
27 |
else:
|
|
|
28 |
print "***** MISMATCH *****"
|
|
|
29 |
else:
|
|
|
30 |
print "***** Creating output for " + filename + ": " + result,
|
|
|
31 |
fOut = open(filenameOut, "w")
|
|
|
32 |
fOut.write(result)
|
|
|
33 |
|
|
|
34 |
def main(dir):
|
|
|
35 |
tally.fTrace = 0
|
|
|
36 |
for dirpath, dirnames, filenames in os.walk(dir):
|
|
|
37 |
for filename in filenames:
|
|
|
38 |
if filename.endswith(".csv"):
|
|
|
39 |
testfile(dirpath, filename)
|
|
|
40 |
|
|
|
41 |
if __name__=="__main__":
|
|
|
42 |
main("tests")
|