OSDN Git Service

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