3 * @covers ::wfBaseConvert
5 class WfBaseConvertTest
extends MediaWikiTestCase
{
6 public static function provideSingleDigitConversions() {
9 array( '0', '0', '0', '0', '0', '0', '0' ),
10 array( '1', '1', '1', '1', '1', '1', '1' ),
11 array( '10', '2', '2', '2', '2', '2', '2' ),
12 array( '11', '10', '3', '3', '3', '3', '3' ),
13 array( '100', '11', '4', '4', '4', '4', '4' ),
14 array( '101', '12', '10', '5', '5', '5', '5' ),
15 array( '110', '20', '11', '6', '6', '6', '6' ),
16 array( '111', '21', '12', '7', '7', '7', '7' ),
17 array( '1000', '22', '13', '10', '8', '8', '8' ),
18 array( '1001', '100', '14', '11', '9', '9', '9' ),
19 array( '1010', '101', '20', '12', '10', 'a', 'a' ),
20 array( '1011', '102', '21', '13', '11', 'b', 'b' ),
21 array( '1100', '110', '22', '14', '12', 'c', 'c' ),
22 array( '1101', '111', '23', '15', '13', 'd', 'd' ),
23 array( '1110', '112', '24', '16', '14', 'e', 'e' ),
24 array( '1111', '120', '30', '17', '15', 'f', 'f' ),
25 array( '10000', '121', '31', '20', '16', '10', 'g' ),
26 array( '10001', '122', '32', '21', '17', '11', 'h' ),
27 array( '10010', '200', '33', '22', '18', '12', 'i' ),
28 array( '10011', '201', '34', '23', '19', '13', 'j' ),
29 array( '10100', '202', '40', '24', '20', '14', 'k' ),
30 array( '10101', '210', '41', '25', '21', '15', 'l' ),
31 array( '10110', '211', '42', '26', '22', '16', 'm' ),
32 array( '10111', '212', '43', '27', '23', '17', 'n' ),
33 array( '11000', '220', '44', '30', '24', '18', 'o' ),
34 array( '11001', '221', '100', '31', '25', '19', 'p' ),
35 array( '11010', '222', '101', '32', '26', '1a', 'q' ),
36 array( '11011', '1000', '102', '33', '27', '1b', 'r' ),
37 array( '11100', '1001', '103', '34', '28', '1c', 's' ),
38 array( '11101', '1002', '104', '35', '29', '1d', 't' ),
39 array( '11110', '1010', '110', '36', '30', '1e', 'u' ),
40 array( '11111', '1011', '111', '37', '31', '1f', 'v' ),
41 array( '100000', '1012', '112', '40', '32', '20', 'w' ),
42 array( '100001', '1020', '113', '41', '33', '21', 'x' ),
43 array( '100010', '1021', '114', '42', '34', '22', 'y' ),
44 array( '100011', '1022', '120', '43', '35', '23', 'z' )
49 * @dataProvider provideSingleDigitConversions
51 public function testDigitToBase2( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
52 $this->assertSame( $base2, wfBaseConvert( $base3, '3', '2' ) );
53 $this->assertSame( $base2, wfBaseConvert( $base5, '5', '2' ) );
54 $this->assertSame( $base2, wfBaseConvert( $base8, '8', '2' ) );
55 $this->assertSame( $base2, wfBaseConvert( $base10, '10', '2' ) );
56 $this->assertSame( $base2, wfBaseConvert( $base16, '16', '2' ) );
57 $this->assertSame( $base2, wfBaseConvert( $base36, '36', '2' ) );
61 * @dataProvider provideSingleDigitConversions
63 public function testDigitToBase3( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
64 $this->assertSame( $base3, wfBaseConvert( $base2, '2', '3' ) );
65 $this->assertSame( $base3, wfBaseConvert( $base5, '5', '3' ) );
66 $this->assertSame( $base3, wfBaseConvert( $base8, '8', '3' ) );
67 $this->assertSame( $base3, wfBaseConvert( $base10, '10', '3' ) );
68 $this->assertSame( $base3, wfBaseConvert( $base16, '16', '3' ) );
69 $this->assertSame( $base3, wfBaseConvert( $base36, '36', '3' ) );
73 * @dataProvider provideSingleDigitConversions
75 public function testDigitToBase5( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
76 $this->assertSame( $base5, wfBaseConvert( $base2, '2', '5' ) );
77 $this->assertSame( $base5, wfBaseConvert( $base3, '3', '5' ) );
78 $this->assertSame( $base5, wfBaseConvert( $base8, '8', '5' ) );
79 $this->assertSame( $base5, wfBaseConvert( $base10, '10', '5' ) );
80 $this->assertSame( $base5, wfBaseConvert( $base16, '16', '5' ) );
81 $this->assertSame( $base5, wfBaseConvert( $base36, '36', '5' ) );
85 * @dataProvider provideSingleDigitConversions
87 public function testDigitToBase8( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
88 $this->assertSame( $base8, wfBaseConvert( $base2, '2', '8' ) );
89 $this->assertSame( $base8, wfBaseConvert( $base3, '3', '8' ) );
90 $this->assertSame( $base8, wfBaseConvert( $base5, '5', '8' ) );
91 $this->assertSame( $base8, wfBaseConvert( $base10, '10', '8' ) );
92 $this->assertSame( $base8, wfBaseConvert( $base16, '16', '8' ) );
93 $this->assertSame( $base8, wfBaseConvert( $base36, '36', '8' ) );
97 * @dataProvider provideSingleDigitConversions
99 public function testDigitToBase10( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
100 $this->assertSame( $base10, wfBaseConvert( $base2, '2', '10' ) );
101 $this->assertSame( $base10, wfBaseConvert( $base3, '3', '10' ) );
102 $this->assertSame( $base10, wfBaseConvert( $base5, '5', '10' ) );
103 $this->assertSame( $base10, wfBaseConvert( $base8, '8', '10' ) );
104 $this->assertSame( $base10, wfBaseConvert( $base16, '16', '10' ) );
105 $this->assertSame( $base10, wfBaseConvert( $base36, '36', '10' ) );
109 * @dataProvider provideSingleDigitConversions
111 public function testDigitToBase16( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
112 $this->assertSame( $base16, wfBaseConvert( $base2, '2', '16' ) );
113 $this->assertSame( $base16, wfBaseConvert( $base3, '3', '16' ) );
114 $this->assertSame( $base16, wfBaseConvert( $base5, '5', '16' ) );
115 $this->assertSame( $base16, wfBaseConvert( $base8, '8', '16' ) );
116 $this->assertSame( $base16, wfBaseConvert( $base10, '10', '16' ) );
117 $this->assertSame( $base16, wfBaseConvert( $base36, '36', '16' ) );
121 * @dataProvider provideSingleDigitConversions
123 public function testDigitToBase36( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) {
124 $this->assertSame( $base36, wfBaseConvert( $base2, '2', '36' ) );
125 $this->assertSame( $base36, wfBaseConvert( $base3, '3', '36' ) );
126 $this->assertSame( $base36, wfBaseConvert( $base5, '5', '36' ) );
127 $this->assertSame( $base36, wfBaseConvert( $base8, '8', '36' ) );
128 $this->assertSame( $base36, wfBaseConvert( $base10, '10', '36' ) );
129 $this->assertSame( $base36, wfBaseConvert( $base16, '16', '36' ) );
132 public function testLargeNumber() {
133 $this->assertSame( '1100110001111010000000101110100', wfBaseConvert( 'sd89ys', 36, 2 ) );
134 $this->assertSame( '11102112120221201101', wfBaseConvert( 'sd89ys', 36, 3 ) );
135 $this->assertSame( '12003102232400', wfBaseConvert( 'sd89ys', 36, 5 ) );
136 $this->assertSame( '14617200564', wfBaseConvert( 'sd89ys', 36, 8 ) );
137 $this->assertSame( '1715274100', wfBaseConvert( 'sd89ys', 36, 10 ) );
138 $this->assertSame( '663d0174', wfBaseConvert( 'sd89ys', 36, 16 ) );
141 public static function provideNumbers() {
143 $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
144 for ( $i = 0; $i < 50; $i++
) {
145 $base = mt_rand( 2, 36 );
146 $len = mt_rand( 10, 100 );
149 for ( $j = 0; $j < $len; $j++
) {
150 $str .= $chars[mt_rand( 0, $base - 1 )];
153 $x[] = array( $base, $str );
160 * @dataProvider provideNumbers
162 public function testIdentity( $base, $number ) {
163 $this->assertSame( $number, wfBaseConvert( $number, $base, $base, strlen( $number ) ) );
166 public function testInvalid() {
167 $this->assertFalse( wfBaseConvert( '101', 1, 15 ) );
168 $this->assertFalse( wfBaseConvert( '101', 15, 1 ) );
169 $this->assertFalse( wfBaseConvert( '101', 37, 15 ) );
170 $this->assertFalse( wfBaseConvert( '101', 15, 37 ) );
171 $this->assertFalse( wfBaseConvert( 'abcde', 10, 11 ) );
172 $this->assertFalse( wfBaseConvert( '12930', 2, 10 ) );
173 $this->assertFalse( wfBaseConvert( '101', 'abc', 15 ) );
174 $this->assertFalse( wfBaseConvert( '101', 15, 'abc' ) );
177 public function testPadding() {
178 $number = "10101010101";
180 strlen( $number ) +
5,
181 strlen( wfBaseConvert( $number, 2, 2, strlen( $number ) +
5 ) )
185 strlen( wfBaseConvert( $number, 2, 2, strlen( $number ) - 5 ) )