OSDN Git Service

Backported a5c94012656902e73e00f46e7a4c7004b24d4578: test/TC_logger.rb depeneded...
[shogi-server/shogi-server.git] / test / TC_game.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'test/mock_player'
4 require 'shogi_server/board'
5 require 'shogi_server/game'
6 require 'shogi_server/player'
7
8 def log_message(str)
9   $stderr.puts str
10 end
11
12 def log_warning(str)
13   $stderr.puts str
14 end
15
16 def log_error(str)
17   $stderr.puts str
18 end
19
20 $league = ShogiServer::League.new(File.dirname(__FILE__))
21 $league.event = "test"
22
23 class TestGame < Test::Unit::TestCase
24
25   def test_new
26     game_name = "hoge-1500-0"
27     board = ShogiServer::Board.new
28     board.initial
29     p1 = MockPlayer.new
30     p1.sente = true
31     p1.name  = "p1"
32     p2 = MockPlayer.new
33     p2.sente = false
34     p2.name  = "p2"
35     
36     game = ShogiServer::Game.new game_name, p1, p2, board 
37     assert_equal "", game.last_move
38
39     p1_out = <<EOF
40 BEGIN Game_Summary
41 Protocol_Version:1.1
42 Protocol_Mode:Server
43 Format:Shogi 1.0
44 Declaration:Jishogi 1.1
45 Game_ID:#{game.game_id}
46 Name+:p1
47 Name-:p2
48 Your_Turn:+
49 Rematch_On_Draw:NO
50 To_Move:+
51 BEGIN Time
52 Time_Unit:1sec
53 Total_Time:1500
54 Byoyomi:0
55 Least_Time_Per_Move:1
56 END Time
57 BEGIN Position
58 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
59 P2 * -HI *  *  *  *  * -KA * 
60 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
61 P4 *  *  *  *  *  *  *  *  * 
62 P5 *  *  *  *  *  *  *  *  * 
63 P6 *  *  *  *  *  *  *  *  * 
64 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
65 P8 * +KA *  *  *  *  * +HI * 
66 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
67 +
68 END Position
69 END Game_Summary
70 EOF
71     assert_equal(p1_out, p1.out.first)
72
73     p2_out = <<EOF
74 BEGIN Game_Summary
75 Protocol_Version:1.1
76 Protocol_Mode:Server
77 Format:Shogi 1.0
78 Declaration:Jishogi 1.1
79 Game_ID:#{game.game_id}
80 Name+:p1
81 Name-:p2
82 Your_Turn:-
83 Rematch_On_Draw:NO
84 To_Move:+
85 BEGIN Time
86 Time_Unit:1sec
87 Total_Time:1500
88 Byoyomi:0
89 Least_Time_Per_Move:1
90 END Time
91 BEGIN Position
92 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
93 P2 * -HI *  *  *  *  * -KA * 
94 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
95 P4 *  *  *  *  *  *  *  *  * 
96 P5 *  *  *  *  *  *  *  *  * 
97 P6 *  *  *  *  *  *  *  *  * 
98 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
99 P8 * +KA *  *  *  *  * +HI * 
100 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
101 +
102 END Position
103 END Game_Summary
104 EOF
105     assert_equal(p2_out, p2.out.first)
106
107     file = Pathname.new(game.logfile)
108     log = file.read
109     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
110 V2
111 N+p1
112 N-p2
113 $EVENT:#{game.game_id}
114 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
115 P2 * -HI *  *  *  *  * -KA * 
116 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
117 P4 *  *  *  *  *  *  *  *  * 
118 P5 *  *  *  *  *  *  *  *  * 
119 P6 *  *  *  *  *  *  *  *  * 
120 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
121 P8 * +KA *  *  *  *  * +HI * 
122 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
123 +
124 EOF
125   end
126
127   def test_new_buoy_1_move
128     game_name = "buoyhoge-1500-0"
129     board = ShogiServer::Board.new
130     board.set_from_moves ["+7776FU"]
131     p1 = MockPlayer.new
132     p1.sente = true
133     p1.name  = "p1"
134     p2 = MockPlayer.new
135     p2.sente = false
136     p2.name  = "p2"
137     
138     game = ShogiServer::Game.new game_name, p1, p2, board 
139     assert_equal "+7776FU,T1", game.last_move
140
141     p1_out = <<EOF
142 BEGIN Game_Summary
143 Protocol_Version:1.1
144 Protocol_Mode:Server
145 Format:Shogi 1.0
146 Declaration:Jishogi 1.1
147 Game_ID:#{game.game_id}
148 Name+:p1
149 Name-:p2
150 Your_Turn:+
151 Rematch_On_Draw:NO
152 To_Move:-
153 BEGIN Time
154 Time_Unit:1sec
155 Total_Time:1500
156 Byoyomi:0
157 Least_Time_Per_Move:1
158 END Time
159 BEGIN Position
160 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
161 P2 * -HI *  *  *  *  * -KA * 
162 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
163 P4 *  *  *  *  *  *  *  *  * 
164 P5 *  *  *  *  *  *  *  *  * 
165 P6 *  *  *  *  *  *  *  *  * 
166 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
167 P8 * +KA *  *  *  *  * +HI * 
168 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
169 +
170 +7776FU,T1
171 END Position
172 END Game_Summary
173 EOF
174     assert_equal(p1_out, p1.out.first)
175
176     p2_out = <<EOF
177 BEGIN Game_Summary
178 Protocol_Version:1.1
179 Protocol_Mode:Server
180 Format:Shogi 1.0
181 Declaration:Jishogi 1.1
182 Game_ID:#{game.game_id}
183 Name+:p1
184 Name-:p2
185 Your_Turn:-
186 Rematch_On_Draw:NO
187 To_Move:-
188 BEGIN Time
189 Time_Unit:1sec
190 Total_Time:1500
191 Byoyomi:0
192 Least_Time_Per_Move:1
193 END Time
194 BEGIN Position
195 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
196 P2 * -HI *  *  *  *  * -KA * 
197 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
198 P4 *  *  *  *  *  *  *  *  * 
199 P5 *  *  *  *  *  *  *  *  * 
200 P6 *  *  *  *  *  *  *  *  * 
201 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
202 P8 * +KA *  *  *  *  * +HI * 
203 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
204 +
205 +7776FU,T1
206 END Position
207 END Game_Summary
208 EOF
209     assert_equal(p2_out, p2.out.first)
210
211     file = Pathname.new(game.logfile)
212     log = file.read
213     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
214 V2
215 N+p1
216 N-p2
217 $EVENT:#{game.game_id}
218 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
219 P2 * -HI *  *  *  *  * -KA * 
220 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
221 P4 *  *  *  *  *  *  *  *  * 
222 P5 *  *  *  *  *  *  *  *  * 
223 P6 *  *  *  *  *  *  *  *  * 
224 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
225 P8 * +KA *  *  *  *  * +HI * 
226 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
227 +
228 'buoy game starting with 1 moves
229 +7776FU
230 T1
231 EOF
232   end
233
234   def test_new_buoy_2_moves
235     game_name = "buoyhoge-1500-0"
236     board = ShogiServer::Board.new
237     board.set_from_moves ["+7776FU", "-3334FU"]
238     p1 = MockPlayer.new
239     p1.sente = true
240     p1.name  = "p1"
241     p2 = MockPlayer.new
242     p2.sente = false
243     p2.name  = "p2"
244     
245     game = ShogiServer::Game.new game_name, p1, p2, board 
246     assert_equal "-3334FU,T1", game.last_move
247
248     p1_out = <<EOF
249 BEGIN Game_Summary
250 Protocol_Version:1.1
251 Protocol_Mode:Server
252 Format:Shogi 1.0
253 Declaration:Jishogi 1.1
254 Game_ID:#{game.game_id}
255 Name+:p1
256 Name-:p2
257 Your_Turn:+
258 Rematch_On_Draw:NO
259 To_Move:+
260 BEGIN Time
261 Time_Unit:1sec
262 Total_Time:1500
263 Byoyomi:0
264 Least_Time_Per_Move:1
265 END Time
266 BEGIN Position
267 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
268 P2 * -HI *  *  *  *  * -KA * 
269 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
270 P4 *  *  *  *  *  *  *  *  * 
271 P5 *  *  *  *  *  *  *  *  * 
272 P6 *  *  *  *  *  *  *  *  * 
273 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
274 P8 * +KA *  *  *  *  * +HI * 
275 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
276 +
277 +7776FU,T1
278 -3334FU,T1
279 END Position
280 END Game_Summary
281 EOF
282     assert_equal(p1_out, p1.out.first)
283
284     p2_out = <<EOF
285 BEGIN Game_Summary
286 Protocol_Version:1.1
287 Protocol_Mode:Server
288 Format:Shogi 1.0
289 Declaration:Jishogi 1.1
290 Game_ID:#{game.game_id}
291 Name+:p1
292 Name-:p2
293 Your_Turn:-
294 Rematch_On_Draw:NO
295 To_Move:+
296 BEGIN Time
297 Time_Unit:1sec
298 Total_Time:1500
299 Byoyomi:0
300 Least_Time_Per_Move:1
301 END Time
302 BEGIN Position
303 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
304 P2 * -HI *  *  *  *  * -KA * 
305 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
306 P4 *  *  *  *  *  *  *  *  * 
307 P5 *  *  *  *  *  *  *  *  * 
308 P6 *  *  *  *  *  *  *  *  * 
309 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
310 P8 * +KA *  *  *  *  * +HI * 
311 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
312 +
313 +7776FU,T1
314 -3334FU,T1
315 END Position
316 END Game_Summary
317 EOF
318     assert_equal(p2_out, p2.out.first)
319
320     file = Pathname.new(game.logfile)
321     log = file.read
322     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
323 V2
324 N+p1
325 N-p2
326 $EVENT:#{game.game_id}
327 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
328 P2 * -HI *  *  *  *  * -KA * 
329 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
330 P4 *  *  *  *  *  *  *  *  * 
331 P5 *  *  *  *  *  *  *  *  * 
332 P6 *  *  *  *  *  *  *  *  * 
333 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
334 P8 * +KA *  *  *  *  * +HI * 
335 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
336 +
337 'buoy game starting with 2 moves
338 +7776FU
339 T1
340 -3334FU
341 T1
342 EOF
343   end
344   
345   def test_monitor_add
346     game_name = "hoge-1500-0"
347     board = ShogiServer::Board.new
348     board.initial
349     p1 = MockPlayer.new
350     p1.sente = true
351     p1.name  = "p1"
352     p2 = MockPlayer.new
353     p2.sente = false
354     p2.name  = "p2"
355     
356     game = ShogiServer::Game.new game_name, p1, p2, board 
357     handler1 = ShogiServer::MonitorHandler1.new p1
358     handler2 = ShogiServer::MonitorHandler2.new p2
359
360     assert_equal(0, game.monitors.size)
361     game.monitoron(handler1)
362     assert_equal(1, game.monitors.size)
363     game.monitoron(handler2)
364     assert_equal(2, game.monitors.size)
365     game.monitoroff(handler1)
366     assert_equal(1, game.monitors.size)
367     assert_equal(handler2, game.monitors.last)
368     game.monitoroff(handler2)
369     assert_equal(0, game.monitors.size)
370   end
371
372   def test_decide_turns
373     p1 = MockPlayer.new
374     p1.name = "p1"
375     p2 = MockPlayer.new
376     p2.name = "p2"
377
378     p1.sente=nil; p2.sente=false
379     ShogiServer::Game::decide_turns(p1, "+", p2)
380     assert_equal true, p1.sente
381
382     p1.sente=nil; p2.sente=nil
383     ShogiServer::Game::decide_turns(p1, "+", p2)
384     assert_equal true, p1.sente
385
386     p1.sente=nil; p2.sente=true
387     ShogiServer::Game::decide_turns(p1, "-", p2)
388     assert_equal false, p1.sente
389
390     p1.sente=nil; p2.sente=nil
391     ShogiServer::Game::decide_turns(p1, "-", p2)
392     assert_equal false, p1.sente
393
394     p1.sente=nil; p2.sente=false
395     ShogiServer::Game::decide_turns(p1, "*", p2)
396     assert_equal true, p1.sente
397
398     p1.sente=nil; p2.sente=true
399     ShogiServer::Game::decide_turns(p1, "*", p2)
400     assert_equal false, p1.sente
401
402     p1.sente=nil; p2.sente=nil
403     ShogiServer::Game::decide_turns(p1, "*", p2)
404     assert (p1.sente == true  && p2.sente == false) ||
405            (p1.sente == false && p2.sente == true)
406   end
407 end
408