OSDN Git Service

Refactor ChessClockWithLeastZero
[shogi-server/shogi-server.git] / test / TC_time_clock.rb
index 151ba81..aa98ae7 100644 (file)
@@ -30,11 +30,11 @@ end
 class TestChessClock < Test::Unit::TestCase
   def test_time_duration
     tc = ShogiServer::ChessClock.new(1, 1500, 60)
-    assert_equal(1, tc.time_duration(100.1, 100.9))
-    assert_equal(1, tc.time_duration(100, 101))
-    assert_equal(1, tc.time_duration(100.1, 101.9))
-    assert_equal(2, tc.time_duration(100.1, 102.9))
-    assert_equal(2, tc.time_duration(100, 102))
+    assert_equal(1, tc.time_duration(nil, 100.1, 100.9))
+    assert_equal(1, tc.time_duration(nil, 100, 101))
+    assert_equal(1, tc.time_duration(nil, 100.1, 101.9))
+    assert_equal(2, tc.time_duration(nil, 100.1, 102.9))
+    assert_equal(2, tc.time_duration(nil, 100, 102))
   end
 
   def test_without_byoyomi
@@ -70,15 +70,56 @@ class TestChessClock < Test::Unit::TestCase
   end
 end
 
+class TestChessClockWithLeastZero < Test::Unit::TestCase
+  def test_time_duration_within_thinking_time
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
+    assert_equal(0, tc.time_duration(100, 100.1, 100.9))  # 0.8
+    assert_equal(1, tc.time_duration(100, 100, 101))      # 1
+    assert_equal(1, tc.time_duration(100, 100.1, 101.9))  # 1.8
+    assert_equal(1, tc.time_duration(1,    100,   101))   # 1
+    assert_equal(2, tc.time_duration(100, 100.1, 102.9))  # 2.8
+    assert_equal(2, tc.time_duration(100, 100, 102))      # 2
+  end
+
+  def test_time_duration_over_thinking_time
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
+    assert_equal(1, tc.time_duration(1,    100.1, 101.9))  # 1.8
+    assert_equal(2, tc.time_duration(2,    100.1, 102.9))  # 2.8
+  end
+
+  def test_with_byoyomi
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
+
+    p = DummyPlayer.new 100
+    assert(!tc.timeout?(p, 100, 101))    # 1
+    assert(!tc.timeout?(p, 100, 209))    # 109
+    assert(!tc.timeout?(p, 100, 209.9))  # 109.9
+    assert(tc.timeout?(p, 100, 210))     # 110
+    assert(tc.timeout?(p, 100, 210.1))   # 110.1
+    assert(tc.timeout?(p, 100, 211))     # 111
+  end
+
+  def test_with_byoyomi2
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 10)
+
+    p = DummyPlayer.new 0
+    assert(!tc.timeout?(p, 100, 109))    # 9
+    assert(!tc.timeout?(p, 100, 109.9))  # 9.9
+    assert(tc.timeout?(p, 100, 110))     # 10
+    assert(tc.timeout?(p, 100, 110.1))   # 10.1
+    assert(tc.timeout?(p, 100, 110))     # 10.1
+  end
+end
+
 class TestStopWatchClock < Test::Unit::TestCase
   def test_time_duration
     tc = ShogiServer::StopWatchClock.new(1, 1500, 60)
-    assert_equal(0, tc.time_duration(100.1, 100.9))
-    assert_equal(0, tc.time_duration(100, 101))
-    assert_equal(0, tc.time_duration(100, 159.9))
-    assert_equal(60, tc.time_duration(100, 160))
-    assert_equal(60, tc.time_duration(100, 219))
-    assert_equal(120, tc.time_duration(100, 220))
+    assert_equal(0,   tc.time_duration(nil, 100.1, 100.9))
+    assert_equal(0,   tc.time_duration(nil, 100, 101))
+    assert_equal(0,   tc.time_duration(nil, 100, 159.9))
+    assert_equal(60,  tc.time_duration(nil, 100, 160))
+    assert_equal(60,  tc.time_duration(nil, 100, 219))
+    assert_equal(120, tc.time_duration(nil, 100, 220))
   end
 
   def test_with_byoyomi