4 * Test the CDB reader/writer
6 class CdbTest
extends MediaWikiTestCase
{
8 protected function setUp() {
10 if ( !CdbReader
::haveExtension() ) {
11 $this->markTestSkipped( 'Native CDB support is not available' );
18 public function testCdb() {
20 if ( !is_writable( $dir ) ) {
21 $this->markTestSkipped( "Temp dir isn't writable" );
24 $phpcdbfile = $this->getNewTempFile();
25 $dbacdbfile = $this->getNewTempFile();
27 $w1 = new CdbWriter_PHP( $phpcdbfile );
28 $w2 = new CdbWriter_DBA( $dbacdbfile );
31 for ( $i = 0; $i < 1000; $i++
) {
32 $key = $this->randomString();
33 $value = $this->randomString();
34 $w1->set( $key, $value );
35 $w2->set( $key, $value );
37 if ( !isset( $data[$key] ) ) {
46 md5_file( $phpcdbfile ),
47 md5_file( $dbacdbfile ),
51 $r1 = new CdbReader_PHP( $phpcdbfile );
52 $r2 = new CdbReader_DBA( $dbacdbfile );
54 foreach ( $data as $key => $value ) {
59 $v1 = $r1->get( $key );
60 $v2 = $r2->get( $key );
62 $v1 = $v1 === false ?
'(not found)' : $v1;
63 $v2 = $v2 === false ?
'(not found)' : $v2;
65 # cdbAssert( 'Mismatch', $key, $v1, $v2 );
66 $this->cdbAssert( "PHP error", $key, $v1, $value );
67 $this->cdbAssert( "DBA error", $key, $v2, $value );
71 private function randomString() {
72 $len = mt_rand( 0, 10 );
74 for ( $j = 0; $j < $len; $j++
) {
75 $s .= chr( mt_rand( 0, 255 ) );
81 private function cdbAssert( $msg, $key, $v1, $v2 ) {
85 $msg . ', k=' . bin2hex( $key )