Revert r84706, r84709 (UtfNormal rewrite). Clearly more work needed to get these...
[lhc/web/wiklou.git] / includes / normal / Utf8Test.php
1 <?php
2 /**
3 * Runs the UTF-8 decoder test at:
4 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
5 *
6 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * http://www.mediawiki.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup UtfNormal
26 */
27
28 /** */
29 require_once 'UtfNormalUtil.php';
30 require_once 'UtfNormal.php';
31 mb_internal_encoding( "utf-8" );
32
33 $verbose = false;
34 #$verbose = true;
35 if( php_sapi_name() != 'cli' ) {
36 die( "Run me from the command line please.\n" );
37 }
38
39 $in = fopen( "UTF-8-test.txt", "rt" );
40 if( !$in ) {
41 print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
42 print "If necessary, manually download this file. It can be obtained at\n";
43 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
44 exit(-1);
45 }
46
47 $columns = 0;
48 while( false !== ( $line = fgets( $in ) ) ) {
49 $matches = array();
50 if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
51 $columns = strpos( $line, '|' );
52 break;
53 }
54 }
55
56 if( !$columns ) {
57 print "Something seems to be wrong; couldn't extract line length.\n";
58 print "Check that UTF-8-test.txt was downloaded correctly from\n";
59 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
60 exit(-1);
61 }
62
63 # print "$columns\n";
64
65 $ignore = array(
66 # These two lines actually seem to be corrupt
67 '2.1.1', '2.2.1' );
68
69 $exceptions = array(
70 # Tests that should mark invalid characters due to using long
71 # sequences beyond what is now considered legal.
72 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
73
74 # Literal 0xffff, which is illegal
75 '2.2.3' );
76
77 $longTests = array(
78 # These tests span multiple lines
79 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
80 '3.4' );
81
82 # These tests are not in proper subsections
83 $sectionTests = array( '3.4' );
84
85 $section = null;
86 $test = '';
87 $failed = 0;
88 $success = 0;
89 $total = 0;
90 while( false !== ( $line = fgets( $in ) ) ) {
91 $matches = array();
92 if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
93 $section = $matches[1];
94 print $line;
95 continue;
96 }
97 if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
98 $test = $matches[1];
99
100 if( in_array( $test, $ignore ) ) {
101 continue;
102 }
103 if( in_array( $test, $longTests ) ) {
104 $line = fgets( $in );
105 for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
106 testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
107 }
108 } else {
109 testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
110 }
111 }
112 }
113
114 if( $failed ) {
115 echo "\nFailed $failed tests.\n";
116 echo "UTF-8 DECODER TEST FAILED\n";
117 exit (-1);
118 }
119
120 echo "UTF-8 DECODER TEST SUCCESS!\n";
121 exit (0);
122
123
124 function testLine( $test, $line, &$total, &$success, &$failed, $columns, $exceptions, $verbose ) {
125 $stripped = $line;
126 UtfNormal::quickisNFCVerify( $stripped );
127
128 $same = ( $line == $stripped );
129 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
130 if( $len == 0 ) {
131 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
132 }
133
134 $ok = $same ^ ($test >= 3 );
135
136 $ok ^= in_array( $test, $exceptions );
137
138 $ok &= ($columns == $len);
139
140 $total++;
141 if( $ok ) {
142 $success++;
143 } else {
144 $failed++;
145 }
146
147 if( $verbose || !$ok ) {
148 print str_replace( "\n", "$len\n", $stripped );
149 }
150 }