OSDN Git Service

Remove a debug message
[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_accessor :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
29   def test_check_clock_fischer
30     c = ShogiServer::TimeClock::factory(1, "hoge-600-10F")
31     assert_instance_of(ShogiServer::ChessClock, c)
32   end
33 end
34
35 class TestChessClock < Test::Unit::TestCase
36   def test_time_duration
37     tc = ShogiServer::ChessClock.new(1, 1500, 60)
38     assert_equal(1, tc.time_duration(nil, 100.1, 100.9))
39     assert_equal(1, tc.time_duration(nil, 100, 101))
40     assert_equal(1, tc.time_duration(nil, 100.1, 101.9))
41     assert_equal(2, tc.time_duration(nil, 100.1, 102.9))
42     assert_equal(2, tc.time_duration(nil, 100, 102))
43   end
44
45   def test_without_byoyomi
46     tc = ShogiServer::ChessClock.new(1, 1500, 0)
47
48     p = DummyPlayer.new 100
49     assert(!tc.timeout?(p, 100, 101))
50     assert(!tc.timeout?(p, 100, 199))
51     assert(tc.timeout?(p, 100, 200))
52     assert(tc.timeout?(p, 100, 201))
53   end
54
55   def test_with_byoyomi
56     tc = ShogiServer::ChessClock.new(1, 1500, 60)
57
58     p = DummyPlayer.new 100
59     assert(!tc.timeout?(p, 100, 101))
60     assert(!tc.timeout?(p, 100, 259))
61     assert(tc.timeout?(p, 100, 260))
62     assert(tc.timeout?(p, 100, 261))
63
64     p = DummyPlayer.new 30
65     assert(!tc.timeout?(p, 100, 189))
66     assert(tc.timeout?(p, 100, 190))
67   end
68
69   def test_with_byoyomi2
70     tc = ShogiServer::ChessClock.new(1, 0, 60)
71
72     p = DummyPlayer.new 0
73     assert(!tc.timeout?(p, 100, 159))
74     assert(tc.timeout?(p, 100, 160))
75   end
76 end
77
78 class TestChessClockWithLeastZero < Test::Unit::TestCase
79   def test_time_duration_within_thinking_time
80     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
81     assert_equal(0, tc.time_duration(100, 100.1, 100.9))  # 0.8
82     assert_equal(1, tc.time_duration(100, 100, 101))      # 1
83     assert_equal(1, tc.time_duration(100, 100.1, 101.9))  # 1.8
84     assert_equal(1, tc.time_duration(1,    100,   101))   # 1
85     assert_equal(2, tc.time_duration(100, 100.1, 102.9))  # 2.8
86     assert_equal(2, tc.time_duration(100, 100, 102))      # 2
87
88     assert_equal(0, tc.time_duration(100, 100, 99.9))     # -0.1
89   end
90
91   def test_time_duration_over_thinking_time
92     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
93     assert_equal(1, tc.time_duration(1,    100.1, 101.9))  # 1.8
94     assert_equal(2, tc.time_duration(2,    100.1, 102.9))  # 2.8
95   end
96
97   def test_with_byoyomi
98     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
99
100     p = DummyPlayer.new 100
101     assert(!tc.timeout?(p, 100, 101))    # 1
102     assert(!tc.timeout?(p, 100, 209))    # 109
103     assert(!tc.timeout?(p, 100, 209.9))  # 109.9
104     assert(tc.timeout?(p, 100, 210))     # 110
105     assert(tc.timeout?(p, 100, 210.1))   # 110.1
106     assert(tc.timeout?(p, 100, 211))     # 111
107   end
108
109   def test_with_byoyomi2
110     tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 10)
111
112     p = DummyPlayer.new 0
113     assert(!tc.timeout?(p, 100, 109))    # 9
114     assert(!tc.timeout?(p, 100, 109.9))  # 9.9
115     assert(tc.timeout?(p, 100, 110))     # 10
116     assert(tc.timeout?(p, 100, 110.1))   # 10.1
117     assert(tc.timeout?(p, 100, 110))     # 10.1
118   end
119 end
120
121 class TestChessClockWithLeastZeroFischer < Test::Unit::TestCase
122   def test_time_duration_within_thinking_time
123     tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 10)
124     assert_equal(0, tc.time_duration(100, 100.1, 100.9))  # 0.8
125     assert_equal(1, tc.time_duration(100, 100, 101))      # 1
126     assert_equal(1, tc.time_duration(100, 100.1, 101.9))  # 1.8
127     assert_equal(1, tc.time_duration(1,    100,   101))   # 1
128     assert_equal(2, tc.time_duration(100, 100.1, 102.9))  # 2.8
129     assert_equal(2, tc.time_duration(100, 100, 102))      # 2
130
131     assert_equal(0, tc.time_duration(100, 100, 99.9))     # -0.1
132   end
133
134   def test_time_duration_over_thinking_time
135     tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0 ,10)
136     assert_equal(1, tc.time_duration(1,    100.1, 101.9))  # 1.8
137     assert_equal(2, tc.time_duration(2,    100.1, 102.9))  # 2.8
138   end
139
140   def test_with_fischer
141     tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 10)
142
143     p = DummyPlayer.new 100
144     assert(!tc.timeout?(p, 100, 101))    # 1
145     assert(!tc.timeout?(p, 100, 209))    # 109
146     assert(!tc.timeout?(p, 100, 209.9))  # 109.9
147     assert(tc.timeout?(p, 100, 210))     # 110
148     assert(tc.timeout?(p, 100, 210.1))   # 110.1
149     assert(tc.timeout?(p, 100, 211))     # 111
150   end
151
152   def test_with_fischer2
153     tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 0, 10)
154
155     p = DummyPlayer.new 0
156     assert(!tc.timeout?(p, 100, 109))    # 9
157     assert(!tc.timeout?(p, 100, 109.9))  # 9.9
158     assert(tc.timeout?(p, 100, 110))     # 10
159     assert(tc.timeout?(p, 100, 110.1))   # 10.1
160     assert(tc.timeout?(p, 100, 110))     # 10.1
161   end
162
163   def test_process_time
164     tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 10)
165
166     p = DummyPlayer.new 100
167     tc.process_time(p, 100, 101) # 1
168     assert_equal(109, p.mytime)
169
170     p = DummyPlayer.new 100
171     tc.process_time(p, 100, 111) # 11
172     assert_equal(99, p.mytime)
173
174     p = DummyPlayer.new 100
175     tc.process_time(p, 100, 209) # 109
176     assert_equal(1, p.mytime)
177   end
178 end
179
180 class TestStopWatchClock < Test::Unit::TestCase
181   def test_time_duration
182     tc = ShogiServer::StopWatchClock.new(1, 1500, 60)
183     assert_equal(0,   tc.time_duration(nil, 100.1, 100.9))
184     assert_equal(0,   tc.time_duration(nil, 100, 101))
185     assert_equal(0,   tc.time_duration(nil, 100, 159.9))
186     assert_equal(60,  tc.time_duration(nil, 100, 160))
187     assert_equal(60,  tc.time_duration(nil, 100, 219))
188     assert_equal(120, tc.time_duration(nil, 100, 220))
189   end
190
191   def test_with_byoyomi
192     tc = ShogiServer::StopWatchClock.new(1, 600, 60)
193
194     p = DummyPlayer.new 60
195     assert(!tc.timeout?(p, 100, 159))
196     assert(tc.timeout?(p, 100, 160))
197   end
198 end
199