2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 # Runs the UTF-8 decoder test at:
21 # http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
23 require_once 'UtfNormalUtil.php';
24 require_once 'UtfNormal.php';
25 mb_internal_encoding( "utf-8" );
28 if( php_sapi_name() != 'cli' ) {
29 die( "Run me from the command line please.\n" );
32 $in = fopen( "UTF-8-test.txt", "rt" );
34 print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
35 print "If necessary, manually download this file. It can be obtained at\n";
36 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
41 while( false !== ( $line = fgets( $in ) ) ) {
42 if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
43 $columns = strpos( $line, '|' );
49 print "Something seems to be wrong; couldn't extract line length.\n";
50 print "Check that UTF-8-test.txt was downloaded correctly from\n";
51 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
58 # These two lines actually seem to be corrupt
62 # Tests that should mark invalid characters due to using long
63 # sequences beyond what is now considered legal.
64 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
66 # Literal 0xffff, which is illegal
70 # These tests span multiple lines
71 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
74 # These tests are not in proper subsections
75 $sectionTests = array( '3.4' );
82 while( false !== ( $line = fgets( $in ) ) ) {
83 if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
84 $section = $matches[1];
88 if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
91 if( in_array( $test, $ignore ) ) {
94 if( in_array( $test, $longTests ) ) {
96 for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
97 testLine( $test, $line, $total, $success, $failed );
100 testLine( $test, $line, $total, $success, $failed );
106 echo "\nFailed $failed tests.\n";
107 echo "UTF-8 DECODER TEST FAILED\n";
111 echo "UTF-8 DECODER TEST SUCCESS!\n";
115 function testLine( $test, $line, &$total, &$success, &$failed ) {
117 UtfNormal
::quickisNFCVerify( $stripped );
119 $same = ( $line == $stripped );
120 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
122 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
126 $ok = $same ^
($test >= 3 );
129 $ok ^
= in_array( $test, $exceptions );
131 $ok &= ($columns == $len);
140 if( $verbose ||
!$ok ) {
141 print str_replace( "\n", "$len\n", $stripped );