OSDN Git Service

utils/eval_graph.rb: Support Fischer time control.
[shogi-server/shogi-server.git] / test / TC_floodgate_next_time_generator.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'shogi_server'
4 require 'shogi_server/league/floodgate'
5 require 'fileutils'
6 require 'test/mock_log_message'
7
8 $topdir = File.expand_path File.dirname(__FILE__)
9
10 class TestNextTimeGenerator < Test::Unit::TestCase
11   def setup
12     @game_name = "floodgate-3600-0"
13     @config_path = File.join($topdir, "#{@game_name}.conf")
14   end
15
16   def teardown
17     if File.exist? @config_path
18       FileUtils.rm @config_path
19     end
20   end
21
22   def test_assure_file_does_not_exist
23     assert !File.exist?(@config_path)
24   end
25
26   def test_factory_from_config_file
27     # no config file
28     assert !File.exist?(@config_path)
29     assert_instance_of ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0, 
30                        ShogiServer::League::Floodgate::NextTimeGenerator.factory(@game_name)
31
32     # there is a config file
33     FileUtils.touch(@config_path)
34     assert_instance_of ShogiServer::League::Floodgate::NextTimeGeneratorConfig,
35                        ShogiServer::League::Floodgate::NextTimeGenerator.factory(@game_name)
36   end
37 end
38
39 class TestNextTimeGenerator_900_0 < Test::Unit::TestCase
40   def setup
41     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_900_0.new
42   end
43
44   def test_0_min
45     now = Time.mktime(2009,12,25,22,0)
46     assert_equal(Time.mktime(2009,12,25,22,30), @next.call(now))
47   end
48
49   def test_20_min
50     now = Time.mktime(2009,12,25,22,20)
51     assert_equal(Time.mktime(2009,12,25,22,30), @next.call(now))
52   end
53
54   def test_30_min
55     now = Time.mktime(2009,12,25,22,30)
56     assert_equal(Time.mktime(2009,12,25,23,00), @next.call(now))
57   end
58
59   def test_50_min
60     now = Time.mktime(2009,12,25,22,50)
61     assert_equal(Time.mktime(2009,12,25,23,00), @next.call(now))
62   end
63
64   def test_50_min_next_day
65     now = Time.mktime(2009,12,25,23,50)
66     assert_equal(Time.mktime(2009,12,26,0,0), @next.call(now))
67
68     now = Time.mktime(2010,7,25,23,30)
69     assert_equal(Time.mktime(2010,7,26,0,0), @next.call(now))
70     now = Time.mktime(2010,7,26,23,30)
71     assert_equal(Time.mktime(2010,7,27,0,0), @next.call(now))
72     now = Time.mktime(2010,7,27,23,30)
73     assert_equal(Time.mktime(2010,7,28,0,0), @next.call(now))
74     now = Time.mktime(2010,7,28,23,30)
75     assert_equal(Time.mktime(2010,7,29,0,0), @next.call(now))
76     now = Time.mktime(2010,7,29,23,30)
77     assert_equal(Time.mktime(2010,7,30,0,0), @next.call(now))
78   end
79
80   def test_50_min_next_month
81     now = Time.mktime(2009,11,30,23,50)
82     assert_equal(Time.mktime(2009,12,1,0,0), @next.call(now))
83   end
84
85   def test_50_min_next_year
86     now = Time.mktime(2009,12,31,23,50)
87     assert_equal(Time.mktime(2010,1,1,0,0), @next.call(now))
88   end
89
90   def test_50_min_new_year
91     now = Time.mktime(2012,1,1,0,0)
92     assert_equal(Time.mktime(2012,1,1,0,30), @next.call(now))
93   end
94 end
95
96 class TestNextTimeGenerator_3600_0 < Test::Unit::TestCase
97   def setup
98     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0.new
99   end
100
101   def test_22_00
102     now = Time.mktime(2009,12,25,22,0)
103     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
104   end
105
106   def test_22_30
107     now = Time.mktime(2009,12,25,22,0)
108     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
109   end
110
111   def test_23_00
112     now = Time.mktime(2009,12,25,23,0)
113     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
114   end
115
116   def test_23_30
117     now = Time.mktime(2009,12,25,23,30)
118     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
119   end
120
121   def test_00_00
122     now = Time.mktime(2009,12,26,0,0)
123     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
124   end
125
126   def test_23_30_next_month
127     now = Time.mktime(2009,11,30,23,30)
128     assert_equal(Time.mktime(2009,12,1,1,0), @next.call(now))
129   end
130
131   def test_23_30_next_year
132     now = Time.mktime(2009,12,31,23,30)
133     assert_equal(Time.mktime(2010,1,1,1,0), @next.call(now))
134   end
135
136   def test_new_year
137     now = Time.mktime(2012,1,1,0,0)
138     assert_equal(Time.mktime(2012,1,1,1,0), @next.call(now))
139   end
140 end
141
142 class TestNextTimeGeneratorConfig < Test::Unit::TestCase
143   def setup
144   end
145
146   def test_comment
147     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
148     lines = %w(#\ comment1 Thu\ 22:00 #\ comment2)
149     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
150     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
151   end
152
153   def test_empty_line
154     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
155     lines = %w(\  Thu\ 22:00 \  hoge)
156     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
157     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
158   end
159
160   def test_read
161     now = DateTime.new(2010, 6, 10, 21, 20, 15) # Thu
162     assert_equal DateTime.parse("10-06-2010 21:20:15"), now
163
164     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
165     assert_instance_of Time, ntc.call(now)
166     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
167     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:15"]
168     assert_equal Time.parse("10-06-2010 22:15"), ntc.call(now)
169     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Fri 22:00"]
170     assert_equal Time.parse("11-06-2010 22:00"), ntc.call(now)
171     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sat 22:00"]
172     assert_equal Time.parse("12-06-2010 22:00"), ntc.call(now)
173     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sun 22:00"]
174     assert_equal Time.parse("13-06-2010 22:00"), ntc.call(now)
175     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 22:00"]
176     assert_equal Time.parse("14-06-2010 22:00"), ntc.call(now)
177     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 20:00"]
178     assert_equal Time.parse("17-06-2010 20:00"), ntc.call(now)
179   end
180
181   def test_next_year01
182     now = DateTime.new(2011, 12, 30, 21, 20, 15) # Fri
183     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sun 00:00"]
184     assert_equal Time.parse("01-01-2012 00:00"), ntc.call(now)
185     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sun 01:00"]
186     assert_equal Time.parse("01-01-2012 01:00"), ntc.call(now)
187   end
188
189   def test_next_year02
190     now = DateTime.new(2011, 12, 30, 21, 20, 15) # Fri
191     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 00:00"]
192     assert_equal Time.parse("02-01-2012 00:00"), ntc.call(now)
193     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 01:00"]
194     assert_equal Time.parse("02-01-2012 01:00"), ntc.call(now)
195   end
196
197   def test_new_year
198     now = DateTime.new(2012, 1, 1, 1) # Sun; cwyear=2011, cweek=52
199     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 00:00"]
200     assert_equal Time.parse("02-01-2012 00:00"), ntc.call(now)
201     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sat 00:00"]
202     assert_equal Time.parse("07-01-2012 00:00"), ntc.call(now)
203   end
204
205   def test_read_time
206     now = Time.mktime(2010, 6, 10, 21, 20, 15)
207     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
208     assert_instance_of Time, ntc.call(now)
209   end
210
211   def test_read_change
212     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
213     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
214     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
215
216     now = DateTime.new(2010, 6, 10, 22, 0, 0) # Thu
217     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
218     assert_equal Time.parse("17-06-2010 22:00"), ntc.call(now)
219   end
220
221   def test_default_pairing_factory
222     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
223     lines = %w(Thu\ 22:00)
224     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
225     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
226     assert_equal("default_factory", ntc.pairing_factory)
227   end
228
229   def test_read_pairing_factory
230     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
231     lines = %w(set\ pairing_factory\ least_diff_pairing Thu\ 22:00)
232     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
233     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
234     assert_equal("least_diff_pairing", ntc.pairing_factory)
235   end
236
237   def test_default_sacrifice
238     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
239     lines = %w(Thu\ 22:00)
240     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
241     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
242     assert_equal("gps500+e293220e3f8a3e59f79f6b0efffaa931", ntc.sacrifice)
243   end
244
245   def test_read_sacrifice
246     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
247     lines = %w(set\ sacrifice\ yowai_gps+95908f6c18338f5340371f71523fc5e3 Thu\ 22:00)
248     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
249     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
250     assert_equal("yowai_gps+95908f6c18338f5340371f71523fc5e3", ntc.sacrifice)
251   end
252
253   def test_default_max_moves
254     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
255     lines = %w(Thu\ 22:00)
256     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
257     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
258     assert_equal(256, ntc.max_moves)
259   end
260
261   def test_read_max_moves
262     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
263     lines = %w(set\ max_moves\ 200 Thu\ 22:00)
264     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
265     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
266     assert_equal(200, ntc.max_moves)
267   end
268 end