OSDN Git Service

* [mk_rate] - Added a new option, --ignore, which is imported from mk_rate-from-grep.
[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(100.1, 100.9))
34     assert_equal(1, tc.time_duration(100, 101))
35     assert_equal(1, tc.time_duration(100.1, 101.9))
36     assert_equal(2, tc.time_duration(100.1, 102.9))
37     assert_equal(2, tc.time_duration(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 TestStopWatchClock < Test::Unit::TestCase
74   def test_time_duration
75     tc = ShogiServer::StopWatchClock.new(1, 1500, 60)
76     assert_equal(0, tc.time_duration(100.1, 100.9))
77     assert_equal(0, tc.time_duration(100, 101))
78     assert_equal(0, tc.time_duration(100, 159.9))
79     assert_equal(60, tc.time_duration(100, 160))
80     assert_equal(60, tc.time_duration(100, 219))
81     assert_equal(120, tc.time_duration(100, 220))
82   end
83
84   def test_with_byoyomi
85     tc = ShogiServer::StopWatchClock.new(1, 600, 60)
86
87     p = DummyPlayer.new 60
88     assert(!tc.timeout?(p, 100, 159))
89     assert(tc.timeout?(p, 100, 160))
90   end
91 end
92