Add similar hackish "fix" for IRVL deadlocking on empty vote
[rubyvote] / test / condorcet_test.rb
1 #!/usr/bin/ruby -Ilib
2
3 require 'test/unit'
4 require 'rubyvote/election'
5 require 'rubyvote/condorcet'
6
7 class TestCondorcetVote < Test::Unit::TestCase
8   def test_condorcet_empty
9     vote_array =  [[]]
10     assert_nil PureCondorcetVote.new(vote_array).result.winners[0]
11   end
12
13   def test_condorcet
14     vote_array = Array.new
15     3.times {vote_array << "ABC".split("")}
16     3.times {vote_array << "CBA".split("")}
17     2.times {vote_array << "BAC".split("")}
18
19     assert_equal "B", PureCondorcetVote.new(vote_array).result.winners[0]
20     assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).results
21   end
22
23   def test_condorcet_2
24     vote_array = Array.new
25     3.times {vote_array << "678".split("")}
26     3.times {vote_array << "768".split("")}
27     2.times {vote_array << "8".split("")}
28
29     v = PureCondorcetVote.new(vote_array)
30     assert_equal ["6", "7"], v.result.winners
31     assert_equal [['6', '7'], ['8']], v.results
32   end
33
34   def test_ssd_empty
35     vote_array = [[]]
36     assert_nil  CloneproofSSDVote.new(vote_array).result.winners[0]
37   end
38   
39   def test_ssd
40     vote_array = Array.new
41     5.times {vote_array << "ACBED".split("")}
42     5.times {vote_array << "ADECB".split("")}
43     8.times {vote_array << "BEDAC".split("")}
44     3.times {vote_array << "CABED".split("")}
45     7.times {vote_array << "CAEBD".split("")}
46     2.times {vote_array << "CBADE".split("")}
47     7.times {vote_array << "DCEBA".split("")}
48     8.times {vote_array << "EBADC".split("")}
49
50     assert_equal "E", CloneproofSSDVote.new(vote_array).result.winners[0]
51     assert_equal [['E'], ['A'], ['C'], ['B'], ['D']], 
52                  CloneproofSSDVote.new(vote_array).results
53   end
54
55   def test_ssd2
56     vote_array = Array.new
57     5.times {vote_array << "ACBD".split("")}
58     2.times {vote_array << "ACDB".split("")}
59     3.times {vote_array << "ADCB".split("")}
60     4.times {vote_array << "BACD".split("")}
61     3.times {vote_array << "CBDA".split("")}
62     3.times {vote_array << "CDBA".split("")}
63     1.times {vote_array << "DACB".split("")}
64     5.times {vote_array << "DBAC".split("")}
65     4.times {vote_array << "DCBA".split("")}
66
67     assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0] 
68     assert_equal [['D'], ['A'], ['C'], ['B']], 
69                  CloneproofSSDVote.new(vote_array).results
70   end
71
72   def test_ssd3
73     vote_array = Array.new
74     3.times {vote_array << "ABCD".split("")}
75     2.times {vote_array << "DABC".split("")}
76     2.times {vote_array << "DBCA".split("")}
77     2.times {vote_array << "CBDA".split("")}
78
79     assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
80     assert_equal [['B'], ['C'], ['D'], ['A']], 
81                  CloneproofSSDVote.new(vote_array).results
82   end
83
84   def test_ssd_incomplete_votes
85     vote_array = Array.new
86     3.times {vote_array << "ABCD".split("")}
87     2.times {vote_array << "DABC".split("")}
88     2.times {vote_array << "DBCA".split("")}
89     4.times {vote_array << ["C"]}
90     2.times {vote_array << "DBC".split("")}
91
92     vote = CloneproofSSDVote.new(vote_array)
93     assert_equal "B", vote.result.winners[0]
94     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
95   end
96
97   def test_ssd_incomplete_votes_2
98     vote_array = Array.new
99     4.times {vote_array << ["C"]}
100     3.times {vote_array << "ABCD".split("")}
101     2.times {vote_array << "DABC".split("")}
102     2.times {vote_array << "DBCA".split("")}
103     2.times {vote_array << "DBC".split("")}
104
105     vote = CloneproofSSDVote.new(vote_array)
106     assert_equal "B", vote.result.winners[0]
107     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
108   end
109
110   # 
111   # At one point, we happened to be getting correct results due to the
112   # happy accident that, for example, 'B'.each returns 'B'. The
113   # following election with a single integer vote catches that
114   # condition.
115   #
116   def test_ssd_single_vote
117     vote = CloneproofSSDVote.new([[78]])
118     assert_equal 78, vote.result.winners[0]
119     assert_equal [[78]], vote.results
120   end
121
122   def test_ssd_sparse
123     vote_array = Array.new
124     vote_array << ['B', 'D']
125     vote_array << ['A', 'C']
126     vote_array << ['E', 'C']
127     results = CloneproofSSDVote.new(vote_array).results
128     assert_equal 5, results.flatten.size
129   end
130
131   def test_ssd_sparse_2
132     vote_array = Array.new
133     vote_array << [65, 63, 64]
134     vote_array << [64, 65, 66, 63]
135     vote = CloneproofSSDVote.new(vote_array)
136     assert_equal 65, vote.result.winners[0]
137     assert_equal [[65, 64], [63, 66]], vote.results
138   end
139
140   def test_ssd_multiple_equivalent
141     vote_array = Array.new
142     vote_array << ['B', ['A', 'C'], 'D']
143     vote_array << ['A', 'C']
144     vote_array << [['E', 'D'], 'C']
145     results = CloneproofSSDVote.new(vote_array).results
146     assert_equal 5, results.flatten.size
147     assert_equal [['A', 'C'], ['B', 'D'], ['E']], results
148   end
149
150   def test_ssd_multiple_equivalent_2
151     vote_array = Array.new
152     vote_array << ['B', ['A'], 'C']
153     vote_array << ['B', ['C'], 'A']
154     results = CloneproofSSDVote.new(vote_array).results
155     assert_equal 3, results.flatten.size
156     assert_equal [['B'], ['A', 'C']], results
157   end
158
159
160 end

Benjamin Mako Hill || Want to submit a patch?