Got rid of the MagicWord indexing constants (MAG_xxx), replaced them by string indexi...
[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 * @package 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 if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
49 $columns = strpos( $line, '|' );
50 break;
51 }
52 }
53
54 if( !$columns ) {
55 print "Something seems to be wrong; couldn't extract line length.\n";
56 print "Check that UTF-8-test.txt was downloaded correctly from\n";
57 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
58 exit(-1);
59 }
60
61 # print "$columns\n";
62
63 $ignore = array(
64 # These two lines actually seem to be corrupt
65 '2.1.1', '2.2.1' );
66
67 $exceptions = array(
68 # Tests that should mark invalid characters due to using long
69 # sequences beyond what is now considered legal.
70 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
71
72 # Literal 0xffff, which is illegal
73 '2.2.3' );
74
75 $longTests = array(
76 # These tests span multiple lines
77 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
78 '3.4' );
79
80 # These tests are not in proper subsections
81 $sectionTests = array( '3.4' );
82
83 $section = NULL;
84 $test = '';
85 $failed = 0;
86 $success = 0;
87 $total = 0;
88 while( false !== ( $line = fgets( $in ) ) ) {
89 if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
90 $section = $matches[1];
91 print $line;
92 continue;
93 }
94 if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
95 $test = $matches[1];
96
97 if( in_array( $test, $ignore ) ) {
98 continue;
99 }
100 if( in_array( $test, $longTests ) ) {
101 $line = fgets( $in );
102 for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
103 testLine( $test, $line, $total, $success, $failed );
104 }
105 } else {
106 testLine( $test, $line, $total, $success, $failed );
107 }
108 }
109 }
110
111 if( $failed ) {
112 echo "\nFailed $failed tests.\n";
113 echo "UTF-8 DECODER TEST FAILED\n";
114 exit (-1);
115 }
116
117 echo "UTF-8 DECODER TEST SUCCESS!\n";
118 exit (0);
119
120
121 function testLine( $test, $line, &$total, &$success, &$failed ) {
122 $stripped = $line;
123 UtfNormal::quickisNFCVerify( $stripped );
124
125 $same = ( $line == $stripped );
126 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
127 if( $len == 0 ) {
128 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
129 }
130
131 global $columns;
132 $ok = $same ^ ($test >= 3 );
133
134 global $exceptions;
135 $ok ^= in_array( $test, $exceptions );
136
137 $ok &= ($columns == $len);
138
139 $total++;
140 if( $ok ) {
141 $success++;
142 } else {
143 $failed++;
144 }
145 global $verbose;
146 if( $verbose || !$ok ) {
147 print str_replace( "\n", "$len\n", $stripped );
148 }
149 }
150
151 ?>