4 require 'rubyvote/election'
5 require 'rubyvote/condorcet'
7 class TestCondorcetVote < Test::Unit::TestCase
10 vote_array = Array.new
11 3.times {vote_array << "ABC".split("")}
12 3.times {vote_array << "CBA".split("")}
13 2.times {vote_array << "BAC".split("")}
15 assert_equal "B", PureCondorcetVote.new(vote_array).result.winners[0]
16 assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).results
20 vote_array = Array.new
21 3.times {vote_array << "678".split("")}
22 3.times {vote_array << "768".split("")}
23 2.times {vote_array << "8".split("")}
25 v = PureCondorcetVote.new(vote_array)
26 assert_equal ["6", "7"], v.result.winners
27 assert_equal [['6', '7'], ['8']], v.results
31 vote_array = Array.new
32 5.times {vote_array << "ACBED".split("")}
33 5.times {vote_array << "ADECB".split("")}
34 8.times {vote_array << "BEDAC".split("")}
35 3.times {vote_array << "CABED".split("")}
36 7.times {vote_array << "CAEBD".split("")}
37 2.times {vote_array << "CBADE".split("")}
38 7.times {vote_array << "DCEBA".split("")}
39 8.times {vote_array << "EBADC".split("")}
41 assert_equal "E", CloneproofSSDVote.new(vote_array).result.winners[0]
42 assert_equal [['E'], ['A'], ['C'], ['B'], ['D']],
43 CloneproofSSDVote.new(vote_array).results
47 vote_array = Array.new
48 5.times {vote_array << "ACBD".split("")}
49 2.times {vote_array << "ACDB".split("")}
50 3.times {vote_array << "ADCB".split("")}
51 4.times {vote_array << "BACD".split("")}
52 3.times {vote_array << "CBDA".split("")}
53 3.times {vote_array << "CDBA".split("")}
54 1.times {vote_array << "DACB".split("")}
55 5.times {vote_array << "DBAC".split("")}
56 4.times {vote_array << "DCBA".split("")}
58 assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0]
59 assert_equal [['D'], ['A'], ['C'], ['B']],
60 CloneproofSSDVote.new(vote_array).results
64 vote_array = Array.new
65 3.times {vote_array << "ABCD".split("")}
66 2.times {vote_array << "DABC".split("")}
67 2.times {vote_array << "DBCA".split("")}
68 2.times {vote_array << "CBDA".split("")}
70 assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
71 assert_equal [['B'], ['C'], ['D'], ['A']],
72 CloneproofSSDVote.new(vote_array).results
75 def test_ssd_incomplete_votes
76 vote_array = Array.new
77 3.times {vote_array << "ABCD".split("")}
78 2.times {vote_array << "DABC".split("")}
79 2.times {vote_array << "DBCA".split("")}
80 4.times {vote_array << ["C"]}
81 2.times {vote_array << "DBC".split("")}
83 vote = CloneproofSSDVote.new(vote_array)
84 assert_equal "B", vote.result.winners[0]
85 assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
88 def test_ssd_incomplete_votes_2
89 vote_array = Array.new
90 4.times {vote_array << ["C"]}
91 3.times {vote_array << "ABCD".split("")}
92 2.times {vote_array << "DABC".split("")}
93 2.times {vote_array << "DBCA".split("")}
94 2.times {vote_array << "DBC".split("")}
96 vote = CloneproofSSDVote.new(vote_array)
97 assert_equal "B", vote.result.winners[0]
98 assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
102 # At one point, we happened to be getting correct results due to the
103 # happy accident that, for example, 'B'.each returns 'B'. The
104 # following election with a single integer vote catches that
107 def test_ssd_single_vote
108 vote = CloneproofSSDVote.new([[78]])
109 assert_equal 78, vote.result.winners[0]
110 assert_equal [[78]], vote.results
114 vote_array = Array.new
115 vote_array << ['B', 'D']
116 vote_array << ['A', 'C']
117 vote_array << ['E', 'C']
118 results = CloneproofSSDVote.new(vote_array).results
119 assert_equal 5, results.flatten.size
122 def test_ssd_sparse_2
123 vote_array = Array.new
124 vote_array << [65, 63, 64]
125 vote_array << [64, 65, 66, 63]
126 vote = CloneproofSSDVote.new(vote_array)
127 assert_equal 65, vote.result.winners[0]
128 assert_equal [[65, 64], [63, 66]], vote.results