-#!/usr/bin/ruby
+#!/usr/bin/ruby -Ilib
require 'test/unit'
-require 'election_test_helper'
+require 'rubyvote/election'
+require 'rubyvote/irv'
class TestRunoffVote < Test::Unit::TestCase
- include ElectionTestHelper
+ def test_irv_empty
+ vote_array = Array.new
+ assert_nil InstantRunoffVote.new(vote_array).result.winners[0]
+ assert_equal(false, InstantRunoffVote.new(vote_array).result.winner?)
+ end
+
def test_irv
vote_array = Array.new
142.times {vote_array << "ABCD".split("")}
15.times {vote_array << "CDBA".split("")}
17.times {vote_array << "DCBA".split("")}
- test_winner( "A", InstantRunoffVote.new(vote_array).result )
+ assert_equal( "A", InstantRunoffVote.new(vote_array).result.winners[0] )
end
def test_irv2
15.times {vote_array << "CDBA".split("")}
17.times {vote_array << "DCBA".split("")}
- test_winner( "D", InstantRunoffVote.new(vote_array).result )
+ assert_equal( "D", InstantRunoffVote.new(vote_array).result.winners[0] )
end
def test_irv3
39.times {vote_array << "DCAB".split("")}
12.times {vote_array << "DCBA".split("")}
- test_winner( "C", InstantRunoffVote.new(vote_array).result )
+ assert_equal( "C", InstantRunoffVote.new(vote_array).result.winners[0] )
+ end
+
+ def test_irv4
+ # this was causing selectricity to crash
+
+ raw_vote_array = ["GFECADBIH", "ABCDEFGHI", "IGHBADFCE",
+ "FABCDEGHI", "ABCDEFGHI", "ABCDEFGHI",
+ "ABCDEFGHI", "ABCDEFGHI", "ABCDEFGHI",
+ "EHBICDAGF", "ECGDFBIAH", "BDHIGFECA",
+ "ECDBFIHAG", "FEDBCAHIG", "CDABHIEFG",
+ "FCBDIHAEG", "AIHFBECGD", "BACHDEFIG",
+ "CDEFIBHAG", "BDICAFEGH", "ABCDEFGHI",
+ "CBIHDFAEG", "ABCDEFGHI", "CDFIBAGEH",
+ "ECDBIFHGA", "BDACEHFIG", "CFDIHABEG",
+ "ADCFGBIHE", "CDHIBEAGF", "ABCFDHIEG",
+ "ABCDEFGHI", "DCFHIBAGE", "CDFEABHIG",
+ "DFHIBEAGC", "EDCBFIGAH", "BAECDFHGI",
+ "BAHCEDFGI", "HBCDIAFEG", "ABCDEFGHI",
+ "ABCDEFGHI", "EDCBIGAHF", "EIBDCGAFH",
+ "HIACGDFBE", "DEACBIFGH", "CIDEFABGH",
+ "ABHDIECGF", "ECIDBFGHA", "CEDFBHIGA",
+ "ABHCEIDFG", "CFDBEIHGA", "ICEDBGFAH",
+ "EDCIFBAGH", "ECFDBAHGI", "EBCADFIGH",
+ "EFBHAGICD", "CDBIEFAHG", "ABCDEFGHI",
+ "FCDBEIHGA", "AIEBDCFHG", "CDFEBIGAH",
+ "CABDEFGHI", "DCEIBFGHA", "EADCIBHFG",
+ "DBCAHIGEF", "EDFBCIAHG", "EDCBIHAGF",
+ "CFIAEDGHB", "CDEIHABGF", "CEBHIDAFG",
+ "BCFEDIHAG", "CDIHBFGEA", "CFEDIGBAH",
+ "IHFEADCBG", "EBDCIGAHF", "BCEDFIAGH",
+ "ABHEGICDF", "CABFHDIEG", "HEABDCFGI",
+ "CDEBFAIHG", "CDBFIHAGE", "ABGFEDHCI",
+ "IBHDCAEFG", "EBDICHAFG", "ABCDEFGHI",
+ "EFBHAICGD", "CBDFHAIEG", "CDBAEIHGF",
+ "ABCDEFGHI", "BECDHIFGA", "DAGCIHFBE",
+ "BIECDGAHF", "ABCDEFGHI", "ACDEBHIFG",
+ "AEBCIDHFG", "ABCDEFGHI", "ABCDEFGHI",
+ "ABCDEFGHI", "ABCDEFGHI", "ABCDEFGHI",
+ "ABCDEFGHI", "ECFDIHBAG", "BIDCHEAGF",
+ "BCEDFIGHA", "CBDAFEGHI", "ABCDEFGHI",
+ "FACEGDHBI", "ABCDEFGHI", "FDIBCGHAE",
+ "EBIHADCFG", "EDBIHAGCF", "AHDICBFEG",
+ "DCBIHAGFE", "CABFIDEHG", "IFCBHADEG",
+ "EDCFHIBAG", "DCABEFGHI", "FCBDIGHEA",
+ "ABCDEFGHI", "HBACIDEFG", "ABCDEFGHI",
+ "ABCDEFGHI", "EADCBIHGF", "BDCIHAGFE",
+ "ABCDEFGHI", "ABCDEFGHI", "ABCDEFGHI",
+ "ABCDEFGHI", "ABCDEFGHI", "ICDGAHBEF",
+ "EGBCFIHDA", "DFGACHIEB", "BCAGDFHIE",
+ "DBGHCEAFI", "IGHEBCADF", "FEDIHBCAG",
+ "HAICBGEFD", "EBFDAICGH", "ECGAHFIBD",
+ "GEFICAHDB", "ACEHGIBDF", "EGIFBHCAD",
+ "GHFEDCIBA", "DGAEHFICB", "HGDICBAFE",
+ "HEACGDBFI", "IAGDFHCEB", "EGIACHBDF",
+ "CBFGDIEAH", "CGAIBDEFH", "AHEDIGCBF",
+ "BAEFHIDCG", "BIDGAFCHE", "CHBAEDFIG",
+ "IBGCFADEH", "GHACDEFIB", "CFDBEAHIG",
+ "GDEFAICHB"]
+ vote_array = raw_vote_array.collect {|v| v.split("")}
+ print InstantRunoffVote.new(vote_array).result.winners, "\n"
+ assert_equal(true, InstantRunoffVote.new(vote_array).result.winner?)
end
+ def test_irv_logic_empty
+ vote_array = Array.new
+ assert_nil InstantRunoffLogicVote.new(vote_array).result.winners[0]
+ assert_equal(false, InstantRunoffLogicVote.new(vote_array).result.winner?)
+ end
+
def test_irv_logic1
vote_array = Array.new
42.times {vote_array << "ABCD".split("")}
15.times {vote_array << "CDBA".split("")}
15.times {vote_array << "DCBA".split("")}
- test_winner( "B", InstantRunoffLogicVote.new(vote_array).result )
+ assert_equal( "B", InstantRunoffLogicVote.new(vote_array).result.winners[0] )
end
###TODO: test all the other variants
end