OSDN Git Service

shogi_server/{game,time_clock}.rb: Adds variations of thinking time calculation:...
[shogi-server/shogi-server.git] / test / TC_floodgate.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'shogi_server'
4 require 'shogi_server/player'
5 require 'shogi_server/pairing'
6 require 'shogi_server/league/floodgate'
7 require 'test/mock_log_message'
8
9 $topdir = File.expand_path File.dirname(__FILE__)
10
11 class TestFloodgate < Test::Unit::TestCase
12   def setup
13     @fg = ShogiServer::League::Floodgate.new(nil)
14   end
15
16   def teardown
17
18   end
19
20   def test_game_name
21     assert(ShogiServer::League::Floodgate.game_name?("floodgate-900-0"))
22     assert(ShogiServer::League::Floodgate.game_name?("floodgate-0-10"))
23     assert(!ShogiServer::League::Floodgate.game_name?("floodgat-900-0"))
24   end
25
26   def test_instance_game_name
27     fg = ShogiServer::League::Floodgate.new(nil, "floodgate-900-0")
28     assert(fg.game_name?("floodgate-900-0"))
29     assert(!fg.game_name?("floodgate-3600-0"))
30     fg = ShogiServer::League::Floodgate.new(nil, "floodgate-3600-0")
31     assert(fg.game_name?("floodgate-900-0"))
32     assert(!fg.game_name?("floodgate-3600-0"))
33   end
34
35 end
36
37 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
38   def setup
39     @pairing= ShogiServer::DeleteMostPlayingPlayer.new
40     @a = ShogiServer::BasicPlayer.new
41     @a.win  = 1
42     @a.loss = 2
43     @a.rate = 0
44     @b = ShogiServer::BasicPlayer.new
45     @b.win  = 10
46     @b.loss = 20
47     @b.rate = 1500
48     @c = ShogiServer::BasicPlayer.new
49     @c.win  = 100
50     @c.loss = 200
51     @c.rate = 1000
52   end
53
54   def test_match
55     players = [@a, @b, @c]
56     @pairing.match(players)
57     assert_equal([@a,@b], players)
58   end
59 end
60
61 class TestMakeEven < Test::Unit::TestCase  
62   def setup
63     srand(10)
64     @pairing= ShogiServer::MakeEven.new
65     @a = ShogiServer::BasicPlayer.new
66     @a.name = "a"
67     @a.win  = 1
68     @a.loss = 2
69     @a.rate = 0
70     @b = ShogiServer::BasicPlayer.new
71     @b.name = "b"
72     @b.win  = 10
73     @b.loss = 20
74     @b.rate = 1500
75     @c = ShogiServer::BasicPlayer.new
76     @c.name = "c"
77     @c.win  = 100
78     @c.loss = 200
79     @c.rate = 1000
80   end
81
82  def test_match_even
83     players = [@a, @b]
84     @pairing.match(players)
85     assert_equal([@a,@b], players)
86  end
87
88  def test_match_odd
89     players = [@a, @b, @c]
90     @pairing.match(players)
91     assert_equal([@a, @b], players)
92   end
93 end
94
95 class TestLeastRatePlayer < Test::Unit::TestCase  
96   def setup
97     @pairing= ShogiServer::DeleteLeastRatePlayer.new
98     @a = ShogiServer::BasicPlayer.new
99     @a.win  = 1
100     @a.loss = 2
101     @a.rate = 0
102     @b = ShogiServer::BasicPlayer.new
103     @b.win  = 10
104     @b.loss = 20
105     @b.rate = 1500
106     @c = ShogiServer::BasicPlayer.new
107     @c.win  = 100
108     @c.loss = 200
109     @c.rate = 1000
110   end
111
112  def test_match
113     players = [@a, @b, @c]
114     @pairing.match(players)
115     assert_equal([@b,@c], players)
116   end
117 end
118
119 class TestRandomize < Test::Unit::TestCase  
120   def setup
121     srand(10) # makes the random number generator determistic
122     @pairing = ShogiServer::Randomize.new
123     @a = ShogiServer::BasicPlayer.new
124     @a.name = "a"
125     @a.win  = 1
126     @a.loss = 2
127     @b = ShogiServer::BasicPlayer.new
128     @b.name = "b"
129     @b.win  = 10
130     @b.loss = 20
131     @c = ShogiServer::BasicPlayer.new
132     @c.name = "c"
133     @c.win  = 100
134     @c.loss = 200
135   end
136
137   def test_match
138     players = [@a, @b, @c]
139     @pairing.match(players)
140     assert_equal([@b,@a,@c], players)
141   end
142 end
143
144 class TestSortByRate < Test::Unit::TestCase  
145   def setup
146     @pairing = ShogiServer::SortByRate.new
147     @a = ShogiServer::BasicPlayer.new
148     @a.name = "a"
149     @a.win  = 1
150     @a.loss = 2
151     @a.rate = 1500
152     @b = ShogiServer::BasicPlayer.new
153     @b.name = "b"
154     @b.win  = 10
155     @b.loss = 20
156     @b.rate = 2000
157     @c = ShogiServer::BasicPlayer.new
158     @c.name = "c"
159     @c.win  = 100
160     @c.loss = 200
161     @c.rate = 700
162   end
163
164   def test_match
165     players = [@a, @b, @c]
166     @pairing.match(players)
167     assert_equal([@c,@a,@b], players)
168   end
169 end
170
171 class TestSortByRateWithRandomness < Test::Unit::TestCase  
172   def setup
173     srand(10) # makes the random number generator determistic
174     @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
175     @a = ShogiServer::BasicPlayer.new
176     @a.name = "a"
177     @a.win  = 1
178     @a.loss = 2
179     @a.rate = 1500
180     @b = ShogiServer::BasicPlayer.new
181     @b.name = "b"
182     @b.win  = 10
183     @b.loss = 20
184     @b.rate = 2000
185     @c = ShogiServer::BasicPlayer.new
186     @c.name = "c"
187     @c.win  = 100
188     @c.loss = 200
189     @c.rate = 700
190   end
191
192   def test_match
193     players = [@a, @b, @c]
194     @pairing.match(players)
195     assert_equal([@c,@b,@a], players)
196   end
197 end
198
199 class TestExcludeSacrifice < Test::Unit::TestCase  
200   def setup
201     @obj = ShogiServer::ExcludeSacrificeGps500.new
202     @a = ShogiServer::BasicPlayer.new
203     @a.player_id   = "a"
204     @a.name = "a"
205     @a.win  = 1
206     @a.loss = 2
207     @a.rate = 0
208     @a.last_game_win = false
209     @b = ShogiServer::BasicPlayer.new
210     @b.player_id   = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
211     @b.name = "gps500"
212     @b.win  = 10
213     @b.loss = 20
214     @b.rate = 1500
215     @b.last_game_win = true
216     @c = ShogiServer::BasicPlayer.new
217     @c.player_id   = "c"
218     @c.name = "c"
219     @c.win  = 100
220     @c.loss = 200
221     @c.rate = 1000
222     @c.last_game_win = true
223   end
224
225   def test_match_1
226     players = [@a]
227     @obj.match(players)
228     assert_equal([@a], players)
229   end
230   
231   def test_match_2
232     players = [@b]
233     @obj.match(players)
234     assert_equal([], players)
235   end
236   
237   def test_match_3
238     players = [@a, @b]
239     @obj.match(players)
240     assert_equal([@a,@b], players)
241   end
242
243   def test_match_4
244     players = [@a, @b, @c]
245     @obj.match(players)
246     assert_equal([@a, @c], players)
247   end
248
249   def test_match_5
250     players = [@a, @c]
251     @obj.match(players)
252     assert_equal([@a,@c], players)
253   end
254 end
255
256 class TestSwissPairing < Test::Unit::TestCase
257   def setup
258     srand(10)
259     @a = ShogiServer::BasicPlayer.new
260     @a.player_id = "a"
261     @a.rate = 0
262     @a.game_name = "floodgate-900-0"
263     @b = ShogiServer::BasicPlayer.new
264     @b.player_id = "b"
265     @b.rate = 1000
266     @b.game_name = "floodgate-900-0"
267     @c = ShogiServer::BasicPlayer.new
268     @c.player_id = "c"
269     @c.rate = 1500
270     @c.game_name = "floodgate-900-0"
271     @d = ShogiServer::BasicPlayer.new
272     @d.player_id = "d"
273     @d.rate = 2000
274     @d.game_name = "floodgate-900-0"
275
276     @players = [@a, @b, @c, @d]
277
278     @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
279     @history = ShogiServer::League::Floodgate::History.factory @file
280
281     @swiss = ShogiServer::Swiss.new
282   end
283
284   def teardown
285     @file.delete if @file.exist?
286   end
287
288   def test_none
289     players = []
290     @swiss.match players
291     assert(players.empty?)
292   end
293
294   def test_all_win
295     ShogiServer::League::Floodgate::History.class_eval do
296       def last_win?(player_id)
297         true
298       end
299     end
300     @swiss.match @players
301     assert_equal([@d, @c, @b, @a], @players)
302   end
303
304   def test_all_lose
305     ShogiServer::League::Floodgate::History.class_eval do
306       def last_win?(player_id)
307         false
308       end
309     end
310     @swiss.match @players
311     assert_equal([@d, @c, @b, @a], @players)
312   end
313
314   def test_one_win
315     ShogiServer::League::Floodgate::History.class_eval do
316       def last_win?(player_id)
317         if player_id == "a"
318           true
319         else
320           false
321         end
322       end
323     end
324     @swiss.match @players
325     assert_equal([@a, @d, @c, @b], @players)
326   end
327
328   def test_two_win
329     ShogiServer::League::Floodgate::History.class_eval do
330       def last_win?(player_id)
331         if player_id == "a" || player_id == "d"
332           true
333         else
334           false
335         end
336       end
337     end
338     @swiss.match @players
339     assert_equal([@d, @a, @c, @b], @players)
340   end
341 end
342
343 class TestFloodgateHistory < Test::Unit::TestCase
344   def setup
345     @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
346     @history = ShogiServer::League::Floodgate::History.new @file
347   end
348
349   def teardown
350     @file.delete if @file.exist?
351   end
352
353   def test_new
354     file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
355     history = ShogiServer::League::Floodgate::History.new file
356     history.save
357     assert file.exist?
358     file.delete if file.exist?
359   end
360
361   def test_update
362     dummy = nil
363     def @history.make_record(game_result)
364       {:game_id => "wdoor+floodgate-900-0-hoge-foo-1", 
365        :black => "hoge",  :white => "foo",
366        :winner => "foo", :loser => "hoge"}
367     end
368     @history.update(dummy)
369
370     def @history.make_record(game_result)
371       {:game_id => "wdoor+floodgate-900-0-hoge-foo-2", 
372        :black => "hoge",  :white => "foo",
373        :winner => "hoge", :loser => "foo"}
374     end
375     @history.update(dummy)
376
377     def @history.make_record(game_result)
378       {:game_id => "wdoor+floodgate-900-0-hoge-foo-3", 
379        :black => "hoge",  :white => "foo",
380        :winner => nil, :loser => nil}
381     end
382     @history.update(dummy)
383
384     @history.load
385     assert_equal 3, @history.records.size
386     assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
387     assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
388     assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
389     assert_equal "hoge", @history.records[1][:black]
390     assert_equal "foo",  @history.records[1][:white]
391     assert_equal "hoge", @history.records[1][:winner]
392     assert_equal "foo",  @history.records[1][:loser]
393
394     assert @history.last_win? "hoge"
395     assert !@history.last_win?("foo")
396     assert !@history.last_lose?("hoge")
397     assert @history.last_lose?("foo")
398   end
399 end
400
401