fa05dc99bd62ebef470826eec4bad690a1b6d94b
[rubyvote] / test / irv_test.rb
1 #!/usr/bin/ruby -Ilib
2
3 require 'test/unit'
4 require 'rubyvote/election'
5 require 'rubyvote/irv'
6
7 class TestRunoffVote < Test::Unit::TestCase
8
9   def test_irv_empty
10     vote_array = Array.new
11     assert_nil InstantRunoffVote.new(vote_array).result.winners[0]
12   end
13   
14   def test_irv
15     vote_array = Array.new
16     142.times {vote_array << "ABCD".split("")}
17     26.times {vote_array << "BCDA".split("")}
18     15.times {vote_array << "CDBA".split("")}
19     17.times {vote_array << "DCBA".split("")}
20
21     assert_equal( "A", InstantRunoffVote.new(vote_array).result.winners[0] )
22   end
23
24   def test_irv2
25     vote_array = Array.new
26     42.times {vote_array << "ABCD".split("")}
27     26.times {vote_array << "BCDA".split("")}
28     15.times {vote_array << "CDBA".split("")}
29     17.times {vote_array << "DCBA".split("")}
30
31     assert_equal( "D", InstantRunoffVote.new(vote_array).result.winners[0] )
32   end
33
34   def test_irv3
35     vote_array = Array.new
36     42.times {vote_array << "ABCD".split("")}
37     26.times {vote_array << "ACBD".split("")}
38     15.times {vote_array << "BACD".split("")}
39     32.times {vote_array << "BCAD".split("")}
40     14.times {vote_array << "CABD".split("")}
41     49.times {vote_array << "CBAD".split("")}
42     17.times {vote_array << "ABDC".split("")}
43     23.times {vote_array << "BADC".split("")}
44     37.times {vote_array << "BCDA".split("")}
45     11.times {vote_array << "CADB".split("")}
46     16.times {vote_array << "CBDA".split("")}
47     54.times {vote_array << "ADBC".split("")}
48     36.times {vote_array << "BDCA".split("")}
49     42.times {vote_array << "CDAB".split("")}
50     13.times {vote_array << "CDBA".split("")}
51     51.times {vote_array << "DABC".split("")}
52     33.times {vote_array << "DBCA".split("")}
53     39.times {vote_array << "DCAB".split("")}
54     12.times {vote_array << "DCBA".split("")}
55
56     assert_equal( "C", InstantRunoffVote.new(vote_array).result.winners[0] )
57   end
58   
59   def test_irv_logic_empty
60     vote_array = Array.new
61     assert_nil InstantRunoffLogicVote.new(vote_array).result.winners[0]
62   end
63   
64   def test_irv_logic1
65     vote_array = Array.new
66     42.times {vote_array << "ABCD".split("")}
67     26.times {vote_array << "BCDA".split("")}
68     15.times {vote_array << "CDBA".split("")}
69     15.times {vote_array << "DCBA".split("")}
70
71     assert_equal( "B", InstantRunoffLogicVote.new(vote_array).result.winners[0] )
72   end
73   ###TODO: test all the other variants
74 end
75

Benjamin Mako Hill || Want to submit a patch?