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