MediaWiki
REL1_19
|
00001 <?php 00002 00007 class CdbTest extends MediaWikiTestCase { 00008 00009 public function setUp() { 00010 if ( !CdbReader::haveExtension() ) { 00011 $this->markTestIncomplete( 'This test requires native CDB support to be present.' ); 00012 } 00013 } 00014 00015 public function testCdb() { 00016 $dir = wfTempDir(); 00017 if ( !is_writable( $dir ) ) { 00018 $this->markTestSkipped( "Temp dir isn't writable" ); 00019 } 00020 00021 $w1 = new CdbWriter_PHP( "$dir/php.cdb" ); 00022 $w2 = new CdbWriter_DBA( "$dir/dba.cdb" ); 00023 00024 $data = array(); 00025 for ( $i = 0; $i < 1000; $i++ ) { 00026 $key = $this->randomString(); 00027 $value = $this->randomString(); 00028 $w1->set( $key, $value ); 00029 $w2->set( $key, $value ); 00030 00031 if ( !isset( $data[$key] ) ) { 00032 $data[$key] = $value; 00033 } 00034 } 00035 00036 $w1->close(); 00037 $w2->close(); 00038 00039 $this->assertEquals( 00040 md5_file( "$dir/dba.cdb" ), 00041 md5_file( "$dir/php.cdb" ), 00042 'same hash' 00043 ); 00044 00045 $r1 = new CdbReader_PHP( "$dir/php.cdb" ); 00046 $r2 = new CdbReader_DBA( "$dir/dba.cdb" ); 00047 00048 foreach ( $data as $key => $value ) { 00049 if ( $key === '' ) { 00050 // Known bug 00051 continue; 00052 } 00053 $v1 = $r1->get( $key ); 00054 $v2 = $r2->get( $key ); 00055 00056 $v1 = $v1 === false ? '(not found)' : $v1; 00057 $v2 = $v2 === false ? '(not found)' : $v2; 00058 00059 # cdbAssert( 'Mismatch', $key, $v1, $v2 ); 00060 $this->cdbAssert( "PHP error", $key, $v1, $value ); 00061 $this->cdbAssert( "DBA error", $key, $v2, $value ); 00062 } 00063 00064 unlink( "$dir/dba.cdb" ); 00065 unlink( "$dir/php.cdb" ); 00066 } 00067 00068 private function randomString() { 00069 $len = mt_rand( 0, 10 ); 00070 $s = ''; 00071 for ( $j = 0; $j < $len; $j++ ) { 00072 $s .= chr( mt_rand( 0, 255 ) ); 00073 } 00074 return $s; 00075 } 00076 00077 private function cdbAssert( $msg, $key, $v1, $v2 ) { 00078 $this->assertEquals( 00079 $v2, 00080 $v1, 00081 $msg . ', k=' . bin2hex( $key ) 00082 ); 00083 } 00084 }