MediaWiki
REL1_22
|
00001 <?php 00005 class WfBaseConvertTest extends MediaWikiTestCase { 00006 public static function provideSingleDigitConversions() { 00007 return array( 00008 // 2 3 5 8 10 16 36 00009 array( '0', '0', '0', '0', '0', '0', '0' ), 00010 array( '1', '1', '1', '1', '1', '1', '1' ), 00011 array( '10', '2', '2', '2', '2', '2', '2' ), 00012 array( '11', '10', '3', '3', '3', '3', '3' ), 00013 array( '100', '11', '4', '4', '4', '4', '4' ), 00014 array( '101', '12', '10', '5', '5', '5', '5' ), 00015 array( '110', '20', '11', '6', '6', '6', '6' ), 00016 array( '111', '21', '12', '7', '7', '7', '7' ), 00017 array( '1000', '22', '13', '10', '8', '8', '8' ), 00018 array( '1001', '100', '14', '11', '9', '9', '9' ), 00019 array( '1010', '101', '20', '12', '10', 'a', 'a' ), 00020 array( '1011', '102', '21', '13', '11', 'b', 'b' ), 00021 array( '1100', '110', '22', '14', '12', 'c', 'c' ), 00022 array( '1101', '111', '23', '15', '13', 'd', 'd' ), 00023 array( '1110', '112', '24', '16', '14', 'e', 'e' ), 00024 array( '1111', '120', '30', '17', '15', 'f', 'f' ), 00025 array( '10000', '121', '31', '20', '16', '10', 'g' ), 00026 array( '10001', '122', '32', '21', '17', '11', 'h' ), 00027 array( '10010', '200', '33', '22', '18', '12', 'i' ), 00028 array( '10011', '201', '34', '23', '19', '13', 'j' ), 00029 array( '10100', '202', '40', '24', '20', '14', 'k' ), 00030 array( '10101', '210', '41', '25', '21', '15', 'l' ), 00031 array( '10110', '211', '42', '26', '22', '16', 'm' ), 00032 array( '10111', '212', '43', '27', '23', '17', 'n' ), 00033 array( '11000', '220', '44', '30', '24', '18', 'o' ), 00034 array( '11001', '221', '100', '31', '25', '19', 'p' ), 00035 array( '11010', '222', '101', '32', '26', '1a', 'q' ), 00036 array( '11011', '1000', '102', '33', '27', '1b', 'r' ), 00037 array( '11100', '1001', '103', '34', '28', '1c', 's' ), 00038 array( '11101', '1002', '104', '35', '29', '1d', 't' ), 00039 array( '11110', '1010', '110', '36', '30', '1e', 'u' ), 00040 array( '11111', '1011', '111', '37', '31', '1f', 'v' ), 00041 array( '100000', '1012', '112', '40', '32', '20', 'w' ), 00042 array( '100001', '1020', '113', '41', '33', '21', 'x' ), 00043 array( '100010', '1021', '114', '42', '34', '22', 'y' ), 00044 array( '100011', '1022', '120', '43', '35', '23', 'z' ) 00045 ); 00046 } 00047 00051 public function testDigitToBase2( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00052 $this->assertSame( $base2, wfBaseConvert( $base3, '3', '2' ) ); 00053 $this->assertSame( $base2, wfBaseConvert( $base5, '5', '2' ) ); 00054 $this->assertSame( $base2, wfBaseConvert( $base8, '8', '2' ) ); 00055 $this->assertSame( $base2, wfBaseConvert( $base10, '10', '2' ) ); 00056 $this->assertSame( $base2, wfBaseConvert( $base16, '16', '2' ) ); 00057 $this->assertSame( $base2, wfBaseConvert( $base36, '36', '2' ) ); 00058 } 00059 00063 public function testDigitToBase3( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00064 $this->assertSame( $base3, wfBaseConvert( $base2, '2', '3' ) ); 00065 $this->assertSame( $base3, wfBaseConvert( $base5, '5', '3' ) ); 00066 $this->assertSame( $base3, wfBaseConvert( $base8, '8', '3' ) ); 00067 $this->assertSame( $base3, wfBaseConvert( $base10, '10', '3' ) ); 00068 $this->assertSame( $base3, wfBaseConvert( $base16, '16', '3' ) ); 00069 $this->assertSame( $base3, wfBaseConvert( $base36, '36', '3' ) ); 00070 } 00071 00075 public function testDigitToBase5( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00076 $this->assertSame( $base5, wfBaseConvert( $base2, '2', '5' ) ); 00077 $this->assertSame( $base5, wfBaseConvert( $base3, '3', '5' ) ); 00078 $this->assertSame( $base5, wfBaseConvert( $base8, '8', '5' ) ); 00079 $this->assertSame( $base5, wfBaseConvert( $base10, '10', '5' ) ); 00080 $this->assertSame( $base5, wfBaseConvert( $base16, '16', '5' ) ); 00081 $this->assertSame( $base5, wfBaseConvert( $base36, '36', '5' ) ); 00082 } 00083 00087 public function testDigitToBase8( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00088 $this->assertSame( $base8, wfBaseConvert( $base2, '2', '8' ) ); 00089 $this->assertSame( $base8, wfBaseConvert( $base3, '3', '8' ) ); 00090 $this->assertSame( $base8, wfBaseConvert( $base5, '5', '8' ) ); 00091 $this->assertSame( $base8, wfBaseConvert( $base10, '10', '8' ) ); 00092 $this->assertSame( $base8, wfBaseConvert( $base16, '16', '8' ) ); 00093 $this->assertSame( $base8, wfBaseConvert( $base36, '36', '8' ) ); 00094 } 00095 00099 public function testDigitToBase10( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00100 $this->assertSame( $base10, wfBaseConvert( $base2, '2', '10' ) ); 00101 $this->assertSame( $base10, wfBaseConvert( $base3, '3', '10' ) ); 00102 $this->assertSame( $base10, wfBaseConvert( $base5, '5', '10' ) ); 00103 $this->assertSame( $base10, wfBaseConvert( $base8, '8', '10' ) ); 00104 $this->assertSame( $base10, wfBaseConvert( $base16, '16', '10' ) ); 00105 $this->assertSame( $base10, wfBaseConvert( $base36, '36', '10' ) ); 00106 } 00107 00111 public function testDigitToBase16( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00112 $this->assertSame( $base16, wfBaseConvert( $base2, '2', '16' ) ); 00113 $this->assertSame( $base16, wfBaseConvert( $base3, '3', '16' ) ); 00114 $this->assertSame( $base16, wfBaseConvert( $base5, '5', '16' ) ); 00115 $this->assertSame( $base16, wfBaseConvert( $base8, '8', '16' ) ); 00116 $this->assertSame( $base16, wfBaseConvert( $base10, '10', '16' ) ); 00117 $this->assertSame( $base16, wfBaseConvert( $base36, '36', '16' ) ); 00118 } 00119 00123 public function testDigitToBase36( $base2, $base3, $base5, $base8, $base10, $base16, $base36 ) { 00124 $this->assertSame( $base36, wfBaseConvert( $base2, '2', '36' ) ); 00125 $this->assertSame( $base36, wfBaseConvert( $base3, '3', '36' ) ); 00126 $this->assertSame( $base36, wfBaseConvert( $base5, '5', '36' ) ); 00127 $this->assertSame( $base36, wfBaseConvert( $base8, '8', '36' ) ); 00128 $this->assertSame( $base36, wfBaseConvert( $base10, '10', '36' ) ); 00129 $this->assertSame( $base36, wfBaseConvert( $base16, '16', '36' ) ); 00130 } 00131 00132 public function testLargeNumber() { 00133 $this->assertSame( '1100110001111010000000101110100', wfBaseConvert( 'sd89ys', 36, 2 ) ); 00134 $this->assertSame( '11102112120221201101', wfBaseConvert( 'sd89ys', 36, 3 ) ); 00135 $this->assertSame( '12003102232400', wfBaseConvert( 'sd89ys', 36, 5 ) ); 00136 $this->assertSame( '14617200564', wfBaseConvert( 'sd89ys', 36, 8 ) ); 00137 $this->assertSame( '1715274100', wfBaseConvert( 'sd89ys', 36, 10 ) ); 00138 $this->assertSame( '663d0174', wfBaseConvert( 'sd89ys', 36, 16 ) ); 00139 } 00140 00141 public static function provideNumbers() { 00142 $x = array(); 00143 $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; 00144 for ( $i = 0; $i < 50; $i++ ) { 00145 $base = mt_rand( 2, 36 ); 00146 $len = mt_rand( 10, 100 ); 00147 00148 $str = ''; 00149 for ( $j = 0; $j < $len; $j++ ) { 00150 $str .= $chars[mt_rand( 0, $base - 1 )]; 00151 } 00152 00153 $x[] = array( $base, $str ); 00154 } 00155 00156 return $x; 00157 } 00158 00162 public function testIdentity( $base, $number ) { 00163 $this->assertSame( $number, wfBaseConvert( $number, $base, $base, strlen( $number ) ) ); 00164 } 00165 00166 public function testInvalid() { 00167 $this->assertFalse( wfBaseConvert( '101', 1, 15 ) ); 00168 $this->assertFalse( wfBaseConvert( '101', 15, 1 ) ); 00169 $this->assertFalse( wfBaseConvert( '101', 37, 15 ) ); 00170 $this->assertFalse( wfBaseConvert( '101', 15, 37 ) ); 00171 $this->assertFalse( wfBaseConvert( 'abcde', 10, 11 ) ); 00172 $this->assertFalse( wfBaseConvert( '12930', 2, 10 ) ); 00173 $this->assertFalse( wfBaseConvert( '101', 'abc', 15 ) ); 00174 $this->assertFalse( wfBaseConvert( '101', 15, 'abc' ) ); 00175 } 00176 00177 public function testPadding() { 00178 $number = "10101010101"; 00179 $this->assertSame( strlen( $number ) + 5, strlen( wfBaseConvert( $number, 2, 2, strlen( $number ) + 5 ) ) ); 00180 $this->assertSame( strlen( $number ), strlen( wfBaseConvert( $number, 2, 2, strlen( $number ) - 5 ) ) ); 00181 } 00182 }