1 $:.unshift File.join(File.dirname(__FILE__), "..")
4 require 'shogi_server/player'
5 require 'shogi_server/pairing'
6 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]))