Added matrix to condorcet to draw a table of winners vs losers. Also added "points...
[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   def test_condorcet_empty
9     vote_array =  [[]]
10     assert_nil PureCondorcetVote.new(vote_array).result.winners[0]
11     assert_equal(false, PureCondorcetVote.new(vote_array).result.winner?)
12   end
13
14   def test_condorcet
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("")}
19
20     assert_equal "B", PureCondorcetVote.new(vote_array).result.winners[0]
21     assert_equal [['B'], ['A'], ['C']], PureCondorcetVote.new(vote_array).results
22   end
23
24   def test_condorcet_2
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("")}
29
30     v = PureCondorcetVote.new(vote_array)
31     assert_equal ["6", "7"], v.result.winners
32     assert_equal [['6', '7'], ['8']], v.results
33   end
34
35   def test_ssd_empty
36     vote_array = [[]]
37     assert_nil  CloneproofSSDVote.new(vote_array).result.winners[0]
38     assert_equal(false, CloneproofSSDVote.new(vote_array).result.winner?)
39   end
40   
41   def test_ssd
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("")}
51
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
55   end
56
57   def test_ssd2
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("")}
68
69     assert_equal "D", CloneproofSSDVote.new(vote_array).result.winners[0] 
70     assert_equal [['D'], ['A'], ['C'], ['B']], 
71                  CloneproofSSDVote.new(vote_array).results
72   end
73
74   def test_ssd3
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("")}
80
81     assert_equal "B", CloneproofSSDVote.new(vote_array).result.winners[0]
82     assert_equal [['B'], ['C'], ['D'], ['A']], 
83                  CloneproofSSDVote.new(vote_array).results
84   end
85
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("")}
93
94     vote = CloneproofSSDVote.new(vote_array)
95     assert_equal "B", vote.result.winners[0]
96     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
97   end
98
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("")}
106
107     vote = CloneproofSSDVote.new(vote_array)
108     assert_equal "B", vote.result.winners[0]
109     assert_equal [['B'], ['C'], ['D'], ['A']], vote.results
110   end
111
112   # 
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
116   # condition.
117   #
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
122   end
123
124   def test_ssd_sparse
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
131   end
132
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
140   end
141
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
150   end
151
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
159   end
160
161
162 end

Benjamin Mako Hill || Want to submit a patch?