OSDN Git Service

Merge branch 'logger'
[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 *  * +FU *  *  *  *  *  * 
166 P7+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 END Position
171 END Game_Summary
172 EOF
173     assert_equal(p1_out, p1.out.first)
174
175     p2_out = <<EOF
176 BEGIN Game_Summary
177 Protocol_Version:1.1
178 Protocol_Mode:Server
179 Format:Shogi 1.0
180 Declaration:Jishogi 1.1
181 Game_ID:#{game.game_id}
182 Name+:p1
183 Name-:p2
184 Your_Turn:-
185 Rematch_On_Draw:NO
186 To_Move:-
187 BEGIN Time
188 Time_Unit:1sec
189 Total_Time:1500
190 Byoyomi:0
191 Least_Time_Per_Move:1
192 END Time
193 BEGIN Position
194 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
195 P2 * -HI *  *  *  *  * -KA * 
196 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
197 P4 *  *  *  *  *  *  *  *  * 
198 P5 *  *  *  *  *  *  *  *  * 
199 P6 *  * +FU *  *  *  *  *  * 
200 P7+FU+FU * +FU+FU+FU+FU+FU+FU
201 P8 * +KA *  *  *  *  * +HI * 
202 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
203 -
204 END Position
205 END Game_Summary
206 EOF
207     assert_equal(p2_out, p2.out.first)
208
209     file = Pathname.new(game.logfile)
210     log = file.read
211     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
212 V2
213 N+p1
214 N-p2
215 $EVENT:#{game.game_id}
216 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
217 P2 * -HI *  *  *  *  * -KA * 
218 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
219 P4 *  *  *  *  *  *  *  *  * 
220 P5 *  *  *  *  *  *  *  *  * 
221 P6 *  *  *  *  *  *  *  *  * 
222 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
223 P8 * +KA *  *  *  *  * +HI * 
224 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
225 +
226 'buoy game starting with 1 moves
227 +7776FU
228 T1
229 EOF
230   end
231
232   def test_new_buoy_2_moves
233     game_name = "buoyhoge-1500-0"
234     board = ShogiServer::Board.new
235     board.set_from_moves ["+7776FU", "-3334FU"]
236     p1 = MockPlayer.new
237     p1.sente = true
238     p1.name  = "p1"
239     p2 = MockPlayer.new
240     p2.sente = false
241     p2.name  = "p2"
242     
243     game = ShogiServer::Game.new game_name, p1, p2, board 
244     assert_equal "-3334FU,T1", game.last_move
245
246     p1_out = <<EOF
247 BEGIN Game_Summary
248 Protocol_Version:1.1
249 Protocol_Mode:Server
250 Format:Shogi 1.0
251 Declaration:Jishogi 1.1
252 Game_ID:#{game.game_id}
253 Name+:p1
254 Name-:p2
255 Your_Turn:+
256 Rematch_On_Draw:NO
257 To_Move:+
258 BEGIN Time
259 Time_Unit:1sec
260 Total_Time:1500
261 Byoyomi:0
262 Least_Time_Per_Move:1
263 END Time
264 BEGIN Position
265 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
266 P2 * -HI *  *  *  *  * -KA * 
267 P3-FU-FU-FU-FU-FU-FU * -FU-FU
268 P4 *  *  *  *  *  * -FU *  * 
269 P5 *  *  *  *  *  *  *  *  * 
270 P6 *  * +FU *  *  *  *  *  * 
271 P7+FU+FU * +FU+FU+FU+FU+FU+FU
272 P8 * +KA *  *  *  *  * +HI * 
273 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
274 +
275 END Position
276 END Game_Summary
277 EOF
278     assert_equal(p1_out, p1.out.first)
279
280     p2_out = <<EOF
281 BEGIN Game_Summary
282 Protocol_Version:1.1
283 Protocol_Mode:Server
284 Format:Shogi 1.0
285 Declaration:Jishogi 1.1
286 Game_ID:#{game.game_id}
287 Name+:p1
288 Name-:p2
289 Your_Turn:-
290 Rematch_On_Draw:NO
291 To_Move:+
292 BEGIN Time
293 Time_Unit:1sec
294 Total_Time:1500
295 Byoyomi:0
296 Least_Time_Per_Move:1
297 END Time
298 BEGIN Position
299 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
300 P2 * -HI *  *  *  *  * -KA * 
301 P3-FU-FU-FU-FU-FU-FU * -FU-FU
302 P4 *  *  *  *  *  * -FU *  * 
303 P5 *  *  *  *  *  *  *  *  * 
304 P6 *  * +FU *  *  *  *  *  * 
305 P7+FU+FU * +FU+FU+FU+FU+FU+FU
306 P8 * +KA *  *  *  *  * +HI * 
307 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
308 +
309 END Position
310 END Game_Summary
311 EOF
312     assert_equal(p2_out, p2.out.first)
313
314     file = Pathname.new(game.logfile)
315     log = file.read
316     assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
317 V2
318 N+p1
319 N-p2
320 $EVENT:#{game.game_id}
321 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
322 P2 * -HI *  *  *  *  * -KA * 
323 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
324 P4 *  *  *  *  *  *  *  *  * 
325 P5 *  *  *  *  *  *  *  *  * 
326 P6 *  *  *  *  *  *  *  *  * 
327 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
328 P8 * +KA *  *  *  *  * +HI * 
329 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
330 +
331 'buoy game starting with 2 moves
332 +7776FU
333 T1
334 -3334FU
335 T1
336 EOF
337   end
338   
339   def test_monitor_add
340     game_name = "hoge-1500-0"
341     board = ShogiServer::Board.new
342     board.initial
343     p1 = MockPlayer.new
344     p1.sente = true
345     p1.name  = "p1"
346     p2 = MockPlayer.new
347     p2.sente = false
348     p2.name  = "p2"
349     
350     game = ShogiServer::Game.new game_name, p1, p2, board 
351     handler1 = ShogiServer::MonitorHandler1.new p1
352     handler2 = ShogiServer::MonitorHandler2.new p2
353
354     assert_equal(0, game.monitors.size)
355     game.monitoron(handler1)
356     assert_equal(1, game.monitors.size)
357     game.monitoron(handler2)
358     assert_equal(2, game.monitors.size)
359     game.monitoroff(handler1)
360     assert_equal(1, game.monitors.size)
361     assert_equal(handler2, game.monitors.last)
362     game.monitoroff(handler2)
363     assert_equal(0, game.monitors.size)
364   end
365 end
366