ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
UtfNormalTest.php
Go to the documentation of this file.
1<?php
2# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3# http://www.mediawiki.org/
4#
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.
9#
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.
14#
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
19
27$verbose = true;
28#define( 'PRETTY_UTF8', true );
29
30if( defined( 'PRETTY_UTF8' ) ) {
31 function pretty( $string ) {
32 return preg_replace_callback(
33 '/([\x00-\xff])/',
34 function($hit) {
35 return sprintf("%02X", ord($hit[1]));
36 },
37 $string
38 );
39 }
40} else {
44 function pretty( $string ) {
45 return trim( preg_replace_callback(
46 '/(.)/us',
47 function($hit) {
48 return sprintf("%04X ", utf8ToCodepoint($hit[1]));
49 },
50 $string
51 ));
52 }
53}
54
55if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
56 dl( 'php_utfnormal.so' );
57}
58
59require_once 'UtfNormalUtil.php';
60require_once 'UtfNormal.php';
61
62if( php_sapi_name() != 'cli' ) {
63 die( "Run me from the command line please.\n" );
64}
65
66$in = fopen("NormalizationTest.txt", "rt");
67if( !$in ) {
68 print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
69 print "If necessary, manually download this file. It can be obtained at\n";
70 print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
71 exit(-1);
72}
73
75
79$ok = true;
80$testedChars = array();
81while( false !== ( $line = fgets( $in ) ) ) {
82 list( $data, $comment ) = explode( '#', $line );
83 if( $data === '' ) continue;
84 $matches = array();
85 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
86 if( $matches[1] > 0 ) {
88 }
89 print "Part {$matches[1]}: $comment";
90 continue;
91 }
92
93 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
94 array_unshift( $columns, '' );
95
96 $testedChars[$columns[1]] = true;
97 $total++;
99 $success++;
100 } else {
101 $failure++;
102 # print "FAILED: $comment";
103 }
104 if( $total % 100 == 0 ) print "$total ";
105}
106fclose( $in );
107
109
110$in = fopen("UnicodeData.txt", "rt" );
111if( !$in ) {
112 print "Can't open UnicodeData.txt for reading.\n";
113 print "If necessary, fetch this file from the internet:\n";
114 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
115 exit(-1);
116}
117print "Now testing invariants...\n";
118while( false !== ($line = fgets( $in ) ) ) {
119 $cols = explode( ';', $line );
120 $char = codepointToUtf8( hexdec( $cols[0] ) );
121 $desc = $cols[0] . ": " . $cols[1];
122 if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
123 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
124 # Skip other control characters, as we strip them for XML safety.
125 # Surrogates are illegal on their own or in UTF-8, ignore.
126 continue;
127 }
128 if( empty( $testedChars[$char] ) ) {
129 $total++;
130 if( testInvariant( $normalizer, $char, $desc ) ) {
131 $success++;
132 } else {
133 $failure++;
134 }
135 if( $total % 100 == 0 ) print "$total ";
136 }
137}
138fclose( $in );
139
141
142if( $ok ) {
143 print "TEST SUCCEEDED!\n";
144 exit(0);
145} else {
146 print "TEST FAILED!\n";
147 exit(-1);
148}
149
150## ------
151
153 $percSucc = intval( $success * 100 / $total );
154 $percFail = intval( $failure * 100 / $total );
155 print "\n";
156 print "$success tests successful ($percSucc%)\n";
157 print "$failure tests failed ($percFail%)\n\n";
158 $ok = ($success > 0 && $failure == 0);
159 $total = 0;
160 $success = 0;
161 $failure = 0;
162 return $ok;
163}
164
165function testNormals( &$u, $c, $comment, $reportFailure = false ) {
166 $result = testNFC( $u, $c, $comment, $reportFailure );
167 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
168 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
169 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
170 $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
171
172 global $verbose;
173 if( $verbose && !$result && !$reportFailure ) {
175 testNormals( $u, $c, $comment, true );
176 }
177 return $result;
178}
179
180function verbosify( $a, $b, $col, $form, $verbose ) {
181 #$result = ($a === $b);
182 $result = (strcmp( $a, $b ) == 0);
183 if( $verbose ) {
184 $aa = pretty( $a );
185 $bb = pretty( $b );
186 $ok = $result ? "succeed" : " failed";
187 $eq = $result ? "==" : "!=";
188 print " $ok $form c$col '$aa' $eq '$bb'\n";
189 }
190 return $result;
191}
192
193function testNFC( &$u, $c, $comment, $verbose ) {
194 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
195 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
196 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
197 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
198 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
199 return $result;
200}
201
202function testCleanUp( &$u, $c, $comment, $verbose ) {
203 $x = $c[1];
204 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
205 $x = $c[2];
206 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
207 $x = $c[3];
208 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
209 $x = $c[4];
210 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
211 $x = $c[5];
212 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
213 return $result;
214}
215
216function testNFD( &$u, $c, $comment, $verbose ) {
217 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
218 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
219 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
220 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
221 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
222 return $result;
223}
224
225function testNFKC( &$u, $c, $comment, $verbose ) {
226 $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
227 $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
228 $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
229 $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
230 $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
231 return $result;
232}
233
234function 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;
240 return $result;
241}
242
243function testInvariant( &$u, $char, $desc, $reportFailure = false ) {
244 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
245 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
246 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
247 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
248 $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
249 global $verbose;
250 if( $verbose && !$result && !$reportFailure ) {
251 print $desc;
252 testInvariant( $u, $char, $desc, true );
253 }
254 return $result;
255}
256
257?>
$result
if(! $in) $columns
Definition: Utf8Test.php:46
if(php_sapi_name() !='cli') $in
$failure
$total
$verbose
testNFC(&$u, $c, $comment, $verbose)
testNormals(&$u, $c, $comment, $reportFailure=false)
testCleanUp(&$u, $c, $comment, $verbose)
testNFKC(&$u, $c, $comment, $verbose)
testNFD(&$u, $c, $comment, $verbose)
$success
testNFKD(&$u, $c, $comment, $verbose)
if(! $in) print
verbosify( $a, $b, $col, $form, $verbose)
testInvariant(&$u, $char, $desc, $reportFailure=false)
$testedChars
reportResults(&$total, &$success, &$failure)
if(! $in) $normalizer
$comment
Definition: buildRTE.php:83
$x
Definition: example_009.php:98
$data
utf8ToCodepoint( $char)
Determine the Unicode codepoint of a single-character UTF-8 sequence.
codepointToUtf8( $codepoint)
Return UTF-8 sequence for a given Unicode code point.
const UTF8_SURROGATE_FIRST
Definition: UtfNormal.php:80
const UTF8_SURROGATE_LAST
Definition: UtfNormal.php:81
exit
Definition: login.php:54
Unicode normalization routines for working with UTF-8 strings.
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']