OSDN Git Service

Refactored MockLogger class.
[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 end
27
28 class TestDeleteMostPlayingPlayer < Test::Unit::TestCase
29   def setup
30     @pairing= ShogiServer::DeleteMostPlayingPlayer.new
31     @a = ShogiServer::BasicPlayer.new
32     @a.win  = 1
33     @a.loss = 2
34     @a.rate = 0
35     @b = ShogiServer::BasicPlayer.new
36     @b.win  = 10
37     @b.loss = 20
38     @b.rate = 1500
39     @c = ShogiServer::BasicPlayer.new
40     @c.win  = 100
41     @c.loss = 200
42     @c.rate = 1000
43   end
44
45   def test_match
46     players = [@a, @b, @c]
47     @pairing.match(players)
48     assert_equal([@a,@b], players)
49   end
50 end
51
52 class TestMakeEven < Test::Unit::TestCase  
53   def setup
54     srand(10)
55     @pairing= ShogiServer::MakeEven.new
56     @a = ShogiServer::BasicPlayer.new
57     @a.name = "a"
58     @a.win  = 1
59     @a.loss = 2
60     @a.rate = 0
61     @b = ShogiServer::BasicPlayer.new
62     @b.name = "b"
63     @b.win  = 10
64     @b.loss = 20
65     @b.rate = 1500
66     @c = ShogiServer::BasicPlayer.new
67     @c.name = "c"
68     @c.win  = 100
69     @c.loss = 200
70     @c.rate = 1000
71   end
72
73  def test_match_even
74     players = [@a, @b]
75     @pairing.match(players)
76     assert_equal([@a,@b], players)
77  end
78
79  def test_match_odd
80     players = [@a, @b, @c]
81     @pairing.match(players)
82     assert_equal([@a, @b], players)
83   end
84 end
85
86 class TestLeastRatePlayer < Test::Unit::TestCase  
87   def setup
88     @pairing= ShogiServer::DeleteLeastRatePlayer.new
89     @a = ShogiServer::BasicPlayer.new
90     @a.win  = 1
91     @a.loss = 2
92     @a.rate = 0
93     @b = ShogiServer::BasicPlayer.new
94     @b.win  = 10
95     @b.loss = 20
96     @b.rate = 1500
97     @c = ShogiServer::BasicPlayer.new
98     @c.win  = 100
99     @c.loss = 200
100     @c.rate = 1000
101   end
102
103  def test_match
104     players = [@a, @b, @c]
105     @pairing.match(players)
106     assert_equal([@b,@c], players)
107   end
108 end
109
110 class TestRandomize < Test::Unit::TestCase  
111   def setup
112     srand(10) # makes the random number generator determistic
113     @pairing = ShogiServer::Randomize.new
114     @a = ShogiServer::BasicPlayer.new
115     @a.name = "a"
116     @a.win  = 1
117     @a.loss = 2
118     @b = ShogiServer::BasicPlayer.new
119     @b.name = "b"
120     @b.win  = 10
121     @b.loss = 20
122     @c = ShogiServer::BasicPlayer.new
123     @c.name = "c"
124     @c.win  = 100
125     @c.loss = 200
126   end
127
128   def test_match
129     players = [@a, @b, @c]
130     @pairing.match(players)
131     assert_equal([@b,@a,@c], players)
132   end
133 end
134
135 class TestSortByRate < Test::Unit::TestCase  
136   def setup
137     @pairing = ShogiServer::SortByRate.new
138     @a = ShogiServer::BasicPlayer.new
139     @a.name = "a"
140     @a.win  = 1
141     @a.loss = 2
142     @a.rate = 1500
143     @b = ShogiServer::BasicPlayer.new
144     @b.name = "b"
145     @b.win  = 10
146     @b.loss = 20
147     @b.rate = 2000
148     @c = ShogiServer::BasicPlayer.new
149     @c.name = "c"
150     @c.win  = 100
151     @c.loss = 200
152     @c.rate = 700
153   end
154
155   def test_match
156     players = [@a, @b, @c]
157     @pairing.match(players)
158     assert_equal([@c,@a,@b], players)
159   end
160 end
161
162 class TestSortByRateWithRandomness < Test::Unit::TestCase  
163   def setup
164     srand(10) # makes the random number generator determistic
165     @pairing = ShogiServer::SortByRateWithRandomness.new(1200, 2400)
166     @a = ShogiServer::BasicPlayer.new
167     @a.name = "a"
168     @a.win  = 1
169     @a.loss = 2
170     @a.rate = 1500
171     @b = ShogiServer::BasicPlayer.new
172     @b.name = "b"
173     @b.win  = 10
174     @b.loss = 20
175     @b.rate = 2000
176     @c = ShogiServer::BasicPlayer.new
177     @c.name = "c"
178     @c.win  = 100
179     @c.loss = 200
180     @c.rate = 700
181   end
182
183   def test_match
184     players = [@a, @b, @c]
185     @pairing.match(players)
186     assert_equal([@c,@b,@a], players)
187   end
188 end
189
190 class TestExcludeSacrifice < Test::Unit::TestCase  
191   def setup
192     @obj = ShogiServer::ExcludeSacrificeGps500.new
193     @a = ShogiServer::BasicPlayer.new
194     @a.player_id   = "a"
195     @a.name = "a"
196     @a.win  = 1
197     @a.loss = 2
198     @a.rate = 0
199     @a.last_game_win = false
200     @b = ShogiServer::BasicPlayer.new
201     @b.player_id   = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
202     @b.name = "gps500"
203     @b.win  = 10
204     @b.loss = 20
205     @b.rate = 1500
206     @b.last_game_win = true
207     @c = ShogiServer::BasicPlayer.new
208     @c.player_id   = "c"
209     @c.name = "c"
210     @c.win  = 100
211     @c.loss = 200
212     @c.rate = 1000
213     @c.last_game_win = true
214   end
215
216   def test_match_1
217     players = [@a]
218     @obj.match(players)
219     assert_equal([@a], players)
220   end
221   
222   def test_match_2
223     players = [@b]
224     @obj.match(players)
225     assert_equal([], players)
226   end
227   
228   def test_match_3
229     players = [@a, @b]
230     @obj.match(players)
231     assert_equal([@a,@b], players)
232   end
233
234   def test_match_4
235     players = [@a, @b, @c]
236     @obj.match(players)
237     assert_equal([@a, @c], players)
238   end
239
240   def test_match_5
241     players = [@a, @c]
242     @obj.match(players)
243     assert_equal([@a,@c], players)
244   end
245 end
246
247 class TestSwissPairing < Test::Unit::TestCase
248   def setup
249     srand(10)
250     @a = ShogiServer::BasicPlayer.new
251     @a.player_id = "a"
252     @a.rate = 0
253     @a.game_name = "floodgate-900-0"
254     @b = ShogiServer::BasicPlayer.new
255     @b.player_id = "b"
256     @b.rate = 1000
257     @b.game_name = "floodgate-900-0"
258     @c = ShogiServer::BasicPlayer.new
259     @c.player_id = "c"
260     @c.rate = 1500
261     @c.game_name = "floodgate-900-0"
262     @d = ShogiServer::BasicPlayer.new
263     @d.player_id = "d"
264     @d.rate = 2000
265     @d.game_name = "floodgate-900-0"
266
267     @players = [@a, @b, @c, @d]
268
269     @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history_900_0.yaml"))
270     @history = ShogiServer::League::Floodgate::History.factory @file
271
272     @swiss = ShogiServer::Swiss.new
273   end
274
275   def teardown
276     @file.delete if @file.exist?
277   end
278
279   def test_none
280     players = []
281     @swiss.match players
282     assert(players.empty?)
283   end
284
285   def test_all_win
286     ShogiServer::League::Floodgate::History.class_eval do
287       def last_win?(player_id)
288         true
289       end
290     end
291     @swiss.match @players
292     assert_equal([@d, @c, @b, @a], @players)
293   end
294
295   def test_all_lose
296     ShogiServer::League::Floodgate::History.class_eval do
297       def last_win?(player_id)
298         false
299       end
300     end
301     @swiss.match @players
302     assert_equal([@d, @c, @b, @a], @players)
303   end
304
305   def test_one_win
306     ShogiServer::League::Floodgate::History.class_eval do
307       def last_win?(player_id)
308         if player_id == "a"
309           true
310         else
311           false
312         end
313       end
314     end
315     @swiss.match @players
316     assert_equal([@a, @d, @c, @b], @players)
317   end
318
319   def test_two_win
320     ShogiServer::League::Floodgate::History.class_eval do
321       def last_win?(player_id)
322         if player_id == "a" || player_id == "d"
323           true
324         else
325           false
326         end
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