MediaWiki  REL1_24
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 
00109             // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
00110             for ( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
00111                 // @codingStandardsIgnoreEnd
00112 
00113                 testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
00114             }
00115         } else {
00116             testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
00117         }
00118     }
00119 }
00120 
00121 if ( $failed ) {
00122     echo "\nFailed $failed tests.\n";
00123     echo "UTF-8 DECODER TEST FAILED\n";
00124     exit ( -1 );
00125 }
00126 
00127 echo "UTF-8 DECODER TEST SUCCESS!\n";
00128 exit ( 0 );
00129 
00130 function testLine( $test, $line, &$total, &$success, &$failed, $columns, $exceptions, $verbose ) {
00131     $stripped = $line;
00132     UtfNormal::quickisNFCVerify( $stripped );
00133 
00134     $same = ( $line == $stripped );
00135     $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
00136     if ( $len == 0 ) {
00137         $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
00138     }
00139 
00140     $ok = $same ^ ( $test >= 3 );
00141 
00142     $ok ^= in_array( $test, $exceptions );
00143 
00144     $ok &= ( $columns == $len );
00145 
00146     $total++;
00147     if ( $ok ) {
00148         $success++;
00149     } else {
00150         $failed++;
00151     }
00152 
00153     if ( $verbose || !$ok ) {
00154         print str_replace( "\n", "$len\n", $stripped );
00155     }
00156 }