From 11d22a5307695910493266b16718e1ffa201676d Mon Sep 17 00:00:00 2001 From: John Dong Date: Wed, 15 Aug 2007 20:17:05 +0000 Subject: [PATCH] Fix problem with a [nil] result being taken as having a winner; add testcases git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@36 1440c7f4-e209-0410-9a04-881b5eb134a8 --- lib/rubyvote/condorcet.rb | 4 ++-- lib/rubyvote/election.rb | 2 +- lib/rubyvote/irv.rb | 2 +- test/condorcet_test.rb | 2 ++ test/election_test.rb | 2 ++ test/irv_test.rb | 2 ++ test/positional_test.rb | 1 + test/range_test.rb | 1 + 8 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/rubyvote/condorcet.rb b/lib/rubyvote/condorcet.rb index c0539ab..0b7a306 100644 --- a/lib/rubyvote/condorcet.rb +++ b/lib/rubyvote/condorcet.rb @@ -185,7 +185,7 @@ class PureCondorcetResult < CondorcetResult votes = @election.votes candidates = @election.candidates unless votes.length>0 and candidates.length>0 - @winners=[nil] + @winners=[] return @winners end victors = Hash.new @@ -224,7 +224,7 @@ class CloneproofSSDResult < CondorcetResult def cpssd votes = @election.votes candidates = *@election.candidates - + def in_schwartz_set?(candidate, candidates, transitive_defeats) candidates.each do |challenger| next if candidate == challenger diff --git a/lib/rubyvote/election.rb b/lib/rubyvote/election.rb index b9bb557..42f18bb 100644 --- a/lib/rubyvote/election.rb +++ b/lib/rubyvote/election.rb @@ -133,7 +133,7 @@ class ElectionResult end def winner? - @winners.length > 0 + @winners.length > 0 and not @winners[0].nil? end end diff --git a/lib/rubyvote/irv.rb b/lib/rubyvote/irv.rb index b26b1d1..c3fabe6 100644 --- a/lib/rubyvote/irv.rb +++ b/lib/rubyvote/irv.rb @@ -88,7 +88,7 @@ class InstantRunoffResult < ElectionResult end unless votes.length > 0 - @winners=[nil] + @winners=[] return end diff --git a/test/condorcet_test.rb b/test/condorcet_test.rb index 9d10169..b2d5dc4 100644 --- a/test/condorcet_test.rb +++ b/test/condorcet_test.rb @@ -8,6 +8,7 @@ class TestCondorcetVote < Test::Unit::TestCase def test_condorcet_empty vote_array = [[]] assert_nil PureCondorcetVote.new(vote_array).result.winners[0] + assert_equal(false, PureCondorcetVote.new(vote_array).result.winner?) end def test_condorcet @@ -34,6 +35,7 @@ class TestCondorcetVote < Test::Unit::TestCase def test_ssd_empty vote_array = [[]] assert_nil CloneproofSSDVote.new(vote_array).result.winners[0] + assert_equal(false, CloneproofSSDVote.new(vote_array).result.winner?) end def test_ssd diff --git a/test/election_test.rb b/test/election_test.rb index 2afab27..26d8b91 100644 --- a/test/election_test.rb +++ b/test/election_test.rb @@ -8,6 +8,7 @@ class TestElectionVote < Test::Unit::TestCase def test_plurality_empty vote_array = [] assert_nil PluralityVote.new(vote_array).result.winners[0] + assert_equal(false, PluralityVote.new(vote_array).result.winner?) end def test_plurality @@ -29,6 +30,7 @@ class TestElectionVote < Test::Unit::TestCase def test_approval_empty vote_array = [] assert_nil ApprovalVote.new(vote_array).result.winners[0] + assert_equal(false, ApprovalVote.new(vote_array).result.winner?) end def test_approval diff --git a/test/irv_test.rb b/test/irv_test.rb index fa05dc9..d3a57a7 100644 --- a/test/irv_test.rb +++ b/test/irv_test.rb @@ -9,6 +9,7 @@ class TestRunoffVote < Test::Unit::TestCase 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 @@ -59,6 +60,7 @@ class TestRunoffVote < Test::Unit::TestCase 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 diff --git a/test/positional_test.rb b/test/positional_test.rb index 76be74d..bb81c7e 100644 --- a/test/positional_test.rb +++ b/test/positional_test.rb @@ -9,6 +9,7 @@ class TestPositionalVote < Test::Unit::TestCase def test_borda_empty vote_array = Array.new assert_nil BordaVote.new(vote_array).result.winners[0] + assert_equal(false, BordaVote.new(vote_array).result.winner?) end def test_borda diff --git a/test/range_test.rb b/test/range_test.rb index 79635ec..799ba3f 100644 --- a/test/range_test.rb +++ b/test/range_test.rb @@ -9,6 +9,7 @@ class TestRangeVote < Test::Unit::TestCase def test_range_empty vote_array = [] assert_nil RangeVote.new(vote_array).result.winners[0] + assert_equal(false, RangeVote.new(vote_array).result.winner?) end def test_range -- 2.39.5