OSDN Git Service

Merge branch '201312-usiToCsa'
[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, {:game_name => "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, {:game_name => "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(2, players.size)
92     assert(players[0] != players[1])
93   end
94 end
95
96 class TestLeastRatePlayer < Test::Unit::TestCase  
97   def setup
98     @pairing= ShogiServer::DeleteLeastRatePlayer.new
99     @a = ShogiServer::BasicPlayer.new
100     @a.win  = 1
101     @a.loss = 2
102     @a.rate = 0
103     @b = ShogiServer::BasicPlayer.new
104     @b.win  = 10
105     @b.loss = 20
106     @b.rate = 1500
107     @c = ShogiServer::BasicPlayer.new
108     @c.win  = 100
109     @c.loss = 200
110     @c.rate = 1000
111   end
112
113  def test_match
114     players = [@a, @b, @c]
115     @pairing.match(players)
116     assert_equal([@b,@c], players)
117   end
118 end
119
120 class TestRandomize < Test::Unit::TestCase  
121   def setup
122     srand(10) # makes the random number generator determistic
123     @pairing = ShogiServer::Randomize.new
124     @a = ShogiServer::BasicPlayer.new
125     @a.name = "a"
126     @a.win  = 1
127     @a.loss = 2
128     @b = ShogiServer::BasicPlayer.new
129     @b.name = "b"
130     @b.win  = 10
131     @b.loss = 20
132     @c = ShogiServer::BasicPlayer.new
133     @c.name = "c"
134     @c.win  = 100
135     @c.loss = 200
136   end
137
138   def test_match
139     players = [@a, @b, @c]
140     @pairing.match(players)
141     assert_equal(3, players.size)
142     assert(players.include? @a)
143     assert(players.include? @b)
144     assert(players.include? @c)
145   end
146 end
147
148 class TestSortByRate < Test::Unit::TestCase  
149   def setup
150     @pairing = ShogiServer::SortByRate.new
151     @a = ShogiServer::BasicPlayer.new
152     @a.name = "a"
153     @a.win  = 1
154     @a.loss = 2
155     @a.rate = 1500
156     @b = ShogiServer::BasicPlayer.new
157     @b.name = "b"
158     @b.win  = 10
159     @b.loss = 20
160     @b.rate = 2000
161     @c = ShogiServer::BasicPlayer.new
162     @c.name = "c"
163     @c.win  = 100
164     @c.loss = 200
165     @c.rate = 700
166   end
167
168   def test_match
169     players = [@a, @b, @c]
170     @pairing.match(players)
171     assert_equal([@c,@a,@b], players)
172   end
173 end
174
175 class TestSortByRateWithRandomness < Test::Unit::TestCase  
176   def setup
177     srand(10) # makes the random number generator determistic
178     @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
179     @a = ShogiServer::BasicPlayer.new
180     @a.name = "a"
181     @a.win  = 1
182     @a.loss = 2
183     @a.rate = 1500
184     @b = ShogiServer::BasicPlayer.new
185     @b.name = "b"
186     @b.win  = 10
187     @b.loss = 20
188     @b.rate = 2000
189     @c = ShogiServer::BasicPlayer.new
190     @c.name = "c"
191     @c.win  = 100
192     @c.loss = 200
193     @c.rate = 700
194   end
195
196   def test_match
197     players = [@a, @b, @c]
198     @pairing.match(players)
199     assert_equal([@c,@b,@a], players)
200   end
201 end
202
203 class TestExcludeSacrifice < Test::Unit::TestCase  
204   def setup
205     @obj = ShogiServer::ExcludeSacrificeGps500.new
206     @a = ShogiServer::BasicPlayer.new
207     @a.player_id   = "a"
208     @a.name = "a"
209     @a.win  = 1
210     @a.loss = 2
211     @a.rate = 0
212     @a.last_game_win = false
213     @b = ShogiServer::BasicPlayer.new
214     @b.player_id   = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
215     @b.name = "gps500"
216     @b.win  = 10
217     @b.loss = 20
218     @b.rate = 1500
219     @b.last_game_win = true
220     @c = ShogiServer::BasicPlayer.new
221     @c.player_id   = "c"
222     @c.name = "c"
223     @c.win  = 100
224     @c.loss = 200
225     @c.rate = 1000
226     @c.last_game_win = true
227   end
228
229   def test_match_1
230     players = [@a]
231     @obj.match(players)
232     assert_equal([@a], players)
233   end
234   
235   def test_match_2
236     players = [@b]
237     @obj.match(players)
238     assert_equal([], players)
239   end
240   
241   def test_match_3
242     players = [@a, @b]
243     @obj.match(players)
244     assert_equal([@a,@b], players)
245   end
246
247   def test_match_4
248     players = [@a, @b, @c]
249     @obj.match(players)
250     assert_equal([@a, @c], players)
251   end
252
253   def test_match_5
254     players = [@a, @c]
255     @obj.match(players)
256     assert_equal([@a,@c], players)
257   end
258 end
259
260 class TestSwissPairing < Test::Unit::TestCase
261   def setup
262     srand(10)
263     @a = ShogiServer::BasicPlayer.new
264     @a.player_id = "a"
265     @a.rate = 0
266     @a.game_name = "floodgate-900-0"
267     @b = ShogiServer::BasicPlayer.new
268     @b.player_id = "b"
269     @b.rate = 1000
270     @b.game_name = "floodgate-900-0"
271     @c = ShogiServer::BasicPlayer.new
272     @c.player_id = "c"
273     @c.rate = 1500
274     @c.game_name = "floodgate-900-0"
275     @d = ShogiServer::BasicPlayer.new
276     @d.player_id = "d"
277     @d.rate = 2000
278     @d.game_name = "floodgate-900-0"
279
280     @players = [@a, @b, @c, @d]
281
282     @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
283     @history = ShogiServer::League::Floodgate::History.factory @file
284
285     @swiss = ShogiServer::Swiss.new
286   end
287
288   def teardown
289     @file.delete if @file.exist?
290   end
291
292   def test_none
293     players = []
294     @swiss.match players
295     assert(players.empty?)
296   end
297
298   def test_all_win
299     ShogiServer::League::Floodgate::History.class_eval do
300       def last_win?(player_id)
301         true
302       end
303     end
304     @swiss.match @players
305     assert_equal([@d, @c, @b, @a], @players)
306   end
307
308   def test_all_lose
309     ShogiServer::League::Floodgate::History.class_eval do
310       def last_win?(player_id)
311         false
312       end
313     end
314     @swiss.match @players
315     assert_equal([@d, @c, @b, @a], @players)
316   end
317
318   def test_one_win
319     ShogiServer::League::Floodgate::History.class_eval do
320       def last_win?(player_id)
321         if player_id == "a"
322           true
323         else
324           false
325         end
326       end
327     end
328     @swiss.match @players
329     assert_equal([@a, @d, @c, @b], @players)
330   end
331
332   def test_two_win
333     ShogiServer::League::Floodgate::History.class_eval do
334       def last_win?(player_id)
335         if player_id == "a" || player_id == "d"
336           true
337         else
338           false
339         end
340       end
341     end
342     @swiss.match @players
343     assert_equal([@d, @a, @c, @b], @players)
344   end
345 end
346
347 class TestFloodgateHistory < Test::Unit::TestCase
348   def setup
349     @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
350     @history = ShogiServer::League::Floodgate::History.new @file
351   end
352
353   def teardown
354     @file.delete if @file.exist?
355   end
356
357   def test_new
358     file = Pathname.new(File.join(File.dirname(__FILE__), "hoge.yaml"))
359     history = ShogiServer::League::Floodgate::History.new file
360     history.save
361     assert file.exist?
362     file.delete if file.exist?
363   end
364
365   def test_update
366     dummy = nil
367     def @history.make_record(game_result)
368       {:game_id => "wdoor+floodgate-900-0-hoge-foo-1", 
369        :black => "hoge",  :white => "foo",
370        :winner => "foo", :loser => "hoge"}
371     end
372     @history.update(dummy)
373
374     def @history.make_record(game_result)
375       {:game_id => "wdoor+floodgate-900-0-hoge-foo-2", 
376        :black => "hoge",  :white => "foo",
377        :winner => "hoge", :loser => "foo"}
378     end
379     @history.update(dummy)
380
381     def @history.make_record(game_result)
382       {:game_id => "wdoor+floodgate-900-0-hoge-foo-3", 
383        :black => "hoge",  :white => "foo",
384        :winner => nil, :loser => nil}
385     end
386     @history.update(dummy)
387
388     @history.load
389     assert_equal 3, @history.records.size
390     assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
391     assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
392     assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
393     assert_equal "hoge", @history.records[1][:black]
394     assert_equal "foo",  @history.records[1][:white]
395     assert_equal "hoge", @history.records[1][:winner]
396     assert_equal "foo",  @history.records[1][:loser]
397
398     assert @history.last_win? "hoge"
399     assert !@history.last_win?("foo")
400     assert !@history.last_lose?("hoge")
401     assert @history.last_lose?("foo")
402
403     assert_equal("foo", @history.last_opponent("hoge"))
404     assert_equal("hoge", @history.last_opponent("foo"))
405
406     games = @history.win_games("hoge")
407     assert_equal(1, games.size )
408     assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
409     games = @history.win_games("foo")
410     assert_equal(1, games.size )
411     assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
412     games = @history.loss_games("hoge")
413     assert_equal(1, games.size )
414     assert_equal("wdoor+floodgate-900-0-hoge-foo-1", games[0][:game_id])
415     games = @history.loss_games("foo")
416     assert_equal(1, games.size )
417     assert_equal("wdoor+floodgate-900-0-hoge-foo-2", games[0][:game_id])
418   end
419 end
420
421