1 $:.unshift File.join(File.dirname(__FILE__), "..")
4 require 'shogi_server/league.rb'
5 require 'shogi_server/player'
6 require 'shogi_server/pairing'
7 require 'test/mock_log_message'
10 unless a.size == 2 && b.size == 2
14 return true if [a.first, a.last] == b || [a.last, a.first] == b
17 class TestPairing < Test::Unit::TestCase
19 @pairing= ShogiServer::Pairing.new
21 def @pairing.start_game(p1,p2)
24 @a = ShogiServer::BasicPlayer.new
29 @a.last_game_win = false
30 @b = ShogiServer::BasicPlayer.new
35 @b.last_game_win = true
36 @c = ShogiServer::BasicPlayer.new
41 @c.last_game_win = true
42 @d = ShogiServer::BasicPlayer.new
47 @d.last_game_win = true
50 def test_include_newbie
51 assert(@pairing.include_newbie?([@a]))
52 assert(!@pairing.include_newbie?([@b]))
53 assert(@pairing.include_newbie?([@b,@a]))
54 assert(!@pairing.include_newbie?([@b,@c]))
58 class TestStartGame < Test::Unit::TestCase
60 @pairing= ShogiServer::StartGame.new
62 def @pairing.start_game(p1,p2)
65 @a = ShogiServer::BasicPlayer.new
70 @b = ShogiServer::BasicPlayer.new
75 @c = ShogiServer::BasicPlayer.new
80 @d = ShogiServer::BasicPlayer.new
87 def test_match_two_players
89 @pairing.match(players)
90 assert_equal(1, $called)
93 def test_match_one_player
95 @pairing.match(players)
96 assert_equal(0, $called)
99 def test_match_zero_player
101 @pairing.match(players)
102 assert_equal(0, $called)
105 def test_match_three_players
107 @pairing.match(players)
108 assert_equal(1, $called)
111 def test_match_four_players
112 players = [@a,@b,@c,@d]
113 @pairing.match(players)
114 assert_equal(2, $called)
118 class TestStartGameWithoutHumans < Test::Unit::TestCase
120 @pairing= ShogiServer::StartGameWithoutHumans.new
123 def @pairing.start_game(p1,p2)
127 @a = ShogiServer::BasicPlayer.new
132 @b = ShogiServer::BasicPlayer.new
137 @c = ShogiServer::BasicPlayer.new
142 @d = ShogiServer::BasicPlayer.new
147 @e = ShogiServer::BasicPlayer.new
152 @f = ShogiServer::BasicPlayer.new
157 @g = ShogiServer::BasicPlayer.new
162 @h = ShogiServer::BasicPlayer.new
169 def test_match_one_player
171 @pairing.match(players)
172 assert_equal(0, $called)
175 def test_match_one_player_human
178 @pairing.match(players)
179 assert_equal(0, $called)
182 def test_match_two_players
184 @pairing.match(players)
185 assert_equal(1, $called)
188 def test_match_two_players_humans
192 @pairing.match(players)
193 assert_equal(1, $called)
196 def test_match_zero_player
198 @pairing.match(players)
199 assert_equal(0, $called)
202 def test_match_three_players
204 @pairing.match(players)
205 assert_equal(1, $called)
208 def test_match_three_players_a_human
211 @pairing.match(players)
212 assert_equal(1, $called)
213 assert_equal(1, players.size)
214 assert_equal(@c, players[0])
217 def test_match_three_players_b_human
220 @pairing.match(players)
221 assert_equal(1, $called)
222 assert_equal(1, players.size)
223 assert_equal(@c, players[0])
226 def test_match_three_players_c_human
229 @pairing.match(players)
230 assert_equal(1, $called)
231 assert_equal(1, players.size)
232 assert_equal(@c, players[0])
235 def test_match_three_players_ab_human
239 @pairing.match(players)
240 assert_equal(1, $called)
241 assert_equal(1, players.size)
242 assert_equal(@b, players[0])
245 def test_match_three_players_bc_human
249 @pairing.match(players)
250 assert_equal(1, $called)
251 assert_equal(1, players.size)
252 assert_equal(@c, players[0])
255 def test_match_four_players
256 players = [@a,@b,@c,@d]
257 @pairing.match(players)
258 assert_equal(2, $called)
261 def test_match_four_players_ab_human
264 players = [@a,@b,@c,@d]
265 @pairing.match(players)
266 assert_equal(2, $paired.size)
267 assert(same_pair?([@a,@c], $paired[0]))
268 assert(same_pair?([@b,@d], $paired[1]))
271 def test_match_four_players_bc_human
274 players = [@a,@b,@c,@d]
275 @pairing.match(players)
276 assert_equal(2, $paired.size)
277 assert(same_pair?([@a,@b], $paired[0]))
278 assert(same_pair?([@c,@d], $paired[1]))
281 def test_match_four_players_abc_human
285 players = [@a,@b,@c,@d]
286 @pairing.match(players)
287 assert_equal(2, $paired.size)
288 assert(same_pair?([@a,@d], $paired[0]))
289 assert(same_pair?([@b,@c], $paired[1]))
292 def test_match_four_players_bcd_human
296 players = [@a,@b,@c,@d]
297 @pairing.match(players)
298 assert_equal(2, $paired.size)
299 assert(same_pair?([@a,@c], $paired[0]))
300 assert(same_pair?([@b,@d], $paired[1]))
303 def test_match_four_players_abcd_human
308 players = [@a,@b,@c,@d]
309 @pairing.match(players)
310 assert_equal(2, $paired.size)
311 assert(same_pair?([@a,@b], $paired[0]))
312 assert(same_pair?([@c,@d], $paired[1]))
315 def test_match_eight_players_efgh_human
320 players = [@a,@b,@c,@d,@e,@f,@g,@h]
321 @pairing.match(players)
322 assert_equal(4, $paired.size)
323 assert(same_pair?([@e,@c], $paired[0]))
324 assert(same_pair?([@d,@g], $paired[1]))
325 assert(same_pair?([@a,@f], $paired[2]))
326 assert(same_pair?([@b,@h], $paired[3]))
330 class TestLeastDiff < Test::Unit::TestCase
349 $league = MockLeague.new
351 @pairing= ShogiServer::LeastDiff.new
354 def @pairing.start_game(p1,p2)
359 @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
360 @history = ShogiServer::League::Floodgate::History.new @file
362 @a = ShogiServer::BasicPlayer.new
368 @b = ShogiServer::BasicPlayer.new
374 @c = ShogiServer::BasicPlayer.new
380 @d = ShogiServer::BasicPlayer.new
386 @e = ShogiServer::BasicPlayer.new
392 @f = ShogiServer::BasicPlayer.new
398 @g = ShogiServer::BasicPlayer.new
404 @h = ShogiServer::BasicPlayer.new
410 @x = ShogiServer::BasicPlayer.new
426 @file.delete if @file.exist?
429 def assert_pairs(x_array, y_array)
430 if (x_array.size != y_array.size)
431 assert_equal(x_array.size, y_array.size)
436 if (x_array.size == 1)
437 assert_equal(x_array[0].name, y_array[0].name)
442 while i < x_array.size
443 if i == x_array.size-1
444 assert_equal(x_array[i].name, y_array[i].name)
452 if ! ((px1.name == py1.name && px2.name == py2.name) ||
453 (px1.name == py2.name && px2.name == py1.name))
462 def test_match_one_player
464 assert_equal(0, @pairing.calculate_diff_with_penalty(players,nil))
465 r = @pairing.match(players)
466 assert_pairs([@a], r)
469 def test_match_two_players
471 assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty([@a,@b],nil))
472 assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty([@b,@a],nil))
473 r = @pairing.match(players)
474 assert_pairs([@a,@b], r)
477 def test_match_three_players
479 assert_equal(300, @pairing.calculate_diff_with_penalty([@a,@b,@h],nil))
480 assert_equal(2200, @pairing.calculate_diff_with_penalty([@b,@h,@a],nil))
481 r = @pairing.match(players)
482 assert_pairs([@a,@b,@h], r)
485 def test_calculate_diff_with_penalty
487 assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty(players,nil))
490 def @history.make_record(game_result)
491 {:game_id => "wdoor+floodgate-900-0-a-b-1",
492 :black => "b", :white => "a",
493 :winner => "a", :loser => "b"}
495 @history.update(dummy)
496 assert_equal(@b.rate-@a.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
499 def test_calculate_diff_with_penalty2
500 players = [@a,@b,@g,@h]
501 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
504 def test_calculate_diff_with_penalty2_1
505 players = [@a,@b,@g,@h]
506 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
508 def @history.make_record(game_result)
509 {:game_id => "wdoor+floodgate-900-0-a-b-1",
510 :black => "b", :white => "a",
511 :winner => "a", :loser => "b"}
513 @history.update(dummy)
514 assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players, @history))
517 def test_calculate_diff_with_penalty2_2
518 players = [@a,@b,@g,@h]
519 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
521 def @history.make_record(game_result)
522 {:game_id => "wdoor+floodgate-900-0-a-b-1",
523 :black => "g", :white => "h",
524 :winner => "h", :loser => "g"}
526 @history.update(dummy)
527 assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players, @history))
528 #assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, [@b,@a,@h,@g]))
531 def test_calculate_diff_with_penalty2_3
532 players = [@a,@b,@g,@h]
533 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
535 def @history.make_record(game_result)
536 {:game_id => "wdoor+floodgate-900-0-a-b-1",
537 :black => "g", :white => "h",
538 :winner => "h", :loser => "g"}
540 @history.update(dummy)
541 def @history.make_record(game_result)
542 {:game_id => "wdoor+floodgate-900-0-a-b-1",
543 :black => "b", :white => "a",
544 :winner => "a", :loser => "b"}
546 @history.update(dummy)
547 assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
550 def test_get_player_rate_0
551 assert_equal(2150, @pairing.get_player_rate(@x, @history))
554 def @history.make_record(game_result)
555 {:game_id => "wdoor+floodgate-900-0-x-a-1",
556 :black => "x", :white => "a",
557 :winner => "x", :loser => "a"}
559 @history.update(dummy)
560 assert_equal(@a.rate+100, @pairing.get_player_rate(@x, @history))
562 def @history.make_record(game_result)
563 {:game_id => "wdoor+floodgate-900-0-x-b-1",
564 :black => "x", :white => "b",
565 :winner => "b", :loser => "x"}
567 @history.update(dummy)
569 assert_equal((@a.rate+100+@b.rate-100)/2, @pairing.get_player_rate(@x, @history))