1 $:.unshift File.join(File.dirname(__FILE__), "..")
4 require 'shogi_server/player'
5 require 'shogi_server/pairing'
6 require 'shogi_server/league/floodgate'
8 $topdir = File.expand_path File.dirname(__FILE__)
25 $logger = MockLogger.new
38 class TestFloodgate < Test::Unit::TestCase
40 @fg = ShogiServer::League::Floodgate.new(nil)
48 assert(ShogiServer::League::Floodgate.game_name?("floodgate-900-0"))
49 assert(ShogiServer::League::Floodgate.game_name?("floodgate-0-10"))
50 assert(!ShogiServer::League::Floodgate.game_name?("floodgat-900-0"))
55 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
57 @pairing= ShogiServer::DeleteMostPlayingPlayer.new
58 @a = ShogiServer::BasicPlayer.new
62 @b = ShogiServer::BasicPlayer.new
66 @c = ShogiServer::BasicPlayer.new
73 players = [@a, @b, @c]
74 @pairing.match(players)
75 assert_equal([@a,@b], players)
79 class TestMakeEven < Test::Unit::TestCase
82 @pairing= ShogiServer::MakeEven.new
83 @a = ShogiServer::BasicPlayer.new
88 @b = ShogiServer::BasicPlayer.new
93 @c = ShogiServer::BasicPlayer.new
102 @pairing.match(players)
103 assert_equal([@a,@b], players)
107 players = [@a, @b, @c]
108 @pairing.match(players)
109 assert_equal([@a, @b], players)
113 class TestLeastRatePlayer < Test::Unit::TestCase
115 @pairing= ShogiServer::DeleteLeastRatePlayer.new
116 @a = ShogiServer::BasicPlayer.new
120 @b = ShogiServer::BasicPlayer.new
124 @c = ShogiServer::BasicPlayer.new
131 players = [@a, @b, @c]
132 @pairing.match(players)
133 assert_equal([@b,@c], players)
137 class TestRandomize < Test::Unit::TestCase
139 srand(10) # makes the random number generator determistic
140 @pairing = ShogiServer::Randomize.new
141 @a = ShogiServer::BasicPlayer.new
145 @b = ShogiServer::BasicPlayer.new
149 @c = ShogiServer::BasicPlayer.new
156 players = [@a, @b, @c]
157 @pairing.match(players)
158 assert_equal([@b,@a,@c], players)
162 class TestSortByRate < Test::Unit::TestCase
164 @pairing = ShogiServer::SortByRate.new
165 @a = ShogiServer::BasicPlayer.new
170 @b = ShogiServer::BasicPlayer.new
175 @c = ShogiServer::BasicPlayer.new
183 players = [@a, @b, @c]
184 @pairing.match(players)
185 assert_equal([@c,@a,@b], players)
189 class TestSortByRateWithRandomness < Test::Unit::TestCase
191 srand(10) # makes the random number generator determistic
192 @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
193 @a = ShogiServer::BasicPlayer.new
198 @b = ShogiServer::BasicPlayer.new
203 @c = ShogiServer::BasicPlayer.new
211 players = [@a, @b, @c]
212 @pairing.match(players)
213 assert_equal([@c,@b,@a], players)
217 class TestExcludeSacrifice < Test::Unit::TestCase
219 @obj = ShogiServer::ExcludeSacrificeGps500.new
220 @a = ShogiServer::BasicPlayer.new
226 @a.last_game_win = false
227 @b = ShogiServer::BasicPlayer.new
228 @b.player_id = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
233 @b.last_game_win = true
234 @c = ShogiServer::BasicPlayer.new
240 @c.last_game_win = true
246 assert_equal([@a], players)
252 assert_equal([], players)
258 assert_equal([@a,@b], players)
262 players = [@a, @b, @c]
264 assert_equal([@a, @c], players)
270 assert_equal([@a,@c], players)
274 class TestSwissPairing < Test::Unit::TestCase
277 @a = ShogiServer::BasicPlayer.new
280 @a.game_name = "floodgate-900-0"
281 @b = ShogiServer::BasicPlayer.new
284 @b.game_name = "floodgate-900-0"
285 @c = ShogiServer::BasicPlayer.new
288 @c.game_name = "floodgate-900-0"
289 @d = ShogiServer::BasicPlayer.new
292 @d.game_name = "floodgate-900-0"
294 @players = [@a, @b, @c, @d]
296 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
297 @history = ShogiServer::League::Floodgate::History.factory @file
299 @swiss = ShogiServer::Swiss.new
303 @file.delete if @file.exist?
309 assert(players.empty?)
313 ShogiServer::League::Floodgate::History.class_eval do
314 def last_win?(player_id)
318 @swiss.match @players
319 assert_equal([@d, @c, @b, @a], @players)
323 ShogiServer::League::Floodgate::History.class_eval do
324 def last_win?(player_id)
328 @swiss.match @players
329 assert_equal([@d, @c, @b, @a], @players)
333 ShogiServer::League::Floodgate::History.class_eval do
334 def last_win?(player_id)
342 @swiss.match @players
343 assert_equal([@a, @d, @c, @b], @players)
347 ShogiServer::League::Floodgate::History.class_eval do
348 def last_win?(player_id)
349 if player_id == "a" || player_id == "d"
356 @swiss.match @players
357 assert_equal([@d, @a, @c, @b], @players)
361 class TestFloodgateHistory < Test::Unit::TestCase
363 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
364 @history = ShogiServer::League::Floodgate::History.new @file
368 @file.delete if @file.exist?
372 file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
373 history = ShogiServer::League::Floodgate::History.new file
376 file.delete if file.exist?
381 def @history.make_record(game_result)
382 {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
383 :black => "hoge", :white => "foo",
384 :winner => "foo", :loser => "hoge"}
386 @history.update(dummy)
388 def @history.make_record(game_result)
389 {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
390 :black => "hoge", :white => "foo",
391 :winner => "hoge", :loser => "foo"}
393 @history.update(dummy)
395 def @history.make_record(game_result)
396 {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
397 :black => "hoge", :white => "foo",
398 :winner => nil, :loser => nil}
400 @history.update(dummy)
403 assert_equal 3, @history.records.size
404 assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
405 assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
406 assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
407 assert_equal "hoge", @history.records[1][:black]
408 assert_equal "foo", @history.records[1][:white]
409 assert_equal "hoge", @history.records[1][:winner]
410 assert_equal "foo", @history.records[1][:loser]
412 assert @history.last_win? "hoge"
413 assert !@history.last_win?("foo")
414 assert !@history.last_lose?("hoge")
415 assert @history.last_lose?("foo")