Merged in changes that added unit test support by Joe Slag. Thanks!
authorBenjamin Mako Hill <mako@atdot.cc>
Mon, 12 Mar 2007 20:17:25 +0000 (20:17 +0000)
committerBenjamin Mako Hill <mako@atdot.cc>
Mon, 12 Mar 2007 20:17:25 +0000 (20:17 +0000)
We now have two unit test setups in here. We should deal with this one
way or another. I'd like to keep around *a* test.rb file but the
rakefile is a good method as well.

git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@14 1440c7f4-e209-0410-9a04-881b5eb134a8

test.rb

diff --git a/test.rb b/test.rb
index 6b0b5c1cda8950a19fce2a1cecd3e3795a861f94..3c73ffa253440ea395e1d5b1f3371dff68fc828d 100755 (executable)
--- a/test.rb
+++ b/test.rb
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
+require 'test/unit'
 require 'lib/rubyvote'
 
 require 'lib/rubyvote'
 
-def print_winner(result)
-  if not result.winner?
-    puts "There is no winner."
-  elsif result.winners.length == 1
-    puts "The winner is %s" % result.winners[0]
-  else
-    puts "There is a tie between the following candidates: %s" % result.winners.join(", ")
-  end
-end
-
-def condorcet_test1
-  puts "USING CONDORCET..."
-  puts "The winner should be: B" 
-
-  vote_array = Array.new
-  3.times {vote_array << "ABC".split("")}
-  3.times {vote_array << "CBA".split("")}
-  2.times {vote_array << "BAC".split("")}
-
-  print_winner( PureCondorcetVote.new(vote_array).result )
-end
-
-def ssd_test1
-  puts "USING CloneProofSSD..."
-  puts "The winner should be: E" 
-
-  vote_array = Array.new
-  5.times {vote_array << "ACBED".split("")}
-  5.times {vote_array << "ADECB".split("")}
-  8.times {vote_array << "BEDAC".split("")}
-  3.times {vote_array << "CABED".split("")}
-  7.times {vote_array << "CAEBD".split("")}
-  2.times {vote_array << "CBADE".split("")}
-  7.times {vote_array << "DCEBA".split("")}
-  8.times {vote_array << "EBADC".split("")}
-
-  print_winner( CloneproofSSDVote.new(vote_array).result )
-end
-
-def ssd_test2
-  puts "USING CloneProofSSD..."
-  puts "The winner should be: D" 
-
-  vote_array = Array.new
-  5.times {vote_array << "ACBD".split("")}
-  2.times {vote_array << "ACDB".split("")}
-  3.times {vote_array << "ADCB".split("")}
-  4.times {vote_array << "BACD".split("")}
-  3.times {vote_array << "CBDA".split("")}
-  3.times {vote_array << "CDBA".split("")}
-  1.times {vote_array << "DACB".split("")}
-  5.times {vote_array << "DBAC".split("")}
-  4.times {vote_array << "DCBA".split("")}
-
-  print_winner( CloneproofSSDVote.new(vote_array).result )
-end
-
-def ssd_test3
-  puts "USING CloneProofSSD..."
-  puts "The winner should be: B(?)" 
-
-  vote_array = Array.new
-  3.times {vote_array << "ABCD".split("")}
-  2.times {vote_array << "DABC".split("")}
-  2.times {vote_array << "DBCA".split("")}
-  2.times {vote_array << "CBDA".split("")}
-
-  print_winner( CloneproofSSDVote.new(vote_array).result )
-end
+class TestRubyvote < Test::Unit::TestCase
 
 
+  def test_condorcet
+    vote_array = Array.new
+    3.times {vote_array << "ABC".split("")}
+    3.times {vote_array << "CBA".split("")}
+    2.times {vote_array << "BAC".split("")}
 
 
-def borda_test1
-  puts "USING BORDA..."
-  puts "The winner should be: B" 
-
-  vote_array = Array.new
-  3.times {vote_array << "ABC".split("")}
-  3.times {vote_array << "CBA".split("")}
-  2.times {vote_array << "BAC".split("")}
-
-  print_winner( BordaVote.new(vote_array).result )
-end
+    assert_equal 'B', PureCondorcetVote.new(vote_array).result.winners[0][0]
+  end
 
 
-def plurality_test1
-  puts "USING PLURALITY..."
-  puts "The winner should be: C"
+  def test_ssd_1
+    vote_array = Array.new
+    5.times {vote_array << "ACBED".split("")}
+    5.times {vote_array << "ADECB".split("")}
+    8.times {vote_array << "BEDAC".split("")}
+    3.times {vote_array << "CABED".split("")}
+    7.times {vote_array << "CAEBD".split("")}
+    2.times {vote_array << "CBADE".split("")}
+    7.times {vote_array << "DCEBA".split("")}
+    8.times {vote_array << "EBADC".split("")}
+
+    assert_equal 'E', CloneproofSSDVote.new(vote_array).result.winners[0]
+  end
 
 
-  vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
+  def test_ssd_2
+    vote_array = Array.new
+    5.times {vote_array << "ACBD".split("")}
+    2.times {vote_array << "ACDB".split("")}
+    3.times {vote_array << "ADCB".split("")}
+    4.times {vote_array << "BACD".split("")}
+    3.times {vote_array << "CBDA".split("")}
+    3.times {vote_array << "CDBA".split("")}
+    1.times {vote_array << "DACB".split("")}
+    5.times {vote_array << "DBAC".split("")}
+    4.times {vote_array << "DCBA".split("")}
+
+    assert_equal 'D', CloneproofSSDVote.new(vote_array).result.winners[0]
+  end
 
 
-  print_winner( PluralityVote.new(vote_array).result )
-end
+  def test_ssd_3
+    vote_array = Array.new
+    3.times {vote_array << "ABCD".split("")}
+    2.times {vote_array << "DABC".split("")}
+    2.times {vote_array << "DBCA".split("")}
+    2.times {vote_array << "CBDA".split("")}
 
 
+    assert_equal 'B', CloneproofSSDVote.new(vote_array).result.winners[0]
+  end
 
 
-def approval_test1
-  puts "USING APPROVAL..."
-  puts "The winner should be: A"
+  def test_borda
+    vote_array = Array.new
+    3.times {vote_array << "ABC".split("")}
+    3.times {vote_array << "CBA".split("")}
+    2.times {vote_array << "BAC".split("")}
 
 
-  vote_array = Array.new
-  10.times {vote_array << "AB".split("")}
-  10.times {vote_array << "CB".split("")}
-  11.times {vote_array << "AC".split("")}
-  5.times {vote_array << "A".split("")}
+    assert_equal 'B', BordaVote.new(vote_array).result.winners[0]
+  end
 
 
-  print_winner( ApprovalVote.new(vote_array).result )
-end
+  def test_plurality
+    vote_array = "ABCABCABCCCBBAAABABABCCCCCCCCCCCCCA".split("")
 
 
-def irv_test1
-  puts "USING IRV..."
-  puts "The winner shold be: A"
+    assert_equal 'C', PluralityVote.new(vote_array).result.winners[0]
+  end
 
 
-  vote_array = Array.new
-  142.times {vote_array << "ABCD".split("")}
-  26.times {vote_array << "BCDA".split("")}
-  15.times {vote_array << "CDBA".split("")}
-  17.times {vote_array << "DCBA".split("")}
+  def test_approval
+    vote_array = Array.new
+    10.times {vote_array << "AB".split("")}
+    10.times {vote_array << "CB".split("")}
+    11.times {vote_array << "AC".split("")}
+    5.times {vote_array << "A".split("")}
 
 
-  print_winner( InstantRunoffVote.new(vote_array).result )
-end
+    assert_equal 'A', ApprovalVote.new(vote_array).result.winners[0] 
+  end
 
 
-def irv_test2
-  puts "USING IRV..."
-  puts "The winner shold be: D"
+  def test_irv_1
+    vote_array = Array.new
+    142.times {vote_array << "ABCD".split("")}
+    26.times {vote_array << "BCDA".split("")}
+    15.times {vote_array << "CDBA".split("")}
+    17.times {vote_array << "DCBA".split("")}
 
 
-  vote_array = Array.new
-  42.times {vote_array << "ABCD".split("")}
-  26.times {vote_array << "BCDA".split("")}
-  15.times {vote_array << "CDBA".split("")}
-  17.times {vote_array << "DCBA".split("")}
+    assert_equal 'A', InstantRunoffVote.new(vote_array).result.winners[0]
+  end
 
 
-  print_winner( InstantRunoffVote.new(vote_array).result )
-end
+  def test_irv_2
+    vote_array = Array.new
+    42.times {vote_array << "ABCD".split("")}
+    26.times {vote_array << "BCDA".split("")}
+    15.times {vote_array << "CDBA".split("")}
+    17.times {vote_array << "DCBA".split("")}
 
 
-def irv_test3
-  puts "USING IRV..."
-  puts "The winner shold be: C"
-
-  vote_array = Array.new
-  42.times {vote_array << "ABCD".split("")}
-  26.times {vote_array << "ACBD".split("")}
-  15.times {vote_array << "BACD".split("")}
-  32.times {vote_array << "BCAD".split("")}
-  14.times {vote_array << "CABD".split("")}
-  49.times {vote_array << "CBAD".split("")}
-  17.times {vote_array << "ABDC".split("")}
-  23.times {vote_array << "BADC".split("")}
-  37.times {vote_array << "BCDA".split("")}
-  11.times {vote_array << "CADB".split("")}
-  16.times {vote_array << "CBDA".split("")}
-  54.times {vote_array << "ADBC".split("")}
-  36.times {vote_array << "BDCA".split("")}
-  42.times {vote_array << "CDAB".split("")}
-  13.times {vote_array << "CDBA".split("")}
-  51.times {vote_array << "DABC".split("")}
-  33.times {vote_array << "DBCA".split("")}
-  39.times {vote_array << "DCAB".split("")}
-  12.times {vote_array << "DCBA".split("")}
-
-  print_winner( InstantRunoffVote.new(vote_array).result )
-end
+    assert_equal 'D', InstantRunoffVote.new(vote_array).result.winners[0]
+  end
 
 
-def irvlogic_test1
-  puts "USING IRV LOGIC..."
-  puts "The winner shold be: B"
+  def test_irv_3
+    vote_array = Array.new
+    42.times {vote_array << "ABCD".split("")}
+    26.times {vote_array << "ACBD".split("")}
+    15.times {vote_array << "BACD".split("")}
+    32.times {vote_array << "BCAD".split("")}
+    14.times {vote_array << "CABD".split("")}
+    49.times {vote_array << "CBAD".split("")}
+    17.times {vote_array << "ABDC".split("")}
+    23.times {vote_array << "BADC".split("")}
+    37.times {vote_array << "BCDA".split("")}
+    11.times {vote_array << "CADB".split("")}
+    16.times {vote_array << "CBDA".split("")}
+    54.times {vote_array << "ADBC".split("")}
+    36.times {vote_array << "BDCA".split("")}
+    42.times {vote_array << "CDAB".split("")}
+    13.times {vote_array << "CDBA".split("")}
+    51.times {vote_array << "DABC".split("")}
+    33.times {vote_array << "DBCA".split("")}
+    39.times {vote_array << "DCAB".split("")}
+    12.times {vote_array << "DCBA".split("")}
+
+    assert_equal 'C', InstantRunoffVote.new(vote_array).result.winners[0]
+  end
 
 
-  vote_array = Array.new
-  42.times {vote_array << "ABCD".split("")}
-  26.times {vote_array << "BCDA".split("")}
-  15.times {vote_array << "CDBA".split("")}
-  15.times {vote_array << "DCBA".split("")}
+  def test_irvlogic
+    vote_array = Array.new
+    42.times {vote_array << "ABCD".split("")}
+    26.times {vote_array << "BCDA".split("")}
+    15.times {vote_array << "CDBA".split("")}
+    15.times {vote_array << "DCBA".split("")}
 
 
-  print_winner( InstantRunoffLogicVote.new(vote_array).result )
-end
+    assert_equal 'B', InstantRunoffLogicVote.new(vote_array).result
+  end
 
 
-def range_test1
-  puts "USING RANGE..."
-  puts "The winner shold be: B"
+  def test_range1
+    vote_array = Array.new
+    42.times {vote_array << {:A => 10, :B => 5, :C => 2, :D => 1}}
+    26.times {vote_array << {:A => 1, :B => 10, :C => 5, :D => 2}}
+    15.times {vote_array << {:A => 1, :B => 2, :C => 10, :D => 5}}
+    17.times {vote_array << {:A => 1, :B => 2, :C => 5, :D => 10}}
 
 
-  vote_array = Array.new
-  42.times {vote_array << {:A => 10, :B => 5, :C => 2, :D => 1}}
-  26.times {vote_array << {:A => 1, :B => 10, :C => 5, :D => 2}}
-  15.times {vote_array << {:A => 1, :B => 2, :C => 10, :D => 5}}
-  17.times {vote_array << {:A => 1, :B => 2, :C => 5, :D => 10}}
+    assert_equal 'B', RangeVote.new(vote_array).result
+  end
 
 
-  print_winner( RangeVote.new(vote_array).result )
 end
 end
-
-condorcet_test1()
-ssd_test1()
-ssd_test2()
-ssd_test3()
-borda_test1()
-plurality_test1()
-approval_test1()
-irv_test1()
-irv_test2()
-irv_test3()
-irvlogic_test1()
-range_test1()

Benjamin Mako Hill || Want to submit a patch?