OSDN Git Service

Forgot to add a bug number
[shogi-server/shogi-server.git] / test / TC_rating.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 load 'mk_rate'
3 require 'test/unit'
4
5 class RatingTest < Test::Unit::TestCase
6   def test_rating1
7     wl = GSL::Matrix[[0,3,9],
8                 [1,0,3],
9                 [1,1,0]]
10     rating = Rating.new(wl)
11     rating.rating
12     rating.average!
13     rating.integer!
14     assert( rating.rate[0] > rating.rate[1])
15     assert( rating.rate[1] > rating.rate[2])
16   end
17
18   def test_rating2
19     wl = GSL::Matrix[ # \e$B=54)>-4}\e(B2001\e$BG/\e(B9\e$B7n\e(B12\e$BF|9f$N%G!<%?\e(B
20       [  0,  59,  52,  39,  29,  12],    # \e$BL>?M$*$h$S\e(BA\e$B5i\e(B
21       [ 40,   0,  37,  29,  27,  10],    # B\e$B5i\e(B1\e$BAH\e(B
22       [ 33,  35,   0,  50,  92,  41],    # B\e$B5i\e(B2\e$BAH\e(B
23       [ 21,  19,  51,   0, 140,  80],    # C\e$B5i\e(B1\e$BAH\e(B
24       [  5,  21,  82, 103,   0, 124],    # C\e$B5i\e(B2\e$BAH\e(B
25       [  2,   6,   9,  34,  44,   0],    # \e$B%U%j!<%/%i%9\e(B
26     ]
27     rating = Rating.new(wl)
28     assert_nothing_raised {rating.rating}
29     rating.integer!
30     p1, p2, p3, p4, p5, p6 = rating.rate.to_a
31     assert(p1 > p2)
32     assert(p2 > p3)
33     assert(p3 > p4)
34     assert(p4 > p5)
35     assert(p5 > p6)
36   end
37
38   def test_rating3
39     wl = GSL::Matrix[[0, 3, 18], 
40                 [1, 0, 14], 
41                 [10, 39, 0]]
42     rating = Rating.new(wl)
43     assert_nothing_raised {rating.rating}
44     rating.integer!
45     p1, p2, p3 = rating.rate.to_a
46     assert( p1 > p2 )
47     assert( p3 > p2 )
48   end
49
50   def test_rank2
51     wl = GSL::Matrix[[0, 3],
52                 [1, 0]]
53     rating = Rating.new(wl)
54     rating.rating
55     p1 = rating.rate[0]
56     p2 = rating.rate[1]
57     rating.integer!
58     assert( (180..200).include?(p1 - p2), rating.rate.to_a.inspect )
59   end
60
61   def test_rank3
62     wl = GSL::Matrix[[0, 30, 0],
63                 [10, 0, 30],
64                 [0, 10, 0]]
65     rating = Rating.new(wl)
66     rating.rating
67     rating.average!
68     rating.integer!
69     p1 = rating.rate[0]
70     p2 = rating.rate[1]
71     rating.integer!
72     assert( rating.rate[0] > rating.rate[1])
73     assert( rating.rate[1] > rating.rate[2])
74   end
75 end
76
77
78 class TestWinLossMatrix < Test::Unit::TestCase
79   def setup
80     keys = ['a', 'b', 'c']
81     win_loss = GSL::Matrix[[0,2,3],[1,0,1],[1,1,0]]
82     @matrix = WinLossMatrix.new(keys, win_loss)
83   end
84     
85   def test_delete_row
86     new_matrix = @matrix.delete_row(1)
87     assert_equal(3, @matrix.size)
88     assert_equal(2, new_matrix.size)
89     assert_equal(['a','c'], new_matrix.keys)
90     assert_equal(GSL::Matrix[[0,3],[1,0]], new_matrix.matrix)
91   end
92
93   def test_delete_rows
94     $deleted = []
95     def @matrix.delete_row(index)
96       $deleted << index
97       self
98     end
99     @matrix.delete_rows([0,1])
100     assert_equal([1,0], $deleted)
101   end
102
103   def test_connected_subsets
104     array = %w!
105       0  0  0  0  0  2  9 74  0  0  0
106       0  0  0  0 21  0  0  0  0  0  0
107       0  0  0  0 19  0  0  0  0  0  0
108       0  0  0  0 13  0  0  0  0  0  0
109       0 19 20 27  0  0  0  0  0  0  0
110       1  0  0  0  0  0  0  0  0  0  5
111       1  0  0  0  0  0  0  0  0  0  9
112       5  0  0  0  0  0  0  0  0  0  0
113       0  0  0  0  0  0  0  0  0  0  6
114       0  0  0  0  0  0  0  0  0  0  1
115       0  0  0  0  0  1  1  0 28  1  0!.map{|v| v.to_i}
116     keys = ["gps+11648e4e66e7ed6a86cb7f1d0cf604fe", 
117             "gps1_wPrBn_hand+cf51828e1e4351eea9a70e754b8e5edc",
118             "gps1_wPrBn_simple+d6c7d5e4acfb4a21072824d3be07c6dc",
119             "gps1_woPrBn+ea563881afd2e56d3dd715538d2da850",
120             "gps2_wPrBn_mem+dbd8165c47a193b7e76fa9adb3b4e445",
121             "gps32+aa0ba6bfbd84caa7ef1cda34562ce90c",
122             "gps500+0706915e56798d393c9aec4749789b2f",
123             "guest+068b4eb12b042a72e1c7791344175d82",
124             "guest+471a3f6aea2804130b5b967e8a42ea3c",
125             "kaneko+4cee2e6a81fea84316b13626e705e431",
126             "yowai_gps+95908f6c18338f5340371f71523fc5e3"]
127     win_loss = GSL::Matrix.alloc(array, 11, 11)
128     obj = WinLossMatrix.new(keys, win_loss)
129     objs = obj.connected_subsets
130     assert_equal(2, objs.size)
131   end
132 end
133
134 # vim: ts=2 sw=2 sts=0
135