OSDN Git Service

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