4 require 'rubyvote/election'
5 require 'rubyvote/condorcet'
7 class TestCondorcetVote < Test::Unit::TestCase
8 def test_condorcet_empty
10 assert_nil PureCondorcetVote.new(vote_array).result.winners[0]
11 assert_equal(false, PureCondorcetVote.new(vote_array).result.winner?)
15 vote_array = Array.new
16 3.times {vote_array << "ABC".split("")}
17 3.times {vote_array << "CBA".split("")}
18 2.times {vote_array << "BAC".split("")}
20 assert_equal "B", PureCondorcetVote.new(vote_array).result.winners[0]
21 assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).results
25 vote_array = Array.new
26 3.times {vote_array << "678".split("")}
27 3.times {vote_array << "768".split("")}
28 2.times {vote_array << "8".split("")}
30 v = PureCondorcetVote.new(vote_array)
31 assert_equal ["6", "7"], v.result.winners
32 assert_equal [['6', '7'], ['8']], v.results
37 assert_nil CloneproofSSDVote.new(vote_array).result.winners[0]
38 assert_equal(false, CloneproofSSDVote.new(vote_array).result.winner?)
42 vote_array = Array.new
43 5.times {vote_array << "ACBED".split("")}
44 5.times {vote_array << "ADECB".split("")}
45 8.times {vote_array << "BEDAC".split("")}
46 3.times {vote_array << "CABED".split("")}
47 7.times {vote_array << "CAEBD".split("")}
48 2.times {vote_array << "CBADE".split("")}
49 7.times {vote_array << "DCEBA".split("")}
50 8.times {vote_array << "EBADC".split("")}
52 assert_equal "E", CloneproofSSDVote.new(vote_array).result.winners[0]
53 assert_equal [['E'], ['A'], ['C'], ['B'], ['D']],
54 CloneproofSSDVote.new(vote_array).results
58 vote_array = Array.new
59 5.times {vote_array << "ACBD".split("")}
60 2.times {vote_array << "ACDB".split("")}
61 3.times {vote_array << "ADCB".split("")}
62 4.times {vote_array << "BACD".split("")}
63 3.times {vote_array << "CBDA".split("")}
64 3.times {vote_array << "CDBA".split("")}
65 1.times {vote_array << "DACB".split("")}
66 5.times {vote_array << "DBAC".split("")}
67 4.times {vote_array << "DCBA".split("")}
69 assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0]
70 assert_equal [['D'], ['A'], ['C'], ['B']],
71 CloneproofSSDVote.new(vote_array).results
75 vote_array = Array.new
76 3.times {vote_array << "ABCD".split("")}
77 2.times {vote_array << "DABC".split("")}
78 2.times {vote_array << "DBCA".split("")}
79 2.times {vote_array << "CBDA".split("")}
81 assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
82 assert_equal [['B'], ['C'], ['D'], ['A']],
83 CloneproofSSDVote.new(vote_array).results
86 def test_ssd_incomplete_votes
87 vote_array = Array.new
88 3.times {vote_array << "ABCD".split("")}
89 2.times {vote_array << "DABC".split("")}
90 2.times {vote_array << "DBCA".split("")}
91 4.times {vote_array << ["C"]}
92 2.times {vote_array << "DBC".split("")}
94 vote = CloneproofSSDVote.new(vote_array)
95 assert_equal "B", vote.result.winners[0]
96 assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
99 def test_ssd_incomplete_votes_2
100 vote_array = Array.new
101 4.times {vote_array << ["C"]}
102 3.times {vote_array << "ABCD".split("")}
103 2.times {vote_array << "DABC".split("")}
104 2.times {vote_array << "DBCA".split("")}
105 2.times {vote_array << "DBC".split("")}
107 vote = CloneproofSSDVote.new(vote_array)
108 assert_equal "B", vote.result.winners[0]
109 assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
113 # At one point, we happened to be getting correct results due to the
114 # happy accident that, for example, 'B'.each returns 'B'. The
115 # following election with a single integer vote catches that
118 def test_ssd_single_vote
119 vote = CloneproofSSDVote.new([[78]])
120 assert_equal 78, vote.result.winners[0]
121 assert_equal [[78]], vote.results
125 vote_array = Array.new
126 vote_array << ['B', 'D']
127 vote_array << ['A', 'C']
128 vote_array << ['E', 'C']
129 results = CloneproofSSDVote.new(vote_array).results
130 assert_equal 5, results.flatten.size
133 def test_ssd_sparse_2
134 vote_array = Array.new
135 vote_array << [65, 63, 64]
136 vote_array << [64, 65, 66, 63]
137 vote = CloneproofSSDVote.new(vote_array)
138 assert_equal 65, vote.result.winners[0]
139 assert_equal [[65, 64], [63, 66]], vote.results
142 def test_ssd_multiple_equivalent
143 vote_array = Array.new
144 vote_array << ['B', ['A', 'C'], 'D']
145 vote_array << ['A', 'C']
146 vote_array << [['E', 'D'], 'C']
147 results = CloneproofSSDVote.new(vote_array).results
148 assert_equal 5, results.flatten.size
149 assert_equal [['A', 'C'], ['B', 'D'], ['E']], results
152 def test_ssd_multiple_equivalent_2
153 vote_array = Array.new
154 vote_array << ['B', ['A'], 'C']
155 vote_array << ['B', ['C'], 'A']
156 results = CloneproofSSDVote.new(vote_array).results
157 assert_equal 3, results.flatten.size
158 assert_equal [['B'], ['A', 'C']], results