1 #!/usr/bin/ruby -I./lib
3 # election library -- a ruby library for elections
4 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 require 'lib/rubyvote'
24 class TestRubyvote < Test::Unit::TestCase
27 vote_array = Array.new
28 3.times {vote_array << "ABC".split("")}
29 3.times {vote_array << "CBA".split("")}
30 2.times {vote_array << "BAC".split("")}
32 assert_equal 'B', PureCondorcetVote.new(vote_array).result.winners[0][0]
36 vote_array = Array.new
37 5.times {vote_array << "ACBED".split("")}
38 5.times {vote_array << "ADECB".split("")}
39 8.times {vote_array << "BEDAC".split("")}
40 3.times {vote_array << "CABED".split("")}
41 7.times {vote_array << "CAEBD".split("")}
42 2.times {vote_array << "CBADE".split("")}
43 7.times {vote_array << "DCEBA".split("")}
44 8.times {vote_array << "EBADC".split("")}
46 assert_equal 'E', CloneproofSSDVote.new(vote_array).result.winners[0]
50 vote_array = Array.new
51 5.times {vote_array << "ACBD".split("")}
52 2.times {vote_array << "ACDB".split("")}
53 3.times {vote_array << "ADCB".split("")}
54 4.times {vote_array << "BACD".split("")}
55 3.times {vote_array << "CBDA".split("")}
56 3.times {vote_array << "CDBA".split("")}
57 1.times {vote_array << "DACB".split("")}
58 5.times {vote_array << "DBAC".split("")}
59 4.times {vote_array << "DCBA".split("")}
61 assert_equal 'D', CloneproofSSDVote.new(vote_array).result.winners[0]
65 vote_array = Array.new
66 3.times {vote_array << "ABCD".split("")}
67 2.times {vote_array << "DABC".split("")}
68 2.times {vote_array << "DBCA".split("")}
69 2.times {vote_array << "CBDA".split("")}
71 assert_equal 'B', CloneproofSSDVote.new(vote_array).result.winners[0]
75 vote_array = Array.new
76 3.times {vote_array << "ABC".split("")}
77 3.times {vote_array << "CBA".split("")}
78 2.times {vote_array << "BAC".split("")}
80 assert_equal 'B', BordaVote.new(vote_array).result.winners[0]
84 vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
86 assert_equal 'C', PluralityVote.new(vote_array).result.winners[0]
90 vote_array = Array.new
91 10.times {vote_array << "AB".split("")}
92 10.times {vote_array << "CB".split("")}
93 11.times {vote_array << "AC".split("")}
94 5.times {vote_array << "A".split("")}
96 assert_equal 'A', ApprovalVote.new(vote_array).result.winners[0]
100 vote_array = Array.new
101 142.times {vote_array << "ABCD".split("")}
102 26.times {vote_array << "BCDA".split("")}
103 15.times {vote_array << "CDBA".split("")}
104 17.times {vote_array << "DCBA".split("")}
106 assert_equal 'A', InstantRunoffVote.new(vote_array).result.winners[0]
110 vote_array = Array.new
111 42.times {vote_array << "ABCD".split("")}
112 26.times {vote_array << "BCDA".split("")}
113 15.times {vote_array << "CDBA".split("")}
114 17.times {vote_array << "DCBA".split("")}
116 assert_equal 'D', InstantRunoffVote.new(vote_array).result.winners[0]
120 vote_array = Array.new
121 42.times {vote_array << "ABCD".split("")}
122 26.times {vote_array << "ACBD".split("")}
123 15.times {vote_array << "BACD".split("")}
124 32.times {vote_array << "BCAD".split("")}
125 14.times {vote_array << "CABD".split("")}
126 49.times {vote_array << "CBAD".split("")}
127 17.times {vote_array << "ABDC".split("")}
128 23.times {vote_array << "BADC".split("")}
129 37.times {vote_array << "BCDA".split("")}
130 11.times {vote_array << "CADB".split("")}
131 16.times {vote_array << "CBDA".split("")}
132 54.times {vote_array << "ADBC".split("")}
133 36.times {vote_array << "BDCA".split("")}
134 42.times {vote_array << "CDAB".split("")}
135 13.times {vote_array << "CDBA".split("")}
136 51.times {vote_array << "DABC".split("")}
137 33.times {vote_array << "DBCA".split("")}
138 39.times {vote_array << "DCAB".split("")}
139 12.times {vote_array << "DCBA".split("")}
141 assert_equal 'C', InstantRunoffVote.new(vote_array).result.winners[0]
145 vote_array = Array.new
146 42.times {vote_array << "ABCD".split("")}
147 26.times {vote_array << "BCDA".split("")}
148 15.times {vote_array << "CDBA".split("")}
149 15.times {vote_array << "DCBA".split("")}
151 assert_equal 'B', InstantRunoffLogicVote.new(vote_array).result
155 vote_array = Array.new
156 42.times {vote_array << {:A => 10, :B => 5, :C => 2, :D => 1}}
157 26.times {vote_array << {:A => 1, :B => 10, :C => 5, :D => 2}}
158 15.times {vote_array << {:A => 1, :B => 2, :C => 10, :D => 5}}
159 17.times {vote_array << {:A => 1, :B => 2, :C => 5, :D => 10}}
161 assert_equal 'B', RangeVote.new(vote_array).result