MediaWiki  REL1_19
RandomTest.php
Go to the documentation of this file.
00001 <?php
00029 if( php_sapi_name() != '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 wfMsg($x) {
00061         return $x;
00062 }
00063 
00064 function showDiffs( $a, $b ) {
00065         $ota = explode( "\n", str_replace( "\r\n", "\n", $a ) );
00066         $nta = explode( "\n", str_replace( "\r\n", "\n", $b ) );
00067 
00068         $diffs = new Diff( $ota, $nta );
00069         $formatter = new TableDiffFormatter();
00070         $funky = $formatter->format( $diffs );
00071         $matches = array();
00072         preg_match_all( '/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches );
00073         foreach( $matches[1] as $bit ) {
00074                 $hex = bin2hex( $bit );
00075                 echo "\t$hex\n";
00076         }
00077 }
00078 
00079 $size = 16;
00080 $n = 0;
00081 while( true ) {
00082         $n++;
00083         echo "$n\n";
00084 
00085         $str = randomString( $size, true);
00086         $clean = UtfNormal::cleanUp( $str );
00087         $norm = donorm( $str );
00088 
00089         echo strlen( $clean ) . ", " . strlen( $norm );
00090         if( $clean == $norm ) {
00091                 echo " (match)\n";
00092         } else {
00093                 echo " (FAIL)\n";
00094                 echo "\traw: " . bin2hex( $str ) . "\n" .
00095                          "\tphp: " . bin2hex( $clean ) . "\n" .
00096                          "\ticu: " . bin2hex( $norm ) . "\n";
00097                 echo "\n\tdiffs:\n";
00098                 showDiffs( $clean, $norm );
00099                 die();
00100         }
00101 
00102 
00103         $str = '';
00104         $clean = '';
00105         $norm = '';
00106 }