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