MediaWiki  REL1_24
RandomTest.php
Go to the documentation of this file.
00001 <?php
00029 if ( PHP_SAPI != 'cli' ) {
00030     die( "Run me from the command line please.\n" );
00031 }
00032 
00034 require_once 'UtfNormal.php';
00035 require_once '../diff/DifferenceEngine.php';
00036 
00037 dl( 'php_utfnormal.so' );
00038 
00039 # mt_srand( 99999 );
00040 
00041 function randomString( $length, $nullOk, $ascii = false ) {
00042     $out = '';
00043     for ( $i = 0; $i < $length; $i++ )
00044         $out .= chr( mt_rand( $nullOk ? 0 : 1, $ascii ? 127 : 255 ) );
00045 
00046     return $out;
00047 }
00048 
00049 /* Duplicate of the cleanUp() path for ICU usage */
00050 function donorm( $str ) {
00051     # We exclude a few chars that ICU would not.
00052     $str = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str );
00053     $str = str_replace( UTF8_FFFE, UTF8_REPLACEMENT, $str );
00054     $str = str_replace( UTF8_FFFF, UTF8_REPLACEMENT, $str );
00055 
00056     # UnicodeString constructor fails if the string ends with a head byte.
00057     # Add a junk char at the end, we'll strip it off
00058     return rtrim( utf8_normalize( $str . "\x01", UtfNormal::UNORM_NFC ), "\x01" );
00059 }
00060 
00061 function showDiffs( $a, $b ) {
00062     $ota = explode( "\n", str_replace( "\r\n", "\n", $a ) );
00063     $nta = explode( "\n", str_replace( "\r\n", "\n", $b ) );
00064 
00065     $diffs = new Diff( $ota, $nta );
00066     $formatter = new TableDiffFormatter();
00067     $funky = $formatter->format( $diffs );
00068     $matches = array();
00069     preg_match_all( '/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches );
00070     foreach ( $matches[1] as $bit ) {
00071         $hex = bin2hex( $bit );
00072         echo "\t$hex\n";
00073     }
00074 }
00075 
00076 $size = 16;
00077 $n = 0;
00078 while ( true ) {
00079     $n++;
00080     echo "$n\n";
00081 
00082     $str = randomString( $size, true );
00083     $clean = UtfNormal::cleanUp( $str );
00084     $norm = donorm( $str );
00085 
00086     echo strlen( $clean ) . ", " . strlen( $norm );
00087     if ( $clean == $norm ) {
00088         echo " (match)\n";
00089     } else {
00090         echo " (FAIL)\n";
00091         echo "\traw: " . bin2hex( $str ) . "\n" .
00092             "\tphp: " . bin2hex( $clean ) . "\n" .
00093             "\ticu: " . bin2hex( $norm ) . "\n";
00094         echo "\n\tdiffs:\n";
00095         showDiffs( $clean, $norm );
00096         die();
00097     }
00098 
00099     $str = '';
00100     $clean = '';
00101     $norm = '';
00102 }