OSDN Git Service

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