OSDN Git Service

90cea15b627ccefa80fee34b31706ee826bd4a3c
[shogi-server/shogi-server.git] / test / TC_pairing.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'shogi_server'
4 require 'shogi_server/league.rb'
5 require 'shogi_server/player'
6 require 'shogi_server/pairing'
7 require 'test/mock_log_message'
8
9 def same_pair?(a, b)
10   unless a.size == 2 && b.size == 2
11     return false
12   end
13
14   return true if [a.first, a.last] == b || [a.last, a.first] == b
15 end
16
17 class TestPairing < Test::Unit::TestCase  
18   def setup
19     @pairing= ShogiServer::Pairing.new
20     $pairs = []
21     def @pairing.start_game(p1,p2)
22       $pairs << [p1,p2]
23     end
24     @a = ShogiServer::BasicPlayer.new
25     @a.name = "a"
26     @a.win  = 1
27     @a.loss = 2
28     @a.rate = 0
29     @a.last_game_win = false
30     @b = ShogiServer::BasicPlayer.new
31     @b.name = "b"
32     @b.win  = 10
33     @b.loss = 20
34     @b.rate = 1500
35     @b.last_game_win = true
36     @c = ShogiServer::BasicPlayer.new
37     @c.name = "c"
38     @c.win  = 100
39     @c.loss = 200
40     @c.rate = 1000
41     @c.last_game_win = true
42     @d = ShogiServer::BasicPlayer.new
43     @d.name = "d"
44     @d.win  = 1000
45     @d.loss = 2000
46     @d.rate = 1800
47     @d.last_game_win = true
48   end
49
50   def test_include_newbie
51     assert(@pairing.include_newbie?([@a]))
52     assert(!@pairing.include_newbie?([@b]))
53     assert(@pairing.include_newbie?([@b,@a]))
54     assert(!@pairing.include_newbie?([@b,@c]))
55   end
56 end
57
58 class TestStartGame < Test::Unit::TestCase
59   def setup
60     @pairing= ShogiServer::StartGame.new
61     $called = 0
62     def @pairing.start_game(p1,p2)
63       $called += 1
64     end
65     @a = ShogiServer::BasicPlayer.new
66     @a.name = "a"
67     @a.win  = 1
68     @a.loss = 2
69     @a.rate = 0
70     @b = ShogiServer::BasicPlayer.new
71     @b.name = "b"
72     @b.win  = 10
73     @b.loss = 20
74     @b.rate = 1500
75     @c = ShogiServer::BasicPlayer.new
76     @c.name = "c"
77     @c.win  = 100
78     @c.loss = 200
79     @c.rate = 1000
80     @d = ShogiServer::BasicPlayer.new
81     @d.name = "d"
82     @d.win  = 1000
83     @d.loss = 2000
84     @d.rate = 2000
85   end
86
87   def test_match_two_players
88     players = [@a,@b]
89     @pairing.match(players)
90     assert_equal(1, $called)
91   end
92
93   def test_match_one_player
94     players = [@a]
95     @pairing.match(players)
96     assert_equal(0, $called)
97   end
98
99   def test_match_zero_player
100     players = []
101     @pairing.match(players)
102     assert_equal(0, $called)
103   end
104
105   def test_match_three_players
106     players = [@a,@b,@c]
107     @pairing.match(players)
108     assert_equal(1, $called)
109   end
110
111   def test_match_four_players
112     players = [@a,@b,@c,@d]
113     @pairing.match(players)
114     assert_equal(2, $called)
115   end
116 end
117
118 class TestStartGameWithoutHumans < Test::Unit::TestCase
119   def setup
120     @pairing= ShogiServer::StartGameWithoutHumans.new
121     $paired = []
122     $called = 0
123     def @pairing.start_game(p1,p2)
124       $called += 1
125       $paired << [p1,p2]
126     end
127     @a = ShogiServer::BasicPlayer.new
128     @a.name = "a"
129     @a.win  = 1
130     @a.loss = 2
131     @a.rate = 0
132     @b = ShogiServer::BasicPlayer.new
133     @b.name = "b"
134     @b.win  = 10
135     @b.loss = 20
136     @b.rate = 1500
137     @c = ShogiServer::BasicPlayer.new
138     @c.name = "c"
139     @c.win  = 100
140     @c.loss = 200
141     @c.rate = 1000
142     @d = ShogiServer::BasicPlayer.new
143     @d.name = "d"
144     @d.win  = 1000
145     @d.loss = 2000
146     @d.rate = 2000
147     @e = ShogiServer::BasicPlayer.new
148     @e.name = "e"
149     @e.win  = 3000
150     @e.loss = 3000
151     @e.rate = 3000
152     @f = ShogiServer::BasicPlayer.new
153     @f.name = "f"
154     @f.win  = 4000
155     @f.loss = 4000
156     @f.rate = 4000
157     @g = ShogiServer::BasicPlayer.new
158     @g.name = "g"
159     @g.win  = 5000
160     @g.loss = 5000
161     @g.rate = 5000
162     @h = ShogiServer::BasicPlayer.new
163     @h.name = "h"
164     @h.win  = 6000
165     @h.loss = 6000
166     @h.rate = 6000
167   end
168
169   def test_match_one_player
170     players = [@a]
171     @pairing.match(players)
172     assert_equal(0, $called)
173   end
174
175   def test_match_one_player_human
176     @a.name += "_human"
177     players = [@a]
178     @pairing.match(players)
179     assert_equal(0, $called)
180   end
181
182   def test_match_two_players
183     players = [@a,@b]
184     @pairing.match(players)
185     assert_equal(1, $called)
186   end
187
188   def test_match_two_players_humans
189     @a.name += "_human"
190     @b.name += "_human"
191     players = [@a,@b]
192     @pairing.match(players)
193     assert_equal(1, $called)
194   end
195
196   def test_match_zero_player
197     players = []
198     @pairing.match(players)
199     assert_equal(0, $called)
200   end
201
202   def test_match_three_players
203     players = [@a,@b,@c]
204     @pairing.match(players)
205     assert_equal(1, $called)
206   end
207
208   def test_match_three_players_a_human
209     @a.name += "_human"
210     players = [@a,@b,@c]
211     @pairing.match(players)
212     assert_equal(1, $called)
213     assert_equal(1, players.size)
214     assert_equal(@c, players[0])
215   end
216
217   def test_match_three_players_b_human
218     @b.name += "_human"
219     players = [@a,@b,@c]
220     @pairing.match(players)
221     assert_equal(1, $called)
222     assert_equal(1, players.size)
223     assert_equal(@c, players[0])
224   end
225
226   def test_match_three_players_c_human
227     @c.name += "_human"
228     players = [@a,@b,@c]
229     @pairing.match(players)
230     assert_equal(1, $called)
231     assert_equal(1, players.size)
232     assert_equal(@c, players[0])
233   end
234
235   def test_match_three_players_ab_human
236     @a.name += "_human"
237     @b.name += "_human"
238     players = [@a,@b,@c]
239     @pairing.match(players)
240     assert_equal(1, $called)
241     assert_equal(1, players.size)
242     assert_equal(@b, players[0])
243   end
244
245   def test_match_three_players_bc_human
246     @b.name += "_human"
247     @c.name += "_human"
248     players = [@a,@b,@c]
249     @pairing.match(players)
250     assert_equal(1, $called)
251     assert_equal(1, players.size)
252     assert_equal(@c, players[0])
253   end
254
255   def test_match_four_players
256     players = [@a,@b,@c,@d]
257     @pairing.match(players)
258     assert_equal(2, $called)
259   end
260
261   def test_match_four_players_ab_human
262     @a.name += "_human"
263     @b.name += "_human"
264     players = [@a,@b,@c,@d]
265     @pairing.match(players)
266     assert_equal(2, $paired.size)
267     assert(same_pair?([@a,@c], $paired[0]))
268     assert(same_pair?([@b,@d], $paired[1]))
269   end
270
271   def test_match_four_players_bc_human
272     @b.name += "_human"
273     @c.name += "_human"
274     players = [@a,@b,@c,@d]
275     @pairing.match(players)
276     assert_equal(2, $paired.size)
277     assert(same_pair?([@a,@b], $paired[0]))
278     assert(same_pair?([@c,@d], $paired[1]))
279   end
280
281   def test_match_four_players_abc_human
282     @a.name += "_human"
283     @b.name += "_human"
284     @c.name += "_human"
285     players = [@a,@b,@c,@d]
286     @pairing.match(players)
287     assert_equal(2, $paired.size)
288     assert(same_pair?([@a,@d], $paired[0]))
289     assert(same_pair?([@b,@c], $paired[1]))
290   end
291
292   def test_match_four_players_bcd_human
293     @b.name += "_human"
294     @c.name += "_human"
295     @d.name += "_human"
296     players = [@a,@b,@c,@d]
297     @pairing.match(players)
298     assert_equal(2, $paired.size)
299     assert(same_pair?([@a,@c], $paired[0]))
300     assert(same_pair?([@b,@d], $paired[1]))
301   end
302
303   def test_match_four_players_abcd_human
304     @a.name += "_human"
305     @b.name += "_human"
306     @c.name += "_human"
307     @d.name += "_human"
308     players = [@a,@b,@c,@d]
309     @pairing.match(players)
310     assert_equal(2, $paired.size)
311     assert(same_pair?([@a,@b], $paired[0]))
312     assert(same_pair?([@c,@d], $paired[1]))
313   end
314
315   def test_match_eight_players_efgh_human
316     @e.name += "_human"
317     @f.name += "_human"
318     @g.name += "_human"
319     @h.name += "_human"
320     players = [@a,@b,@c,@d,@e,@f,@g,@h]
321     @pairing.match(players)
322     assert_equal(4, $paired.size)
323     assert(same_pair?([@e,@c], $paired[0]))
324     assert(same_pair?([@d,@g], $paired[1]))
325     assert(same_pair?([@a,@f], $paired[2]))
326     assert(same_pair?([@b,@h], $paired[3]))
327   end
328 end
329
330 class TestLeastDiff < Test::Unit::TestCase
331
332   class MockLeague
333     def initialize
334       @players = []
335     end
336
337     def add(player)
338       @players << player
339     end
340
341     def find(name)
342       @players.find do |p|
343         p.name == name
344       end
345     end
346   end
347
348   def setup
349     $league = MockLeague.new
350
351     @pairing= ShogiServer::LeastDiff.new
352     $paired = []
353     $called = 0
354     def @pairing.start_game(p1,p2)
355       $called += 1
356       $paired << [p1,p2]
357     end
358
359     @file = Pathname.new(File.join(File.dirname(__FILE__), "floodgate_history.yaml"))
360     @history = ShogiServer::League::Floodgate::History.new @file
361
362     @a = ShogiServer::BasicPlayer.new
363     @a.player_id = "a"
364     @a.name = "a"
365     @a.win  = 1
366     @a.loss = 2
367     @a.rate = 500
368     @b = ShogiServer::BasicPlayer.new
369     @b.player_id = "b"
370     @b.name = "b"
371     @b.win  = 10
372     @b.loss = 20
373     @b.rate = 800
374     @c = ShogiServer::BasicPlayer.new
375     @c.player_id = "c"
376     @c.name = "c"
377     @c.win  = 100
378     @c.loss = 200
379     @c.rate = 1000
380     @d = ShogiServer::BasicPlayer.new
381     @d.player_id = "d"
382     @d.name = "d"
383     @d.win  = 1000
384     @d.loss = 2000
385     @d.rate = 1500
386     @e = ShogiServer::BasicPlayer.new
387     @e.player_id = "e"
388     @e.name = "e"
389     @e.win  = 3000
390     @e.loss = 3000
391     @e.rate = 2000
392     @f = ShogiServer::BasicPlayer.new
393     @f.player_id = "f"
394     @f.name = "f"
395     @f.win  = 4000
396     @f.loss = 4000
397     @f.rate = 2150
398     @g = ShogiServer::BasicPlayer.new
399     @g.player_id = "g"
400     @g.name = "g"
401     @g.win  = 5000
402     @g.loss = 5000
403     @g.rate = 2500
404     @h = ShogiServer::BasicPlayer.new
405     @h.player_id = "h"
406     @h.name = "h"
407     @h.win  = 6000
408     @h.loss = 6000
409     @h.rate = 3000
410     @x = ShogiServer::BasicPlayer.new
411     @x.player_id = "x"
412     @x.name = "x"
413
414     $league.add(@a)
415     $league.add(@b)
416     $league.add(@c)
417     $league.add(@d)
418     $league.add(@e)
419     $league.add(@f)
420     $league.add(@g)
421     $league.add(@h)
422     $league.add(@x)
423   end
424
425   def teardown
426     @file.delete if @file.exist?
427   end
428
429   def assert_pairs(x_array, y_array)
430     if (x_array.size != y_array.size)
431       assert_equal(x_array.size, y_array.size)
432       return
433     end
434     i = 0
435
436     if (x_array.size == 1)
437       assert_equal(x_array[0].name, y_array[0].name)
438       return
439     end
440
441     ret = true
442     while i < x_array.size
443       if i == x_array.size-1
444         assert_equal(x_array[i].name, y_array[i].name)
445         break
446       end
447       px1 = x_array[i]
448       px2 = x_array[i+1]
449       py1 = y_array[i]
450       py2 = y_array[i+1]
451
452       if ! ((px1.name == py1.name && px2.name == py2.name) ||
453             (px1.name == py2.name && px2.name == py1.name))
454         ret = false
455       end
456       i += 2
457     end
458
459     assert(ret)
460   end
461
462   def test_match_one_player
463     players = [@a]
464     assert_equal(0, @pairing.calculate_diff_with_penalty(players,nil))
465     r = @pairing.match(players)
466     assert_pairs([@a], r)
467   end
468
469   def test_match_two_players
470     players = [@a,@b]
471     assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty([@a,@b],nil))
472     assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty([@b,@a],nil))
473     r = @pairing.match(players)
474     assert_pairs([@a,@b], r)
475   end
476
477   def test_match_three_players
478     players = [@h,@a,@b]
479     assert_equal(300,  @pairing.calculate_diff_with_penalty([@a,@b,@h],nil))
480     assert_equal(2200, @pairing.calculate_diff_with_penalty([@b,@h,@a],nil))
481     r = @pairing.match(players)
482     assert_pairs([@a,@b,@h], r)
483     assert_pairs([@a,@b,@h], players)
484   end
485
486   def test_calculate_diff_with_penalty
487     players = [@a,@b]
488     assert_equal(@b.rate-@a.rate, @pairing.calculate_diff_with_penalty(players,nil))
489
490     dummy = nil
491     def @history.make_record(game_result)
492       {:game_id => "wdoor+floodgate-900-0-a-b-1", 
493        :black => "b",  :white => "a",
494        :winner => "a", :loser => "b"}
495     end
496     @history.update(dummy)
497     assert_equal(@b.rate-@a.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
498   end
499
500   def test_calculate_diff_with_penalty2
501     players = [@a,@b,@g,@h]
502     assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
503   end
504
505   def test_calculate_diff_with_penalty2_1
506     players = [@a,@b,@g,@h]
507     assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
508     dummy = nil
509     def @history.make_record(game_result)
510       {:game_id => "wdoor+floodgate-900-0-a-b-1", 
511        :black => "b",  :white => "a",
512        :winner => "a", :loser => "b"}
513     end
514     @history.update(dummy)
515     assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players, @history))
516   end
517
518   def test_calculate_diff_with_penalty2_2
519     players = [@a,@b,@g,@h]
520     assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
521     dummy = nil
522     def @history.make_record(game_result)
523       {:game_id => "wdoor+floodgate-900-0-a-b-1", 
524        :black => "g",  :white => "h",
525        :winner => "h", :loser => "g"}
526     end
527     @history.update(dummy)
528     assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players, @history))
529     #assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, [@b,@a,@h,@g]))
530   end
531
532   def test_calculate_diff_with_penalty2_3
533     players = [@a,@b,@g,@h]
534     assert_equal(@b.rate-@a.rate+@h.rate-@g.rate, @pairing.calculate_diff_with_penalty(players,nil))
535     dummy = nil
536     def @history.make_record(game_result)
537       {:game_id => "wdoor+floodgate-900-0-a-b-1", 
538        :black => "g",  :white => "h",
539        :winner => "h", :loser => "g"}
540     end
541     @history.update(dummy)
542     def @history.make_record(game_result)
543       {:game_id => "wdoor+floodgate-900-0-a-b-1", 
544        :black => "b",  :white => "a",
545        :winner => "a", :loser => "b"}
546     end
547     @history.update(dummy)
548     assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
549   end
550
551   def test_get_player_rate_0
552     assert_equal(2150, @pairing.get_player_rate(@x, @history))
553
554     dummy = nil
555     def @history.make_record(game_result)
556       {:game_id => "wdoor+floodgate-900-0-x-a-1", 
557        :black => "x",  :white => "a",
558        :winner => "x", :loser => "a"}
559     end
560     @history.update(dummy)
561     assert_equal(@a.rate+100, @pairing.get_player_rate(@x, @history))
562
563     def @history.make_record(game_result)
564       {:game_id => "wdoor+floodgate-900-0-x-b-1", 
565        :black => "x",  :white => "b",
566        :winner => "b", :loser => "x"}
567     end
568     @history.update(dummy)
569
570     assert_equal((@a.rate+100+@b.rate-100)/2, @pairing.get_player_rate(@x, @history))
571   end
572 end
573