OSDN Git Service

Fix #35795: A time consumed could be -1
[shogi-server/shogi-server.git] / test / TC_time_clock.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'test/mock_player'
4 require 'shogi_server/board'
5 require 'shogi_server/game'
6 require 'shogi_server/player'
7
8 class DummyPlayer
9   def initialize(mytime)
10     @mytime = mytime
11   end
12   attr_reader :mytime
13 end
14
15 class TestTimeClockFactor < Test::Unit::TestCase
16   def test_chess_clock
17     c = ShogiServer::TimeClock::factory(1, "hoge-900-0")
18     assert_instance_of(ShogiServer::ChessClock, c)
19
20     c = ShogiServer::TimeClock::factory(1, "hoge-1500-60")
21     assert_instance_of(ShogiServer::ChessClock, c)
22   end
23
24   def test_stop_watch_clock
25     c = ShogiServer::TimeClock::factory(1, "hoge-1500-060")
26     assert_instance_of(ShogiServer::StopWatchClock, c)
27   end
28 end
29
30 class TestChessClock < Test::Unit::TestCase
31   def test_time_duration
32     tc = ShogiServer::ChessClock.new(1, 1500, 60)
33     assert_equal(1, tc.time_duration(nil, 100.1, 100.9))
34     assert_equal(1, tc.time_duration(nil, 100, 101))
35     assert_equal(1, tc.time_duration(nil, 100.1, 101.9))
36     assert_equal(2, tc.time_duration(nil, 100.1, 102.9))
37     assert_equal(2, tc.time_duration(nil, 100, 102))
38   end
39
40   def test_without_byoyomi
41     tc = ShogiServer::ChessClock.new(1, 1500, 0)
42
43     p = DummyPlayer.new 100
44     assert(!tc.timeout?(p, 100, 101))
45     assert(!tc.timeout?(p, 100, 199))
46     assert(tc.timeout?(p, 100, 200))
47     assert(tc.timeout?(p, 100, 201))
48   end
49
50   def test_with_byoyomi
51     tc = ShogiServer::ChessClock.new(1, 1500, 60)
52
53     p = DummyPlayer.new 100
54     assert(!tc.timeout?(p, 100, 101))
55     assert(!tc.timeout?(p, 100, 259))
56     assert(tc.timeout?(p, 100, 260))
57     assert(tc.timeout?(p, 100, 261))
58
59     p = DummyPlayer.new 30
60     assert(!tc.timeout?(p, 100, 189))
61     assert(tc.timeout?(p, 100, 190))
62   end
63
64   def test_with_byoyomi2
65     tc = ShogiServer::ChessClock.new(1, 0, 60)
66
67     p = DummyPlayer.new 0
68     assert(!tc.timeout?(p, 100, 159))
69     assert(tc.timeout?(p, 100, 160))
70   end
71 end
72
73 class TestChessClockWithLeastZero < Test::Unit::TestCase
74   def test_time_duration_within_thinking_time
75     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
76     assert_equal(0, tc.time_duration(100, 100.1, 100.9))  # 0.8
77     assert_equal(1, tc.time_duration(100, 100, 101))      # 1
78     assert_equal(1, tc.time_duration(100, 100.1, 101.9))  # 1.8
79     assert_equal(1, tc.time_duration(1,    100,   101))   # 1
80     assert_equal(2, tc.time_duration(100, 100.1, 102.9))  # 2.8
81     assert_equal(2, tc.time_duration(100, 100, 102))      # 2
82
83     assert_equal(0, tc.time_duration(100, 100, 99.9))     # -0.1
84   end
85
86   def test_time_duration_over_thinking_time
87     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
88     assert_equal(1, tc.time_duration(1,    100.1, 101.9))  # 1.8
89     assert_equal(2, tc.time_duration(2,    100.1, 102.9))  # 2.8
90   end
91
92   def test_with_byoyomi
93     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
94
95     p = DummyPlayer.new 100
96     assert(!tc.timeout?(p, 100, 101))    # 1
97     assert(!tc.timeout?(p, 100, 209))    # 109
98     assert(!tc.timeout?(p, 100, 209.9))  # 109.9
99     assert(tc.timeout?(p, 100, 210))     # 110
100     assert(tc.timeout?(p, 100, 210.1))   # 110.1
101     assert(tc.timeout?(p, 100, 211))     # 111
102   end
103
104   def test_with_byoyomi2
105     tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 10)
106
107     p = DummyPlayer.new 0
108     assert(!tc.timeout?(p, 100, 109))    # 9
109     assert(!tc.timeout?(p, 100, 109.9))  # 9.9
110     assert(tc.timeout?(p, 100, 110))     # 10
111     assert(tc.timeout?(p, 100, 110.1))   # 10.1
112     assert(tc.timeout?(p, 100, 110))     # 10.1
113   end
114 end
115
116 class TestStopWatchClock < Test::Unit::TestCase
117   def test_time_duration
118     tc = ShogiServer::StopWatchClock.new(1, 1500, 60)
119     assert_equal(0,   tc.time_duration(nil, 100.1, 100.9))
120     assert_equal(0,   tc.time_duration(nil, 100, 101))
121     assert_equal(0,   tc.time_duration(nil, 100, 159.9))
122     assert_equal(60,  tc.time_duration(nil, 100, 160))
123     assert_equal(60,  tc.time_duration(nil, 100, 219))
124     assert_equal(120, tc.time_duration(nil, 100, 220))
125   end
126
127   def test_with_byoyomi
128     tc = ShogiServer::StopWatchClock.new(1, 600, 60)
129
130     p = DummyPlayer.new 60
131     assert(!tc.timeout?(p, 100, 159))
132     assert(tc.timeout?(p, 100, 160))
133   end
134 end
135