+class TestSwissPairing < Test::Unit::TestCase
+ def setup
+ srand(10)
+ @a = ShogiServer::BasicPlayer.new
+ @a.player_id = "a"
+ @a.rate = 0
+ @a.game_name = "floodgate-900-0"
+ @b = ShogiServer::BasicPlayer.new
+ @b.player_id = "b"
+ @b.rate = 1000
+ @b.game_name = "floodgate-900-0"
+ @c = ShogiServer::BasicPlayer.new
+ @c.player_id = "c"
+ @c.rate = 1500
+ @c.game_name = "floodgate-900-0"
+ @d = ShogiServer::BasicPlayer.new
+ @d.player_id = "d"
+ @d.rate = 2000
+ @d.game_name = "floodgate-900-0"
+
+ @players = [@a, @b, @c, @d]
+
+ @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
+ @history = ShogiServer::League::Floodgate::History.factory @file
+
+ @swiss = ShogiServer::Swiss.new
+ end
+
+ def teardown
+ @file.delete if @file.exist?
+ end
+
+ def test_none
+ players = []
+ @swiss.match players
+ assert(players.empty?)
+ end
+
+ def test_all_win
+ ShogiServer::League::Floodgate::History.class_eval do
+ def last_win?(player_id)
+ true
+ end
+ end
+ @swiss.match @players
+ assert_equal([@d, @c, @b, @a], @players)
+ end
+
+ def test_all_lose
+ ShogiServer::League::Floodgate::History.class_eval do
+ def last_win?(player_id)
+ false
+ end
+ end
+ @swiss.match @players
+ assert_equal([@d, @c, @b, @a], @players)
+ end
+
+ def test_one_win
+ ShogiServer::League::Floodgate::History.class_eval do
+ def last_win?(player_id)
+ if player_id == "a"
+ true
+ else
+ false
+ end
+ end
+ end
+ @swiss.match @players
+ assert_equal([@a, @d, @c, @b], @players)
+ end
+
+ def test_two_win
+ ShogiServer::League::Floodgate::History.class_eval do
+ def last_win?(player_id)
+ if player_id == "a" || player_id == "d"
+ true
+ else
+ false
+ end
+ end
+ end
+ @swiss.match @players
+ assert_equal([@d, @a, @c, @b], @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 teardown
+ @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
+
+ def test_update
+ dummy = nil
+ def @history.make_record(game_result)
+ {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
+ :black => "hoge", :white => "foo",
+ :winner => "foo", :loser => "hoge"}
+ end
+ @history.update(dummy)
+
+ def @history.make_record(game_result)
+ {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
+ :black => "hoge", :white => "foo",
+ :winner => "hoge", :loser => "foo"}
+ end
+ @history.update(dummy)
+
+ def @history.make_record(game_result)
+ {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
+ :black => "hoge", :white => "foo",
+ :winner => nil, :loser => nil}
+ end
+ @history.update(dummy)
+
+ @history.load
+ assert_equal 3, @history.records.size
+ assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
+ assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
+ assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
+ assert_equal "hoge", @history.records[1][:black]
+ assert_equal "foo", @history.records[1][:white]
+ assert_equal "hoge", @history.records[1][:winner]
+ assert_equal "foo", @history.records[1][:loser]
+
+ assert @history.last_win? "hoge"
+ assert !@history.last_win?("foo")
+ assert !@history.last_lose?("hoge")
+ assert @history.last_lose?("foo")
+
+ assert_equal("foo", @history.last_opponent("hoge"))
+ assert_equal("hoge", @history.last_opponent("foo"))
+
+ games = @history.win_games("hoge")
+ assert_equal(1, games.size )
+ assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
+ games = @history.win_games("foo")
+ assert_equal(1, games.size )
+ assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
+ games = @history.loss_games("hoge")
+ assert_equal(1, games.size )
+ assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
+ games = @history.loss_games("foo")
+ assert_equal(1, games.size )
+ assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
+ end
+end
+
+