Merged in changes that added unit test support by Joe Slag. Thanks!
[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 'test/unit'
22 require 'lib/rubyvote'
23
24 class TestRubyvote < Test::Unit::TestCase
25
26   def test_condorcet
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("")}
31
32     assert_equal 'B', PureCondorcetVote.new(vote_array).result.winners[0][0]
33   end
34
35   def test_ssd_1
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("")}
45
46     assert_equal 'E', CloneproofSSDVote.new(vote_array).result.winners[0]
47   end
48
49   def test_ssd_2
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("")}
60
61     assert_equal 'D', CloneproofSSDVote.new(vote_array).result.winners[0]
62   end
63
64   def test_ssd_3
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("")}
70
71     assert_equal 'B', CloneproofSSDVote.new(vote_array).result.winners[0]
72   end
73
74   def test_borda
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("")}
79
80     assert_equal 'B', BordaVote.new(vote_array).result.winners[0]
81   end
82
83   def test_plurality
84     vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
85
86     assert_equal 'C', PluralityVote.new(vote_array).result.winners[0]
87   end
88
89   def test_approval
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("")}
95
96     assert_equal 'A', ApprovalVote.new(vote_array).result.winners[0] 
97   end
98
99   def test_irv_1
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("")}
105
106     assert_equal 'A', InstantRunoffVote.new(vote_array).result.winners[0]
107   end
108
109   def test_irv_2
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("")}
115
116     assert_equal 'D', InstantRunoffVote.new(vote_array).result.winners[0]
117   end
118
119   def test_irv_3
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("")}
140
141     assert_equal 'C', InstantRunoffVote.new(vote_array).result.winners[0]
142   end
143
144   def test_irvlogic
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("")}
150
151     assert_equal 'B', InstantRunoffLogicVote.new(vote_array).result
152   end
153
154   def test_range1
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}}
160
161     assert_equal 'B', RangeVote.new(vote_array).result
162   end
163
164 end

Benjamin Mako Hill || Want to submit a patch?