OSDN Git Service

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