simplify access to full results - now available as voteobject.results,
[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
9   def test_condorcet
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("")}
14
15     assert_equal ["B"], PureCondorcetVote.new(vote_array).result.winners[0]
16   end
17
18   def test_ssd
19     vote_array = Array.new
20     5.times {vote_array << "ACBED".split("")}
21     5.times {vote_array << "ADECB".split("")}
22     8.times {vote_array << "BEDAC".split("")}
23     3.times {vote_array << "CABED".split("")}
24     7.times {vote_array << "CAEBD".split("")}
25     2.times {vote_array << "CBADE".split("")}
26     7.times {vote_array << "DCEBA".split("")}
27     8.times {vote_array << "EBADC".split("")}
28
29     assert_equal "E", CloneproofSSDVote.new(vote_array).result.winners[0]
30     assert_equal [['E'], ['A'], ['C'], ['B'], ['D']], 
31                  CloneproofSSDVote.new(vote_array).results
32   end
33
34   def test_ssd2
35     vote_array = Array.new
36     5.times {vote_array << "ACBD".split("")}
37     2.times {vote_array << "ACDB".split("")}
38     3.times {vote_array << "ADCB".split("")}
39     4.times {vote_array << "BACD".split("")}
40     3.times {vote_array << "CBDA".split("")}
41     3.times {vote_array << "CDBA".split("")}
42     1.times {vote_array << "DACB".split("")}
43     5.times {vote_array << "DBAC".split("")}
44     4.times {vote_array << "DCBA".split("")}
45
46     assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0] 
47     assert_equal [['D'], ['A'], ['C'], ['B']], 
48                  CloneproofSSDVote.new(vote_array).results
49   end
50
51   def test_ssd3
52     vote_array = Array.new
53     3.times {vote_array << "ABCD".split("")}
54     2.times {vote_array << "DABC".split("")}
55     2.times {vote_array << "DBCA".split("")}
56     2.times {vote_array << "CBDA".split("")}
57
58     assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
59     assert_equal [['B'], ['C'], ['D'], ['A']], 
60                  CloneproofSSDVote.new(vote_array).results
61   end
62
63   def test_ssd_incomplete_votes
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     4.times {vote_array << ["C"]}
69     2.times {vote_array << "DBC".split("")}
70
71     vote = CloneproofSSDVote.new(vote_array)
72     assert_equal "B", vote.result.winners[0]
73     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
74   end
75
76   def test_ssd_incomplete_votes_2
77     vote_array = Array.new
78     4.times {vote_array << ["C"]}
79     3.times {vote_array << "ABCD".split("")}
80     2.times {vote_array << "DABC".split("")}
81     2.times {vote_array << "DBCA".split("")}
82     2.times {vote_array << "DBC".split("")}
83
84     vote = CloneproofSSDVote.new(vote_array)
85     assert_equal "B", vote.result.winners[0]
86     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
87   end
88
89   # 
90   # At one point, we happened to be getting correct results due to the
91   # happy accident that, for example, 'B'.each returns 'B'. The
92   # following election with a single integer vote catches that
93   # condition.
94   #
95   def test_ssd_single_vote
96     vote = CloneproofSSDVote.new([[78]])
97     assert_equal 78, vote.result.winners[0]
98     assert_equal [[78]], vote.results
99   end
100
101   def test_ssd_sparse
102     vote_array = Array.new
103     vote_array << ['B', 'D']
104     vote_array << ['A', 'C']
105     vote_array << ['E', 'C']
106     results = CloneproofSSDVote.new(vote_array).results
107     assert_equal 5, results.flatten.size
108   end
109
110   def test_ssd_sparse_2
111     vote_array = Array.new
112     vote_array << [65, 63, 64]
113     vote_array << [64, 65, 66, 63]
114     vote = CloneproofSSDVote.new(vote_array)
115     assert_equal 65, vote.result.winners[0]
116     assert_equal [[65, 64], [63, 66]], vote.results
117   end
118
119 end

Benjamin Mako Hill || Want to submit a patch?