OSDN Git Service

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