OSDN Git Service

Applied r247 changes for the test cases (spliting hte file).
[shogi-server/shogi-server.git] / test / TC_floodgate.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 class TestFloodgate < Test::Unit::TestCase
25   def setup
26     @fg = ShogiServer::League::Floodgate.new(nil)
27   end
28
29   def teardown
30
31   end
32
33   def test_game_name
34     assert(ShogiServer::League::Floodgate.game_name?("floodgate-900-0"))
35     assert(ShogiServer::League::Floodgate.game_name?("floodgate-0-10"))
36     assert(!ShogiServer::League::Floodgate.game_name?("floodgat-900-0"))
37   end
38
39 end
40
41 class TestPairing < Test::Unit::TestCase  
42   def setup
43     @pairing= ShogiServer::Pairing.new
44     @a = ShogiServer::BasicPlayer.new
45     @a.win  = 1
46     @a.loss = 2
47     @a.rate = 0
48     @b = ShogiServer::BasicPlayer.new
49     @b.win  = 10
50     @b.loss = 20
51     @b.rate = 1500
52     @c = ShogiServer::BasicPlayer.new
53     @c.win  = 100
54     @c.loss = 200
55     @c.rate = 1000
56   end
57
58   def test_delete_most_playing_player
59     players = [@a, @b, @c]
60     @pairing.delete_most_playing_player(players)
61     assert_equal([@a,@b], players)
62   end
63
64   def test_delete_least_rate_player
65     players = [@a, @b, @c]
66     @pairing.delete_least_rate_player(players)
67     assert_equal([@b,@c], players)
68   end
69 end
70
71 class TestRandomPairing < Test::Unit::TestCase  
72   def setup
73     @pairing= ShogiServer::RandomPairing.new
74     $called = 0
75     def @pairing.start_game(p1,p2)
76       $called += 1
77     end
78     @a = ShogiServer::BasicPlayer.new
79     @a.win  = 1
80     @a.loss = 2
81     @b = ShogiServer::BasicPlayer.new
82     @b.win  = 10
83     @b.loss = 20
84     @c = ShogiServer::BasicPlayer.new
85     @c.win  = 100
86     @c.loss = 200
87   end
88
89   def test_random_match_1
90     players = [@a]
91     @pairing.match(players)
92     assert_equal(0, $called)
93   end
94
95   def test_random_match_2
96     players = [@a,@b]
97     @pairing.match(players)
98     assert_equal(1, $called)
99   end
100   
101   def test_random_match_3
102     players = [@a, @b, @c]
103     @pairing.match(players)
104     assert_equal(1, $called)
105   end
106 end
107
108 class TestSwissPairing < Test::Unit::TestCase  
109   def setup
110     @pairing= ShogiServer::SwissPairing.new
111     $pairs = []
112     def @pairing.start_game(p1,p2)
113       $pairs << [p1,p2]
114     end
115     @a = ShogiServer::BasicPlayer.new
116     @a.name = "a"
117     @a.win  = 1
118     @a.loss = 2
119     @a.rate = 0
120     @a.last_game_win = false
121     @b = ShogiServer::BasicPlayer.new
122     @b.name = "b"
123     @b.win  = 10
124     @b.loss = 20
125     @b.rate = 1500
126     @b.last_game_win = true
127     @c = ShogiServer::BasicPlayer.new
128     @c.name = "c"
129     @c.win  = 100
130     @c.loss = 200
131     @c.rate = 1000
132     @c.last_game_win = true
133     @d = ShogiServer::BasicPlayer.new
134     @d.name = "d"
135     @d.win  = 1000
136     @d.loss = 2000
137     @d.rate = 1800
138     @d.last_game_win = true
139   end
140
141   def sort(players)
142     return players.sort{|a,b| a.name <=> b.name}
143   end
144
145   def test_include_newbie
146     assert(@pairing.include_newbie?([@a]))
147     assert(!@pairing.include_newbie?([@b]))
148     assert(@pairing.include_newbie?([@b,@a]))
149     assert(!@pairing.include_newbie?([@b,@c]))
150   end
151
152   def test_match_1
153     @pairing.match([@a])
154     assert_equal(0, $pairs.size)
155   end
156   
157   def test_match_2
158     @pairing.match([@b])
159     assert_equal(0, $pairs.size)
160   end
161   
162   def test_match_3
163     @pairing.match([@a,@b])
164     assert_equal(1, $pairs.size)
165     assert_equal(sort([@a,@b]), sort($pairs.first))
166   end
167   
168   def test_match_4
169     @pairing.match([@c,@b])
170     assert_equal(1, $pairs.size)
171     assert_equal(sort([@b,@c]), sort($pairs.first))
172   end
173   
174   def test_match_5
175     @pairing.match([@c,@b,@a])
176     assert_equal(1, $pairs.size)
177     assert_equal(sort([@b,@c]), sort($pairs.first))
178   end
179   
180   def test_match_6
181     @pairing.match([@c,@b,@a,@d])
182     assert_equal(2, $pairs.size)
183     assert_equal(sort([@b,@d]), sort($pairs.first))
184     assert_equal(sort([@a,@c]), sort($pairs.last))
185   end
186 end
187
188 class TestExcludeSacrifice < Test::Unit::TestCase  
189   class Dummy
190     attr_reader :players
191     def match(players)
192       @players = players
193     end
194   end
195   
196   def setup
197     @dummy = Dummy.new
198     @obj = ShogiServer::ExcludeSacrifice.new(@dummy)
199     @a = ShogiServer::BasicPlayer.new
200     @a.player_id   = "a"
201     @a.name = "a"
202     @a.win  = 1
203     @a.loss = 2
204     @a.rate = 0
205     @a.last_game_win = false
206     @b = ShogiServer::BasicPlayer.new
207     @b.player_id   = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
208     @b.name = "b"
209     @b.win  = 10
210     @b.loss = 20
211     @b.rate = 1500
212     @b.last_game_win = true
213     @c = ShogiServer::BasicPlayer.new
214     @c.player_id   = "c"
215     @c.name = "c"
216     @c.win  = 100
217     @c.loss = 200
218     @c.rate = 1000
219     @c.last_game_win = true
220   end
221
222   def test_match_1
223     @obj.match([@a])
224     assert_equal(1, @dummy.players.size)
225   end
226   
227   def test_match_2
228     @obj.match([@b])
229     assert_equal(0, @dummy.players.size)
230   end
231   
232   def test_match_3
233     @obj.match([@a, @b])
234     assert_equal(2, @dummy.players.size)
235   end
236
237   def test_match_4
238     @obj.match([@a, @b, @c])
239     assert_equal(2, @dummy.players.size)
240   end
241
242   def test_match_5
243     @obj.match([@a, @c])
244     assert_equal(2, @dummy.players.size)
245   end
246 end
247