-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestElection.py
More file actions
59 lines (45 loc) · 1.83 KB
/
TestElection.py
File metadata and controls
59 lines (45 loc) · 1.83 KB
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
from election import Election
import unittest
from voter import Voter
class TestElection(unittest.TestCase):
def test_constructor(self):
e = Election(10, 100)
self.assertEqual(len(e.candidates), 10)
self.assertEqual(len(e.voters), 100)
def test_vote(self):
e = Election(1, 100)
num_issues = len(e.candidates[0].preferences)
candidates = [
Voter(preferences=[[0, 0] for i in range(num_issues)]),
Voter(preferences=[[-5000, 0] for i in range(num_issues)]),
]
e.set_candidates(candidates)
self.assertEqual(e.vote(), candidates[0])
candidates.append(candidates.pop(0))
self.assertEqual(e.vote(), candidates[1])
def test_rank(self):
e = Election(1, 100)
num_issues = len(e.candidates[0].preferences)
candidates = [
Voter(preferences=[[0, 0] for i in range(num_issues)]),
Voter(preferences=[[-5000, 0] for i in range(num_issues)]),
]
e.set_candidates(candidates)
self.assertEqual(e.ranked_choice(), candidates[0])
candidates.append(candidates.pop(0))
self.assertEqual(e.ranked_choice(), candidates[1])
def test_approval(self):
e = Election(1, 100)
num_issues = len(e.candidates[0].preferences)
candidates = [
Voter(preferences=[[i, 0] for i in range(num_issues)]),
Voter(preferences=[[-5 - i * 10000, 0] for i in range(num_issues)]),
]
voters = [Voter(preferences=[[i, 1] for i in range(num_issues)]) for _ in range(100)]
e.set_candidates(candidates)
e.set_voters(voters)
self.assertEqual(e.approval(), candidates[0])
candidates.append(candidates.pop(0))
self.assertEqual(e.approval(), candidates[1])
if __name__ == '__main__':
unittest.main()