MediaWiki  REL1_22
Utf8Test.php
Go to the documentation of this file.
00001 <?php
00030 if ( PHP_SAPI != 'cli' ) {
00031     die( "Run me from the command line please.\n" );
00032 }
00033 
00034 require_once 'UtfNormalDefines.php';
00035 require_once 'UtfNormalUtil.php';
00036 require_once 'UtfNormal.php';
00037 mb_internal_encoding( "utf-8" );
00038 
00039 $verbose = false;
00040 #$verbose = true;
00041 
00042 $in = fopen( "UTF-8-test.txt", "rt" );
00043 if( !$in ) {
00044     print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
00045     print "If necessary, manually download this file. It can be obtained at\n";
00046     print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
00047     exit(-1);
00048 }
00049 
00050 $columns = 0;
00051 while( false !== ( $line = fgets( $in ) ) ) {
00052     $matches = array();
00053     if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
00054         $columns = strpos( $line, '|' );
00055         break;
00056     }
00057 }
00058 
00059 if( !$columns ) {
00060     print "Something seems to be wrong; couldn't extract line length.\n";
00061     print "Check that UTF-8-test.txt was downloaded correctly from\n";
00062     print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
00063     exit(-1);
00064 }
00065 
00066 # print "$columns\n";
00067 
00068 $ignore = array(
00069     # These two lines actually seem to be corrupt
00070     '2.1.1', '2.2.1' );
00071 
00072 $exceptions = array(
00073     # Tests that should mark invalid characters due to using long
00074     # sequences beyond what is now considered legal.
00075     '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
00076 
00077     # Literal 0xffff, which is illegal
00078     '2.2.3' );
00079 
00080 $longTests = array(
00081     # These tests span multiple lines
00082     '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
00083     '3.4' );
00084 
00085 # These tests are not in proper subsections
00086 $sectionTests = array( '3.4' );
00087 
00088 $section = null;
00089 $test = '';
00090 $failed = 0;
00091 $success = 0;
00092 $total = 0;
00093 while( false !== ( $line = fgets( $in ) ) ) {
00094     $matches = array();
00095     if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
00096         $section = $matches[1];
00097         print $line;
00098         continue;
00099     }
00100     if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
00101         $test = $matches[1];
00102 
00103         if( in_array( $test, $ignore ) ) {
00104             continue;
00105         }
00106         if( in_array( $test, $longTests ) ) {
00107             $line = fgets( $in );
00108             for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
00109                 testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
00110             }
00111         } else {
00112             testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
00113         }
00114     }
00115 }
00116 
00117 if( $failed ) {
00118     echo "\nFailed $failed tests.\n";
00119     echo "UTF-8 DECODER TEST FAILED\n";
00120     exit (-1);
00121 }
00122 
00123 echo "UTF-8 DECODER TEST SUCCESS!\n";
00124 exit (0);
00125 
00126 
00127 function testLine( $test, $line, &$total, &$success, &$failed, $columns, $exceptions, $verbose ) {
00128     $stripped = $line;
00129     UtfNormal::quickisNFCVerify( $stripped );
00130 
00131     $same = ( $line == $stripped );
00132     $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
00133     if( $len == 0 ) {
00134         $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
00135     }
00136 
00137     $ok = $same ^ ($test >= 3 );
00138 
00139     $ok ^= in_array( $test, $exceptions );
00140 
00141     $ok &= ($columns == $len);
00142 
00143     $total++;
00144     if( $ok ) {
00145         $success++;
00146     } else {
00147         $failed++;
00148     }
00149 
00150     if( $verbose || !$ok ) {
00151         print str_replace( "\n", "$len\n", $stripped );
00152     }
00153 }