OSDN Git Service

Remove a debug message
[shogi-server/shogi-server.git] / test / TC_board.rb
index 7f78f84..06e4efe 100644 (file)
@@ -24,24 +24,6 @@ EOM
     assert_equal(true, b.good_kachi?(false))
   end
 
-  def test_kachi_good
-    b = ShogiServer::Board.new
-    b.set_from_str(<<EOM)
-P1+HI+HI+KA+KA+OU *  *  *  * 
-P2+FU+FU+FU+FU+FU+FU *  *  * 
-P+00FU00FU
-EOM
-    assert_equal(true, b.good_kachi?(true))
-
-    b = ShogiServer::Board.new
-    b.set_from_str(<<EOM)
-P8-HI-HI-KA-KA-OU *  *  *  * 
-P9-FU-FU-FU-FU-FU-FU *  *  * 
-P-00FU
-EOM
-    assert_equal(true, b.good_kachi?(false))
-  end
-
   def test_kachi_bad
     b = ShogiServer::Board.new
     b.set_from_str(<<EOM)
@@ -208,6 +190,255 @@ EOM
 end
 
 
+class TestMoveBack < Test::Unit::TestCase
+  def validate_before_after(board, move)
+    orig = board.to_s
+    orig_teban = board.teban
+    orig_move_count = board.move_count
+
+    assert_equal true, board.move_to(move)
+    assert_equal !orig_teban, board.teban
+    assert_equal (orig_move_count+1), board.move_count
+
+    assert_equal true, board.move_back(move)
+    assert_equal orig_teban, board.teban
+    assert_equal orig_move_count, board.move_count
+    assert_equal orig, board.to_s
+  end
+
+  def test_normal
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+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
++
+EOM
+    m = ShogiServer::Move.new(2,7,2,6,"FU",true)
+    validate_before_after(b,m)
+    m = ShogiServer::Move.new(3,3,3,4,"FU",false)
+    validate_before_after(b,m)
+    m = ShogiServer::Move.new(7,7,7,6,"FU",true)
+    validate_before_after(b,m)
+    m = ShogiServer::Move.new(8,3,8,4,"FU",false)
+    validate_before_after(b,m)
+  end
+
+  def test_promote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"TO",true)
+    validate_before_after(b,m)
+  end
+
+  def test_unpromote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"FU",true)
+    validate_before_after(b,m)
+  end
+
+  def test_promoted
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU+TO-FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+EOM
+    m = ShogiServer::Move.new(2,3,1,2,"TO",true)
+    assert !m.promotion
+    validate_before_after(b,m)
+    assert !m.promotion
+  end
+
+  def test_capture
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * -FU * 
+P5 *  *  *  *  *  *  * +FU * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOM
+    m = ShogiServer::Move.new(2,5,2,4,"FU",true)
+    validate_before_after(b,m)
+  end
+  def test_capture_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  * -FU * 
+P5 *  *  *  *  *  *  * +FU * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+EOM
+    m = ShogiServer::Move.new(2,4,2,5,"FU",false)
+    validate_before_after(b,m)
+  end
+  def test_capture_promote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"TO",true)
+    validate_before_after(b,m)
+  end
+
+  def test_capture_promote_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  * -FU * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+EOM
+    m = ShogiServer::Move.new(2,6,2,7,"TO",false)
+    validate_before_after(b,m)
+  end
+
+  def test_capture_unpromote
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
+P4 *  *  *  *  *  *  * +FU * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOM
+    m = ShogiServer::Move.new(2,4,2,3,"FU",true)
+    validate_before_after(b,m)
+  end
+
+  def test_capture_unpromote_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  * -FU * 
+P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+EOM
+    m = ShogiServer::Move.new(2,6,2,7,"FU",false)
+    validate_before_after(b,m)
+  end
+
+  def test_drop
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+P+00FU
+P-00FU
+EOM
+    m = ShogiServer::Move.new(0,0,2,3,"FU",true)
+    validate_before_after(b,m)
+  end
+
+  def test_drop_white
+    b = ShogiServer::Board.new
+    b.set_from_str(<<EOM)
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU-FU * -FU
+P4 *  *  *  *  *  *  *  *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  *  *  *  *  *  *  *  * 
+P7+FU+FU+FU+FU+FU+FU+FU * +FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+-
+P+00FU
+P-00FU
+EOM
+    m = ShogiServer::Move.new(0,0,2,3,"FU",false)
+    validate_before_after(b,m)
+  end
+end # class TestMoveBack
+
+
 class Test_promote < Test::Unit::TestCase
   def test_fu
     b = ShogiServer::Board.new
