OSDN Git Service

shogi_server/floodgate.rb: Generating next time around the new year day by reading...
[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
7 $topdir = File.expand_path File.dirname(__FILE__)
8
9 class TestNextTimeGenerator < Test::Unit::TestCase
10   def setup
11     @game_name = "floodgate-3600-0"
12     @config_path = File.join($topdir, "#{@game_name}.conf")
13   end
14
15   def teardown
16     if File.exist? @config_path
17       FileUtils.rm @config_path
18     end
19   end
20
21   def test_assure_file_does_not_exist
22     assert !File.exist?(@config_path)
23   end
24
25   def test_factory_from_config_file
26     # no config file
27     assert !File.exist?(@config_path)
28     assert_instance_of ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0, 
29                        ShogiServer::League::Floodgate::NextTimeGenerator.factory(@game_name)
30
31     # there is a config file
32     FileUtils.touch(@config_path)
33     assert_instance_of ShogiServer::League::Floodgate::NextTimeGeneratorConfig,
34                        ShogiServer::League::Floodgate::NextTimeGenerator.factory(@game_name)
35   end
36 end
37
38 class TestNextTimeGenerator_900_0 < Test::Unit::TestCase
39   def setup
40     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_900_0.new
41   end
42
43   def test_0_min
44     now = Time.mktime(2009,12,25,22,0)
45     assert_equal(Time.mktime(2009,12,25,22,30), @next.call(now))
46   end
47
48   def test_20_min
49     now = Time.mktime(2009,12,25,22,20)
50     assert_equal(Time.mktime(2009,12,25,22,30), @next.call(now))
51   end
52
53   def test_30_min
54     now = Time.mktime(2009,12,25,22,30)
55     assert_equal(Time.mktime(2009,12,25,23,00), @next.call(now))
56   end
57
58   def test_50_min
59     now = Time.mktime(2009,12,25,22,50)
60     assert_equal(Time.mktime(2009,12,25,23,00), @next.call(now))
61   end
62
63   def test_50_min_next_day
64     now = Time.mktime(2009,12,25,23,50)
65     assert_equal(Time.mktime(2009,12,26,0,0), @next.call(now))
66
67     now = Time.mktime(2010,7,25,23,30)
68     assert_equal(Time.mktime(2010,7,26,0,0), @next.call(now))
69     now = Time.mktime(2010,7,26,23,30)
70     assert_equal(Time.mktime(2010,7,27,0,0), @next.call(now))
71     now = Time.mktime(2010,7,27,23,30)
72     assert_equal(Time.mktime(2010,7,28,0,0), @next.call(now))
73     now = Time.mktime(2010,7,28,23,30)
74     assert_equal(Time.mktime(2010,7,29,0,0), @next.call(now))
75     now = Time.mktime(2010,7,29,23,30)
76     assert_equal(Time.mktime(2010,7,30,0,0), @next.call(now))
77   end
78
79   def test_50_min_next_month
80     now = Time.mktime(2009,11,30,23,50)
81     assert_equal(Time.mktime(2009,12,1,0,0), @next.call(now))
82   end
83
84   def test_50_min_next_year
85     now = Time.mktime(2009,12,31,23,50)
86     assert_equal(Time.mktime(2010,1,1,0,0), @next.call(now))
87   end
88
89   def test_50_min_new_year
90     now = Time.mktime(2012,1,1,0,0)
91     assert_equal(Time.mktime(2012,1,1,0,30), @next.call(now))
92   end
93 end
94
95 class TestNextTimeGenerator_3600_0 < Test::Unit::TestCase
96   def setup
97     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0.new
98   end
99
100   def test_22_00
101     now = Time.mktime(2009,12,25,22,0)
102     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
103   end
104
105   def test_22_30
106     now = Time.mktime(2009,12,25,22,0)
107     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
108   end
109
110   def test_23_00
111     now = Time.mktime(2009,12,25,23,0)
112     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
113   end
114
115   def test_23_30
116     now = Time.mktime(2009,12,25,23,30)
117     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
118   end
119
120   def test_00_00
121     now = Time.mktime(2009,12,26,0,0)
122     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
123   end
124
125   def test_23_30_next_month
126     now = Time.mktime(2009,11,30,23,30)
127     assert_equal(Time.mktime(2009,12,1,1,0), @next.call(now))
128   end
129
130   def test_23_30_next_year
131     now = Time.mktime(2009,12,31,23,30)
132     assert_equal(Time.mktime(2010,1,1,1,0), @next.call(now))
133   end
134
135   def test_new_year
136     now = Time.mktime(2012,1,1,0,0)
137     assert_equal(Time.mktime(2012,1,1,1,0), @next.call(now))
138   end
139 end
140
141 class TestNextTimeGeneratorConfig < Test::Unit::TestCase
142   def setup
143   end
144
145   def test_read
146     now = DateTime.new(2010, 6, 10, 21, 20, 15) # Thu
147     assert_equal DateTime.parse("10-06-2010 21:20:15"), now
148
149     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
150     assert_instance_of Time, ntc.call(now)
151     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
152     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:15"]
153     assert_equal Time.parse("10-06-2010 22:15"), ntc.call(now)
154     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Fri 22:00"]
155     assert_equal Time.parse("11-06-2010 22:00"), ntc.call(now)
156     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sat 22:00"]
157     assert_equal Time.parse("12-06-2010 22:00"), ntc.call(now)
158     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sun 22:00"]
159     assert_equal Time.parse("13-06-2010 22:00"), ntc.call(now)
160     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 22:00"]
161     assert_equal Time.parse("14-06-2010 22:00"), ntc.call(now)
162     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 20:00"]
163     assert_equal Time.parse("17-06-2010 20:00"), ntc.call(now)
164   end
165
166   def test_next_year01
167     now = DateTime.new(2011, 12, 30, 21, 20, 15) # Fri
168     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sun 00:00"]
169     assert_equal Time.parse("01-01-2012 00:00"), ntc.call(now)
170     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sun 01:00"]
171     assert_equal Time.parse("01-01-2012 01:00"), ntc.call(now)
172   end
173
174   def test_next_year02
175     now = DateTime.new(2011, 12, 30, 21, 20, 15) # Fri
176     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 00:00"]
177     assert_equal Time.parse("02-01-2012 00:00"), ntc.call(now)
178     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 01:00"]
179     assert_equal Time.parse("02-01-2012 01:00"), ntc.call(now)
180   end
181
182   def test_new_year
183     now = DateTime.new(2012, 1, 1, 1) # Sun; cwyear=2011, cweek=52
184     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Mon 00:00"]
185     assert_equal Time.parse("02-01-2012 00:00"), ntc.call(now)
186     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Sat 00:00"]
187     assert_equal Time.parse("07-01-2012 00:00"), ntc.call(now)
188   end
189
190   def test_read_time
191     now = Time.mktime(2010, 6, 10, 21, 20, 15)
192     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
193     assert_instance_of Time, ntc.call(now)
194   end
195
196   def test_read_change
197     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
198     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
199     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
200
201     now = DateTime.new(2010, 6, 10, 22, 0, 0) # Thu
202     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
203     assert_equal Time.parse("17-06-2010 22:00"), ntc.call(now)
204   end
205 end