-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
311 lines (210 loc) · 7.08 KB
/
test.py
File metadata and controls
311 lines (210 loc) · 7.08 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""
To run this program, write in terminal:
>>> py test.py --test <test numbers>
i.e to run the first three tests:
>>> py test.py --test 1 2 3
"""
import argparse
import unittest
from random import seed
from polling import poll, votes, Population, Tally, sigma
from collections import Counter
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--test", default=[], type=int, nargs="*", help="Run given tests.")
parser.add_argument("-s", "--seed", default=0, type=int, help="Set seed.")
options = parser.parse_args()
# ===
class Test_Population(unittest.TestCase):
def setUp(self):
self.pop_dict = {
"a" : 10,
"b" : 20,
"c" : 30,
}
self.empty = Population({})
self.pop = Population(self.pop_dict)
def test_weights(self):
self.assertEqual(self.empty.weights, ())
self.assertEqual(self.pop.weights, tuple(self.pop_dict.values()))
def test_groups(self):
self.assertEqual(self.empty.groups, ())
self.assertEqual(self.pop.groups, tuple(self.pop_dict.keys()))
def test_size(self):
self.assertEqual(self.empty.size, 0)
self.assertEqual(self.pop.size, 60)
def test_len(self):
self.assertEqual(len(self.empty), 0)
self.assertEqual(len(self.pop), 3)
def test_vars(self):
self.assertEqual(vars(self.empty), {})
self.assertEqual(vars(self.pop), self.pop_dict)
def test_getter(self):
self.assertEqual(self.pop["a"], 10)
self.assertEqual(self.pop["b"], 20)
self.assertEqual(self.pop["c"], 30)
def test_p(self):
self.assertEqual(self.pop.p("a"), 10/60)
self.assertEqual(self.pop.p("b"), 20/60)
self.assertEqual(self.pop.p("c"), 30/60)
def test_iterable(self):
for g1, g2 in zip(self.pop, self.pop_dict):
self.assertEqual(g1, g2)
def test_contains(self):
for group in self.pop_dict:
group in self.pop
class Test_Voting(unittest.TestCase):
def setUp(self):
seed(options.seed)
self.votes = votes
def test_empty(self):
population = Population(dict())
vote_count = sum(1 for _vote in self.votes(population, 0))
self.assertEqual(vote_count, 0)
def test_sample(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
for vote in self.votes(population, 5):
self.assertIn(vote, population.groups)
class Test_Tallying(unittest.TestCase):
def setUp(self):
seed(options.seed)
self.Tally = Tally
def test_empty(self):
population = Population(dict())
tally_count = sum(1 for _tally in self.Tally(population, 0, 1))
self.assertEqual(tally_count, 0)
def test_sample(self):
population = Population({
"a" : 20,
"b" : 40,
"c" : 60,
})
N, n = 74, 3
tallies = list(self.Tally(population, N, n))
counts = [sum(tally.values()) for tally in tallies]
self.assertEqual(counts, [25, 25, 24])
self.assertEqual(len(tallies), n)
class Test_Poll_With_Replacement(unittest.TestCase):
def setUp(self):
seed(options.seed)
from functools import partial
self.poll = partial(poll, without_replacement=False)
def test_empty(self):
population = Population(dict())
self.assertEqual(self.poll(population, 0), Counter())
def test_poll_N(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
for N in (3, 7, 31):
polling = self.poll(population, N)
votes = sum(polling.values())
self.assertEqual(votes, N)
def test_uniqueness(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
poll1 = self.poll(population, 15)
poll2 = self.poll(population, 15)
# Probabilistic test
self.assertNotEqual(poll1, poll2)
def test_poll_all(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
N = sum(population.weights)
polling = self.poll(population, N)
# Probabilistic test
self.assertNotEqual(polling, Counter(vars(population)))
self.assertEqual(polling.keys(), vars(population).keys())
class Test_Poll_Without_Replacement(unittest.TestCase):
def setUp(self):
seed(options.seed)
from functools import partial
self.poll = partial(poll, without_replacement=True)
def test_empty(self):
population = Population(dict())
self.assertEqual(self.poll(population, 0), Counter())
def test_poll_N(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
for N in (3, 7, 31):
polling = self.poll(population, N)
votes = sum(polling.values())
self.assertEqual(votes, N)
def test_uniqueness(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
poll1 = self.poll(population, 15)
poll2 = self.poll(population, 15)
# Probabilistic test
self.assertNotEqual(poll1, poll2)
def test_poll_all(self):
population = Population({
"a" : 10,
"b" : 20,
"c" : 30,
})
N = sum(population.weights)
polling = poll(population, N, without_replacement=True)
self.assertEqual(polling, Counter(vars(population)))
class Test_Variance(unittest.TestCase):
def test_simple(self):
args_result = {
(0.0,) : 0.0,
(0.1,) : 0.30000000000000004,
(0.3,) : 0.458257569495584,
(0.5,) : 0.5,
(1.0,) : 0.0,
}
for args, result in args_result.items():
self.assertEqual(sigma(*args), result)
def test_multiple(self):
args_result = {
(0.0, 1) : 0.0,
(0.1, 3) : 0.17320508075688773,
(0.3, 20) : 0.10246950765959598,
(0.5, 90) : 0.05270462766947299,
(1.0, 999) : 0.0,
}
for args, result in args_result.items():
self.assertEqual(sigma(*args), result)
def test_fpc(self):
args_result = {
(0.0, 1, 20) : 0.0,
(0.1, 3, 20) : 0.16383560438182507,
(0.3, 20, 20) : 0.0,
(0.5, 90, 100) : 0.0167506302543202,
(1.0, 999, 1000) : 0.0,
}
for args, result in args_result.items():
self.assertEqual(sigma(*args), result)
tests = [
Test_Population,
Test_Voting,
Test_Tallying,
Test_Poll_With_Replacement,
Test_Poll_Without_Replacement,
Test_Variance,
]
tests = [tests[t-1] for t in options.test]
runner = unittest.TextTestRunner()
for test in tests:
test_suite = unittest.defaultTestLoader.loadTestsFromTestCase(test)
result = runner.run(test_suite)