d3a57a7c7d8cec68e31a4c85cf0ba89c675cea2b
[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     assert_equal(false, InstantRunoffVote.new(vote_array).result.winner?)
13   end
14   
15   def test_irv
16     vote_array = Array.new
17     142.times {vote_array << "ABCD".split("")}
18     26.times {vote_array << "BCDA".split("")}
19     15.times {vote_array << "CDBA".split("")}
20     17.times {vote_array << "DCBA".split("")}
21
22     assert_equal( "A", InstantRunoffVote.new(vote_array).result.winners[0] )
23   end
24
25   def test_irv2
26     vote_array = Array.new
27     42.times {vote_array << "ABCD".split("")}
28     26.times {vote_array << "BCDA".split("")}
29     15.times {vote_array << "CDBA".split("")}
30     17.times {vote_array << "DCBA".split("")}
31
32     assert_equal( "D", InstantRunoffVote.new(vote_array).result.winners[0] )
33   end
34
35   def test_irv3
36     vote_array = Array.new
37     42.times {vote_array << "ABCD".split("")}
38     26.times {vote_array << "ACBD".split("")}
39     15.times {vote_array << "BACD".split("")}
40     32.times {vote_array << "BCAD".split("")}
41     14.times {vote_array << "CABD".split("")}
42     49.times {vote_array << "CBAD".split("")}
43     17.times {vote_array << "ABDC".split("")}
44     23.times {vote_array << "BADC".split("")}
45     37.times {vote_array << "BCDA".split("")}
46     11.times {vote_array << "CADB".split("")}
47     16.times {vote_array << "CBDA".split("")}
48     54.times {vote_array << "ADBC".split("")}
49     36.times {vote_array << "BDCA".split("")}
50     42.times {vote_array << "CDAB".split("")}
51     13.times {vote_array << "CDBA".split("")}
52     51.times {vote_array << "DABC".split("")}
53     33.times {vote_array << "DBCA".split("")}
54     39.times {vote_array << "DCAB".split("")}
55     12.times {vote_array << "DCBA".split("")}
56
57     assert_equal( "C", InstantRunoffVote.new(vote_array).result.winners[0] )
58   end
59   
60   def test_irv_logic_empty
61     vote_array = Array.new
62     assert_nil InstantRunoffLogicVote.new(vote_array).result.winners[0]
63     assert_equal(false, InstantRunoffLogicVote.new(vote_array).result.winner?)
64   end
65   
66   def test_irv_logic1
67     vote_array = Array.new
68     42.times {vote_array << "ABCD".split("")}
69     26.times {vote_array << "BCDA".split("")}
70     15.times {vote_array << "CDBA".split("")}
71     15.times {vote_array << "DCBA".split("")}
72
73     assert_equal( "B", InstantRunoffLogicVote.new(vote_array).result.winners[0] )
74   end
75   ###TODO: test all the other variants
76 end
77

Benjamin Mako Hill || Want to submit a patch?