3 * Implements the conformance test at:
4 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
6 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * https://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
28 if ( PHP_SAPI
!= 'cli' ) {
29 die( "Run me from the command line please.\n" );
33 #define( 'PRETTY_UTF8', true );
35 if ( defined( 'PRETTY_UTF8' ) ) {
36 function pretty( $string ) {
37 return strtoupper( bin2hex( $string ) );
42 * @param string $string
45 function pretty( $string ) {
46 return strtoupper( utf8ToHexSequence( $string ) );
50 if ( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
51 dl( 'php_utfnormal.so' );
54 require_once 'UtfNormalDefines.php';
55 require_once 'UtfNormalUtil.php';
56 require_once 'UtfNormal.php';
58 $in = fopen( "NormalizationTest.txt", "rt" );
60 print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
61 print "If necessary, manually download this file. It can be obtained at\n";
62 print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
66 $normalizer = new UtfNormal
;
72 $testedChars = array();
74 while ( false !== ( $line = fgets( $in ) ) ) {
75 list( $data, $comment ) = explode( '#', $line );
76 if ( $data === '' ) continue;
78 if ( preg_match( '/@Part([\d])/', $data, $matches ) ) {
79 if ( $matches[1] > 0 ) {
80 $ok = reportResults( $total, $success, $failure ) && $ok;
82 print "Part {$matches[1]}: $comment";
86 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
87 array_unshift( $columns, '' );
89 $testedChars[$columns[1]] = true;
91 if ( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
95 # print "FAILED: $comment";
97 if ( $total %
100 == 0 ) print "$total ";
101 $ok = reportResults( $total, $success, $failure ) && $ok;
103 $in = fopen( "UnicodeData.txt", "rt" );
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";
110 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.
122 if ( empty( $testedChars[$char] ) ) {
124 if ( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
129 if ( $total %
100 == 0 ) print "$total ";
134 $ok = reportResults( $total, $success, $failure ) && $ok;
137 print "TEST SUCCEEDED!\n";
140 print "TEST FAILED!\n";
146 function reportResults( &$total, &$success, &$failure ) {
147 $percSucc = intval( $success * 100 / $total );
148 $percFail = intval( $failure * 100 / $total );
150 print "$success tests successful ($percSucc%)\n";
151 print "$failure tests failed ($percFail%)\n\n";
152 $ok = ( $success > 0 && $failure == 0 );
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 ) {
169 testNormals( $u, $c, $comment, $verbose, true );
175 function verbosify( $a, $b, $col, $form, $verbose ) {
176 #$result = ($a === $b);
177 $result = ( strcmp( $a, $b ) == 0 );
181 $ok = $result ?
"succeed" : " failed";
182 $eq = $result ?
"==" : "!=";
183 print " $ok $form c$col '$aa' $eq '$bb'\n";
189 function testNFC( &$u, $c, $comment, $verbose ) {
190 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
191 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
192 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
193 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
194 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
199 function testCleanUp( &$u, $c, $comment, $verbose ) {
201 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
203 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
205 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
207 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
209 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
214 function testNFD( &$u, $c, $comment, $verbose ) {
215 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
216 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
217 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
218 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
219 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
224 function testNFKC( &$u, $c, $comment, $verbose ) {
225 $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
226 $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
227 $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
228 $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
229 $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
234 function testNFKD( &$u, $c, $comment, $verbose ) {
235 $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
236 $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
237 $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
238 $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
239 $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
244 function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
245 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
246 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
247 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
248 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
249 $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
251 if ( $verbose && !$result && !$reportFailure ) {
253 testInvariant( $u, $char, $desc, $verbose, true );