@@ -330,7 +561,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+7172HI"))
     assert_equal(:normal, b.handle_one_move("-9291OU"))
 
+    orig = b.deep_copy
     assert_equal(:oute_sennichite_sente_lose, b.handle_one_move("+7271HI")) # 4
+    assert_equal orig, b
   end
 
   def test_oute_sennichite1 #330
@@ -381,7 +614,9 @@ b.history[b.to_s] = 1
     assert_equal(:normal, b.handle_one_move("-4849RY"))
 
     assert_equal(:normal, b.handle_one_move("+6968OU")) # added
+    orig = b.deep_copy
     assert_equal(:oute_sennichite_gote_lose, b.handle_one_move("-4948RY"))
+    assert_equal orig, b
   end
 
   def test_not_oute_sennichite
@@ -406,7 +641,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+7172HI"))
     assert_equal(:normal, b.handle_one_move("-9291OU"))
 
+    orig = b.deep_copy
     assert_equal(:sennichite, b.handle_one_move("+7271HI")) # 4
+    assert_equal orig, b
   end
 
   def test_sennichite0
@@ -428,7 +665,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+7172OU"))
     assert_equal(:normal, b.handle_one_move("-9192OU"))
     assert_equal(:normal, b.handle_one_move("+7271OU"))
+    orig = b.deep_copy
     assert_equal(:sennichite, b.handle_one_move("-9291OU")) # 4
+    assert_equal orig, b
   end
 
   def test_sennichite1          # 329
@@ -457,7 +696,9 @@ EOM
     assert_equal(:normal, b.handle_one_move("+2858HI"))
     assert_equal(:normal, b.handle_one_move("-8252HI"))
     assert_equal(:normal, b.handle_one_move("+5828HI"))
+    orig = b.deep_copy
     assert_equal(:sennichite, b.handle_one_move("-5282HI"))
+    assert_equal orig, b
   end
 end
 
@@ -685,3 +926,75 @@ EOM
     assert_equal(false, b.uchifuzume?(true))
  end
 end
+
+class TestBoardForBuoy < Test::Unit::TestCase
+  def setup
+    @board = ShogiServer::Board.new
+  end
+
+  def test_set_from_moves_empty
+    moves = []
+    rt = @board.set_from_moves moves
+    assert_equal(:normal, rt)
+  end
+
+  def test_set_from_moves
+    moves = ["+7776FU", "-3334FU"]
+    assert_nothing_raised do
+      @board.set_from_moves moves
+    end
+
+    correct = ShogiServer::Board.new
+    correct.set_from_str <<EOF
+P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
+P2 * -HI *  *  *  *  * -KA * 
+P3-FU-FU-FU-FU-FU-FU * -FU-FU
+P4 *  *  *  *  *  * -FU *  * 
+P5 *  *  *  *  *  *  *  *  * 
+P6 *  * +FU *  *  *  *  *  * 
+P7+FU+FU * +FU+FU+FU+FU+FU+FU
+P8 * +KA *  *  *  *  * +HI * 
+P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
++
+EOF
+    assert_equal(correct.to_s, @board.to_s)
+  end
+
+  def test_set_from_moves_error1
+    moves = ["+7776FU", "-3435FU"]
+    assert_raise ArgumentError do
+      @board.set_from_moves moves
+    end
+  end
+
+  def test_set_from_moves_error2
+    moves = ["+7776FU", "+8786FU"]
+    assert_raise ArgumentError do
+      @board.set_from_moves moves
+    end
+  end
+end # TestBoardForBuoy
+
+class TestSplitMoves < Test::Unit::TestCase
+  def test_split_moves1
+    rs = ShogiServer::Board::split_moves "+7776FU"
+    assert_equal ["+7776FU"], rs
+  end
+
+  def test_split_moves2
+    rs = ShogiServer::Board::split_moves "+7776FU-3334FU"
+    assert_equal ["+7776FU", "-3334FU"], rs
+  end
+
+  def test_split_moves3
+    assert_nothing_raised do
+      ShogiServer::Board::split_moves ""
+    end
+  end
+
+  def test_split_moves_error1
+    assert_raise ShogiServer::WrongMoves do
+      ShogiServer::Board::split_moves "dummy"
+    end
+  end
+end