-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
1331 lines (1160 loc) · 41.2 KB
/
tests.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Unit tests for election.py."""
from __future__ import print_function
import unittest
from election import Ballot, Candidate, Election, NoConfidence
__author__ = "Devin Gund"
__copyright__ = "Copyright 2017, Carnegie Mellon University Undergraduate Student Senate"
__credits__ = ["Sushain Cherivirala"]
__license__ = "GPLv3"
__status__ = "Production"
def candidates_for_ids(candidate_ids):
"""Returns an ordered list of Candidates for an ordered list of ids.
This is helpful for tests where candidates on ballots can be defined as
short strings (i.e. 'A', 'B', 'NC') in the test cases, which can then be
converted to proper Candidates.
Args:
candidate_ids: List of strings representing candidate ids.
Returns:
List of Candidates corresponding to the given ids.
"""
candidate_for_id = {
'NC': NoConfidence(),
'A': Candidate('dgund', name='Devin Gund'),
'B': Candidate('gwashington', name='George Washington'),
'C': Candidate('jadams', name='John Adams'),
'D': Candidate('tjefferson', name='Thomas Jefferson'),
'E': Candidate('jmadison', name='James Madison'),
'F': Candidate('jmonroe', name='James Monroe'),
'G': Candidate('jqadams', name='John Quincy Adams'),
'H': Candidate('ajackson', name='Andrew Jackson'),
'I': Candidate('mvburen', name='Martin Van Buren'),
'J': Candidate('wharrison', name='William Harrison'),
'K': Candidate('jtyler', name='John Tyler'),
'L': Candidate('jpolk', name='James Polk'),
'M': Candidate('ztaylor', name='Zachary Taylor'),
'N': Candidate('mfillmore', name='Millard Fillmore'),
'O': Candidate('fpierce', name='Franklin Pierce'),
'P': Candidate('jbuchanan', name='James Buchanan'),
'Q': Candidate('alincoln', name='Abraham Lincoln'),
'R': Candidate('ajohnson', name='Andrew Johnson'),
'S': Candidate('ugrant', name='Ulysses Grant'),
'T': Candidate('rhayes', name='Rutherford Hayes'),
'U': Candidate('jgarfield', name='James Garfield'),
'V': Candidate('carthur', name='Chester Arthur'),
'W': Candidate('gcleveland', name='Grover Cleveland'),
'X': Candidate('bharrison', name='Benjamin Harrison'),
'Y': Candidate('wmckinley', name='William McKinley'),
'Z': Candidate('troosevelt', name='Theodore Roosevelt'),
}
candidates = []
for candidate_id in candidate_ids:
candidates.append(candidate_for_id[candidate_id])
return candidates
def ballots_for_candidates(candidates, count):
"""Returns a list of Ballots for the given Candidates and count.
Args:
candidates: List of Candidates.
count: Integer value of the number of Ballots to create.
Returns:
List of Ballots for the given Candidates, with the given count.
"""
ballots = []
for i in range(count):
ballot = Ballot()
ballot.set_candidates(candidates)
ballots.append(ballot)
return ballots
def ballots_for_ids(candidate_ids, count):
"""Returns a list of Ballots for the given ids and count.
Args:
candidate_ids: List of strings representing candidate ids.
count: Integer value of the number of Ballots to create.
Returns:
List of Ballots for the given ids, with the given count.
"""
return ballots_for_candidates(candidates_for_ids(candidate_ids), count)
class TestSmallElections(unittest.TestCase):
def test_1_candidate_1_seat(self):
"""Tests a 1 candidate election for 1 seat.
Expected winners: A
Round 0
Ballots:
10 * [A]
Votes:
A: 10
Threshold: (10) / (1+1) + 1 = 6
Result: A is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A']))
seats = 1
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 10))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_2_candidates_1_seat(self):
"""Tests a 2 candidate election for 1 seat.
Expected winners: B
Round 0
Ballots:
5 * [A, B]
10 * [B, A]
Votes:
A: 5
B: 10
Threshold: (15) / (1+1) + 1 = 8.5
Result: B is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['B']))
seats = 1
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A', 'B'], 5) +
ballots_for_ids(['B', 'A'], 10))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_3_candidates_1_seat(self):
"""Tests a 3 candidate election for 1 seat.
Expected winners: C
Round 0
Ballots:
5 * [A, C]
10 * [B]
7 * [C]
Votes:
A: 7
B: 10
C: 5
Threshold: (7+10+5) / (1+1) + 1 = 12
Result: A is eliminated
Round 1
Ballots:
10 * [B]
12 * [C]
Votes:
B: 10
C: 12
Threshold: (7+10+5) / (1+1) + 1 = 12
Result: C is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['C']))
seats = 1
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A', 'C'], 5) +
ballots_for_ids(['B'], 10) +
ballots_for_ids(['C'], 7))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_1_candidate_2_seats(self):
"""Tests a 1 candidate election for 2 seats.
Expected winners: A
Round 0
Ballots:
10 * [A]
Votes:
A: 10
Threshold: (10) / (2+1) + 1 = 4.333
Result: A is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 10))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_3_candidates_2_seats(self):
"""Tests a 3 candidate election for 2 seats.
Expected winners: C
Round 0
Ballots:
7 * [A]
10 * [B, C]
7 * [C]
Votes:
A: 7
B: 10
C: 7
Threshold: (7+10+7) / (2+1) + 1 = 9
Result: B is elected, with a surplus of 1
Each ballot is redistributed with value * (1/10) = value * .1
Round 1
Ballots:
7 * [A]
10 * [C] (with value .1)
7 * [C]
Votes:
A: 7
C: 8
Threshold: (7+8) / (1+1) + 1 = 8.5
Result: A is eliminated
Round 2
Ballots:
10 * [C] (with value .1)
7 * [C]
Votes:
C: 8
Threshold: (8) / (1+1) + 1 = 5
Result: C is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['B', 'C']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 7) +
ballots_for_ids(['B', 'C'], 10) +
ballots_for_ids(['C'], 7))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_4_candidates_2_seats(self):
"""Tests a 4 candidate election for 2 seats.
Expected winners: A, B
Round 0
Ballots:
10 * [A, NC]
16 * [B, NC]
15 * [C, NC]
7 * [D, A]
Votes:
A: 10
B: 16
C: 15
D: 7
Threshold: (10+16+15+7) / (2+1) + 1 = 17
Result: D is eliminated
Round 1
Ballots:
10 * [A, NC]
16 * [B, NC]
15 * [C, NC]
7 * [A]
Votes:
A: 17
B: 16
C: 15
Threshold: (10+16+15+7) / (2+1) + 1 = 17
Result: A is elected
Round 2
Ballots:
16 * [B, NC]
15 * [C, NC]
Votes:
B: 16
C: 15
Threshold: (16+15) / (1+1) + 1 = 16.5
Result: C is eliminated
Round 3
Ballots:
16 * [B, NC]
15 * [NC]
Votes:
B: 16
NC: 15
Threshold: (16+15) / (1+1) + 1 = 16.5
Result: NC is eliminated
Round 4
Ballots:
16 [B, NC]
Votes:
B: 16
Threshold: (16+15) / (1+1) + 1 = 16.5
Result: B is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A', 'B']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A', 'NC'], 10) +
ballots_for_ids(['B', 'NC'], 16) +
ballots_for_ids(['C', 'NC'], 15) +
ballots_for_ids(['D', 'A'], 7))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
class TestNoConfidence(unittest.TestCase):
def test_nc_halt_early(self):
""" Tests a 3 candidate election for 3 seats.
Once No Confidence is elected, the election should end.
Expected winners: B, NC
Round 0
Ballots:
5 * [A, NC]
6 * [B, NC]
4 * [C, NC]
5 * [NC]
Votes:
A: 5
B: 6
C: 4
NC: 5
Threshold: (6+5+4+5) / (3+1) + 1 = 6
Result: B is elected
Round 1
Ballots:
5 * [A, NC]
4 * [C, NC]
5 * [NC]
Votes:
A: 5
C: 4
NC: 5
Threshold: (5+4+5) / (2+1) + 1 = 5.666
Result: C is eliminated
Round 2
Ballots:
5 * [A, NC]
9 * [NC]
Votes:
A: 5
NC: 9
Threshold: (5+9) / (2+1) + 1 = 5.666
Result: NC is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['B', 'NC']))
seats = 3
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A', 'NC'], 5) +
ballots_for_ids(['B', 'NC'], 6) +
ballots_for_ids(['C', 'NC'], 4) +
ballots_for_ids(['NC'], 5))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_nc_halt_end(self):
"""Tests a 2 candidate election for 3 seats.
When empty seats are filled, only candidates with votes exceeding that
of No Confidence may be elected.
Expected winners: A, NC
Round 0
Ballots:
10 * [A]
6 * [B]
8 * [NC]
Votes:
A: 10
B: 6
NC: 8
Threshold: (10+6+8) / (3+1) + 1 = 7
Result: A is elected
As the 3 candidates (including No Confidence) can fill the 3 vacant
seats, the candidates are slated to fill the seats. However, B's
votes fall below that of No Confidence, so B is not elected.
"""
# Setup
expected_winners = set(candidates_for_ids(['A', 'NC']))
seats = 3
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 10) +
ballots_for_ids(['B'], 6) +
ballots_for_ids(['NC'], 8))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_election_with_nc_elimination(self):
"""Tests a 3 candidate election for 1 seat.
All of the ballots list a single candidate followed by No Confidence.
Expected winners: A
Round 0
Ballots:
6 * [A, NC]
5 * [B, NC]
1 * [C, NC]
Votes:
A: 6
B: 5
C: 1
Threshold: (6+5+1) / (1+1) + 1 = 7
Result: C is eliminated
Round 1
Ballots:
6 * [A, NC]
5 * [B, NC]
1 * [NC]
Votes:
A: 6
B: 5
NC: 1
Threshold: (6+5+1) / (1+1) + 1 = 7
Result: NC is eliminated
Round 2
Ballots:
6 * [A]
5 * [B]
Votes:
A: 6
B: 5
Threshold: (6+5) / (1+1) + 1 = 6.5
Result: B is eliminated
Round 3
Ballots:
6 * [A]
Votes:
A: 6
Threshold: (6) / (1+1) + 1 = 4
Result: A is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A']))
seats = 1
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A', 'NC'], 6) +
ballots_for_ids(['B', 'NC'], 5) +
ballots_for_ids(['C', 'NC'], 1))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_election_without_nc_elimination(self):
"""Tests a 3 candidate election for 1 seat.
All of the ballots list a single candidate followed by No Confidence.
No Confidence is cannot be eliminated for having the fewest votes.
Expected winners: NC
Round 0
Ballots:
6 * [A, NC]
5 * [B, NC]
1 * [C, NC]
Votes:
A: 6
B: 5
C: 1
Threshold: (6+5+1) / (1+1) + 1 = 7
Result: C is eliminated
Round 1
Ballots:
6 * [A, NC]
5 * [B, NC]
1 * [NC]
Votes:
A: 6
B: 5
NC: 1
Threshold: (6+5+1) / (1+1) + 1 = 7
Result: B is eliminated (because NC cannot be)
Round 2
Ballots:
6 * [A, NC]
6 * [NC]
Votes:
A: 6
NC: 6
Threshold: (6+6) / (1+1) + 1 = 7
Result: A is eliminated (because NC cannot be)
Round 3
Ballots:
12 * [NC]
Votes:
NC: 12
Threshold: (12) / (1+1) + 1 = 7
Result: NC is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['NC']))
seats = 1
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A', 'NC'], 6) +
ballots_for_ids(['B', 'NC'], 5) +
ballots_for_ids(['C', 'NC'], 1))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric,
can_eliminate_no_confidence=False)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
class TestTiebreaks(unittest.TestCase):
def test_bulk_elimination(self):
"""Tests a 4 candidate election for 2 seats.
In the event of a tie to eliminate a candidate, of the tied candidates'
combined vote total is lass than that of the next highest candidate,
eliminate all of the tied candidates.
Expected winners: A, B
Round 0
Ballots:
3 * [A]
3 * [B]
1 * [C, B]
1 * [D, A]
Votes:
A: 3
B: 3
C: 1
D: 1
Threshold: (3+3+1+1) / (2+1) + 1 = 3.666
Result: C and D are eliminated
C and D are tied for fewest votes, but their combined vote total (2)
is less than the next highest candidate (3), so they may be
eliminated in bulk.
Round 0
Ballots:
4 * [A]
4 * [B]
Votes:
A: 4
B: 4
Threshold: (4+4) / (2+1) + 1 = 3.666
Result: A and B are elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A', 'B']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 3) +
ballots_for_ids(['B'], 3) +
ballots_for_ids(['C', 'B'], 1) +
ballots_for_ids(['D', 'A'], 1))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_backward_tiebreak(self):
"""Tests a 4 candidate election for 2 seats.
In the event of a tie to eliminate a candidate, eliminate the candidate
with the fewest votes in the previous round. Repeat for all previous
rounds if necessary.
Expected winners: A, B
Round 0
Ballots:
6 * [A]
3 * [B]
2 * [C]
1 * [D, C]
Votes:
A: 6
B: 3
C: 3
D: 1
Threshold: (6+3+2+1) / (2+1) + 1 = 5
Result: A is elected
Round 1
Ballots:
3 * [B]
2 * [C]
1 * [D, C]
Votes:
B: 3
C: 3
D: 1
Threshold: (3+2+1) / (1+1) + 1 = 4
Result: D is eliminated
Round 2
Ballots:
3 * [B]
3 * [C]
Votes:
B: 3
C: 3
Threshold: (3+3) / (1+1) + 1 = 4
Result: C is eliminated
B and C are tied for fewest votes, but C has fewer votes in the
previous round, so C is eliminated.
Round 3
Ballots:
3 * [B]
Votes:
B: 3
Threshold: (3) / (1+1) + 1 = 2.5
Result: B is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A', 'B']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 6) +
ballots_for_ids(['B'], 3) +
ballots_for_ids(['C'], 2) +
ballots_for_ids(['D', 'C'], 1))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_forward_tiebreak(self):
"""Tests a 3 candidate election for 2 seats.
In the event of a tie to eliminate a candidate, and no backward
tiebreak, eliminate the candidate with the fewest next-choice votes.
Repeat for all subsequent ranks as necessary.
Expected winners: A, C
Round 0
Ballots:
6 * [A]
3 * [B, C]
3 * [C]
Votes:
A: 6
B: 3
C: 3
Threshold: (6+3+3) / (2+1) + 1 = 5
Result: A is elected
Round 1
Ballots:
3 * [B, C]
3 * [C]
Votes:
B: 3
C: 3
Threshold: (3+2+1) / (1+1) + 1 = 4
Result: B is eliminated
B and C are tied for fewest votes in this and previous rounds.
B has fewer next-rank votes, so B is eliminated.
Round 2
Ballots:
6 * [C]
Votes:
C: 6
Threshold: (6) / (1+1) + 1 = 4
Result: C is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A', 'C']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 6) +
ballots_for_ids(['B', 'C'], 3) +
ballots_for_ids(['C'], 3))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_random_tiebreak(self):
"""Tests a 3 candidate election for 2 seats.
In the event of a tie to eliminate a candidate, and no backward
tiebreak or forward tiebreak, eliminate a random candidate based on uid.
Expected winners: A, C
Round 0
Ballots:
6 * [A]
3 * [B]
3 * [C]
Votes:
A: 6
B: 3
C: 3
Threshold: (6+3+3) / (2+1) + 1 = 5
Result: A is elected
Round 1
Ballots:
3 * [B]
3 * [C]
Votes:
B: 3
C: 3
Threshold: (3+3) / (1+1) + 1 = 4
Result: B is elected
B and C are tied for fewest votes in this and all previous rounds,
as well as future ranks. B's uid ('gwashington') is ordered before
C's uid ('jadams') in the tiebreak_alphanumeric, so B is eliminated.
Round 2
Ballots:
3 * [C]
Votes:
C: 3
Threshold: (3) / (1+1) + 1 = 2.5
Result: C is elected
"""
# Setup
expected_winners = set(candidates_for_ids(['A', 'C']))
seats = 2
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_ids(['A'], 6) +
ballots_for_ids(['B'], 3) +
ballots_for_ids(['C'], 3))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
class TestLargeElections(unittest.TestCase):
def test_cgp_grey_animal_kingdom(self):
"""Tests CGP Grey example STV election 'Politics in the Animal Kingdom'
Link: https://www.youtube.com/watch?v=l8XOZJkozfI
Expected winners: Gorilla, Monkey, Tiger
"""
# Setup
tarsier = Candidate('tarsier', name='Tarsier')
gorilla = Candidate('gorilla', name='Gorilla')
monkey = Candidate('monkey', name='Monkey')
tiger = Candidate('tiger', name='Tiger')
lynx = Candidate('lynx', name='Lynx')
expected_winners = set([gorilla, monkey, tiger])
seats = 3
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
num_ballots = 10000
ballots = (
ballots_for_candidates([tarsier, gorilla],
int(.05 * num_ballots)) +
ballots_for_candidates([gorilla, tarsier, monkey],
int(.28 * num_ballots)) +
ballots_for_candidates([monkey],
int(.33 * num_ballots)) +
ballots_for_candidates([tiger],
int(.21 * num_ballots)) +
ballots_for_candidates([lynx, tiger, tarsier, monkey, gorilla],
int(.13 * num_ballots)))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_cgp_grey_stv_election_walkthrough(self):
"""Tests CGP Grey example STV election 'Extra: STV Election Walkthrough'
Link: https://www.youtube.com/watch?v=Ac9070OIMUg
Expected winners: Gorilla, Silverback, Owl, Turtle, Tiger
"""
# Setup
tarsier = Candidate('tarsier', name='Tarsier')
gorilla = Candidate('gorilla', name='Gorilla')
silverback = Candidate('silverback', name='Silverback')
owl = Candidate('owl', name='Owl')
turtle = Candidate('turtle', name='Turtle')
snake = Candidate('snake', name='Snake')
tiger = Candidate('tiger', name='Tiger')
lynx = Candidate('lynx', name='Lynx')
jackalope = Candidate('jackalope', name='Jackalope')
buffalo = Candidate('buffalo', name='Buffalo')
expected_winners = set([gorilla, silverback, owl, turtle, tiger])
seats = 5
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
num_ballots = 10000
ballots = (
ballots_for_candidates([tarsier, silverback],
int(.05 * num_ballots)) +
ballots_for_candidates([gorilla, silverback],
int(.21 * num_ballots)) +
ballots_for_candidates([gorilla, tarsier, silverback],
int(.11 * num_ballots)) +
ballots_for_candidates([silverback],
int(.03 * num_ballots)) +
ballots_for_candidates([owl, turtle],
int(.33 * num_ballots)) +
ballots_for_candidates([turtle],
int(.01 * num_ballots)) +
ballots_for_candidates([snake, turtle],
int(.01 * num_ballots)) +
ballots_for_candidates([tiger],
int(.16 * num_ballots)) +
ballots_for_candidates([lynx, tiger],
int(.04 * num_ballots)) +
ballots_for_candidates([jackalope],
int(.02 * num_ballots)) +
ballots_for_candidates([buffalo, jackalope],
int(.02 * num_ballots)) +
ballots_for_candidates([buffalo, jackalope, turtle],
int(.01 * num_ballots)))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_wikipedia_food_selection(self):
"""Tests Wikipedia's example STV election, using choices of food.
Link: https://en.wikipedia.org/wiki/Single_transferable_vote#Example
Expected winners: Chocolate, Oranges, Strawberries
"""
# Setup
chocolate = Candidate('chocolate', name='Chocolate')
oranges = Candidate('oranges', name='Oranges')
pears = Candidate('pears', name='Pears')
strawberries = Candidate('strawberries', name='Strawberries')
sweets = Candidate('sweets', name='Sweets')
expected_winners = set([chocolate, oranges, strawberries])
seats = 3
tiebreak_alphanumeric = 'abcdefghijklmnopqrstuvwxyz'
ballots = (
ballots_for_candidates([oranges], 4) +
ballots_for_candidates([pears, oranges], 2) +
ballots_for_candidates([chocolate, strawberries], 8) +
ballots_for_candidates([chocolate, sweets], 4) +
ballots_for_candidates([strawberries], 1) +
ballots_for_candidates([sweets], 1))
# Test
election = Election(seats=seats,
ballots=ballots,
random_alphanumeric=tiebreak_alphanumeric)
results = election.compute_results()
self.assertEqual(expected_winners, results.candidates_elected)
def test_florida_2000_presidential(self):
"""Tests the 2000 Florida presidential election, scaled 1%.
An example of a election between two major candidates and a third-party.
For the sake of ranking votes, this test assumes that Nader supporters
prefer Gore to Bush by a 2:1 margin, which is not necessarily true.
Expected winners: Gore
Round 0
Ballots:
29127 * [Bush]
29122 * [Gore]
324 * [Nader, Bush]
649 * [Nader, Gore]
Votes:
Bush: 29127
Gore: 29122
Nader: 974
Threshold: (29127+29122+974) / (1+1) + 1 = 29612.5
Result: Nader is eliminated