OSDN Git Service

Fix Dockerfile
[shogi-server/shogi-server.git] / test / TC_time_clock.rb
index 151ba81..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,16 +25,21 @@ 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
   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 +75,117 @@ 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
+
+    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, 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 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)
-    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