Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / normal / UtfNormalTest.php
blob511836666e839870a02d877c202f453ab298692f
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 if( PHP_SAPI != 'cli' ) {
29 die( "Run me from the command line please.\n" );
32 $verbose = true;
33 #define( 'PRETTY_UTF8', true );
35 if( defined( 'PRETTY_UTF8' ) ) {
36 function pretty( $string ) {
37 return strtoupper( bin2hex( $string ) );
39 } else {
40 /**
41 * @ignore
42 * @return string
44 function pretty( $string ) {
45 return strtoupper( utf8ToHexSequence( $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 $in = fopen("NormalizationTest.txt", "rt");
58 if( !$in ) {
59 print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
60 print "If necessary, manually download this file. It can be obtained at\n";
61 print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
62 exit(-1);
65 $normalizer = new UtfNormal;
67 $total = 0;
68 $success = 0;
69 $failure = 0;
70 $ok = true;
71 $testedChars = array();
72 while( false !== ( $line = fgets( $in ) ) ) {
73 list( $data, $comment ) = explode( '#', $line );
74 if( $data === '' ) continue;
75 $matches = array();
76 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
77 if( $matches[1] > 0 ) {
78 $ok = reportResults( $total, $success, $failure ) && $ok;
80 print "Part {$matches[1]}: $comment";
81 continue;
84 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
85 array_unshift( $columns, '' );
87 $testedChars[$columns[1]] = true;
88 $total++;
89 if( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
90 $success++;
91 } else {
92 $failure++;
93 # print "FAILED: $comment";
95 if( $total % 100 == 0 ) print "$total ";
97 fclose( $in );
99 $ok = reportResults( $total, $success, $failure ) && $ok;
101 $in = fopen("UnicodeData.txt", "rt" );
102 if( !$in ) {
103 print "Can't open UnicodeData.txt for reading.\n";
104 print "If necessary, fetch this file from the internet:\n";
105 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
106 exit(-1);
108 print "Now testing invariants...\n";
109 while( false !== ($line = fgets( $in ) ) ) {
110 $cols = explode( ';', $line );
111 $char = codepointToUtf8( hexdec( $cols[0] ) );
112 $desc = $cols[0] . ": " . $cols[1];
113 if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
114 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
115 # Skip other control characters, as we strip them for XML safety.
116 # Surrogates are illegal on their own or in UTF-8, ignore.
117 continue;
119 if( empty( $testedChars[$char] ) ) {
120 $total++;
121 if( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
122 $success++;
123 } else {
124 $failure++;
126 if( $total % 100 == 0 ) print "$total ";
129 fclose( $in );
131 $ok = reportResults( $total, $success, $failure ) && $ok;
133 if( $ok ) {
134 print "TEST SUCCEEDED!\n";
135 exit(0);
136 } else {
137 print "TEST FAILED!\n";
138 exit(-1);
141 ## ------
143 function reportResults( &$total, &$success, &$failure ) {
144 $percSucc = intval( $success * 100 / $total );
145 $percFail = intval( $failure * 100 / $total );
146 print "\n";
147 print "$success tests successful ($percSucc%)\n";
148 print "$failure tests failed ($percFail%)\n\n";
149 $ok = ($success > 0 && $failure == 0);
150 $total = 0;
151 $success = 0;
152 $failure = 0;
153 return $ok;
156 function testNormals( &$u, $c, $comment, $verbose, $reportFailure = false ) {
157 $result = testNFC( $u, $c, $comment, $reportFailure );
158 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
159 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
160 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
161 $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
163 if( $verbose && !$result && !$reportFailure ) {
164 print $comment;
165 testNormals( $u, $c, $comment, $verbose, true );
167 return $result;
170 function verbosify( $a, $b, $col, $form, $verbose ) {
171 #$result = ($a === $b);
172 $result = (strcmp( $a, $b ) == 0);
173 if( $verbose ) {
174 $aa = pretty( $a );
175 $bb = pretty( $b );
176 $ok = $result ? "succeed" : " failed";
177 $eq = $result ? "==" : "!=";
178 print " $ok $form c$col '$aa' $eq '$bb'\n";
180 return $result;
183 function testNFC( &$u, $c, $comment, $verbose ) {
184 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
185 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
186 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
187 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
188 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
189 return $result;
192 function testCleanUp( &$u, $c, $comment, $verbose ) {
193 $x = $c[1];
194 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
195 $x = $c[2];
196 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
197 $x = $c[3];
198 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
199 $x = $c[4];
200 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
201 $x = $c[5];
202 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
203 return $result;
206 function testNFD( &$u, $c, $comment, $verbose ) {
207 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
208 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
209 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
210 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
211 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
212 return $result;
215 function testNFKC( &$u, $c, $comment, $verbose ) {
216 $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
217 $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
218 $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
219 $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
220 $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
221 return $result;
224 function testNFKD( &$u, $c, $comment, $verbose ) {
225 $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
226 $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
227 $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
228 $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
229 $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
230 return $result;
233 function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
234 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
235 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
236 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
237 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
238 $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
240 if( $verbose && !$result && !$reportFailure ) {
241 print $desc;
242 testInvariant( $u, $char, $desc, $verbose, true );
244 return $result;