1 $:.unshift File.join(File.dirname(__FILE__), "..")
3 require 'test/mock_player'
4 require 'shogi_server/board'
5 require 'shogi_server/game'
6 require 'shogi_server/player'
15 class TestTimeClockFactor < Test::Unit::TestCase
17 c = ShogiServer::TimeClock::factory(1, "hoge-900-0")
18 assert_instance_of(ShogiServer::ChessClock, c)
20 c = ShogiServer::TimeClock::factory(1, "hoge-1500-60")
21 assert_instance_of(ShogiServer::ChessClock, c)
24 def test_stop_watch_clock
25 c = ShogiServer::TimeClock::factory(1, "hoge-1500-060")
26 assert_instance_of(ShogiServer::StopWatchClock, c)
29 def test_check_clock_fischer
30 c = ShogiServer::TimeClock::factory(1, "hoge-600-10F")
31 assert_instance_of(ShogiServer::ChessClock, c)
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))
45 def test_without_byoyomi
46 tc = ShogiServer::ChessClock.new(1, 1500, 0)
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))
56 tc = ShogiServer::ChessClock.new(1, 1500, 60)
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))
64 p = DummyPlayer.new 30
65 assert(!tc.timeout?(p, 100, 189))
66 assert(tc.timeout?(p, 100, 190))
69 def test_with_byoyomi2
70 tc = ShogiServer::ChessClock.new(1, 0, 60)
73 assert(!tc.timeout?(p, 100, 159))
74 assert(tc.timeout?(p, 100, 160))
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
88 assert_equal(0, tc.time_duration(100, 100, 99.9)) # -0.1
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
98 tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
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
109 def test_with_byoyomi2
110 tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 10)
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
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
131 assert_equal(0, tc.time_duration(100, 100, 99.9)) # -0.1
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
140 def test_with_fischer
141 tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 10)
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
152 def test_with_fischer2
153 tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 0, 10)
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
163 def test_process_time
164 tc = ShogiServer::ChessClockWithLeastZero.new(0, 600, 0, 10)
166 p = DummyPlayer.new 100
167 tc.process_time(p, 100, 101) # 1
168 assert_equal(109, p.mytime)
170 p = DummyPlayer.new 100
171 tc.process_time(p, 100, 111) # 11
172 assert_equal(99, p.mytime)
174 p = DummyPlayer.new 100
175 tc.process_time(p, 100, 209) # 109
176 assert_equal(1, p.mytime)
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))
191 def test_with_byoyomi
192 tc = ShogiServer::StopWatchClock.new(1, 600, 60)
194 p = DummyPlayer.new 60
195 assert(!tc.timeout?(p, 100, 159))
196 assert(tc.timeout?(p, 100, 160))