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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 # Implements the conformance test at:
21 # http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
24 #define( 'PRETTY_UTF8', true );
26 if( defined( 'PRETTY_UTF8' ) ) {
27 function pretty( $string ) {
28 return preg_replace( '/([\x00-\xff])/e',
29 'sprintf("%02X", ord("$1"))',
33 function pretty( $string ) {
34 return trim( preg_replace( '/(.)/use',
35 'sprintf("%04X ", utf8ToCodepoint("$1"))',
40 require_once 'UtfNormalUtil.php';
41 require_once 'UtfNormal.php';
43 if( php_sapi_name() != 'cli' ) {
44 die( "Run me from the command line please.\n" );
47 $in = fopen("NormalizationTest.txt", "rt");
49 print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
50 print "If necessary, manually download this file. It can be obtained at\n";
51 print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
55 $normalizer = new UtfNormal
;
61 $testedChars = array();
62 while( false !== ( $line = fgets( $in ) ) ) {
63 list( $data, $comment ) = explode( '#', $line );
64 if( $data === '' ) continue;
65 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
66 if( $matches[1] > 0 ) {
67 $ok = reportResults( $total, $success, $failure ) && $ok;
69 print "Part {$matches[1]}: $comment";
73 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
74 array_unshift( $columns, '' );
76 $testedChars[$columns[1]] = true;
78 if( testNormals( $normalizer, $columns, $comment ) ) {
82 # print "FAILED: $comment";
84 if( $total %
100 == 0 ) print "$total ";
88 $ok = reportResults( $total, $success, $failure ) && $ok;
90 $in = fopen("UnicodeData.txt", "rt" );
92 print "Can't open UnicodeData.txt for reading.\n";
93 print "If necessary, fetch this file from the internet:\n";
94 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
97 print "Now testing invariants...\n";
98 while( false !== ($line = fgets( $in ) ) ) {
99 $cols = explode( ';', $line );
100 $char = codepointToUtf8( hexdec( $cols[0] ) );
101 $desc = $cols[0] . ": " . $cols[1];
102 if( empty( $testedChars[$char] ) ) {
104 if( testInvariant( $normalizer, $char, $desc ) ) {
109 if( $total %
100 == 0 ) print "$total ";
114 $ok = reportResults( $total, $success, $failure ) && $ok;
117 print "TEST SUCCEEDED!\n";
120 print "TEST FAILED!\n";
126 function reportResults( &$total, &$success, &$failure ) {
127 $percSucc = IntVal( $success * 100 / $total );
128 $percFail = IntVal( $failure * 100 / $total );
130 print "$success tests successful ($percSucc%)\n";
131 print "$failure tests failed ($percFail%)\n\n";
132 $ok = ($success > 0 && $failure == 0);
139 function testNormals( &$u, $c, $comment, $reportFailure = false ) {
140 $result = testNFC( $u, $c, $comment, $reportFailure );
141 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
142 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
143 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
146 if( $verbose && !$result && !$reportFailure ) {
148 testNormals( $u, $c, $comment, true );
153 function verbosify( $a, $b, $col, $form, $verbose ) {
154 #$result = ($a === $b);
155 $result = (strcmp( $a, $b ) == 0);
159 $ok = $result ?
"succeed" : " failed";
160 $eq = $result ?
"==" : "!=";
161 print " $ok $form c$col '$aa' $eq '$bb'\n";
166 function testNFC( &$u, $c, $comment, $verbose ) {
167 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
168 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
169 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
170 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
171 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
175 function testNFD( &$u, $c, $comment, $verbose ) {
176 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
177 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
178 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
179 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
180 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
184 function testNFKC( &$u, $c, $comment, $verbose ) {
185 $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
186 $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
187 $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
188 $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
189 $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
193 function testNFKD( &$u, $c, $comment, $verbose ) {
194 $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
195 $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
196 $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
197 $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
198 $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
202 function testInvariant( &$u, $char, $desc, $reportFailure = false ) {
203 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
204 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
205 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
206 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
208 if( $verbose && !$result && !$reportFailure ) {
210 testInvariant( $u, $char, $desc, true );