1 $:.unshift File.join(File.dirname(__FILE__), "..")
4 require 'shogi_server/player'
5 require 'shogi_server/pairing'
6 require 'shogi_server/league/floodgate'
7 require 'test/mock_log_message'
9 $topdir = File.expand_path File.dirname(__FILE__)
11 class TestFloodgate < Test::Unit::TestCase
13 @fg = ShogiServer::League::Floodgate.new(nil)
21 assert(ShogiServer::League::Floodgate.game_name?("floodgate-900-0"))
22 assert(ShogiServer::League::Floodgate.game_name?("floodgate-0-10"))
23 assert(!ShogiServer::League::Floodgate.game_name?("floodgat-900-0"))
26 def test_instance_game_name
27 fg = ShogiServer::League::Floodgate.new(nil, {:game_name => "floodgate-900-0"})
28 assert(fg.game_name?("floodgate-900-0"))
29 assert(!fg.game_name?("floodgate-3600-0"))
30 fg = ShogiServer::League::Floodgate.new(nil, {:game_name => "floodgate-3600-0"})
31 assert(!fg.game_name?("floodgate-900-0"))
32 assert(fg.game_name?("floodgate-3600-0"))
37 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
39 @pairing= ShogiServer::DeleteMostPlayingPlayer.new
40 @a = ShogiServer::BasicPlayer.new
44 @b = ShogiServer::BasicPlayer.new
48 @c = ShogiServer::BasicPlayer.new
55 players = [@a, @b, @c]
56 @pairing.match(players)
57 assert_equal([@a,@b], players)
61 class TestMakeEven < Test::Unit::TestCase
64 @pairing= ShogiServer::MakeEven.new
65 @a = ShogiServer::BasicPlayer.new
70 @b = ShogiServer::BasicPlayer.new
75 @c = ShogiServer::BasicPlayer.new
84 @pairing.match(players)
85 assert_equal([@a,@b], players)
89 players = [@a, @b, @c]
90 @pairing.match(players)
91 assert_equal(2, players.size)
92 assert(players[0] != players[1])
96 class TestLeastRatePlayer < Test::Unit::TestCase
98 @pairing= ShogiServer::DeleteLeastRatePlayer.new
99 @a = ShogiServer::BasicPlayer.new
103 @b = ShogiServer::BasicPlayer.new
107 @c = ShogiServer::BasicPlayer.new
114 players = [@a, @b, @c]
115 @pairing.match(players)
116 assert_equal([@b,@c], players)
120 class TestRandomize < Test::Unit::TestCase
122 srand(10) # makes the random number generator determistic
123 @pairing = ShogiServer::Randomize.new
124 @a = ShogiServer::BasicPlayer.new
128 @b = ShogiServer::BasicPlayer.new
132 @c = ShogiServer::BasicPlayer.new
139 players = [@a, @b, @c]
140 @pairing.match(players)
141 assert_equal(3, players.size)
142 assert(players.include? @a)
143 assert(players.include? @b)
144 assert(players.include? @c)
148 class TestSortByRate < Test::Unit::TestCase
150 @pairing = ShogiServer::SortByRate.new
151 @a = ShogiServer::BasicPlayer.new
156 @b = ShogiServer::BasicPlayer.new
161 @c = ShogiServer::BasicPlayer.new
169 players = [@a, @b, @c]
170 @pairing.match(players)
171 assert_equal([@c,@a,@b], players)
175 class TestSortByRateWithRandomness < Test::Unit::TestCase
177 srand(10) # makes the random number generator determistic
178 @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
179 @a = ShogiServer::BasicPlayer.new
184 @b = ShogiServer::BasicPlayer.new
189 @c = ShogiServer::BasicPlayer.new
197 players = [@a, @b, @c]
198 @pairing.match(players)
199 assert_equal([@c,@b,@a], players)
203 class TestExcludeSacrifice < Test::Unit::TestCase
205 @obj = ShogiServer::ExcludeSacrificeGps500.new
206 @a = ShogiServer::BasicPlayer.new
212 @a.last_game_win = false
213 @b = ShogiServer::BasicPlayer.new
214 @b.player_id = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
219 @b.last_game_win = true
220 @c = ShogiServer::BasicPlayer.new
226 @c.last_game_win = true
232 assert_equal([@a], players)
238 assert_equal([], players)
244 assert_equal([@a,@b], players)
248 players = [@a, @b, @c]
250 assert_equal([@a, @c], players)
256 assert_equal([@a,@c], players)
260 class TestSwissPairing < Test::Unit::TestCase
263 @a = ShogiServer::BasicPlayer.new
266 @a.game_name = "floodgate-900-0"
267 @b = ShogiServer::BasicPlayer.new
270 @b.game_name = "floodgate-900-0"
271 @c = ShogiServer::BasicPlayer.new
274 @c.game_name = "floodgate-900-0"
275 @d = ShogiServer::BasicPlayer.new
278 @d.game_name = "floodgate-900-0"
280 @players = [@a, @b, @c, @d]
282 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
283 @history = ShogiServer::League::Floodgate::History.factory @file
285 @swiss = ShogiServer::Swiss.new
289 @file.delete if @file.exist?
295 assert(players.empty?)
299 ShogiServer::League::Floodgate::History.class_eval do
300 def last_win?(player_id)
304 @swiss.match @players
305 assert_equal([@d, @c, @b, @a], @players)
309 ShogiServer::League::Floodgate::History.class_eval do
310 def last_win?(player_id)
314 @swiss.match @players
315 assert_equal([@d, @c, @b, @a], @players)
319 ShogiServer::League::Floodgate::History.class_eval do
320 def last_win?(player_id)
328 @swiss.match @players
329 assert_equal([@a, @d, @c, @b], @players)
333 ShogiServer::League::Floodgate::History.class_eval do
334 def last_win?(player_id)
335 if player_id == "a" || player_id == "d"
342 @swiss.match @players
343 assert_equal([@d, @a, @c, @b], @players)
347 class TestFloodgateHistory < Test::Unit::TestCase
349 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
350 @history = ShogiServer::League::Floodgate::History.new @file
354 @file.delete if @file.exist?
358 file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
359 history = ShogiServer::League::Floodgate::History.new file
362 file.delete if file.exist?
367 def @history.make_record(game_result)
368 {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
369 :black => "hoge", :white => "foo",
370 :winner => "foo", :loser => "hoge"}
372 @history.update(dummy)
374 def @history.make_record(game_result)
375 {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
376 :black => "hoge", :white => "foo",
377 :winner => "hoge", :loser => "foo"}
379 @history.update(dummy)
381 def @history.make_record(game_result)
382 {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
383 :black => "hoge", :white => "foo",
384 :winner => nil, :loser => nil}
386 @history.update(dummy)
389 assert_equal 3, @history.records.size
390 assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
391 assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
392 assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
393 assert_equal "hoge", @history.records[1][:black]
394 assert_equal "foo", @history.records[1][:white]
395 assert_equal "hoge", @history.records[1][:winner]
396 assert_equal "foo", @history.records[1][:loser]
398 assert @history.last_win? "hoge"
399 assert !@history.last_win?("foo")
400 assert !@history.last_lose?("hoge")
401 assert @history.last_lose?("foo")
403 assert_equal("foo", @history.last_opponent("hoge"))
404 assert_equal("hoge", @history.last_opponent("foo"))
406 games = @history.win_games("hoge")
407 assert_equal(1, games.size )
408 assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
409 games = @history.win_games("foo")
410 assert_equal(1, games.size )
411 assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
412 games = @history.loss_games("hoge")
413 assert_equal(1, games.size )
414 assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
415 games = @history.loss_games("foo")
416 assert_equal(1, games.size )
417 assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])