Load form C data on demand; if we are dealing in all-ASCII text we can save some...
[lhc/web/wiklou.git] / includes / normal / Utf8Test.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 /**
21 * Runs the UTF-8 decoder test at:
22 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
23 *
24 * @package MediaWiki
25 */
26
27 /** */
28 require_once 'UtfNormalUtil.php';
29 require_once 'UtfNormal.php';
30 mb_internal_encoding( "utf-8" );
31
32 #$verbose = true;
33 if( php_sapi_name() != 'cli' ) {
34 die( "Run me from the command line please.\n" );
35 }
36
37 $in = fopen( "UTF-8-test.txt", "rt" );
38 if( !$in ) {
39 print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
40 print "If necessary, manually download this file. It can be obtained at\n";
41 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
42 exit(-1);
43 }
44
45 $columns = 0;
46 while( false !== ( $line = fgets( $in ) ) ) {
47 if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
48 $columns = strpos( $line, '|' );
49 break;
50 }
51 }
52
53 if( !$columns ) {
54 print "Something seems to be wrong; couldn't extract line length.\n";
55 print "Check that UTF-8-test.txt was downloaded correctly from\n";
56 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
57 exit(-1);
58 }
59
60 # print "$columns\n";
61
62 $ignore = array(
63 # These two lines actually seem to be corrupt
64 '2.1.1', '2.2.1' );
65
66 $exceptions = array(
67 # Tests that should mark invalid characters due to using long
68 # sequences beyond what is now considered legal.
69 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
70
71 # Literal 0xffff, which is illegal
72 '2.2.3' );
73
74 $longTests = array(
75 # These tests span multiple lines
76 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
77 '3.4' );
78
79 # These tests are not in proper subsections
80 $sectionTests = array( '3.4' );
81
82 $section = NULL;
83 $test = '';
84 $failed = 0;
85 $success = 0;
86 $total = 0;
87 while( false !== ( $line = fgets( $in ) ) ) {
88 if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
89 $section = $matches[1];
90 print $line;
91 continue;
92 }
93 if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
94 $test = $matches[1];
95
96 if( in_array( $test, $ignore ) ) {
97 continue;
98 }
99 if( in_array( $test, $longTests ) ) {
100 $line = fgets( $in );
101 for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
102 testLine( $test, $line, $total, $success, $failed );
103 }
104 } else {
105 testLine( $test, $line, $total, $success, $failed );
106 }
107 }
108 }
109
110 if( $failed ) {
111 echo "\nFailed $failed tests.\n";
112 echo "UTF-8 DECODER TEST FAILED\n";
113 exit (-1);
114 }
115
116 echo "UTF-8 DECODER TEST SUCCESS!\n";
117 exit (0);
118
119
120 function testLine( $test, $line, &$total, &$success, &$failed ) {
121 $stripped = $line;
122 UtfNormal::quickisNFCVerify( $stripped );
123
124 $same = ( $line == $stripped );
125 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
126 if( $len == 0 ) {
127 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
128 }
129
130 global $columns;
131 $ok = $same ^ ($test >= 3 );
132
133 global $exceptions;
134 $ok ^= in_array( $test, $exceptions );
135
136 $ok &= ($columns == $len);
137
138 $total++;
139 if( $ok ) {
140 $success++;
141 } else {
142 $failed++;
143 }
144 global $verbose;
145 if( $verbose || !$ok ) {
146 print str_replace( "\n", "$len\n", $stripped );
147 }
148 }
149
150 ?>