Add testcase for non-string plurality vote candidates
[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     assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).results
17   end
18
19   def test_condorcet_2
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("")}
24
25     v = PureCondorcetVote.new(vote_array)
26     assert_equal ["6", "7"], v.result.winners
27     assert_equal [['6', '7'], ['8']], v.results
28   end
29
30   def test_ssd
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("")}
40
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
44   end
45
46   def test_ssd2
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("")}
57
58     assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0] 
59     assert_equal [['D'], ['A'], ['C'], ['B']], 
60                  CloneproofSSDVote.new(vote_array).results
61   end
62
63   def test_ssd3
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("")}
69
70     assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
71     assert_equal [['B'], ['C'], ['D'], ['A']], 
72                  CloneproofSSDVote.new(vote_array).results
73   end
74
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("")}
82
83     vote = CloneproofSSDVote.new(vote_array)
84     assert_equal "B", vote.result.winners[0]
85     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
86   end
87
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("")}
95
96     vote = CloneproofSSDVote.new(vote_array)
97     assert_equal "B", vote.result.winners[0]
98     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
99   end
100
101   # 
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
105   # condition.
106   #
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
111   end
112
113   def test_ssd_sparse
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
120   end
121
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
129   end
130
131   def test_ssd_multiple_equivalent
132     vote_array = Array.new
133     vote_array << ['B', ['A', 'C'], 'D']
134     vote_array << ['A', 'C']
135     vote_array << [['E', 'D'], 'C']
136     results = CloneproofSSDVote.new(vote_array).results
137     assert_equal 5, results.flatten.size
138     assert_equal [['A', 'C'], ['B', 'D'], ['E']], results
139   end
140
141   def test_ssd_multiple_equivalent_2
142     vote_array = Array.new
143     vote_array << ['B', ['A'], 'C']
144     vote_array << ['B', ['C'], 'A']
145     results = CloneproofSSDVote.new(vote_array).results
146     assert_equal 3, results.flatten.size
147     assert_equal [['B'], ['A', 'C']], results
148   end
149
150
151 end

Benjamin Mako Hill || Want to submit a patch?