OSDN Git Service

Record Floodgate games' history
[shogi-server/shogi-server.git] / test / TC_floodgate.rb
index 7ca3617..f9ac2a7 100644 (file)
@@ -1,13 +1,15 @@
 $:.unshift File.join(File.dirname(__FILE__), "..")
 require 'test/unit'
-load 'shogi-server'
-require 'pairing' 
+require 'shogi_server'
+require 'shogi_server/player'
+require 'shogi_server/pairing'
+require 'shogi_server/league/floodgate'
 
 class MockLogger
   def debug(str)
   end
   def info(str)
-    # puts str
+    #puts str
   end
   def warn(str)
   end
@@ -16,6 +18,13 @@ class MockLogger
 end
 
 $logger = MockLogger.new
+def log_message(msg)
+  $logger.info(msg)
+end
+
+def log_warning(msg)
+  $logger.warn(msg)
+end
 
 class TestFloodgate < Test::Unit::TestCase
   def setup
@@ -37,161 +46,269 @@ end
 class TestPairing < Test::Unit::TestCase  
   def setup
     @pairing= ShogiServer::Pairing.new
+    $pairs = []
+    def @pairing.start_game(p1,p2)
+      $pairs << [p1,p2]
+    end
     @a = ShogiServer::BasicPlayer.new
+    @a.name = "a"
     @a.win  = 1
     @a.loss = 2
     @a.rate = 0
+    @a.last_game_win = false
     @b = ShogiServer::BasicPlayer.new
+    @b.name = "b"
     @b.win  = 10
     @b.loss = 20
     @b.rate = 1500
+    @b.last_game_win = true
     @c = ShogiServer::BasicPlayer.new
+    @c.name = "c"
     @c.win  = 100
     @c.loss = 200
     @c.rate = 1000
+    @c.last_game_win = true
+    @d = ShogiServer::BasicPlayer.new
+    @d.name = "d"
+    @d.win  = 1000
+    @d.loss = 2000
+    @d.rate = 1800
+    @d.last_game_win = true
   end
 
-  def test_delete_most_playing_player
-    players = [@a, @b, @c]
-    @pairing.delete_most_playing_player(players)
-    assert_equal([@a,@b], players)
-  end
-
-  def test_delete_least_rate_player
-    players = [@a, @b, @c]
-    @pairing.delete_least_rate_player(players)
-    assert_equal([@b,@c], players)
+  def test_include_newbie
+    assert(@pairing.include_newbie?([@a]))
+    assert(!@pairing.include_newbie?([@b]))
+    assert(@pairing.include_newbie?([@b,@a]))
+    assert(!@pairing.include_newbie?([@b,@c]))
   end
 end
 
-class TestRandomPairing < Test::Unit::TestCase  
+class TestStartGame < Test::Unit::TestCase
   def setup
-    @pairing= ShogiServer::RandomPairing.new
+    @pairing= ShogiServer::StartGame.new
     $called = 0
     def @pairing.start_game(p1,p2)
       $called += 1
     end
     @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_two_players
+    players = [@a,@b]
+    @pairing.match(players)
+    assert_equal(1, $called)
   end
 
-  def test_random_match_1
+  def test_match_one_player
     players = [@a]
     @pairing.match(players)
     assert_equal(0, $called)
   end
 
-  def test_random_match_2
-    players = [@a,@b]
+  def test_match_zero_player
+    players = []
+    @pairing.match(players)
+    assert_equal(0, $called)
+  end
+
+  def test_match_three_players
+    players = [@a,@b,@c]
     @pairing.match(players)
     assert_equal(1, $called)
   end
-  
-  def test_random_match_3
+
+  def test_match_four_players
+    players = [@a,@b,@c,@d]
+    @pairing.match(players)
+    assert_equal(2, $called)
+  end
+end
+
+class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
+  def setup
+    @pairing= ShogiServer::DeleteMostPlayingPlayer.new
+    @a = ShogiServer::BasicPlayer.new
+    @a.win  = 1
+    @a.loss = 2
+    @a.rate = 0
+    @b = ShogiServer::BasicPlayer.new
+    @b.win  = 10
+    @b.loss = 20
+    @b.rate = 1500
+    @c = ShogiServer::BasicPlayer.new
+    @c.win  = 100
+    @c.loss = 200
+    @c.rate = 1000
+  end
+
+  def test_match
     players = [@a, @b, @c]
     @pairing.match(players)
