2 require 'rubyvote/election'
3 require 'rubyvote/condorcet'
5 class TestCondorcetVote < Test::Unit::TestCase
6 def test_condorcet_empty
8 assert_nil PureCondorcetVote.new(vote_array).result.winners[0]
9 assert_equal(false, PureCondorcetVote.new(vote_array).result.winner?)
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("")}
18 assert_equal "B", PureCondorcetVote.new(vote_array).result.winners[0]
19 assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).result.ranked_candidates
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("")}
28 v = PureCondorcetVote.new(vote_array)
29 assert_equal ["6", "7"], v.result.winners
30 assert_equal [['6', '7'], ['8']], v.result.ranked_candidates
35 assert_nil CloneproofSSDVote.new(vote_array).result.winners[0]
36 assert_equal(false, CloneproofSSDVote.new(vote_array).result.winner?)
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("")}
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
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("")}
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
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("")}
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
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("")}
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
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("")}
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
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
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
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
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
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
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