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