OSDN Git Service

Fix test failures related to MAX_MOVES and least time per move
[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   end
83
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
88   end
89
90   def test_with_byoyomi
91     tc = ShogiServer::ChessClockWithLeastZero.new(0, 900, 10)
92
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
100   end
101
102   def test_with_byoyomi2
103     tc = ShogiServer::ChessClockWithLeastZero.new(0, 0, 10)
104
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
111   end
112 end
113
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))
123   end
124
125   def test_with_byoyomi
126     tc = ShogiServer::StopWatchClock.new(1, 600, 60)
127
128     p = DummyPlayer.new 60
129     assert(!tc.timeout?(p, 100, 159))
130     assert(tc.timeout?(p, 100, 160))
131   end
132 end
133