00001 <?php 00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com> 00003 # http://www.mediawiki.org/ 00004 # 00005 # This program is free software; you can redistribute it and/or modify 00006 # it under the terms of the GNU General Public License as published by 00007 # the Free Software Foundation; either version 2 of the License, or 00008 # (at your option) any later version. 00009 # 00010 # This program is distributed in the hope that it will be useful, 00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 # GNU General Public License for more details. 00014 # 00015 # You should have received a copy of the GNU General Public License along 00016 # with this program; if not, write to the Free Software Foundation, Inc., 00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 # http://www.gnu.org/copyleft/gpl.html 00019 00029 require_once 'UtfNormalUtil.php'; 00030 require_once 'UtfNormal.php'; 00031 mb_internal_encoding( "utf-8" ); 00032 00033 #$verbose = true; 00034 if( php_sapi_name() != 'cli' ) { 00035 die( "Run me from the command line please.\n" ); 00036 } 00037 00038 $in = fopen( "UTF-8-test.txt", "rt" ); 00039 if( !$in ) { 00040 print "Couldn't open UTF-8-test.txt -- can't run tests.\n"; 00041 print "If necessary, manually download this file. It can be obtained at\n"; 00042 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt"; 00043 exit(-1); 00044 } 00045 00046 $columns = 0; 00047 while( false !== ( $line = fgets( $in ) ) ) { 00048 $matches = array(); 00049 if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) { 00050 $columns = strpos( $line, '|' ); 00051 break; 00052 } 00053 } 00054 00055 if( !$columns ) { 00056 print "Something seems to be wrong; couldn't extract line length.\n"; 00057 print "Check that UTF-8-test.txt was downloaded correctly from\n"; 00058 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt"; 00059 exit(-1); 00060 } 00061 00062 # print "$columns\n"; 00063 00064 $ignore = array( 00065 # These two lines actually seem to be corrupt 00066 '2.1.1', '2.2.1' ); 00067 00068 $exceptions = array( 00069 # Tests that should mark invalid characters due to using long 00070 # sequences beyond what is now considered legal. 00071 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5', 00072 00073 # Literal 0xffff, which is illegal 00074 '2.2.3' ); 00075 00076 $longTests = array( 00077 # These tests span multiple lines 00078 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5', 00079 '3.4' ); 00080 00081 # These tests are not in proper subsections 00082 $sectionTests = array( '3.4' ); 00083 00084 $section = NULL; 00085 $test = ''; 00086 $failed = 0; 00087 $success = 0; 00088 $total = 0; 00089 while( false !== ( $line = fgets( $in ) ) ) { 00090 $matches = array(); 00091 if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) { 00092 $section = $matches[1]; 00093 print $line; 00094 continue; 00095 } 00096 if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) { 00097 $test = $matches[1]; 00098 00099 if( in_array( $test, $ignore ) ) { 00100 continue; 00101 } 00102 if( in_array( $test, $longTests ) ) { 00103 $line = fgets( $in ); 00104 for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) { 00105 testLine( $test, $line, $total, $success, $failed ); 00106 } 00107 } else { 00108 testLine( $test, $line, $total, $success, $failed ); 00109 } 00110 } 00111 } 00112 00113 if( $failed ) { 00114 echo "\nFailed $failed tests.\n"; 00115 echo "UTF-8 DECODER TEST FAILED\n"; 00116 exit (-1); 00117 } 00118 00119 echo "UTF-8 DECODER TEST SUCCESS!\n"; 00120 exit (0); 00121 00122 00123 function testLine( $test, $line, &$total, &$success, &$failed ) { 00124 $stripped = $line; 00125 UtfNormal::quickisNFCVerify( $stripped ); 00126 00127 $same = ( $line == $stripped ); 00128 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) ); 00129 if( $len == 0 ) { 00130 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) ); 00131 } 00132 00133 global $columns; 00134 $ok = $same ^ ($test >= 3 ); 00135 00136 global $exceptions; 00137 $ok ^= in_array( $test, $exceptions ); 00138 00139 $ok &= ($columns == $len); 00140 00141 $total++; 00142 if( $ok ) { 00143 $success++; 00144 } else { 00145 $failed++; 00146 } 00147 global $verbose; 00148 if( $verbose || !$ok ) { 00149 print str_replace( "\n", "$len\n", $stripped ); 00150 } 00151 }