-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcv_test.py
60 lines (50 loc) · 2.42 KB
/
rcv_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
from rcv import *
import unittest
from test import support
import csv
class ElectionTest(unittest.TestCase):
def test_quota(self):
self.assertEquals(calculate_quota(num_winners=1, num_votes=10), 5)
self.assertEquals(calculate_quota(num_winners=1, num_votes=100), 50)
self.assertEquals(calculate_quota(num_winners=3, num_votes=100), 25)
def test_abc_1(self):
winners, exhausted = count_ballots("results/abc.csv", num_winners=1)
self.assertEquals(len(winners), 1)
self.assertEquals(winners[0].name, "Alice")
self.assertEquals(winners[0].votes, 60_000)
self.assertEquals(exhausted.votes, 0)
def test_abc_2(self):
winners, exhausted = count_ballots("results/abc.csv", num_winners=2)
self.assertEquals(len(winners), 2)
self.assertEquals(winners[0].name, "Alice")
self.assertEquals(winners[1].name, "Bill")
self.assertEquals(winners[0].votes, 30_000)
self.assertEquals(winners[1].votes, 20_000)
self.assertEquals(exhausted.votes, 30_000)
def test_bookclub(self):
winners, exhausted = count_ballots("results/book_club_responses.csv", num_winners=1)
self.assertEquals(len(winners), 1)
self.assertEquals(max(winners).name, "[One Day]")
self.assertEquals(exhausted.votes, 0)
def test_elections(self):
winners, exhausted = count_ballots("results/elections.csv", num_winners=1)
self.assertEquals(len(winners), 1)
self.assertEquals(max(winners).votes, 40_000)
self.assertEquals(exhausted.votes, 0)
def test_tie(self):
winners, exhausted = count_ballots("results/tie.csv", num_winners=1)
self.assertEqual(1, len(winners))
self.assertEqual(60_000, max(winners).votes)
def test_7_way_3(self):
winners, exhausted = count_ballots("results/seven_way.csv", num_winners=3)
self.assertEqual(3, len(winners))
self.assertAlmostEqual(35_000, winners[0].votes, delta=1.0)
self.assertIn(winners[1].name, ["Alice", "Fred", "Greg"])
self.assertIn(winners[2].name, ["Alice", "Fred", "Greg"])
self.assertNotEqual(winners[2].name, winners[1].name)
def test_7_way_1(self):
winners, exhausted = count_ballots("results/seven_way.csv", num_winners=1)
self.assertEqual(1, len(winners))
self.assertEqual("Dave", winners[0].name)
if __name__ == '__main__':
unittest.main()