Index: trunk/phase3/maintenance/cdb-test.php |
— | — | @@ -1,83 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Test the CDB reader/writer |
6 | | - */ |
7 | | - |
8 | | -require_once( dirname(__FILE__) . '/Maintenance.php' ); |
9 | | - |
10 | | -class CdbTest extends Maintenance { |
11 | | - public function __construct() { |
12 | | - parent::__construct(); |
13 | | - $this->mDescription = "CDB read/write test"; |
14 | | - } |
15 | | - |
16 | | - public function execute() { |
17 | | - $this->output( "Write test...\n" ); |
18 | | - |
19 | | - $w1 = new CdbWriter_PHP( 'php.cdb' ); |
20 | | - $w2 = new CdbWriter_DBA( 'dba.cdb' ); |
21 | | - |
22 | | - $data = array(); |
23 | | - for ( $i = 0; $i < 100000; $i++ ) { |
24 | | - $key = $this->randomString(); |
25 | | - $value = $this->randomString(); |
26 | | - $w1->set( $key, $value ); |
27 | | - $w2->set( $key, $value ); |
28 | | - |
29 | | - if ( !isset( $data[$key] ) ) { |
30 | | - $data[$key] = $value; |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - $w1->close(); |
35 | | - $w2->close(); |
36 | | - |
37 | | - passthru( 'md5sum php.cdb dba.cdb' ); |
38 | | - |
39 | | - $this->output( "Read test...\n" ); |
40 | | - |
41 | | - $r1 = new CdbReader_PHP( 'php.cdb' ); |
42 | | - $r2 = new CdbReader_DBA( 'dba.cdb' ); |
43 | | - |
44 | | - foreach ( $data as $key => $value ) { |
45 | | - if ( $key === '' ) { |
46 | | - // Known bug |
47 | | - continue; |
48 | | - } |
49 | | - $v1 = $r1->get( $key ); |
50 | | - $v2 = $r2->get( $key ); |
51 | | - |
52 | | - $v1 = $v1 === false ? '(not found)' : $v1; |
53 | | - $v2 = $v2 === false ? '(not found)' : $v2; |
54 | | - |
55 | | - #cdbAssert( 'Mismatch', $key, $v1, $v2 ); |
56 | | - $this->cdbAssert( "PHP error", $key, $v1, $value ); |
57 | | - $this->cdbAssert( "DBA error", $key, $v2, $value ); |
58 | | - } |
59 | | - $this->output( "Done.\n" ); |
60 | | - } |
61 | | - |
62 | | - private function randomString() { |
63 | | - $len = mt_rand( 0, 10 ); |
64 | | - $s = ''; |
65 | | - for ( $j = 0; $j < $len; $j++ ) { |
66 | | - $s .= chr( mt_rand( 0, 255 ) ); |
67 | | - } |
68 | | - return $s; |
69 | | - } |
70 | | - |
71 | | - private function cdbAssert( $msg, $key, $v1, $v2 ) { |
72 | | - if ( $v1 !== $v2 ) { |
73 | | - $this->output( $msg . ', k=' . bin2hex( $key ) . |
74 | | - ', v1=' . bin2hex( $v1 ) . |
75 | | - ', v2=' . bin2hex( $v2 ) . "\n" ); |
76 | | - return false; |
77 | | - } else { |
78 | | - return true; |
79 | | - } |
80 | | - } |
81 | | -} |
82 | | - |
83 | | -$maintClass = "CdbTest"; |
84 | | -require_once( DO_MAINTENANCE ); |
Index: trunk/phase3/maintenance/tests/CdbTest.php |
— | — | @@ -0,0 +1,73 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Test the CDB reader/writer |
| 6 | + */ |
| 7 | + |
| 8 | +class CdbTest extends PHPUnit_Framework_TestCase { |
| 9 | + |
| 10 | + public function testCdb() { |
| 11 | + $w1 = new CdbWriter_PHP( 'php.cdb' ); |
| 12 | + $w2 = new CdbWriter_DBA( 'dba.cdb' ); |
| 13 | + |
| 14 | + $data = array(); |
| 15 | + for ( $i = 0; $i < 1000; $i++ ) { |
| 16 | + $key = $this->randomString(); |
| 17 | + $value = $this->randomString(); |
| 18 | + $w1->set( $key, $value ); |
| 19 | + $w2->set( $key, $value ); |
| 20 | + |
| 21 | + if ( !isset( $data[$key] ) ) { |
| 22 | + $data[$key] = $value; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + $w1->close(); |
| 27 | + $w2->close(); |
| 28 | + |
| 29 | + $this->assertEquals( |
| 30 | + md5_file( 'dba.cdb' ), |
| 31 | + md5_file( 'php.cdb' ), |
| 32 | + 'same hash' |
| 33 | + ); |
| 34 | + |
| 35 | + $r1 = new CdbReader_PHP( 'php.cdb' ); |
| 36 | + $r2 = new CdbReader_DBA( 'dba.cdb' ); |
| 37 | + |
| 38 | + foreach ( $data as $key => $value ) { |
| 39 | + if ( $key === '' ) { |
| 40 | + // Known bug |
| 41 | + continue; |
| 42 | + } |
| 43 | + $v1 = $r1->get( $key ); |
| 44 | + $v2 = $r2->get( $key ); |
| 45 | + |
| 46 | + $v1 = $v1 === false ? '(not found)' : $v1; |
| 47 | + $v2 = $v2 === false ? '(not found)' : $v2; |
| 48 | + |
| 49 | + #cdbAssert( 'Mismatch', $key, $v1, $v2 ); |
| 50 | + $this->cdbAssert( "PHP error", $key, $v1, $value ); |
| 51 | + $this->cdbAssert( "DBA error", $key, $v2, $value ); |
| 52 | + } |
| 53 | + |
| 54 | + unlink( 'dba.cdb' ); |
| 55 | + unlink( 'php.cdb' ); |
| 56 | + } |
| 57 | + |
| 58 | + private function randomString() { |
| 59 | + $len = mt_rand( 0, 10 ); |
| 60 | + $s = ''; |
| 61 | + for ( $j = 0; $j < $len; $j++ ) { |
| 62 | + $s .= chr( mt_rand( 0, 255 ) ); |
| 63 | + } |
| 64 | + return $s; |
| 65 | + } |
| 66 | + |
| 67 | + private function cdbAssert( $msg, $key, $v1, $v2 ) { |
| 68 | + $this->assertEquals( |
| 69 | + $v2, |
| 70 | + $v1, |
| 71 | + $msg . ', k=' . bin2hex( $key ) |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
Property changes on: trunk/phase3/maintenance/tests/CdbTest.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 75 | + native |