OSDN Git Service

- shogi_server/pairing.rb:
[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/player'
5 require 'shogi_server/pairing'
6 require 'test/mock_log_message'
7
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