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)
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))
40 def test_without_byoyomi
41 tc = ShogiServer::ChessClock.new(1, 1500, 0)
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))
51 tc = ShogiServer::ChessClock.new(1, 1500, 60)
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))
59 p = DummyPlayer.new 30
60 assert(!tc.timeout?(p, 100, 189))
61 assert(tc.timeout?(p, 100, 190))
64 def test_with_byoyomi2
65 tc = ShogiServer::ChessClock.new(1, 0, 60)
68 assert(!tc.timeout?(p, 100, 159))
69 assert(tc.timeout?(p, 100, 160))
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
84 def test_time_duration_over_thinking_time
85 tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
86 assert_equal(1, tc.time_duration(1, 100.1, 101.9)) # 1.8
87 assert_equal(2, tc.time_duration(2, 100.1, 102.9)) # 2.8
91 tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
93 p = DummyPlayer.new 100
94 assert(!tc.timeout?(p, 100, 101)) # 1
95 assert(!tc.timeout?(p, 100, 209)) # 109
96 assert(!tc.timeout?(p, 100, 209.9)) # 109.9
97 assert(tc.timeout?(p, 100, 210)) # 110
98 assert(tc.timeout?(p, 100, 210.1)) # 110.1
99 assert(tc.timeout?(p, 100, 211)) # 111
102 def test_with_byoyomi2
103 tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 10)
105 p = DummyPlayer.new 0
106 assert(!tc.timeout?(p, 100, 109)) # 9
107 assert(!tc.timeout?(p, 100, 109.9)) # 9.9
108 assert(tc.timeout?(p, 100, 110)) # 10
109 assert(tc.timeout?(p, 100, 110.1)) # 10.1
110 assert(tc.timeout?(p, 100, 110)) # 10.1
114 class TestStopWatchClock < Test::Unit::TestCase
115 def test_time_duration
116 tc = ShogiServer::StopWatchClock.new(1, 1500, 60)
117 assert_equal(0, tc.time_duration(nil, 100.1, 100.9))
118 assert_equal(0, tc.time_duration(nil, 100, 101))
119 assert_equal(0, tc.time_duration(nil, 100, 159.9))
120 assert_equal(60, tc.time_duration(nil, 100, 160))
121 assert_equal(60, tc.time_duration(nil, 100, 219))
122 assert_equal(120, tc.time_duration(nil, 100, 220))
125 def test_with_byoyomi
126 tc = ShogiServer::StopWatchClock.new(1, 600, 60)
128 p = DummyPlayer.new 60
129 assert(!tc.timeout?(p, 100, 159))
130 assert(tc.timeout?(p, 100, 160))