Added support for instant run-off voting with a patch from
[rubyvote] / test.rb
1 #!/usr/bin/ruby -I./lib
2
3 # election library -- a ruby library for elections
4 # copyright © 2005 MIT Media Lab and Benjamin Mako Hill
5
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.
10
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.
15
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
19 # 02110-1301, USA.
20
21 require 'election'
22 require 'condorcet'
23 require 'positional'
24 require 'runoff'
25
26 def print_winner(result)
27   if not result.winner?
28     puts "There is no winner."
29   elsif result.winners.length == 1
30     puts "The winner is %s" % result.winners[0]
31   else
32     puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
33   end
34 end
35
36 def condorcet_test1
37   puts "USING CONDORCET..."
38   puts "The winner should be: B" 
39
40   vote_array = Array.new
41   3.times {vote_array << "ABC".split("")}
42   3.times {vote_array << "CBA".split("")}
43   2.times {vote_array << "BAC".split("")}
44
45   print_winner( PureCondorcetVote.new(vote_array).result )
46 end
47
48 def ssd_test1
49   puts "USING CloneProofSSD..."
50   puts "The winner should be: E" 
51
52   vote_array = Array.new
53   5.times {vote_array << "ACBED".split("")}
54   5.times {vote_array << "ADECB".split("")}
55   8.times {vote_array << "BEDAC".split("")}
56   3.times {vote_array << "CABED".split("")}
57   7.times {vote_array << "CAEBD".split("")}
58   2.times {vote_array << "CBADE".split("")}
59   7.times {vote_array << "DCEBA".split("")}
60   8.times {vote_array << "EBADC".split("")}
61
62   print_winner( CloneproofSSDVote.new(vote_array).result )
63 end
64
65 def ssd_test2
66   puts "USING CloneProofSSD..."
67   puts "The winner should be: D" 
68
69   vote_array = Array.new
70   5.times {vote_array << "ACBD".split("")}
71   2.times {vote_array << "ACDB".split("")}
72   3.times {vote_array << "ADCB".split("")}
73   4.times {vote_array << "BACD".split("")}
74   3.times {vote_array << "CBDA".split("")}
75   3.times {vote_array << "CDBA".split("")}
76   1.times {vote_array << "DACB".split("")}
77   5.times {vote_array << "DBAC".split("")}
78   4.times {vote_array << "DCBA".split("")}
79
80   print_winner( CloneproofSSDVote.new(vote_array).result )
81 end
82
83 def ssd_test3
84   puts "USING CloneProofSSD..."
85   puts "The winner should be: B(?)" 
86
87   vote_array = Array.new
88   3.times {vote_array << "ABCD".split("")}
89   2.times {vote_array << "DABC".split("")}
90   2.times {vote_array << "DBCA".split("")}
91   2.times {vote_array << "CBDA".split("")}
92
93   print_winner( CloneproofSSDVote.new(vote_array).result )
94 end
95
96
97 def borda_test1
98   puts "USING BORDA..."
99   puts "The winner should be: B" 
100
101   vote_array = Array.new
102   3.times {vote_array << "ABC".split("")}
103   3.times {vote_array << "CBA".split("")}
104   2.times {vote_array << "BAC".split("")}
105
106   print_winner( BordaVote.new(vote_array).result )
107 end
108
109 def plurality_test1
110   puts "USING PLURALITY..."
111   puts "The winner should be: C"
112
113   vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
114
115   print_winner( PluralityVote.new(vote_array).result )
116 end
117
118
119 def approval_test1
120   puts "USING APPROVAL..."
121   puts "The winner should be: A"
122
123   vote_array = Array.new
124   10.times {vote_array << "AB".split("")}
125   10.times {vote_array << "CB".split("")}
126   11.times {vote_array << "AC".split("")}
127   5.times {vote_array << "A".split("")}
128
129   print_winner( ApprovalVote.new(vote_array).result )
130 end
131
132 def runoff_test1
133   puts "USING RUNOFF..."
134   puts "The winner shold be: A"
135
136   vote_array = Array.new
137   142.times {vote_array << "ABCD".split("")}
138   26.times {vote_array << "BCDA".split("")}
139   15.times {vote_array << "CDBA".split("")}
140   17.times {vote_array << "DCBA".split("")}
141
142   print_winner( InstantRunoffVote.new(vote_array).result )
143 end
144
145 def runoff_test2
146   puts "USING RUNOFF..."
147   puts "The winner shold be: D"
148
149   vote_array = Array.new
150   42.times {vote_array << "ABCD".split("")}
151   26.times {vote_array << "BCDA".split("")}
152   15.times {vote_array << "CDBA".split("")}
153   17.times {vote_array << "DCBA".split("")}
154
155   print_winner( InstantRunoffVote.new(vote_array).result )
156 end
157
158 def runoff_test3
159   puts "USING RUNOFF..."
160   puts "The winner shold be: C"
161
162   vote_array = Array.new
163   42.times {vote_array << "ABCD".split("")}
164   26.times {vote_array << "ACBD".split("")}
165   15.times {vote_array << "BACD".split("")}
166   32.times {vote_array << "BCAD".split("")}
167   14.times {vote_array << "CABD".split("")}
168   49.times {vote_array << "CBAD".split("")}
169   17.times {vote_array << "ABDC".split("")}
170   23.times {vote_array << "BADC".split("")}
171   37.times {vote_array << "BCDA".split("")}
172   11.times {vote_array << "CADB".split("")}
173   16.times {vote_array << "CBDA".split("")}
174   54.times {vote_array << "ADBC".split("")}
175   36.times {vote_array << "BDCA".split("")}
176   42.times {vote_array << "CDAB".split("")}
177   13.times {vote_array << "CDBA".split("")}
178   51.times {vote_array << "DABC".split("")}
179   33.times {vote_array << "DBCA".split("")}
180   39.times {vote_array << "DCAB".split("")}
181   12.times {vote_array << "DCBA".split("")}
182
183   print_winner( InstantRunoffVote.new(vote_array).result )
184 end
185
186 condorcet_test1()
187 ssd_test1()
188 ssd_test2()
189 ssd_test3()
190 borda_test1()
191 plurality_test1()
192 approval_test1()
193 runoff_test1()
194 runoff_test2()
195 runoff_test3()

Benjamin Mako Hill || Want to submit a patch?