MediaWiki  REL1_22
UtfNormalTest.php
Go to the documentation of this file.
00001 <?php
00028 if( PHP_SAPI != 'cli' ) {
00029     die( "Run me from the command line please.\n" );
00030 }
00031 
00032 $verbose = true;
00033 #define( 'PRETTY_UTF8', true );
00034 
00035 if( defined( 'PRETTY_UTF8' ) ) {
00036     function pretty( $string ) {
00037         return strtoupper( bin2hex( $string ) );
00038     }
00039 } else {
00044     function pretty( $string ) {
00045         return strtoupper( utf8ToHexSequence( $string ) );
00046     }
00047 }
00048 
00049 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
00050     dl( 'php_utfnormal.so' );
00051 }
00052 
00053 require_once 'UtfNormalDefines.php';
00054 require_once 'UtfNormalUtil.php';
00055 require_once 'UtfNormal.php';
00056 
00057 $in = fopen("NormalizationTest.txt", "rt");
00058 if( !$in ) {
00059     print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
00060     print "If necessary, manually download this file. It can be obtained at\n";
00061     print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
00062     exit(-1);
00063 }
00064 
00065 $normalizer = new UtfNormal;
00066 
00067 $total = 0;
00068 $success = 0;
00069 $failure = 0;
00070 $ok = true;
00071 $testedChars = array();
00072 while( false !== ( $line = fgets( $in ) ) ) {
00073     list( $data, $comment ) = explode( '#', $line );
00074     if( $data === '' ) continue;
00075     $matches = array();
00076     if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
00077         if( $matches[1] > 0 ) {
00078             $ok = reportResults( $total, $success, $failure ) && $ok;
00079         }
00080         print "Part {$matches[1]}: $comment";
00081         continue;
00082     }
00083 
00084     $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
00085     array_unshift( $columns, '' );
00086 
00087     $testedChars[$columns[1]] = true;
00088     $total++;
00089     if( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
00090         $success++;
00091     } else {
00092         $failure++;
00093         # print "FAILED: $comment";
00094     }
00095     if( $total % 100 == 0 ) print "$total ";
00096 }
00097 fclose( $in );
00098 
00099 $ok = reportResults( $total, $success, $failure ) && $ok;
00100 
00101 $in = fopen("UnicodeData.txt", "rt" );
00102 if( !$in ) {
00103     print "Can't open UnicodeData.txt for reading.\n";
00104     print "If necessary, fetch this file from the internet:\n";
00105     print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
00106     exit(-1);
00107 }
00108 print "Now testing invariants...\n";
00109 while( false !== ($line = fgets( $in ) ) ) {
00110     $cols = explode( ';', $line );
00111     $char = codepointToUtf8( hexdec( $cols[0] ) );
00112     $desc = $cols[0] . ": " . $cols[1];
00113     if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
00114         # Can't check NULL with the ICU plugin, as null bytes fail in C land.
00115         # Skip other control characters, as we strip them for XML safety.
00116         # Surrogates are illegal on their own or in UTF-8, ignore.
00117         continue;
00118     }
00119     if( empty( $testedChars[$char] ) ) {
00120         $total++;
00121         if( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
00122             $success++;
00123         } else {
00124             $failure++;
00125         }
00126         if( $total % 100 == 0 ) print "$total ";
00127     }
00128 }
00129 fclose( $in );
00130 
00131 $ok = reportResults( $total, $success, $failure ) && $ok;
00132 
00133 if( $ok ) {
00134     print "TEST SUCCEEDED!\n";
00135     exit(0);
00136 } else {
00137     print "TEST FAILED!\n";
00138     exit(-1);
00139 }
00140 
00141 ## ------
00142 
00143 function reportResults( &$total, &$success, &$failure ) {
00144     $percSucc = intval( $success * 100 / $total );
00145     $percFail = intval( $failure * 100 / $total );
00146     print "\n";
00147     print "$success tests successful ($percSucc%)\n";
00148     print "$failure tests failed ($percFail%)\n\n";
00149     $ok = ($success > 0 && $failure == 0);
00150     $total = 0;
00151     $success = 0;
00152     $failure = 0;
00153     return $ok;
00154 }
00155 
00156 function testNormals( &$u, $c, $comment, $verbose, $reportFailure = false ) {
00157     $result = testNFC( $u, $c, $comment, $reportFailure );
00158     $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
00159     $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
00160     $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
00161     $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
00162 
00163     if( $verbose && !$result && !$reportFailure ) {
00164         print $comment;
00165         testNormals( $u, $c, $comment, $verbose, true );
00166     }
00167     return $result;
00168 }
00169 
00170 function verbosify( $a, $b, $col, $form, $verbose ) {
00171     #$result = ($a === $b);
00172     $result = (strcmp( $a, $b ) == 0);
00173     if( $verbose ) {
00174         $aa = pretty( $a );
00175         $bb = pretty( $b );
00176         $ok = $result ? "succeed" : " failed";
00177         $eq = $result ? "==" : "!=";
00178         print "  $ok $form c$col '$aa' $eq '$bb'\n";
00179     }
00180     return $result;
00181 }
00182 
00183 function testNFC( &$u, $c, $comment, $verbose ) {
00184     $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
00185     $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
00186     $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
00187     $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
00188     $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
00189     return $result;
00190 }
00191 
00192 function testCleanUp( &$u, $c, $comment, $verbose ) {
00193     $x = $c[1];
00194     $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
00195     $x = $c[2];
00196     $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
00197     $x = $c[3];
00198     $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
00199     $x = $c[4];
00200     $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
00201     $x = $c[5];
00202     $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
00203     return $result;
00204 }
00205 
00206 function testNFD( &$u, $c, $comment, $verbose ) {
00207     $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
00208     $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
00209     $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
00210     $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
00211     $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
00212     return $result;
00213 }
00214 
00215 function testNFKC( &$u, $c, $comment, $verbose ) {
00216     $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
00217     $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
00218     $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
00219     $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
00220     $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
00221     return $result;
00222 }
00223 
00224 function testNFKD( &$u, $c, $comment, $verbose ) {
00225     $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
00226     $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
00227     $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
00228     $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
00229     $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
00230     return $result;
00231 }
00232 
00233 function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
00234     $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
00235     $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
00236     $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
00237     $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
00238     $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
00239 
00240     if( $verbose && !$result && !$reportFailure ) {
00241         print $desc;
00242         testInvariant( $u, $char, $desc, $verbose, true );
00243     }
00244     return $result;
00245 }