Follow up r78101.
[lhc/web/wiklou.git] / includes / normal / UtfNormalTest.php
1 <?php
2 /**
3 * Implements the conformance test at:
4 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.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 $verbose = true;
29 #define( 'PRETTY_UTF8', true );
30
31 if( defined( 'PRETTY_UTF8' ) ) {
32 function pretty( $string ) {
33 return preg_replace( '/([\x00-\xff])/e',
34 'sprintf("%02X", ord("$1"))',
35 $string );
36 }
37 } else {
38 /**
39 * @ignore
40 */
41 function pretty( $string ) {
42 return trim( preg_replace( '/(.)/use',
43 'sprintf("%04X ", utf8ToCodepoint("$1"))',
44 $string ) );
45 }
46 }
47
48 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
49 dl( 'php_utfnormal.so' );
50 }
51
52 require_once 'UtfNormalUtil.php';
53 require_once 'UtfNormal.php';
54
55 if( php_sapi_name() != 'cli' ) {
56 die( "Run me from the command line please.\n" );
57 }
58
59 $in = fopen("NormalizationTest.txt", "rt");
60 if( !$in ) {
61 print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
62 print "If necessary, manually download this file. It can be obtained at\n";
63 print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
64 exit(-1);
65 }
66
67 $normalizer = new UtfNormal;
68
69 $total = 0;
70 $success = 0;
71 $failure = 0;
72 $ok = true;
73 $testedChars = array();
74 while( false !== ( $line = fgets( $in ) ) ) {
75 list( $data, $comment ) = explode( '#', $line );
76 if( $data === '' ) continue;
77 $matches = array();
78 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
79 if( $matches[1] > 0 ) {
80 $ok = reportResults( $total, $success, $failure ) && $ok;
81 }
82 print "Part {$matches[1]}: $comment";
83 continue;
84 }
85
86 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
87 array_unshift( $columns, '' );
88
89 $testedChars[$columns[1]] = true;
90 $total++;
91 if( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
92 $success++;
93 } else {
94 $failure++;
95 # print "FAILED: $comment";
96 }
97 if( $total % 100 == 0 ) print "$total ";
98 }
99 fclose( $in );
100
101 $ok = reportResults( $total, $success, $failure ) && $ok;
102
103 $in = fopen("UnicodeData.txt", "rt" );
104 if( !$in ) {
105 print "Can't open UnicodeData.txt for reading.\n";
106 print "If necessary, fetch this file from the internet:\n";
107 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
108 exit(-1);
109 }
110 print "Now testing invariants...\n";
111 while( false !== ($line = fgets( $in ) ) ) {
112 $cols = explode( ';', $line );
113 $char = codepointToUtf8( hexdec( $cols[0] ) );
114 $desc = $cols[0] . ": " . $cols[1];
115 if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
116 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
117 # Skip other control characters, as we strip them for XML safety.
118 # Surrogates are illegal on their own or in UTF-8, ignore.
119 continue;
120 }
121 if( empty( $testedChars[$char] ) ) {
122 $total++;
123 if( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
124 $success++;
125 } else {
126 $failure++;
127 }
128 if( $total % 100 == 0 ) print "$total ";
129 }
130 }
131 fclose( $in );
132
133 $ok = reportResults( $total, $success, $failure ) && $ok;
134
135 if( $ok ) {
136 print "TEST SUCCEEDED!\n";
137 exit(0);
138 } else {
139 print "TEST FAILED!\n";
140 exit(-1);
141 }
142
143 ## ------
144
145 function reportResults( &$total, &$success, &$failure ) {
146 $percSucc = intval( $success * 100 / $total );
147 $percFail = intval( $failure * 100 / $total );
148 print "\n";
149 print "$success tests successful ($percSucc%)\n";
150 print "$failure tests failed ($percFail%)\n\n";
151 $ok = ($success > 0 && $failure == 0);
152 $total = 0;
153 $success = 0;
154 $failure = 0;
155 return $ok;
156 }
157
158 function testNormals( &$u, $c, $comment, $verbose, $reportFailure = false ) {
159 $result = testNFC( $u, $c, $comment, $reportFailure );
160 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
161 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
162 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
163 $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
164
165 if( $verbose && !$result && !$reportFailure ) {
166 print $comment;
167 testNormals( $u, $c, $comment, $verbose, true );
168 }
169 return $result;
170 }
171
172 function verbosify( $a, $b, $col, $form, $verbose ) {
173 #$result = ($a === $b);
174 $result = (strcmp( $a, $b ) == 0);
175 if( $verbose ) {
176 $aa = pretty( $a );
177 $bb = pretty( $b );
178 $ok = $result ? "succeed" : " failed";
179 $eq = $result ? "==" : "!=";
180 print " $ok $form c$col '$aa' $eq '$bb'\n";
181 }
182 return $result;
183 }
184
185 function testNFC( &$u, $c, $comment, $verbose ) {
186 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
187 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
188 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
189 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
190 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
191 return $result;
192 }
193
194 function testCleanUp( &$u, $c, $comment, $verbose ) {
195 $x = $c[1];
196 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
197 $x = $c[2];
198 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
199 $x = $c[3];
200 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
201 $x = $c[4];
202 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
203 $x = $c[5];
204 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
205 return $result;
206 }
207
208 function testNFD( &$u, $c, $comment, $verbose ) {
209 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
210 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
211 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
212 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
213 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
214 return $result;
215 }
216
217 function testNFKC( &$u, $c, $comment, $verbose ) {
218 $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
219 $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
220 $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
221 $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
222 $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
223 return $result;
224 }
225
226 function testNFKD( &$u, $c, $comment, $verbose ) {
227 $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
228 $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
229 $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
230 $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
231 $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
232 return $result;
233 }
234
235 function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
236 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
237 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
238 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
239 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
240 $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
241
242 if( $verbose && !$result && !$reportFailure ) {
243 print $desc;
244 testInvariant( $u, $char, $desc, $verbose, true );
245 }
246 return $result;
247 }