OSDN Git Service

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