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