OSDN Git Service

[shogi-server] shogi_server/pairing.rb: Attempt more trials
[shogi-server/shogi-server.git] / test / TC_pairing.rb
index 90cea15..86970ca 100644 (file)
@@ -411,6 +411,19 @@ class TestLeastDiff < Test::Unit::TestCase
     @x.player_id = "x"
     @x.name = "x"
 
+    @abcdefg1 = ShogiServer::BasicPlayer.new
+    @abcdefg1.player_id = "abcdefg1"
+    @abcdefg1.name = "abcdefg1"
+    @abcdefg1.rate = 2100
+    @abcdefg2 = ShogiServer::BasicPlayer.new
+    @abcdefg2.player_id = "abcdefg2"
+    @abcdefg2.name = "abcdefg2"
+    @abcdefg2.rate = 2200
+    @abcdxyz = ShogiServer::BasicPlayer.new
+    @abcdxyz.player_id = "abcdxyz"
+    @abcdxyz.name = "abcdxyz"
+    @abcdxyz.rate = 2300
+
     $league.add(@a)
     $league.add(@b)
     $league.add(@c)
@@ -420,6 +433,9 @@ class TestLeastDiff < Test::Unit::TestCase
     $league.add(@g)
     $league.add(@h)
     $league.add(@x)
+    $league.add(@abcdefg1)
+    $league.add(@abcdefg2)
+    $league.add(@abcdxyz)
   end
 
   def teardown
@@ -483,6 +499,12 @@ class TestLeastDiff < Test::Unit::TestCase
     assert_pairs([@a,@b,@h], players)
   end
 
+  def test_match_many_players
+    players = [@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h,@a,@b,@h]
+    r = @pairing.match(players)
+    assert true
+  end
+
   def test_calculate_diff_with_penalty
     players = [@a,@b]
     assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty(players,nil))
@@ -548,9 +570,20 @@ class TestLeastDiff < Test::Unit::TestCase
     assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
   end
 
+  def test_calculate_diff_with_kin_4_players
+    players = [@abcdefg1, @abcdxyz]
+    assert_equal(@abcdxyz.rate - @abcdefg1.rate + 400, @pairing.calculate_diff_with_penalty(players,nil))
+  end
+
+  def test_calculate_diff_with_kin_7_players
+    players = [@abcdefg1, @abcdefg2]
+    assert_equal(@abcdefg2.rate - @abcdefg1.rate + 800, @pairing.calculate_diff_with_penalty(players,nil))
+  end
+
   def test_get_player_rate_0
     assert_equal(2150, @pairing.get_player_rate(@x, @history))
 
+    @x.estimated_rate = 0
     dummy = nil
     def @history.make_record(game_result)
       {:game_id => "wdoor+floodgate-900-0-x-a-1", 
@@ -558,8 +591,9 @@ class TestLeastDiff < Test::Unit::TestCase
        :winner => "x", :loser => "a"}
     end
     @history.update(dummy)
-    assert_equal(@a.rate+100, @pairing.get_player_rate(@x, @history))
+    assert_equal(@a.rate+200, @pairing.get_player_rate(@x, @history))
 
+    @x.estimated_rate = 0
     def @history.make_record(game_result)
       {:game_id => "wdoor+floodgate-900-0-x-b-1", 
        :black => "x",  :white => "b",
@@ -567,7 +601,69 @@ class TestLeastDiff < Test::Unit::TestCase
     end
     @history.update(dummy)
 
-    assert_equal((@a.rate+100+@b.rate-100)/2, @pairing.get_player_rate(@x, @history))
+    assert_equal(@b.rate-200, @pairing.get_player_rate(@x, @history))
+  end
+
+  def test_total_posibilities
+    assert_equal 1, @pairing.total_posibilities(2)
+    assert_equal 1, @pairing.total_posibilities(3)
+    assert_equal 3, @pairing.total_posibilities(4)
+    assert_equal 945, @pairing.total_posibilities(10)
   end
 end
 
+class TestExcludeUnratedPlayers < Test::Unit::TestCase
+  def setup
+    @pairing= ShogiServer::ExcludeUnratedPlayers.new
+    @a = ShogiServer::BasicPlayer.new
+    @a.name = "a"
+    @a.win  = 1
+    @a.loss = 2
+    @a.rate = 0
+    @b = ShogiServer::BasicPlayer.new
+    @b.name = "b"
+    @b.win  = 10
+    @b.loss = 20
+    @b.rate = 1500
+    @c = ShogiServer::BasicPlayer.new
+    @c.name = "c"
+    @c.win  = 100
+    @c.loss = 200
+    @c.rate = 1000
+    @d = ShogiServer::BasicPlayer.new
+    @d.name = "d"
+    @d.win  = 1000
+    @d.loss = 2000
+    @d.rate = 2000
+  end
+
+  def test_match_without_any_players
+    players = []
+    @pairing.match(players)
+    assert_equal([], players)
+  end
+
+  def test_match_without_unrated_player_1
+    players = [@b, @c, @d]
+    @pairing.match(players)
+    assert_equal([@b, @c, @d], players)
+  end
+
+  def test_match_without_unrated_player_2
+    players = [@b]
+    @pairing.match(players)
+    assert_equal([@b], players)
+  end
+
+  def test_match_with_unrated_player_1
+    players = [@a, @b, @c, @d]
+    @pairing.match(players)
+    assert_equal([@b, @c, @d], players)
+  end
+
+  def test_match_with_unrated_player_2
+    players = [@a]
+    @pairing.match(players)
+    assert_equal([], players)
+  end
+end