OSDN Git Service

Merge branch 'logger'
[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   end
148
149   def test_match_one_player
150     players = [@a]
151     @pairing.match(players)
152     assert_equal(0, $called)
153   end
154
155   def test_match_one_player_human
156     @a.name += "_human"
157     players = [@a]
158     @pairing.match(players)
159     assert_equal(0, $called)
160   end
161
162   def test_match_two_players
163     players = [@a,@b]
164     @pairing.match(players)
165     assert_equal(1, $called)
166   end
167
168   def test_match_two_players_humans
169     @a.name += "_human"
170     @b.name += "_human"
171     players = [@a,@b]
172     @pairing.match(players)
173     assert_equal(1, $called)
174   end
175
176   def test_match_zero_player
177     players = []
178     @pairing.match(players)
179     assert_equal(0, $called)
180   end
181
182   def test_match_three_players
183     players = [@a,@b,@c]
184     @pairing.match(players)
185     assert_equal(1, $called)
186   end
187
188   def test_match_three_players_a_human
189     @a.name += "_human"
190     players = [@a,@b,@c]
191     @pairing.match(players)
192     assert_equal(1, $called)
193     assert_equal(1, players.size)
194     assert_equal(@c, players[0])
195   end
196
197   def test_match_three_players_b_human
198     @b.name += "_human"
199     players = [@a,@b,@c]
200     @pairing.match(players)
201     assert_equal(1, $called)
202     assert_equal(1, players.size)
203     assert_equal(@c, players[0])
204   end
205
206   def test_match_three_players_c_human
207     @c.name += "_human"
208     players = [@a,@b,@c]
209     @pairing.match(players)
210     assert_equal(1, $called)
211     assert_equal(1, players.size)
212     assert_equal(@c, players[0])
213   end
214
215   def test_match_three_players_ab_human
216     @a.name += "_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(@b, players[0])
223   end
224
225   def test_match_three_players_bc_human
226     @b.name += "_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_four_players
236     players = [@a,@b,@c,@d]
237     @pairing.match(players)
238     assert_equal(2, $called)
239   end
240
241   def test_match_four_players_ab_human
242     @a.name += "_human"
243     @b.name += "_human"
244     players = [@a,@b,@c,@d]
245     @pairing.match(players)
246     assert_equal(2, $paired.size)
247     assert(same_pair?([@a,@c], $paired[0]))
248     assert(same_pair?([@b,@d], $paired[1]))
249   end
250
251   def test_match_four_players_bc_human
252     @b.name += "_human"
253     @c.name += "_human"
254     players = [@a,@b,@c,@d]
255     @pairing.match(players)
256     assert_equal(2, $paired.size)
257     assert(same_pair?([@a,@b], $paired[0]))
258     assert(same_pair?([@c,@d], $paired[1]))
259   end
260
261   def test_match_four_players_abc_human
262     @a.name += "_human"
263     @b.name += "_human"
264     @c.name += "_human"
265     players = [@a,@b,@c,@d]
266     @pairing.match(players)
267     assert_equal(2, $paired.size)
268     assert(same_pair?([@a,@d], $paired[0]))
269     assert(same_pair?([@b,@c], $paired[1]))
270   end
271
272   def test_match_four_players_bcd_human
273     @b.name += "_human"
274     @c.name += "_human"
275     @d.name += "_human"
276     players = [@a,@b,@c,@d]
277     @pairing.match(players)
278     assert_equal(2, $paired.size)
279     assert(same_pair?([@a,@b], $paired[0]))
280     assert(same_pair?([@c,@d], $paired[1]))
281   end
282
283   def test_match_four_players_abcd_human
284     @a.name += "_human"
285     @b.name += "_human"
286     @c.name += "_human"
287     @d.name += "_human"
288     players = [@a,@b,@c,@d]
289     @pairing.match(players)
290     assert_equal(2, $paired.size)
291     assert(same_pair?([@a,@b], $paired[0]))
292     assert(same_pair?([@c,@d], $paired[1]))
293   end
294 end
295
296