a number of improvements
[rubyvote] / test / condorcet_test.rb
1 require 'test/unit'
2 require 'rubyvote/election'
3 require 'rubyvote/condorcet'
4
5 class TestCondorcetVote < Test::Unit::TestCase
6   def test_condorcet_empty
7     vote_array =  [[]]
8     assert_nil PureCondorcetVote.new(vote_array).result.winners[0]
9     assert_equal(false, PureCondorcetVote.new(vote_array).result.winner?)
10   end
11
12   def test_condorcet
13     vote_array = Array.new
14     3.times {vote_array << "ABC".split("")}
15     3.times {vote_array << "CBA".split("")}
16     2.times {vote_array << "BAC".split("")}
17
18     assert_equal "B", PureCondorcetVote.new(vote_array).result.winners[0]
19     assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).result.ranked_candidates
20   end
21
22   def test_condorcet_2
23     vote_array = Array.new
24     3.times {vote_array << "678".split("")}
25     3.times {vote_array << "768".split("")}
26     2.times {vote_array << "8".split("")}
27
28     v = PureCondorcetVote.new(vote_array)
29     assert_equal ["6", "7"], v.result.winners
30     assert_equal [['6', '7'], ['8']], v.result.ranked_candidates
31   end
32
33   def test_ssd_empty
34     vote_array = [[]]
35     assert_nil  CloneproofSSDVote.new(vote_array).result.winners[0]
36     assert_equal(false, CloneproofSSDVote.new(vote_array).result.winner?)
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).result.ranked_candidates
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).result.ranked_candidates
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).result.ranked_candidates
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.result.ranked_candidates
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.result.ranked_candidates
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.result.ranked_candidates
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     ranked_candidates = CloneproofSSDVote.new(vote_array).result.ranked_candidates
128     assert_equal 5, ranked_candidates.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.result.ranked_candidates
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     ranked_candidates = CloneproofSSDVote.new(vote_array).result.ranked_candidates
146     assert_equal 5, ranked_candidates.flatten.size
147     assert_equal [['A', 'C'], ['B', 'D'], ['E']], ranked_candidates
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     ranked_candidates = CloneproofSSDVote.new(vote_array).result.ranked_candidates
155     assert_equal 3, ranked_candidates.flatten.size
156     assert_equal [['B'], ['A', 'C']], ranked_candidates
157   end
158 end

Benjamin Mako Hill || Want to submit a patch?