MediaWiki  REL1_22
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     return $out;
00046 }
00047 
00048 /* Duplicate of the cleanUp() path for ICU usage */
00049 function donorm( $str ) {
00050     # We exclude a few chars that ICU would not.
00051     $str = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str );
00052     $str = str_replace( UTF8_FFFE, UTF8_REPLACEMENT, $str );
00053     $str = str_replace( UTF8_FFFF, UTF8_REPLACEMENT, $str );
00054 
00055     # UnicodeString constructor fails if the string ends with a head byte.
00056     # Add a junk char at the end, we'll strip it off
00057     return rtrim( utf8_normalize( $str . "\x01", UtfNormal::UNORM_NFC ), "\x01" );
00058 }
00059 
00060 function showDiffs( $a, $b ) {
00061     $ota = explode( "\n", str_replace( "\r\n", "\n", $a ) );
00062     $nta = explode( "\n", str_replace( "\r\n", "\n", $b ) );
00063 
00064     $diffs = new Diff( $ota, $nta );
00065     $formatter = new TableDiffFormatter();
00066     $funky = $formatter->format( $diffs );
00067     $matches = array();
00068     preg_match_all( '/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches );
00069     foreach( $matches[1] as $bit ) {
00070         $hex = bin2hex( $bit );
00071         echo "\t$hex\n";
00072     }
00073 }
00074 
00075 $size = 16;
00076 $n = 0;
00077 while( true ) {
00078     $n++;
00079     echo "$n\n";
00080 
00081     $str = randomString( $size, true);
00082     $clean = UtfNormal::cleanUp( $str );
00083     $norm = donorm( $str );
00084 
00085     echo strlen( $clean ) . ", " . strlen( $norm );
00086     if( $clean == $norm ) {
00087         echo " (match)\n";
00088     } else {
00089         echo " (FAIL)\n";
00090         echo "\traw: " . bin2hex( $str ) . "\n" .
00091              "\tphp: " . bin2hex( $clean ) . "\n" .
00092              "\ticu: " . bin2hex( $norm ) . "\n";
00093         echo "\n\tdiffs:\n";
00094         showDiffs( $clean, $norm );
00095         die();
00096     }
00097 
00098 
00099     $str = '';
00100     $clean = '';
00101     $norm = '';
00102 }