-    assert_equal(1, $called)
+    assert_equal([@a,@b], players)
   end
 end
 
-class TestSwissPairing < Test::Unit::TestCase  
+class TestMakeEven < Test::Unit::TestCase  
   def setup
-    @pairing= ShogiServer::SwissPairing.new
-    $pairs = []
-    def @pairing.start_game(p1,p2)
-      $pairs << [p1,p2]
-    end
+    srand(10)
+    @pairing= ShogiServer::MakeEven.new
     @a = ShogiServer::BasicPlayer.new
     @a.name = "a"
     @a.win  = 1
     @a.loss = 2
     @a.rate = 0
-    @a.last_game_win = false
     @b = ShogiServer::BasicPlayer.new
     @b.name = "b"
     @b.win  = 10
     @b.loss = 20
     @b.rate = 1500
-    @b.last_game_win = true
     @c = ShogiServer::BasicPlayer.new
     @c.name = "c"
     @c.win  = 100
     @c.loss = 200
     @c.rate = 1000
-    @c.last_game_win = true
-    @d = ShogiServer::BasicPlayer.new
-    @d.name = "d"
-    @d.win  = 1000
-    @d.loss = 2000
-    @d.rate = 1800
-    @d.last_game_win = true
   end
 
-  def sort(players)
-    return players.sort{|a,b| a.name <=> b.name}
+ def test_match_even
+    players = [@a, @b]
+    @pairing.match(players)
+    assert_equal([@a,@b], players)
+ end
+
+ def test_match_odd
+    players = [@a, @b, @c]
+    @pairing.match(players)
+    assert_equal([@a, @b], players)
   end
+end
 
-  def test_include_newbie
-    assert(@pairing.include_newbie?([@a]))
-    assert(!@pairing.include_newbie?([@b]))
-    assert(@pairing.include_newbie?([@b,@a]))
-    assert(!@pairing.include_newbie?([@b,@c]))
+class TestLeastRatePlayer < Test::Unit::TestCase  
+  def setup
+    @pairing= ShogiServer::DeleteLeastRatePlayer.new
+    @a = ShogiServer::BasicPlayer.new
+    @a.win  = 1
+    @a.loss = 2
+    @a.rate = 0
+    @b = ShogiServer::BasicPlayer.new
+    @b.win  = 10
+    @b.loss = 20
+    @b.rate = 1500
+    @c = ShogiServer::BasicPlayer.new
+    @c.win  = 100
+    @c.loss = 200
+    @c.rate = 1000
   end
 
-  def test_match_1
-    @pairing.match([@a])
-    assert_equal(0, $pairs.size)
+ def test_match
+    players = [@a, @b, @c]
+    @pairing.match(players)
+    assert_equal([@b,@c], players)
   end
-  
-  def test_match_2
-    @pairing.match([@b])
-    assert_equal(0, $pairs.size)
+end
+
+class TestRandomize < Test::Unit::TestCase  
+  def setup
+    srand(10) # makes the random number generator determistic
+    @pairing = ShogiServer::Randomize.new
+    @a = ShogiServer::BasicPlayer.new
+    @a.name = "a"
+    @a.win  = 1
+    @a.loss = 2
+    @b = ShogiServer::BasicPlayer.new
+    @b.name = "b"
+    @b.win  = 10
+    @b.loss = 20
+    @c = ShogiServer::BasicPlayer.new
+    @c.name = "c"
+    @c.win  = 100
+    @c.loss = 200
   end
-  
-  def test_match_3
-    @pairing.match([@a,@b])
-    assert_equal(1, $pairs.size)
-    assert_equal(sort([@a,@b]), sort($pairs.first))
+
+  def test_match
+    players = [@a, @b, @c]
+    @pairing.match(players)
+    assert_equal([@b,@a,@c], players)
   end
-  
-  def test_match_4
-    @pairing.match([@c,@b])
-    assert_equal(1, $pairs.size)
-    assert_equal(sort([@b,@c]), sort($pairs.first))
+end
+
+class TestSortByRate < Test::Unit::TestCase  
+  def setup
+    @pairing = ShogiServer::SortByRate.new
+    @a = ShogiServer::BasicPlayer.new
+    @a.name = "a"
+    @a.win  = 1
+    @a.loss = 2
+    @a.rate = 1500
+    @b = ShogiServer::BasicPlayer.new
+    @b.name = "b"
+    @b.win  = 10
+    @b.loss = 20
+    @b.rate = 2000
+    @c = ShogiServer::BasicPlayer.new
+    @c.name = "c"
+    @c.win  = 100
+    @c.loss = 200
+    @c.rate = 700
   end
