OSDN Git Service

77f0522fbab5220592c0daf51d73097b792d0835
[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 TestStartGame < Test::Unit::TestCase
37   def setup
38     @pairing= ShogiServer::StartGame.new
39     $called = 0
40     def @pairing.start_game(p1,p2)
41       $called += 1
42     end
43     @a = ShogiServer::BasicPlayer.new
44     @a.name = "a"
45     @a.win  = 1
46     @a.loss = 2
47     @a.rate = 0
48     @b = ShogiServer::BasicPlayer.new
49     @b.name = "b"
50     @b.win  = 10
51     @b.loss = 20
52     @b.rate = 1500
53     @c = ShogiServer::BasicPlayer.new
54     @c.name = "c"
55     @c.win  = 100
56     @c.loss = 200
57     @c.rate = 1000
58     @d = ShogiServer::BasicPlayer.new
59     @d.name = "d"
60     @d.win  = 1000
61     @d.loss = 2000
62     @d.rate = 2000
63   end
64
65   def test_match_two_players
66     players = [@a,@b]
67     @pairing.match(players)
68     assert_equal(1, $called)
69   end
70
71   def test_match_one_player
72     players = [@a]
73     @pairing.match(players)
74     assert_equal(0, $called)
75   end
76
77   def test_match_zero_player
78     players = []
79     @pairing.match(players)
80     assert_equal(0, $called)
81   end
82
83   def test_match_three_players
84     players = [@a,@b,@c]
85     @pairing.match(players)
86     assert_equal(1, $called)
87   end
88
89   def test_match_four_players
90     players = [@a,@b,@c,@d]
91     @pairing.match(players)
92     assert_equal(2, $called)
93   end
94 end
95
96 class TestStartGameWithoutHumans < Test::Unit::TestCase
97   def setup
98     @pairing= ShogiServer::StartGameWithoutHumans.new
99     $paired = []
100     $called = 0
101     def @pairing.start_game(p1,p2)
102       $called += 1
103       $paired << [p1,p2]
104     end
105     @a = ShogiServer::BasicPlayer.new
106     @a.name = "a"
107     @a.win  = 1
108     @a.loss = 2
109     @a.rate = 0
110     @b = ShogiServer::BasicPlayer.new
111     @b.name = "b"
112     @b.win  = 10
113     @b.loss = 20
114     @b.rate = 1500
115     @c = ShogiServer::BasicPlayer.new
116     @c.name = "c"
117     @c.win  = 100
118     @c.loss = 200
119     @c.rate = 1000
120     @d = ShogiServer::BasicPlayer.new
121     @d.name = "d"
122     @d.win  = 1000
123     @d.loss = 2000
124     @d.rate = 2000
125   end
126
127   def test_match_one_player
128     players = [@a]
129     @pairing.match(players)
130     assert_equal(0, $called)
131   end
132
133   def test_match_one_player_human
134     @a.name += "_human"
135     players = [@a]
136     @pairing.match(players)
137     assert_equal(0, $called)
138   end
139
140   def test_match_two_players
141     players = [@a,@b]
142     @pairing.match(players)
143     assert_equal(1, $called)
144   end
145
146   def test_match_two_players_humans
147     @a.name += "_human"
148     @b.name += "_human"
149     players = [@a,@b]
150     @pairing.match(players)
151     assert_equal(1, $called)
152   end
153
154   def test_match_zero_player
155     players = []
156     @pairing.match(players)
157     assert_equal(0, $called)
158   end
159
160   def test_match_three_players
161     players = [@a,@b,@c]
162     @pairing.match(players)
163     assert_equal(1, $called)
164   end
165
166   def test_match_three_players_a_human
167     @a.name += "_human"
168     players = [@a,@b,@c]
169     @pairing.match(players)
170     assert_equal(1, $called)
171     assert_equal(1, players.size)
172     assert_equal(@c, players[0])
173   end
174
175   def test_match_three_players_b_human
176     @b.name += "_human"
177     players = [@a,@b,@c]
178     @pairing.match(players)
179     assert_equal(1, $called)
180     assert_equal(1, players.size)
181     assert_equal(@c, players[0])
182   end
183
184   def test_match_three_players_c_human
185     @c.name += "_human"
186     players = [@a,@b,@c]
187     @pairing.match(players)
188     assert_equal(1, $called)
189     assert_equal(1, players.size)
190     assert_equal(@c, players[0])
191   end
192
193   def test_match_three_players_ab_human
194     @a.name += "_human"
195     @b.name += "_human"
196     players = [@a,@b,@c]
197     @pairing.match(players)
198     assert_equal(1, $called)
199     assert_equal(1, players.size)
200     assert_equal(@b, players[0])
201   end
202
203   def test_match_three_players_bc_human
204     @b.name += "_human"
205     @c.name += "_human"
206     players = [@a,@b,@c]
207     @pairing.match(players)
208     assert_equal(1, $called)
209     assert_equal(1, players.size)
210     assert_equal(@c, players[0])
211   end
212
213   def test_match_four_players
214     players = [@a,@b,@c,@d]
215     @pairing.match(players)
216     assert_equal(2, $called)
217   end
218
219   def test_match_four_players_ab_human
220     @a.name += "_human"
221     @b.name += "_human"
222     players = [@a,@b,@c,@d]
223     @pairing.match(players)
224     assert_equal(2, $paired.size)
225     assert(same_pair?([@a,@c], $paired[0]))
226     assert(same_pair?([@b,@d], $paired[1]))
227   end
228
229   def test_match_four_players_bc_human
230     @b.name += "_human"
231     @c.name += "_human"
232     players = [@a,@b,@c,@d]
233     @pairing.match(players)
234     assert_equal(2, $paired.size)
235     assert(same_pair?([@a,@b], $paired[0]))
236     assert(same_pair?([@c,@d], $paired[1]))
237   end
238
239   def test_match_four_players_abc_human
240     @a.name += "_human"
241     @b.name += "_human"
242     @c.name += "_human"
243     players = [@a,@b,@c,@d]
244     @pairing.match(players)
245     assert_equal(2, $paired.size)
246     assert(same_pair?([@a,@d], $paired[0]))
247     assert(same_pair?([@b,@c], $paired[1]))
248   end
249
250   def test_match_four_players_bcd_human
251     @b.name += "_human"
252     @c.name += "_human"
253     @d.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_abcd_human
262     @a.name += "_human"
263     @b.name += "_human"
264     @c.name += "_human"
265     @d.name += "_human"
266     players = [@a,@b,@c,@d]
267     @pairing.match(players)
268     assert_equal(2, $paired.size)
269     assert(same_pair?([@a,@b], $paired[0]))
270     assert(same_pair?([@c,@d], $paired[1]))
271   end
272 end
273
274