2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * Implements the conformance test at:
22 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
28 #define( 'PRETTY_UTF8', true );
30 if( defined( 'PRETTY_UTF8' ) ) {
31 function pretty( $string ) {
32 return preg_replace( '/([\x00-\xff])/e',
33 'sprintf("%02X", ord("$1"))',
40 function pretty( $string ) {
41 return trim( preg_replace( '/(.)/use',
42 'sprintf("%04X ", utf8ToCodepoint("$1"))',
47 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
48 dl( 'php_utfnormal.so' );
51 require_once 'UtfNormalUtil.php';
52 require_once 'UtfNormal.php';
54 if( php_sapi_name() != 'cli' ) {
55 die( "Run me from the command line please.\n" );
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();
73 while( false !== ( $line = fgets( $in ) ) ) {
74 list( $data, $comment ) = explode( '#', $line );
75 if( $data === '' ) continue;
77 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
78 if( $matches[1] > 0 ) {
79 $ok = reportResults( $total, $success, $failure ) && $ok;
81 print "Part {$matches[1]}: $comment";
85 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
86 array_unshift( $columns, '' );
88 $testedChars[$columns[1]] = true;
90 if( testNormals( $normalizer, $columns, $comment ) ) {
94 # print "FAILED: $comment";
96 if( $total %
100 == 0 ) print "$total ";
100 $ok = reportResults( $total, $success, $failure ) && $ok;
102 $in = fopen("UnicodeData.txt", "rt" );
104 print "Can't open UnicodeData.txt for reading.\n";
105 print "If necessary, fetch this file from the internet:\n";
106 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
109 print "Now testing invariants...\n";
110 while( false !== ($line = fgets( $in ) ) ) {
111 $cols = explode( ';', $line );
112 $char = codepointToUtf8( hexdec( $cols[0] ) );
113 $desc = $cols[0] . ": " . $cols[1];
114 if( $char < "\x20" ||
$char >= UTF8_SURROGATE_FIRST
&& $char <= UTF8_SURROGATE_LAST
) {
115 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
116 # Skip other control characters, as we strip them for XML safety.
117 # Surrogates are illegal on their own or in UTF-8, ignore.
120 if( empty( $testedChars[$char] ) ) {
122 if( testInvariant( $normalizer, $char, $desc ) ) {
127 if( $total %
100 == 0 ) print "$total ";
132 $ok = reportResults( $total, $success, $failure ) && $ok;
135 print "TEST SUCCEEDED!\n";
138 print "TEST FAILED!\n";
144 function reportResults( &$total, &$success, &$failure ) {
145 $percSucc = intval( $success * 100 / $total );
146 $percFail = intval( $failure * 100 / $total );
148 print "$success tests successful ($percSucc%)\n";
149 print "$failure tests failed ($percFail%)\n\n";
150 $ok = ($success > 0 && $failure == 0);
157 function testNormals( &$u, $c, $comment, $reportFailure = false ) {
158 $result = testNFC( $u, $c, $comment, $reportFailure );
159 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
160 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
161 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
162 $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
165 if( $verbose && !$result && !$reportFailure ) {
167 testNormals( $u, $c, $comment, true );
172 function verbosify( $a, $b, $col, $form, $verbose ) {
173 #$result = ($a === $b);
174 $result = (strcmp( $a, $b ) == 0);
178 $ok = $result ?
"succeed" : " failed";
179 $eq = $result ?
"==" : "!=";
180 print " $ok $form c$col '$aa' $eq '$bb'\n";
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;
194 function testCleanUp( &$u, $c, $comment, $verbose ) {
196 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
198 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
200 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
202 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
204 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
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;
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;
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;
235 function testInvariant( &$u, $char, $desc, $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;
242 if( $verbose && !$result && !$reportFailure ) {
244 testInvariant( $u, $char, $desc, true );