OSDN Git Service

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