OSDN Git Service

* [shogi-server] Support a graceful shutdown. (Closes #38544)
[shogi-server/shogi-server.git] / test / TC_game.rb
index 09ab2a7..6fba0f0 100644 (file)
@@ -5,6 +5,10 @@ require 'shogi_server/board'
 require 'shogi_server/game'
 require 'shogi_server/player'
 
+$options = {}
+$options["least-time-per-move"] = 1
+$options["max-moves"] = 0
+
 def log_message(str)
   $stderr.puts str
 end
@@ -22,6 +26,14 @@ $league.event = "test"
 
 class TestGame < Test::Unit::TestCase
 
+  def test_parse_time
+    assert_equal ShogiServer::Game::TimeControlParams.new(1500,0,0,false,false),
+                 ShogiServer::Game.parse_time("hoge-1500-0")
+    assert_equal ShogiServer::Game::TimeControlParams.new(600, 0, 10, false, false),
+                 ShogiServer::Game.parse_time("hoge-600-10F")
+    assert_equal true, ShogiServer::Game.parse_time("hoge-600-10f").error
+  end
+
   def test_new
     game_name = "hoge-1500-0"
     board = ShogiServer::Board.new
@@ -38,7 +50,7 @@ class TestGame < Test::Unit::TestCase
 
     p1_out = <<EOF
 BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
 Protocol_Mode:Server
 Format:Shogi 1.0
 Declaration:Jishogi 1.1
@@ -48,11 +60,13 @@ Name-:p2
 Your_Turn:+
 Rematch_On_Draw:NO
 To_Move:+
+Max_Moves:#{$options["max-moves"]}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:1500
 Byoyomi:0
-Least_Time_Per_Move:1
+Increment:0
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
 END Time
 BEGIN Position
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
@@ -72,7 +86,7 @@ EOF
 
     p2_out = <<EOF
 BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
 Protocol_Mode:Server
 Format:Shogi 1.0
 Declaration:Jishogi 1.1
@@ -82,11 +96,13 @@ Name-:p2
 Your_Turn:-
 Rematch_On_Draw:NO
 To_Move:+
+Max_Moves:#{$options["max-moves"]}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:1500
 Byoyomi:0
-Least_Time_Per_Move:1
+Increment:0
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
 END Time
 BEGIN Position
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
@@ -110,6 +126,117 @@ EOF
 V2
 N+p1
 N-p2
+'Max_Moves:#{$options["max-moves"]}
+'Least_Time_Per_Move:#{$options["least-time-per-move"]}
+$EVENT:#{game.game_id}
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOF
+  end
+
+  def test_new_fischer
+    game_name = "hoge-600-10F"
+    board = ShogiServer::Board.new
+    board.initial
+    p1 = MockPlayer.new
+    p1.sente = true
+    p1.name  = "p1"
+    p2 = MockPlayer.new
+    p2.sente = false
+    p2.name  = "p2"
+
+    game = ShogiServer::Game.new game_name, p1, p2, board
+    assert_equal "", game.last_move
+
+    p1_out = <<EOF
+BEGIN Game_Summary
+Protocol_Version:1.2
+Protocol_Mode:Server
+Format:Shogi 1.0
+Declaration:Jishogi 1.1
+Game_ID:#{game.game_id}
+Name+:p1
+Name-:p2
+Your_Turn:+
+Rematch_On_Draw:NO
+To_Move:+
+Max_Moves:#{$options["max-moves"]}
+BEGIN Time
+Time_Unit:1sec
+Total_Time:600
+Byoyomi:0
+Increment:10
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
+END Time
+BEGIN Position
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+END Position
+END Game_Summary
+EOF
+    assert_equal(p1_out, p1.out.first)
+
+    p2_out = <<EOF
+BEGIN Game_Summary
+Protocol_Version:1.2
+Protocol_Mode:Server
+Format:Shogi 1.0
+Declaration:Jishogi 1.1
+Game_ID:#{game.game_id}
+Name+:p1
+Name-:p2
+Your_Turn:-
+Rematch_On_Draw:NO
+To_Move:+
+Max_Moves:#{$options["max-moves"]}
+BEGIN Time
+Time_Unit:1sec
+Total_Time:600
+Byoyomi:0
+Increment:10
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
+END Time
+BEGIN Position
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+END Position
+END Game_Summary
+EOF
+    assert_equal(p2_out, p2.out.first)
+
+    file = Pathname.new(game.logfile)
+    log = file.read
+    assert_equal(<<EOF, log.gsub(/^\$START_TIME.*?\n/,''))
+V2
+N+p1
+N-p2
+'Max_Moves:#{$options["max-moves"]}
+'Least_Time_Per_Move:#{$options["least-time-per-move"]}
+'Increment:10
 $EVENT:#{game.game_id}
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
 P2 * -HI *  *  *  *  * -KA * 
@@ -140,7 +267,7 @@ EOF
 
     p1_out = <<EOF
 BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
 Protocol_Mode:Server
 Format:Shogi 1.0
 Declaration:Jishogi 1.1
@@ -150,11 +277,13 @@ Name-:p2
 Your_Turn:+
 Rematch_On_Draw:NO
 To_Move:-
+Max_Moves:#{$options["max-moves"]}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:1500
 Byoyomi:0
-Least_Time_Per_Move:1
+Increment:0
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
 END Time
 BEGIN Position
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
@@ -162,11 +291,12 @@ P2 * -HI *  *  *  *  * -KA *
 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
 P4 *  *  *  *  *  *  *  *  * 
 P5 *  *  *  *  *  *  *  *  * 
-P6 *  * +FU *  *  *  *  *  * 
-P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
 P8 * +KA *  *  *  *  * +HI * 
 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
--
++
++7776FU,T1
 END Position
 END Game_Summary
 EOF
@@ -174,7 +304,7 @@ EOF
 
     p2_out = <<EOF
 BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
 Protocol_Mode:Server
 Format:Shogi 1.0
 Declaration:Jishogi 1.1
@@ -184,11 +314,13 @@ Name-:p2
 Your_Turn:-
 Rematch_On_Draw:NO
 To_Move:-
+Max_Moves:#{$options["max-moves"]}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:1500
 Byoyomi:0
-Least_Time_Per_Move:1
+Increment:0
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
 END Time
 BEGIN Position
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
@@ -196,11 +328,12 @@ P2 * -HI *  *  *  *  * -KA *
 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
 P4 *  *  *  *  *  *  *  *  * 
 P5 *  *  *  *  *  *  *  *  * 
-P6 *  * +FU *  *  *  *  *  * 
-P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
 P8 * +KA *  *  *  *  * +HI * 
 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
--
++
++7776FU,T1
 END Position
 END Game_Summary
 EOF
@@ -212,6 +345,8 @@ EOF
 V2
 N+p1
 N-p2
+'Max_Moves:#{$options["max-moves"]}
+'Least_Time_Per_Move:#{$options["least-time-per-move"]}
 $EVENT:#{game.game_id}
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
 P2 * -HI *  *  *  *  * -KA * 
@@ -245,7 +380,7 @@ EOF
 
     p1_out = <<EOF
 BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
 Protocol_Mode:Server
 Format:Shogi 1.0
 Declaration:Jishogi 1.1
@@ -255,23 +390,27 @@ Name-:p2
 Your_Turn:+
 Rematch_On_Draw:NO
 To_Move:+
+Max_Moves:#{$options["max-moves"]}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:1500
 Byoyomi:0
-Least_Time_Per_Move:1
+Increment:0
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
 END Time
 BEGIN Position
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
 P2 * -HI *  *  *  *  * -KA * 
-P3-FU-FU-FU-FU-FU-FU * -FU-FU
-P4 *  *  *  *  *  * -FU *  * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  *  *  * 
 P5 *  *  *  *  *  *  *  *  * 
-P6 *  * +FU *  *  *  *  *  * 
-P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
 P8 * +KA *  *  *  *  * +HI * 
 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
 +
++7776FU,T1
+-3334FU,T1
 END Position
 END Game_Summary
 EOF
@@ -279,7 +418,7 @@ EOF
 
     p2_out = <<EOF
 BEGIN Game_Summary
-Protocol_Version:1.1
+Protocol_Version:1.2
 Protocol_Mode:Server
 Format:Shogi 1.0
 Declaration:Jishogi 1.1
@@ -289,23 +428,27 @@ Name-:p2
 Your_Turn:-
 Rematch_On_Draw:NO
 To_Move:+
+Max_Moves:#{$options["max-moves"]}
 BEGIN Time
 Time_Unit:1sec
 Total_Time:1500
 Byoyomi:0
-Least_Time_Per_Move:1
+Increment:0
+Least_Time_Per_Move:#{$options["least-time-per-move"]}
 END Time
 BEGIN Position
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
 P2 * -HI *  *  *  *  * -KA * 
-P3-FU-FU-FU-FU-FU-FU * -FU-FU
-P4 *  *  *  *  *  * -FU *  * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  *  *  * 
 P5 *  *  *  *  *  *  *  *  * 
-P6 *  * +FU *  *  *  *  *  * 
-P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
 P8 * +KA *  *  *  *  * +HI * 
 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
 +
++7776FU,T1
+-3334FU,T1
 END Position
 END Game_Summary
 EOF
@@ -317,6 +460,8 @@ EOF
 V2
 N+p1
 N-p2
+'Max_Moves:#{$options["max-moves"]}
+'Least_Time_Per_Move:#{$options["least-time-per-move"]}
 $EVENT:#{game.game_id}
 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
 P2 * -HI *  *  *  *  * -KA * 
@@ -362,5 +507,41 @@ EOF
     game.monitoroff(handler2)
     assert_equal(0, game.monitors.size)
   end
+
+  def test_decide_turns
+    p1 = MockPlayer.new
+    p1.name = "p1"
+    p2 = MockPlayer.new
+    p2.name = "p2"
+
+    p1.sente=nil; p2.sente=false
+    ShogiServer::Game::decide_turns(p1, "+", p2)
+    assert_equal true, p1.sente
+
+    p1.sente=nil; p2.sente=nil
+    ShogiServer::Game::decide_turns(p1, "+", p2)
+    assert_equal true, p1.sente
+
+    p1.sente=nil; p2.sente=true
+    ShogiServer::Game::decide_turns(p1, "-", p2)
+    assert_equal false, p1.sente
+
+    p1.sente=nil; p2.sente=nil
+    ShogiServer::Game::decide_turns(p1, "-", p2)
+    assert_equal false, p1.sente
+
+    p1.sente=nil; p2.sente=false
+    ShogiServer::Game::decide_turns(p1, "*", p2)
+    assert_equal true, p1.sente
+
+    p1.sente=nil; p2.sente=true
+    ShogiServer::Game::decide_turns(p1, "*", p2)
+    assert_equal false, p1.sente
+
+    p1.sente=nil; p2.sente=nil
+    ShogiServer::Game::decide_turns(p1, "*", p2)
+    assert (p1.sente == true  && p2.sente == false) ||
+           (p1.sente == false && p2.sente == true)
+  end
 end