diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2010-07-28 11:52:48 +0200 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2010-07-28 11:52:48 +0200 |
commit | 222b01f5169f1c7e69762e0e8904c24f78f71882 (patch) | |
tree | 8e932e12546bb991357ec48eb1638d1770be7a35 /maintenance/tests/CdbTest.php | |
parent | 00ab76a6b686e98a914afc1975812d2b1aaa7016 (diff) |
update to MediaWiki 1.16.0
Diffstat (limited to 'maintenance/tests/CdbTest.php')
-rw-r--r-- | maintenance/tests/CdbTest.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/maintenance/tests/CdbTest.php b/maintenance/tests/CdbTest.php new file mode 100644 index 00000000..444229e7 --- /dev/null +++ b/maintenance/tests/CdbTest.php @@ -0,0 +1,79 @@ +<?php + +/** + * Test the CDB reader/writer + */ + +class CdbTest extends PHPUnit_Framework_TestCase { + + public function setup() { + if ( !CdbReader::haveExtension() ) { + $this->markTestIncomplete( 'This test requires native CDB support to be present.' ); + } + } + + public function testCdb() { + $w1 = new CdbWriter_PHP( 'php.cdb' ); + $w2 = new CdbWriter_DBA( 'dba.cdb' ); + + $data = array(); + for ( $i = 0; $i < 1000; $i++ ) { + $key = $this->randomString(); + $value = $this->randomString(); + $w1->set( $key, $value ); + $w2->set( $key, $value ); + + if ( !isset( $data[$key] ) ) { + $data[$key] = $value; + } + } + + $w1->close(); + $w2->close(); + + $this->assertEquals( + md5_file( 'dba.cdb' ), + md5_file( 'php.cdb' ), + 'same hash' + ); + + $r1 = new CdbReader_PHP( 'php.cdb' ); + $r2 = new CdbReader_DBA( 'dba.cdb' ); + + foreach ( $data as $key => $value ) { + if ( $key === '' ) { + // Known bug + continue; + } + $v1 = $r1->get( $key ); + $v2 = $r2->get( $key ); + + $v1 = $v1 === false ? '(not found)' : $v1; + $v2 = $v2 === false ? '(not found)' : $v2; + + #cdbAssert( 'Mismatch', $key, $v1, $v2 ); + $this->cdbAssert( "PHP error", $key, $v1, $value ); + $this->cdbAssert( "DBA error", $key, $v2, $value ); + } + + unlink( 'dba.cdb' ); + unlink( 'php.cdb' ); + } + + private function randomString() { + $len = mt_rand( 0, 10 ); + $s = ''; + for ( $j = 0; $j < $len; $j++ ) { + $s .= chr( mt_rand( 0, 255 ) ); + } + return $s; + } + + private function cdbAssert( $msg, $key, $v1, $v2 ) { + $this->assertEquals( + $v2, + $v1, + $msg . ', k=' . bin2hex( $key ) + ); + } +} |