-  
-  def test_match_5
-    @pairing.match([@c,@b,@a])
-    assert_equal(1, $pairs.size)
-    assert_equal(sort([@b,@c]), sort($pairs.first))
+
+  def test_match
+    players = [@a, @b, @c]
+    @pairing.match(players)
+    assert_equal([@c,@a,@b], players)
   end
-  
-  def test_match_6
-    @pairing.match([@c,@b,@a,@d])
-    assert_equal(2, $pairs.size)
-    assert_equal(sort([@b,@d]), sort($pairs.first))
-    assert_equal(sort([@a,@c]), sort($pairs.last))
+end
+
+class TestSortByRateWithRandomness < Test::Unit::TestCase  
+  def setup
+    srand(10) # makes the random number generator determistic
+    @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
+    @a = ShogiServer::BasicPlayer.new
+    @a.name = "a"
+    @a.win  = 1
+    @a.loss = 2
+    @a.rate = 1500
+    @b = ShogiServer::BasicPlayer.new
+    @b.name = "b"
+    @b.win  = 10
+    @b.loss = 20
+    @b.rate = 2000
+    @c = ShogiServer::BasicPlayer.new
+    @c.name = "c"
+    @c.win  = 100
+    @c.loss = 200
+    @c.rate = 700
+  end
+
+  def test_match
+    players = [@a, @b, @c]
+    @pairing.match(players)
+    assert_equal([@c,@b,@a], players)
   end
 end
 
 class TestExcludeSacrifice < Test::Unit::TestCase  
-  class Dummy
-    attr_reader :players
-    def match(players)
-      @players = players
-    end
-  end
-  
   def setup
-    @dummy = Dummy.new
-    @obj = ShogiServer::ExcludeSacrifice.new(@dummy)
+    @obj = ShogiServer::ExcludeSacrificeGps500.new
     @a = ShogiServer::BasicPlayer.new
     @a.player_id   = "a"
     @a.name = "a"
@@ -201,7 +318,7 @@ class TestExcludeSacrifice < Test::Unit::TestCase
     @a.last_game_win = false
     @b = ShogiServer::BasicPlayer.new
     @b.player_id   = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
-    @b.name = "b"
+    @b.name = "gps500"
     @b.win  = 10
     @b.loss = 20
     @b.rate = 1500
@@ -216,28 +333,52 @@ class TestExcludeSacrifice < Test::Unit::TestCase
   end
 
   def test_match_1
-    @obj.match([@a])
-    assert_equal(1, @dummy.players.size)
+    players = [@a]
+    @obj.match(players)
+    assert_equal([@a], players)
   end
   
   def test_match_2
-    @obj.match([@b])
-    assert_equal(0, @dummy.players.size)
+    players = [@b]
+    @obj.match(players)
+    assert_equal([], players)
   end
   
   def test_match_3
-    @obj.match([@a, @b])
-    assert_equal(2, @dummy.players.size)
+    players = [@a, @b]
+    @obj.match(players)
+    assert_equal([@a,@b], players)
   end
 
   def test_match_4
-    @obj.match([@a, @b, @c])
-    assert_equal(2, @dummy.players.size)
+    players = [@a, @b, @c]
+    @obj.match(players)
+    assert_equal([@a, @c], players)
   end
 
   def test_match_5
-    @obj.match([@a, @c])
-    assert_equal(2, @dummy.players.size)
+    players = [@a, @c]
+    @obj.match(players)
+    assert_equal([@a,@c], players)
   end
 end
 
+
+class TestFloodgateHistory < Test::Unit::TestCase
+  def setup
+    @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
+    @history = ShogiServer::League::Floodgate::History.new @file
+  end
+
+  def teaup
+    @file.delete if @file.exist?
+  end
+
+  def test_new
+    file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
+    history = ShogiServer::League::Floodgate::History.new file
+    history.save
+    assert file.exist?
+    file.delete if file.exist?
+  end
+end