]> projects.mako.cc - rubyvote/blob - test/irv_test.rb
a number of improvements
[rubyvote] / test / irv_test.rb
1 #!/usr/bin/ruby -Ilib
2
3 require 'test/unit'
4 require 'rubyvote/election'
5 require 'rubyvote/irv'
6
7 class TestRunoffVote < Test::Unit::TestCase
8
9   def test_irv_empty
10     vote_array = Array.new
11     assert_nil InstantRunoffVote.new(vote_array).result.winners[0]
12     assert_equal(false, InstantRunoffVote.new(vote_array).result.winner?)
13   end
14   
15   def test_irv
16     vote_array = Array.new
17     142.times {vote_array << "ABCD".split("")}
18     26.times {vote_array << "BCDA".split("")}
19     15.times {vote_array << "CDBA".split("")}
20     17.times {vote_array << "DCBA".split("")}
21
22     assert_equal( "A", InstantRunoffVote.new(vote_array).result.winners[0] )
23   end
24
25   def test_irv2
26     vote_array = Array.new
27     42.times {vote_array << "ABCD".split("")}
28     26.times {vote_array << "BCDA".split("")}
29     15.times {vote_array << "CDBA".split("")}
30     17.times {vote_array << "DCBA".split("")}
31
32     assert_equal( "D", InstantRunoffVote.new(vote_array).result.winners[0] )
33   end
34
35   def test_irv3
36     vote_array = Array.new
37     42.times {vote_array << "ABCD".split("")}
38     26.times {vote_array << "ACBD".split("")}
39     15.times {vote_array << "BACD".split("")}
40     32.times {vote_array << "BCAD".split("")}
41     14.times {vote_array << "CABD".split("")}
42     49.times {vote_array << "CBAD".split("")}
43     17.times {vote_array << "ABDC".split("")}
44     23.times {vote_array << "BADC".split("")}
45     37.times {vote_array << "BCDA".split("")}
46     11.times {vote_array << "CADB".split("")}
47     16.times {vote_array << "CBDA".split("")}
48     54.times {vote_array << "ADBC".split("")}
49     36.times {vote_array << "BDCA".split("")}
50     42.times {vote_array << "CDAB".split("")}
51     13.times {vote_array << "CDBA".split("")}
52     51.times {vote_array << "DABC".split("")}
53     33.times {vote_array << "DBCA".split("")}
54     39.times {vote_array << "DCAB".split("")}
55     12.times {vote_array << "DCBA".split("")}
56
57     assert_equal( "C", InstantRunoffVote.new(vote_array).result.winners[0] )
58   end
59  
60   def test_irv4
61     # this was causing selectricity to crash
62
63     raw_vote_array = ["GFECADBIH", "ABCDEFGHI", "IGHBADFCE",
64                       "FABCDEGHI", "ABCDEFGHI", "ABCDEFGHI",
65                       "ABCDEFGHI", "ABCDEFGHI", "ABCDEFGHI",
66                       "EHBICDAGF", "ECGDFBIAH", "BDHIGFECA",
67                       "ECDBFIHAG", "FEDBCAHIG", "CDABHIEFG",
68                       "FCBDIHAEG", "AIHFBECGD", "BACHDEFIG",
69                       "CDEFIBHAG", "BDICAFEGH", "ABCDEFGHI",
70                       "CBIHDFAEG", "ABCDEFGHI", "CDFIBAGEH",
71                       "ECDBIFHGA", "BDACEHFIG", "CFDIHABEG",
72                       "ADCFGBIHE", "CDHIBEAGF", "ABCFDHIEG",
73                       "ABCDEFGHI", "DCFHIBAGE", "CDFEABHIG",
74                       "DFHIBEAGC", "EDCBFIGAH", "BAECDFHGI",
75                       "BAHCEDFGI", "HBCDIAFEG", "ABCDEFGHI",
76                       "ABCDEFGHI", "EDCBIGAHF", "EIBDCGAFH",
77                       "HIACGDFBE", "DEACBIFGH", "CIDEFABGH",
78                       "ABHDIECGF", "ECIDBFGHA", "CEDFBHIGA",
79                       "ABHCEIDFG", "CFDBEIHGA", "ICEDBGFAH",
80                       "EDCIFBAGH", "ECFDBAHGI", "EBCADFIGH",
81                       "EFBHAGICD", "CDBIEFAHG", "ABCDEFGHI",
82                       "FCDBEIHGA", "AIEBDCFHG", "CDFEBIGAH",
83                       "CABDEFGHI", "DCEIBFGHA", "EADCIBHFG",
84                       "DBCAHIGEF", "EDFBCIAHG", "EDCBIHAGF",
85                       "CFIAEDGHB", "CDEIHABGF", "CEBHIDAFG",
86                       "BCFEDIHAG", "CDIHBFGEA", "CFEDIGBAH",
87                       "IHFEADCBG", "EBDCIGAHF", "BCEDFIAGH",
88                       "ABHEGICDF", "CABFHDIEG", "HEABDCFGI",
89                       "CDEBFAIHG", "CDBFIHAGE", "ABGFEDHCI",
90                       "IBHDCAEFG", "EBDICHAFG", "ABCDEFGHI",
91                       "EFBHAICGD", "CBDFHAIEG", "CDBAEIHGF",
92                       "ABCDEFGHI", "BECDHIFGA", "DAGCIHFBE",
93                       "BIECDGAHF", "ABCDEFGHI", "ACDEBHIFG",
94                       "AEBCIDHFG", "ABCDEFGHI", "ABCDEFGHI",
95                       "ABCDEFGHI", "ABCDEFGHI", "ABCDEFGHI",
96                       "ABCDEFGHI", "ECFDIHBAG", "BIDCHEAGF",
97                       "BCEDFIGHA", "CBDAFEGHI", "ABCDEFGHI",
98                       "FACEGDHBI", "ABCDEFGHI", "FDIBCGHAE",
99                       "EBIHADCFG", "EDBIHAGCF", "AHDICBFEG",
100                       "DCBIHAGFE", "CABFIDEHG", "IFCBHADEG",
101                       "EDCFHIBAG", "DCABEFGHI", "FCBDIGHEA",
102                       "ABCDEFGHI", "HBACIDEFG", "ABCDEFGHI",
103                       "ABCDEFGHI", "EADCBIHGF", "BDCIHAGFE",
104                       "ABCDEFGHI", "ABCDEFGHI", "ABCDEFGHI",
105                       "ABCDEFGHI", "ABCDEFGHI", "ICDGAHBEF",
106                       "EGBCFIHDA", "DFGACHIEB", "BCAGDFHIE",
107                       "DBGHCEAFI", "IGHEBCADF", "FEDIHBCAG",
108                       "HAICBGEFD", "EBFDAICGH", "ECGAHFIBD",
109                       "GEFICAHDB", "ACEHGIBDF", "EGIFBHCAD",
110                       "GHFEDCIBA", "DGAEHFICB", "HGDICBAFE",
111                       "HEACGDBFI", "IAGDFHCEB", "EGIACHBDF",
112                       "CBFGDIEAH", "CGAIBDEFH", "AHEDIGCBF",
113                       "BAEFHIDCG", "BIDGAFCHE", "CHBAEDFIG",
114                       "IBGCFADEH", "GHACDEFIB", "CFDBEAHIG",
115                       "GDEFAICHB"]
116     vote_array = raw_vote_array.collect {|v| v.split("")}
117     print InstantRunoffVote.new(vote_array).result.winners, "\n"
118     assert_equal(true, InstantRunoffVote.new(vote_array).result.winner?)
119   end
120
121   def test_irv_logic_empty
122     vote_array = Array.new
123     assert_nil InstantRunoffLogicVote.new(vote_array).result.winners[0]
124     assert_equal(false, InstantRunoffLogicVote.new(vote_array).result.winner?)
125   end
126   
127   def test_irv_logic1
128     vote_array = Array.new
129     42.times {vote_array << "ABCD".split("")}
130     26.times {vote_array << "BCDA".split("")}
131     15.times {vote_array << "CDBA".split("")}
132     15.times {vote_array << "DCBA".split("")}
133
134     assert_equal( "B", InstantRunoffLogicVote.new(vote_array).result.winners[0] )
135   end
136   ###TODO: test all the other variants
137 end
138

Benjamin Mako Hill || Want to submit a patch?