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"))
28 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
30 @pairing= ShogiServer::DeleteMostPlayingPlayer.new
31 @a = ShogiServer::BasicPlayer.new
35 @b = ShogiServer::BasicPlayer.new
39 @c = ShogiServer::BasicPlayer.new
46 players = [@a, @b, @c]
47 @pairing.match(players)
48 assert_equal([@a,@b], players)
52 class TestMakeEven < Test::Unit::TestCase
55 @pairing= ShogiServer::MakeEven.new
56 @a = ShogiServer::BasicPlayer.new
61 @b = ShogiServer::BasicPlayer.new
66 @c = ShogiServer::BasicPlayer.new
75 @pairing.match(players)
76 assert_equal([@a,@b], players)
80 players = [@a, @b, @c]
81 @pairing.match(players)
82 assert_equal([@a, @b], players)
86 class TestLeastRatePlayer < Test::Unit::TestCase
88 @pairing= ShogiServer::DeleteLeastRatePlayer.new
89 @a = ShogiServer::BasicPlayer.new
93 @b = ShogiServer::BasicPlayer.new
97 @c = ShogiServer::BasicPlayer.new
104 players = [@a, @b, @c]
105 @pairing.match(players)
106 assert_equal([@b,@c], players)
110 class TestRandomize < Test::Unit::TestCase
112 srand(10) # makes the random number generator determistic
113 @pairing = ShogiServer::Randomize.new
114 @a = ShogiServer::BasicPlayer.new
118 @b = ShogiServer::BasicPlayer.new
122 @c = ShogiServer::BasicPlayer.new
129 players = [@a, @b, @c]
130 @pairing.match(players)
131 assert_equal([@b,@a,@c], players)
135 class TestSortByRate < Test::Unit::TestCase
137 @pairing = ShogiServer::SortByRate.new
138 @a = ShogiServer::BasicPlayer.new
143 @b = ShogiServer::BasicPlayer.new
148 @c = ShogiServer::BasicPlayer.new
156 players = [@a, @b, @c]
157 @pairing.match(players)
158 assert_equal([@c,@a,@b], players)
162 class TestSortByRateWithRandomness < Test::Unit::TestCase
164 srand(10) # makes the random number generator determistic
165 @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
166 @a = ShogiServer::BasicPlayer.new
171 @b = ShogiServer::BasicPlayer.new
176 @c = ShogiServer::BasicPlayer.new
184 players = [@a, @b, @c]
185 @pairing.match(players)
186 assert_equal([@c,@b,@a], players)
190 class TestExcludeSacrifice < Test::Unit::TestCase
192 @obj = ShogiServer::ExcludeSacrificeGps500.new
193 @a = ShogiServer::BasicPlayer.new
199 @a.last_game_win = false
200 @b = ShogiServer::BasicPlayer.new
201 @b.player_id = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
206 @b.last_game_win = true
207 @c = ShogiServer::BasicPlayer.new
213 @c.last_game_win = true
219 assert_equal([@a], players)
225 assert_equal([], players)
231 assert_equal([@a,@b], players)
235 players = [@a, @b, @c]
237 assert_equal([@a, @c], players)
243 assert_equal([@a,@c], players)
247 class TestSwissPairing < Test::Unit::TestCase
250 @a = ShogiServer::BasicPlayer.new
253 @a.game_name = "floodgate-900-0"
254 @b = ShogiServer::BasicPlayer.new
257 @b.game_name = "floodgate-900-0"
258 @c = ShogiServer::BasicPlayer.new
261 @c.game_name = "floodgate-900-0"
262 @d = ShogiServer::BasicPlayer.new
265 @d.game_name = "floodgate-900-0"
267 @players = [@a, @b, @c, @d]
269 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
270 @history = ShogiServer::League::Floodgate::History.factory @file
272 @swiss = ShogiServer::Swiss.new
276 @file.delete if @file.exist?
282 assert(players.empty?)
286 ShogiServer::League::Floodgate::History.class_eval do
287 def last_win?(player_id)
291 @swiss.match @players
292 assert_equal([@d, @c, @b, @a], @players)
296 ShogiServer::League::Floodgate::History.class_eval do
297 def last_win?(player_id)
301 @swiss.match @players
302 assert_equal([@d, @c, @b, @a], @players)
306 ShogiServer::League::Floodgate::History.class_eval do
307 def last_win?(player_id)
315 @swiss.match @players
316 assert_equal([@a, @d, @c, @b], @players)
320 ShogiServer::League::Floodgate::History.class_eval do
321 def last_win?(player_id)
322 if player_id == "a" || player_id == "d"
329 @swiss.match @players
330 assert_equal([@d, @a, @c, @b], @players)
334 class TestFloodgateHistory < Test::Unit::TestCase
336 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
337 @history = ShogiServer::League::Floodgate::History.new @file
341 @file.delete if @file.exist?
345 file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
346 history = ShogiServer::League::Floodgate::History.new file
349 file.delete if file.exist?
354 def @history.make_record(game_result)
355 {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
356 :black => "hoge", :white => "foo",
357 :winner => "foo", :loser => "hoge"}
359 @history.update(dummy)
361 def @history.make_record(game_result)
362 {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
363 :black => "hoge", :white => "foo",
364 :winner => "hoge", :loser => "foo"}
366 @history.update(dummy)
368 def @history.make_record(game_result)
369 {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
370 :black => "hoge", :white => "foo",
371 :winner => nil, :loser => nil}
373 @history.update(dummy)
376 assert_equal 3, @history.records.size
377 assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
378 assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
379 assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
380 assert_equal "hoge", @history.records[1][:black]
381 assert_equal "foo", @history.records[1][:white]
382 assert_equal "hoge", @history.records[1][:winner]
383 assert_equal "foo", @history.records[1][:loser]
385 assert @history.last_win? "hoge"
386 assert !@history.last_win?("foo")
387 assert !@history.last_lose?("hoge")
388 assert @history.last_lose?("foo")