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