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([@a, @b], players)
95 class TestLeastRatePlayer < Test::Unit::TestCase
97 @pairing= ShogiServer::DeleteLeastRatePlayer.new
98 @a = ShogiServer::BasicPlayer.new
102 @b = ShogiServer::BasicPlayer.new
106 @c = ShogiServer::BasicPlayer.new
113 players = [@a, @b, @c]
114 @pairing.match(players)
115 assert_equal([@b,@c], players)
119 class TestRandomize < Test::Unit::TestCase
121 srand(10) # makes the random number generator determistic
122 @pairing = ShogiServer::Randomize.new
123 @a = ShogiServer::BasicPlayer.new
127 @b = ShogiServer::BasicPlayer.new
131 @c = ShogiServer::BasicPlayer.new
138 players = [@a, @b, @c]
139 @pairing.match(players)
140 assert_equal([@b,@a,@c], players)
144 class TestSortByRate < Test::Unit::TestCase
146 @pairing = ShogiServer::SortByRate.new
147 @a = ShogiServer::BasicPlayer.new
152 @b = ShogiServer::BasicPlayer.new
157 @c = ShogiServer::BasicPlayer.new
165 players = [@a, @b, @c]
166 @pairing.match(players)
167 assert_equal([@c,@a,@b], players)
171 class TestSortByRateWithRandomness < Test::Unit::TestCase
173 srand(10) # makes the random number generator determistic
174 @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
175 @a = ShogiServer::BasicPlayer.new
180 @b = ShogiServer::BasicPlayer.new
185 @c = ShogiServer::BasicPlayer.new
193 players = [@a, @b, @c]
194 @pairing.match(players)
195 assert_equal([@c,@b,@a], players)
199 class TestExcludeSacrifice < Test::Unit::TestCase
201 @obj = ShogiServer::ExcludeSacrificeGps500.new
202 @a = ShogiServer::BasicPlayer.new
208 @a.last_game_win = false
209 @b = ShogiServer::BasicPlayer.new
210 @b.player_id = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
215 @b.last_game_win = true
216 @c = ShogiServer::BasicPlayer.new
222 @c.last_game_win = true
228 assert_equal([@a], players)
234 assert_equal([], players)
240 assert_equal([@a,@b], players)
244 players = [@a, @b, @c]
246 assert_equal([@a, @c], players)
252 assert_equal([@a,@c], players)
256 class TestSwissPairing < Test::Unit::TestCase
259 @a = ShogiServer::BasicPlayer.new
262 @a.game_name = "floodgate-900-0"
263 @b = ShogiServer::BasicPlayer.new
266 @b.game_name = "floodgate-900-0"
267 @c = ShogiServer::BasicPlayer.new
270 @c.game_name = "floodgate-900-0"
271 @d = ShogiServer::BasicPlayer.new
274 @d.game_name = "floodgate-900-0"
276 @players = [@a, @b, @c, @d]
278 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
279 @history = ShogiServer::League::Floodgate::History.factory @file
281 @swiss = ShogiServer::Swiss.new
285 @file.delete if @file.exist?
291 assert(players.empty?)
295 ShogiServer::League::Floodgate::History.class_eval do
296 def last_win?(player_id)
300 @swiss.match @players
301 assert_equal([@d, @c, @b, @a], @players)
305 ShogiServer::League::Floodgate::History.class_eval do
306 def last_win?(player_id)
310 @swiss.match @players
311 assert_equal([@d, @c, @b, @a], @players)
315 ShogiServer::League::Floodgate::History.class_eval do
316 def last_win?(player_id)
324 @swiss.match @players
325 assert_equal([@a, @d, @c, @b], @players)
329 ShogiServer::League::Floodgate::History.class_eval do
330 def last_win?(player_id)
331 if player_id == "a" || player_id == "d"
338 @swiss.match @players
339 assert_equal([@d, @a, @c, @b], @players)
343 class TestFloodgateHistory < Test::Unit::TestCase
345 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
346 @history = ShogiServer::League::Floodgate::History.new @file
350 @file.delete if @file.exist?
354 file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
355 history = ShogiServer::League::Floodgate::History.new file
358 file.delete if file.exist?
363 def @history.make_record(game_result)
364 {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
365 :black => "hoge", :white => "foo",
366 :winner => "foo", :loser => "hoge"}
368 @history.update(dummy)
370 def @history.make_record(game_result)
371 {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
372 :black => "hoge", :white => "foo",
373 :winner => "hoge", :loser => "foo"}
375 @history.update(dummy)
377 def @history.make_record(game_result)
378 {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
379 :black => "hoge", :white => "foo",
380 :winner => nil, :loser => nil}
382 @history.update(dummy)
385 assert_equal 3, @history.records.size
386 assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
387 assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
388 assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
389 assert_equal "hoge", @history.records[1][:black]
390 assert_equal "foo", @history.records[1][:white]
391 assert_equal "hoge", @history.records[1][:winner]
392 assert_equal "foo", @history.records[1][:loser]
394 assert @history.last_win? "hoge"
395 assert !@history.last_win?("foo")
396 assert !@history.last_lose?("hoge")
397 assert @history.last_lose?("foo")