Fixed broken link.
[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
25 def print_winner(result)
26   if not result.winner?
27     puts "There is no winner."
28   elsif result.winners.length == 1
29     puts "The winner is %s" % result.winners[0]
30   else
31     puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
32   end
33 end
34
35 def condorcet_test1
36   puts "USING CONDORCET..."
37   puts "The winner should be: B" 
38
39   vote_array = Array.new
40   3.times {vote_array << "ABC".split("")}
41   3.times {vote_array << "CBA".split("")}
42   2.times {vote_array << "BAC".split("")}
43
44   print_winner( PureCondorcetVote.new(vote_array).result )
45 end
46
47 def ssd_test1
48   puts "USING CloneProofSSD..."
49   puts "The winner should be: E" 
50
51   vote_array = Array.new
52   5.times {vote_array << "ACBED".split("")}
53   5.times {vote_array << "ADECB".split("")}
54   8.times {vote_array << "BEDAC".split("")}
55   3.times {vote_array << "CABED".split("")}
56   7.times {vote_array << "CAEBD".split("")}
57   2.times {vote_array << "CBADE".split("")}
58   7.times {vote_array << "DCEBA".split("")}
59   8.times {vote_array << "EBADC".split("")}
60
61   print_winner( CloneproofSSDVote.new(vote_array).result )
62 end
63
64 def ssd_test2
65   puts "USING CloneProofSSD..."
66   puts "The winner should be: D" 
67
68   vote_array = Array.new
69   5.times {vote_array << "ACBD".split("")}
70   2.times {vote_array << "ACDB".split("")}
71   3.times {vote_array << "ADCB".split("")}
72   4.times {vote_array << "BACD".split("")}
73   3.times {vote_array << "CBDA".split("")}
74   3.times {vote_array << "CDBA".split("")}
75   1.times {vote_array << "DACB".split("")}
76   5.times {vote_array << "DBAC".split("")}
77   4.times {vote_array << "DCBA".split("")}
78
79   print_winner( CloneproofSSDVote.new(vote_array).result )
80 end
81
82 def ssd_test3
83   puts "USING CloneProofSSD..."
84   puts "The winner should be: B(?)" 
85
86   vote_array = Array.new
87   3.times {vote_array << "ABCD".split("")}
88   2.times {vote_array << "DABC".split("")}
89   2.times {vote_array << "DBCA".split("")}
90   2.times {vote_array << "CBDA".split("")}
91
92   print_winner( CloneproofSSDVote.new(vote_array).result )
93 end
94
95
96 def borda_test1
97   puts "USING BORDA..."
98   puts "The winner should be: B" 
99
100   vote_array = Array.new
101   3.times {vote_array << "ABC".split("")}
102   3.times {vote_array << "CBA".split("")}
103   2.times {vote_array << "BAC".split("")}
104
105   print_winner( BordaVote.new(vote_array).result )
106 end
107
108 def plurality_test1
109   puts "USING PLURALITY..."
110   puts "The winner should be: C"
111
112   vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
113
114   print_winner( PluralityVote.new(vote_array).result )
115 end
116
117
118 def approval_test1
119   puts "USING APPROVAL..."
120   puts "The winner should be: A"
121
122   vote_array = Array.new
123   10.times {vote_array << "AB".split("")}
124   10.times {vote_array << "CB".split("")}
125   11.times {vote_array << "AC".split("")}
126   5.times {vote_array << "A".split("")}
127
128   print_winner( ApprovalVote.new(vote_array).result )
129 end
130
131 condorcet_test1()
132 ssd_test1()
133 ssd_test2()
134 ssd_test3()
135 borda_test1()
136 plurality_test1()
137 approval_test1()
138

Benjamin Mako Hill || Want to submit a patch?