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
414 @abcdefg1 = ShogiServer::BasicPlayer.new
415 @abcdefg1.player_id = "abcdefg1"
416 @abcdefg1.name = "abcdefg1"
417 @abcdefg1.rate = 2100
418 @abcdefg2 = ShogiServer::BasicPlayer.new
419 @abcdefg2.player_id = "abcdefg2"
420 @abcdefg2.name = "abcdefg2"
421 @abcdefg2.rate = 2200
422 @abcdxyz = ShogiServer::BasicPlayer.new
423 @abcdxyz.player_id = "abcdxyz"
424 @abcdxyz.name = "abcdxyz"
436 $league.add(@abcdefg1)
437 $league.add(@abcdefg2)
438 $league.add(@abcdxyz)
442 @file.delete if @file.exist?
445 def assert_pairs(x_array, y_array)
446 if (x_array.size != y_array.size)
447 assert_equal(x_array.size, y_array.size)
452 if (x_array.size == 1)
453 assert_equal(x_array[0].name, y_array[0].name)
458 while i < x_array.size
459 if i == x_array.size-1
460 assert_equal(x_array[i].name, y_array[i].name)
468 if ! ((px1.name == py1.name && px2.name == py2.name) ||
469 (px1.name == py2.name && px2.name == py1.name))
478 def test_match_one_player
480 assert_equal(0, @pairing.calculate_diff_with_penalty(players,nil))
481 r = @pairing.match(players)
482 assert_pairs([@a], r)
485 def test_match_two_players
487 assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty([@a,@b],nil))
488 assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty([@b,@a],nil))
489 r = @pairing.match(players)
490 assert_pairs([@a,@b], r)
493 def test_match_three_players
495 assert_equal(300, @pairing.calculate_diff_with_penalty([@a,@b,@h],nil))
496 assert_equal(2200, @pairing.calculate_diff_with_penalty([@b,@h,@a],nil))
497 r = @pairing.match(players)
498 assert_pairs([@a,@b,@h], r)
499 assert_pairs([@a,@b,@h], players)
502 def test_calculate_diff_with_penalty
504 assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty(players,nil))
507 def @history.make_record(game_result)
508 {:game_id => "wdoor+floodgate-900-0-a-b-1",
509 :black => "b", :white => "a",
510 :winner => "a", :loser => "b"}
512 @history.update(dummy)
513 assert_equal(@b.rate-@a.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
516 def test_calculate_diff_with_penalty2
517 players = [@a,@b,@g,@h]
518 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
521 def test_calculate_diff_with_penalty2_1
522 players = [@a,@b,@g,@h]
523 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
525 def @history.make_record(game_result)
526 {:game_id => "wdoor+floodgate-900-0-a-b-1",
527 :black => "b", :white => "a",
528 :winner => "a", :loser => "b"}
530 @history.update(dummy)
531 assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players, @history))
534 def test_calculate_diff_with_penalty2_2
535 players = [@a,@b,@g,@h]
536 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
538 def @history.make_record(game_result)
539 {:game_id => "wdoor+floodgate-900-0-a-b-1",
540 :black => "g", :white => "h",
541 :winner => "h", :loser => "g"}
543 @history.update(dummy)
544 assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players, @history))
545 #assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, [@b,@a,@h,@g]))
548 def test_calculate_diff_with_penalty2_3
549 players = [@a,@b,@g,@h]
550 assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
552 def @history.make_record(game_result)
553 {:game_id => "wdoor+floodgate-900-0-a-b-1",
554 :black => "g", :white => "h",
555 :winner => "h", :loser => "g"}
557 @history.update(dummy)
558 def @history.make_record(game_result)
559 {:game_id => "wdoor+floodgate-900-0-a-b-1",
560 :black => "b", :white => "a",
561 :winner => "a", :loser => "b"}
563 @history.update(dummy)
564 assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
567 def test_calculate_diff_with_kin_4_players
568 players = [@abcdefg1, @abcdxyz]
569 assert_equal(@abcdxyz.rate - @abcdefg1.rate + 400, @pairing.calculate_diff_with_penalty(players,nil))
572 def test_calculate_diff_with_kin_7_players
573 players = [@abcdefg1, @abcdefg2]
574 assert_equal(@abcdefg2.rate - @abcdefg1.rate + 800, @pairing.calculate_diff_with_penalty(players,nil))
577 def test_get_player_rate_0
578 assert_equal(2150, @pairing.get_player_rate(@x, @history))
580 @x.estimated_rate = 0
582 def @history.make_record(game_result)
583 {:game_id => "wdoor+floodgate-900-0-x-a-1",
584 :black => "x", :white => "a",
585 :winner => "x", :loser => "a"}
587 @history.update(dummy)
588 assert_equal(@a.rate+200, @pairing.get_player_rate(@x, @history))
590 @x.estimated_rate = 0
591 def @history.make_record(game_result)
592 {:game_id => "wdoor+floodgate-900-0-x-b-1",
593 :black => "x", :white => "b",
594 :winner => "b", :loser => "x"}
596 @history.update(dummy)
598 assert_equal(@b.rate-200, @pairing.get_player_rate(@x, @history))
602 class TestExcludeUnratedPlayers < Test::Unit::TestCase
604 @pairing= ShogiServer::ExcludeUnratedPlayers.new
605 @a = ShogiServer::BasicPlayer.new
610 @b = ShogiServer::BasicPlayer.new
615 @c = ShogiServer::BasicPlayer.new
620 @d = ShogiServer::BasicPlayer.new
627 def test_match_without_any_players
629 @pairing.match(players)
630 assert_equal([], players)
633 def test_match_without_unrated_player_1
634 players = [@b, @c, @d]
635 @pairing.match(players)
636 assert_equal([@b, @c, @d], players)
639 def test_match_without_unrated_player_2
641 @pairing.match(players)
642 assert_equal([@b], players)
645 def test_match_with_unrated_player_1
646 players = [@a, @b, @c, @d]
647 @pairing.match(players)
648 assert_equal([@b, @c, @d], players)
651 def test_match_with_unrated_player_2
653 @pairing.match(players)
654 assert_equal([], players)