o Test that elections with a single vote return the expected result.
[rubyvote] / lib / rubyvote / condorcet.rb
index f4ed7b74be0e7a9ec79d1ffe20f61fcfcab97ed2..340be9273f05fe7915a74902b4c049f8985834c8 100644 (file)
 
 class CondorcetVote < ElectionVote
 
+  def initialize(votes=nil)
+    unless defined?(@candidates)
+      @candidates = Array.new
+      votes.each do |vote_row|
+        vote_row = vote_row.flatten if vote_row.class == Array
+        vote_row.each do |vote| 
+          @candidates << vote unless @candidates.include?(vote)
+        end
+      end
+    end
+    super(votes)
+  end
+
   def tally_vote(vote=nil)
 
     vote.each_with_index do |winner, index|
@@ -63,13 +76,8 @@ class CondorcetVote < ElectionVote
 
           # make sure we have a comparable object
           @votes[loser][winner] = 0 unless @votes[loser].has_key?( winner )
-
-          @candidates << loser unless @candidates.include?( loser )
         end
       end
-
-      @candidates << winner unless @candidates.include?( winner ) || 
-        winner.class == Array
     end
   end
 
@@ -125,6 +133,7 @@ class CondorcetResult < ElectionResult
     votes = @election.votes unless votes
 
     defeats = Array.new
+    candidates = [candidates] unless candidates.class == Array
     candidates.each do |candidate|
       candidates.each do |challenger|
         next if candidate == challenger
@@ -196,6 +205,7 @@ class CloneproofSSDResult < CondorcetResult
       # see the array with the standard defeats
       transitive_defeats = self.defeats(candidates, votes)
 
+      candidates = [candidates] unless candidates.class == Array
       candidates.each do |cand1|
         candidates.each do |cand2|
           candidates.each do |cand3|

Benjamin Mako Hill || Want to submit a patch?