OSDN Git Service

Improved stability of test results.
[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
7 class MockLogger
8   def debug(str)
9   end
10   def info(str)
11     #puts str
12   end
13   def warn(str)
14   end
15   def error(str)
16   end
17 end
18
19 $logger = MockLogger.new
20 def log_message(msg)
21   $logger.info(msg)
22 end
23
24 def log_warning(msg)
25   $logger.warn(msg)
26 end
27
28 def same_pair?(a, b)
29   unless a.size == 2 && b.size == 2
30     return false
31   end
32
33   return true if [a.first, a.last] == b || [a.last, a.first] == b
34 end
35
36 class TestPairing < Test::Unit::TestCase  
37   def setup
38     @pairing= ShogiServer::Pairing.new
39     $pairs = []
40     def @pairing.start_game(p1,p2)
41       $pairs << [p1,p2]
42     end
43     @a = ShogiServer::BasicPlayer.new
44     @a.name = "a"
45     @a.win  = 1
46     @a.loss = 2
47     @a.rate = 0
48     @a.last_game_win = false
49     @b = ShogiServer::BasicPlayer.new
50     @b.name = "b"
51     @b.win  = 10
52     @b.loss = 20
53     @b.rate = 1500
54     @b.last_game_win = true
55     @c = ShogiServer::BasicPlayer.new
56     @c.name = "c"
57     @c.win  = 100
58     @c.loss = 200
59     @c.rate = 1000
60     @c.last_game_win = true
61     @d = ShogiServer::BasicPlayer.new
62     @d.name = "d"
63     @d.win  = 1000
64     @d.loss = 2000
65     @d.rate = 1800
66     @d.last_game_win = true
67   end
68
69   def test_include_newbie
70     assert(@pairing.include_newbie?([@a]))
71     assert(!@pairing.include_newbie?([@b]))
72     assert(@pairing.include_newbie?([@b,@a]))
73     assert(!@pairing.include_newbie?([@b,@c]))
74   end
75 end
76
77 class TestStartGame < Test::Unit::TestCase
78   def setup
79     @pairing= ShogiServer::StartGame.new
80     $called = 0
81     def @pairing.start_game(p1,p2)
82       $called += 1
83     end
84     @a = ShogiServer::BasicPlayer.new
85     @a.name = "a"
86     @a.win  = 1
87     @a.loss = 2
88     @a.rate = 0
89     @b = ShogiServer::BasicPlayer.new
90     @b.name = "b"
91     @b.win  = 10
92     @b.loss = 20
93     @b.rate = 1500
94     @c = ShogiServer::BasicPlayer.new
95     @c.name = "c"
96     @c.win  = 100
97     @c.loss = 200
98     @c.rate = 1000
99     @d = ShogiServer::BasicPlayer.new
100     @d.name = "d"
101     @d.win  = 1000
102     @d.loss = 2000
103     @d.rate = 2000
104   end
105
106   def test_match_two_players
107     players = [@a,@b]
108     @pairing.match(players)
109     assert_equal(1, $called)
110   end
111
112   def test_match_one_player
113     players = [@a]
114     @pairing.match(players)
115     assert_equal(0, $called)
116   end
117
118   def test_match_zero_player
119     players = []
120     @pairing.match(players)
121     assert_equal(0, $called)
122   end
123
124   def test_match_three_players
125     players = [@a,@b,@c]
126     @pairing.match(players)
127     assert_equal(1, $called)
128   end
129
130   def test_match_four_players
131     players = [@a,@b,@c,@d]
132     @pairing.match(players)
133     assert_equal(2, $called)
134   end
135 end
136
137 class TestStartGameWithoutHumans < Test::Unit::TestCase
138   def setup
139     @pairing= ShogiServer::StartGameWithoutHumans.new
140     $paired = []
141     $called = 0
142     def @pairing.start_game(p1,p2)
143       $called += 1
144       $paired << [p1,p2]
145     end
146     @a = ShogiServer::BasicPlayer.new
147     @a.name = "a"
148     @a.win  = 1
149     @a.loss = 2
150     @a.rate = 0
151     @b = ShogiServer::BasicPlayer.new
152     @b.name = "b"
153     @b.win  = 10
154     @b.loss = 20
155     @b.rate = 1500
156     @c = ShogiServer::BasicPlayer.new
157     @c.name = "c"
158     @c.win  = 100
159     @c.loss = 200
160     @c.rate = 1000
161     @d = ShogiServer::BasicPlayer.new
162     @d.name = "d"
163     @d.win  = 1000
164     @d.loss = 2000
165     @d.rate = 2000
166   end
167
168   def test_match_one_player
169     players = [@a]
170     @pairing.match(players)
171     assert_equal(0, $called)
172   end
173
174   def test_match_one_player_human
175     @a.name += "_human"
176     players = [@a]
177     @pairing.match(players)
178     assert_equal(0, $called)
179   end
180
181   def test_match_two_players
182     players = [@a,@b]
183     @pairing.match(players)
184     assert_equal(1, $called)
185   end
186
187   def test_match_two_players_humans
188     @a.name += "_human"
189     @b.name += "_human"
190     players = [@a,@b]
191     @pairing.match(players)
192     assert_equal(1, $called)
193   end
194
195   def test_match_zero_player
196     players = []
197     @pairing.match(players)
198     assert_equal(0, $called)
199   end
200
201   def test_match_three_players
202     players = [@a,@b,@c]
203     @pairing.match(players)
204     assert_equal(1, $called)
205   end
206
207   def test_match_three_players_a_human
208     @a.name += "_human"
209     players = [@a,@b,@c]
210     @pairing.match(players)
211     assert_equal(1, $called)
212     assert_equal(1, players.size)
213     assert_equal(@c, players[0])
214   end
215
216   def test_match_three_players_b_human
217     @b.name += "_human"
218     players = [@a,@b,@c]
219     @pairing.match(players)
220     assert_equal(1, $called)
221     assert_equal(1, players.size)
222     assert_equal(@c, players[0])
223   end
224
225   def test_match_three_players_c_human
226     @c.name += "_human"
227     players = [@a,@b,@c]
228     @pairing.match(players)
229     assert_equal(1, $called)
230     assert_equal(1, players.size)
231     assert_equal(@c, players[0])
232   end
233
234   def test_match_three_players_ab_human
235     @a.name += "_human"
236     @b.name += "_human"
237     players = [@a,@b,@c]
238     @pairing.match(players)
239     assert_equal(1, $called)
240     assert_equal(1, players.size)
241     assert_equal(@b, players[0])
242   end
243
244   def test_match_three_players_bc_human
245     @b.name += "_human"
246     @c.name += "_human"
247     players = [@a,@b,@c]
248     @pairing.match(players)
249     assert_equal(1, $called)
250     assert_equal(1, players.size)
251     assert_equal(@c, players[0])
252   end
253
254   def test_match_four_players
255     players = [@a,@b,@c,@d]
256     @pairing.match(players)
257     assert_equal(2, $called)
258   end
259
260   def test_match_four_players_ab_human
261     @a.name += "_human"
262     @b.name += "_human"
263     players = [@a,@b,@c,@d]
264     @pairing.match(players)
265     assert_equal(2, $paired.size)
266     assert(same_pair?([@a,@c], $paired[0]))
267     assert(same_pair?([@b,@d], $paired[1]))
268   end
269
270   def test_match_four_players_bc_human
271     @b.name += "_human"
272     @c.name += "_human"
273     players = [@a,@b,@c,@d]
274     @pairing.match(players)
275     assert_equal(2, $paired.size)
276     assert(same_pair?([@a,@b], $paired[0]))
277     assert(same_pair?([@c,@d], $paired[1]))
278   end
279
280   def test_match_four_players_abc_human
281     @a.name += "_human"
282     @b.name += "_human"
283     @c.name += "_human"
284     players = [@a,@b,@c,@d]
285     @pairing.match(players)
286     assert_equal(2, $paired.size)
287     assert(same_pair?([@a,@d], $paired[0]))
288     assert(same_pair?([@b,@c], $paired[1]))
289   end
290
291   def test_match_four_players_bcd_human
292     @b.name += "_human"
293     @c.name += "_human"
294     @d.name += "_human"
295     players = [@a,@b,@c,@d]
296     @pairing.match(players)
297     assert_equal(2, $paired.size)
298     assert(same_pair?([@a,@b], $paired[0]))
299     assert(same_pair?([@c,@d], $paired[1]))
300   end
301
302   def test_match_four_players_abcd_human
303     @a.name += "_human"
304     @b.name += "_human"
305     @c.name += "_human"
306     @d.name += "_human"
307     players = [@a,@b,@c,@d]
308     @pairing.match(players)
309     assert_equal(2, $paired.size)
310     assert(same_pair?([@a,@b], $paired[0]))
311     assert(same_pair?([@c,@d], $paired[1]))
312   end
313 end
314
315