1 $:.unshift File.join(File.dirname(__FILE__), "..")
4 require 'shogi_server/player'
5 require 'shogi_server/pairing'
6 require 'shogi_server/league/floodgate'
20 $logger = MockLogger.new
29 class TestFloodgate < Test::Unit::TestCase
31 @fg = ShogiServer::League::Floodgate.new(nil)
39 assert(ShogiServer::League::Floodgate.game_name?("floodgate-900-0"))
40 assert(ShogiServer::League::Floodgate.game_name?("floodgate-0-10"))
41 assert(!ShogiServer::League::Floodgate.game_name?("floodgat-900-0"))
46 class TestPairing < Test::Unit::TestCase
48 @pairing= ShogiServer::Pairing.new
50 def @pairing.start_game(p1,p2)
53 @a = ShogiServer::BasicPlayer.new
58 @a.last_game_win = false
59 @b = ShogiServer::BasicPlayer.new
64 @b.last_game_win = true
65 @c = ShogiServer::BasicPlayer.new
70 @c.last_game_win = true
71 @d = ShogiServer::BasicPlayer.new
76 @d.last_game_win = true
79 def test_include_newbie
80 assert(@pairing.include_newbie?([@a]))
81 assert(!@pairing.include_newbie?([@b]))
82 assert(@pairing.include_newbie?([@b,@a]))
83 assert(!@pairing.include_newbie?([@b,@c]))
87 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
89 @pairing= ShogiServer::DeleteMostPlayingPlayer.new
90 @a = ShogiServer::BasicPlayer.new
94 @b = ShogiServer::BasicPlayer.new
98 @c = ShogiServer::BasicPlayer.new
105 players = [@a, @b, @c]
106 @pairing.match(players)
107 assert_equal([@a,@b], players)
111 class TestMakeEven < Test::Unit::TestCase
114 @pairing= ShogiServer::MakeEven.new
115 @a = ShogiServer::BasicPlayer.new
120 @b = ShogiServer::BasicPlayer.new
125 @c = ShogiServer::BasicPlayer.new
134 @pairing.match(players)
135 assert_equal([@a,@b], players)
139 players = [@a, @b, @c]
140 @pairing.match(players)
141 assert_equal([@a, @b], players)
145 class TestLeastRatePlayer < Test::Unit::TestCase
147 @pairing= ShogiServer::DeleteLeastRatePlayer.new
148 @a = ShogiServer::BasicPlayer.new
152 @b = ShogiServer::BasicPlayer.new
156 @c = ShogiServer::BasicPlayer.new
163 players = [@a, @b, @c]
164 @pairing.match(players)
165 assert_equal([@b,@c], players)
169 class TestRandomize < Test::Unit::TestCase
171 srand(10) # makes the random number generator determistic
172 @pairing = ShogiServer::Randomize.new
173 @a = ShogiServer::BasicPlayer.new
177 @b = ShogiServer::BasicPlayer.new
181 @c = ShogiServer::BasicPlayer.new
188 players = [@a, @b, @c]
189 @pairing.match(players)
190 assert_equal([@b,@a,@c], players)
194 class TestSortByRate < Test::Unit::TestCase
196 @pairing = ShogiServer::SortByRate.new
197 @a = ShogiServer::BasicPlayer.new
202 @b = ShogiServer::BasicPlayer.new
207 @c = ShogiServer::BasicPlayer.new
215 players = [@a, @b, @c]
216 @pairing.match(players)
217 assert_equal([@c,@a,@b], players)
221 class TestSortByRateWithRandomness < Test::Unit::TestCase
223 srand(10) # makes the random number generator determistic
224 @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
225 @a = ShogiServer::BasicPlayer.new
230 @b = ShogiServer::BasicPlayer.new
235 @c = ShogiServer::BasicPlayer.new
243 players = [@a, @b, @c]
244 @pairing.match(players)
245 assert_equal([@c,@b,@a], players)
249 class TestExcludeSacrifice < Test::Unit::TestCase
251 @obj = ShogiServer::ExcludeSacrificeGps500.new
252 @a = ShogiServer::BasicPlayer.new
258 @a.last_game_win = false
259 @b = ShogiServer::BasicPlayer.new
260 @b.player_id = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
265 @b.last_game_win = true
266 @c = ShogiServer::BasicPlayer.new
272 @c.last_game_win = true
278 assert_equal([@a], players)
284 assert_equal([], players)
290 assert_equal([@a,@b], players)
294 players = [@a, @b, @c]
296 assert_equal([@a, @c], players)
302 assert_equal([@a,@c], players)
306 class TestSwissPairing < Test::Unit::TestCase
309 @a = ShogiServer::BasicPlayer.new
312 @b = ShogiServer::BasicPlayer.new
315 @c = ShogiServer::BasicPlayer.new
318 @d = ShogiServer::BasicPlayer.new
322 @players = [@a, @b, @c, @d]
324 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
325 @history = ShogiServer::League::Floodgate::History.new @file
327 @swiss = ShogiServer::Swiss.new @history
331 @file.delete if @file.exist?
335 def @history.last_win?(player_id)
338 @swiss.match @players
339 assert_equal([@d, @c, @b, @a], @players)
343 def @history.last_win?(player_id)
346 @swiss.match @players
347 assert_equal([@d, @c, @b, @a], @players)
351 def @history.last_win?(player_id)
358 @swiss.match @players
359 assert_equal([@a, @d, @c, @b], @players)
363 def @history.last_win?(player_id)
364 if player_id == "a" || player_id == "d"
370 @swiss.match @players
371 assert_equal([@d, @a, @c, @b], @players)
375 class TestFloodgateHistory < Test::Unit::TestCase
377 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
378 @history = ShogiServer::League::Floodgate::History.new @file
382 @file.delete if @file.exist?
386 file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
387 history = ShogiServer::League::Floodgate::History.new file
390 file.delete if file.exist?
395 def @history.make_record(game_result)
396 {:game_id => "wdoor+floodgate-900-0-hoge-foo-1",
397 :black => "hoge", :white => "foo",
398 :winner => "foo", :loser => "hoge"}
400 @history.update(dummy)
402 def @history.make_record(game_result)
403 {:game_id => "wdoor+floodgate-900-0-hoge-foo-2",
404 :black => "hoge", :white => "foo",
405 :winner => "hoge", :loser => "foo"}
407 @history.update(dummy)
409 def @history.make_record(game_result)
410 {:game_id => "wdoor+floodgate-900-0-hoge-foo-3",
411 :black => "hoge", :white => "foo",
412 :winner => nil, :loser => nil}
414 @history.update(dummy)
417 assert_equal 3, @history.records.size
418 assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
419 assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
420 assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
421 assert_equal "hoge", @history.records[1][:black]
422 assert_equal "foo", @history.records[1][:white]
423 assert_equal "hoge", @history.records[1][:winner]
424 assert_equal "foo", @history.records[1][:loser]
426 assert @history.last_win? "hoge"
427 assert !@history.last_win?("foo")
428 assert !@history.last_lose?("hoge")
429 assert @history.last_lose?("foo")