OSDN Git Service

* [shogi-server] Support a graceful shutdown. (Closes #38544)
[shogi-server/shogi-server.git] / test / TC_time_clock.rb
index aa98ae7..e4d812e 100644 (file)
@@ -9,7 +9,7 @@ class DummyPlayer
   def initialize(mytime)
     @mytime = mytime
   end
-  attr_reader :mytime
+  attr_accessor :mytime
 end
 
 class TestTimeClockFactor < Test::Unit::TestCase
@@ -25,6 +25,11 @@ class TestTimeClockFactor < Test::Unit::TestCase
     c = ShogiServer::TimeClock::factory(1, "hoge-1500-060")
     assert_instance_of(ShogiServer::StopWatchClock, c)
   end
+
+  def test_check_clock_fischer
+    c = ShogiServer::TimeClock::factory(1, "hoge-600-10F")
+    assert_instance_of(ShogiServer::ChessClock, c)
+  end
 end
 
 class TestChessClock < Test::Unit::TestCase
@@ -79,6 +84,8 @@ class TestChessClockWithLeastZero < Test::Unit::TestCase
     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
+
+    assert_equal(0, tc.time_duration(100, 100, 99.9))     # -0.1
   end
 
   def test_time_duration_over_thinking_time
@@ -111,6 +118,65 @@ class TestChessClockWithLeastZero < Test::Unit::TestCase
   end
 end
 
+class TestChessClockWithLeastZeroFischer < Test::Unit::TestCase
+  def test_time_duration_within_thinking_time
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 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
+
+    assert_equal(0, tc.time_duration(100, 100, 99.9))     # -0.1
+  end
+
+  def test_time_duration_over_thinking_time
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0 ,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_fischer
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 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_fischer2
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 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
+
+  def test_process_time
+    tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 10)
+
+    p = DummyPlayer.new 100
+    tc.process_time(p, 100, 101) # 1
+    assert_equal(109, p.mytime)
+
+    p = DummyPlayer.new 100
+    tc.process_time(p, 100, 111) # 11
+    assert_equal(99, p.mytime)
+
+    p = DummyPlayer.new 100
+    tc.process_time(p, 100, 209) # 109
+    assert_equal(1, p.mytime)
+  end
+end
+
 class TestStopWatchClock < Test::Unit::TestCase
   def test_time_duration
     tc = ShogiServer::StopWatchClock.new(1, 1500, 60)