Fix problem with a [nil] result being taken as having a winner; add testcases
authorJohn Dong <jdong@mit.edu>
Wed, 15 Aug 2007 20:17:05 +0000 (20:17 +0000)
committerJohn Dong <jdong@mit.edu>
Wed, 15 Aug 2007 20:17:05 +0000 (20:17 +0000)
git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@36 1440c7f4-e209-0410-9a04-881b5eb134a8

lib/rubyvote/condorcet.rb
lib/rubyvote/election.rb
lib/rubyvote/irv.rb
test/condorcet_test.rb
test/election_test.rb
test/irv_test.rb
test/positional_test.rb
test/range_test.rb

index c0539ab..0b7a306 100644 (file)
@@ -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
index b9bb557..42f18bb 100644 (file)
@@ -133,7 +133,7 @@ class ElectionResult
   end
 
   def winner?
-    @winners.length > 0
+    @winners.length > 0 and not @winners[0].nil?
   end
 
 end
index b26b1d1..c3fabe6 100644 (file)
@@ -88,7 +88,7 @@ class InstantRunoffResult < ElectionResult
     end
     
     unless votes.length > 0
-      @winners=[nil]
+      @winners=[]
       return
     end
 
index 9d10169..b2d5dc4 100644 (file)
@@ -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
index 2afab27..26d8b91 100644 (file)
@@ -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
index fa05dc9..d3a57a7 100644 (file)
@@ -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
index 76be74d..bb81c7e 100644 (file)
@@ -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
index 79635ec..799ba3f 100644 (file)
@@ -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

Benjamin Mako Hill || Want to submit a patch?