Removed space to avoid warning
[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 'lib/rubyvote'
22
23 def print_winner(result)
24   if not result.winner?
25     puts "There is no winner."
26   elsif result.winners.length == 1
27     puts "The winner is %s" % result.winners[0]
28   else
29     puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
30   end
31 end
32
33 def condorcet_test1
34   puts "USING CONDORCET..."
35   puts "The winner should be: B" 
36
37   vote_array = Array.new
38   3.times {vote_array << "ABC".split("")}
39   3.times {vote_array << "CBA".split("")}
40   2.times {vote_array << "BAC".split("")}
41
42   print_winner( PureCondorcetVote.new(vote_array).result )
43 end
44
45 def ssd_test1
46   puts "USING CloneProofSSD..."
47   puts "The winner should be: E" 
48
49   vote_array = Array.new
50   5.times {vote_array << "ACBED".split("")}
51   5.times {vote_array << "ADECB".split("")}
52   8.times {vote_array << "BEDAC".split("")}
53   3.times {vote_array << "CABED".split("")}
54   7.times {vote_array << "CAEBD".split("")}
55   2.times {vote_array << "CBADE".split("")}
56   7.times {vote_array << "DCEBA".split("")}
57   8.times {vote_array << "EBADC".split("")}
58
59   print_winner( CloneproofSSDVote.new(vote_array).result )
60 end
61
62 def ssd_test2
63   puts "USING CloneProofSSD..."
64   puts "The winner should be: D" 
65
66   vote_array = Array.new
67   5.times {vote_array << "ACBD".split("")}
68   2.times {vote_array << "ACDB".split("")}
69   3.times {vote_array << "ADCB".split("")}
70   4.times {vote_array << "BACD".split("")}
71   3.times {vote_array << "CBDA".split("")}
72   3.times {vote_array << "CDBA".split("")}
73   1.times {vote_array << "DACB".split("")}
74   5.times {vote_array << "DBAC".split("")}
75   4.times {vote_array << "DCBA".split("")}
76
77   print_winner( CloneproofSSDVote.new(vote_array).result )
78 end
79
80 def ssd_test3
81   puts "USING CloneProofSSD..."
82   puts "The winner should be: B(?)" 
83
84   vote_array = Array.new
85   3.times {vote_array << "ABCD".split("")}
86   2.times {vote_array << "DABC".split("")}
87   2.times {vote_array << "DBCA".split("")}
88   2.times {vote_array << "CBDA".split("")}
89
90   print_winner( CloneproofSSDVote.new(vote_array).result )
91 end
92
93
94 def borda_test1
95   puts "USING BORDA..."
96   puts "The winner should be: B" 
97
98   vote_array = Array.new
99   3.times {vote_array << "ABC".split("")}
100   3.times {vote_array << "CBA".split("")}
101   2.times {vote_array << "BAC".split("")}
102
103   print_winner( BordaVote.new(vote_array).result )
104 end
105
106 def plurality_test1
107   puts "USING PLURALITY..."
108   puts "The winner should be: C"
109
110   vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
111
112   print_winner( PluralityVote.new(vote_array).result )
113 end
114
115
116 def approval_test1
117   puts "USING APPROVAL..."
118   puts "The winner should be: A"
119
120   vote_array = Array.new
121   10.times {vote_array << "AB".split("")}
122   10.times {vote_array << "CB".split("")}
123   11.times {vote_array << "AC".split("")}
124   5.times {vote_array << "A".split("")}
125
126   print_winner( ApprovalVote.new(vote_array).result )
127 end
128
129 def irv_test1
130   puts "USING IRV..."
131   puts "The winner shold be: A"
132
133   vote_array = Array.new
134   142.times {vote_array << "ABCD".split("")}
135   26.times {vote_array << "BCDA".split("")}
136   15.times {vote_array << "CDBA".split("")}
137   17.times {vote_array << "DCBA".split("")}
138
139   print_winner( InstantRunoffVote.new(vote_array).result )
140 end
141
142 def irv_test2
143   puts "USING IRV..."
144   puts "The winner shold be: D"
145
146   vote_array = Array.new
147   42.times {vote_array << "ABCD".split("")}
148   26.times {vote_array << "BCDA".split("")}
149   15.times {vote_array << "CDBA".split("")}
150   17.times {vote_array << "DCBA".split("")}
151
152   print_winner( InstantRunoffVote.new(vote_array).result )
153 end
154
155 def irv_test3
156   puts "USING IRV..."
157   puts "The winner shold be: C"
158
159   vote_array = Array.new
160   42.times {vote_array << "ABCD".split("")}
161   26.times {vote_array << "ACBD".split("")}
162   15.times {vote_array << "BACD".split("")}
163   32.times {vote_array << "BCAD".split("")}
164   14.times {vote_array << "CABD".split("")}
165   49.times {vote_array << "CBAD".split("")}
166   17.times {vote_array << "ABDC".split("")}
167   23.times {vote_array << "BADC".split("")}
168   37.times {vote_array << "BCDA".split("")}
169   11.times {vote_array << "CADB".split("")}
170   16.times {vote_array << "CBDA".split("")}
171   54.times {vote_array << "ADBC".split("")}
172   36.times {vote_array << "BDCA".split("")}
173   42.times {vote_array << "CDAB".split("")}
174   13.times {vote_array << "CDBA".split("")}
175   51.times {vote_array << "DABC".split("")}
176   33.times {vote_array << "DBCA".split("")}
177   39.times {vote_array << "DCAB".split("")}
178   12.times {vote_array << "DCBA".split("")}
179
180   print_winner( InstantRunoffVote.new(vote_array).result )
181 end
182
183 def irvlogic_test1
184   puts "USING IRV LOGIC..."
185   puts "The winner shold be: B"
186
187   vote_array = Array.new
188   42.times {vote_array << "ABCD".split("")}
189   26.times {vote_array << "BCDA".split("")}
190   15.times {vote_array << "CDBA".split("")}
191   15.times {vote_array << "DCBA".split("")}
192
193   print_winner( InstantRunoffLogicVote.new(vote_array).result )
194 end
195
196 def range_test1
197   puts "USING RANGE..."
198   puts "The winner shold be: B"
199
200   vote_array = Array.new
201   42.times {vote_array << {:A => 10, :B => 5, :C => 2, :D => 1}}
202   26.times {vote_array << {:A => 1, :B => 10, :C => 5, :D => 2}}
203   15.times {vote_array << {:A => 1, :B => 2, :C => 10, :D => 5}}
204   17.times {vote_array << {:A => 1, :B => 2, :C => 5, :D => 10}}
205
206   print_winner( RangeVote.new(vote_array).result )
207 end
208
209 condorcet_test1()
210 ssd_test1()
211 ssd_test2()
212 ssd_test3()
213 borda_test1()
214 plurality_test1()
215 approval_test1()
216 irv_test1()
217 irv_test2()
218 irv_test3()
219 irvlogic_test1()
220 range_test1()

Benjamin Mako Hill || Want to submit a patch?