ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 'include/Unicode/UtfNormal.php';
60
61if( php_sapi_name() != 'cli' ) {
62 die( "Run me from the command line please.\n" );
63}
64
65$in = fopen("NormalizationTest.txt", "rt");
66if( !$in ) {
67 print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
68 print "If necessary, manually download this file. It can be obtained at\n";
69 print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
70 exit(-1);
71}
72
74
78$ok = true;
79$testedChars = array();
80while( false !== ( $line = fgets( $in ) ) ) {
81 list( $data, $comment ) = explode( '#', $line );
82 if( $data === '' ) continue;
83 $matches = array();
84 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
85 if( $matches[1] > 0 ) {
87 }
88 print "Part {$matches[1]}: $comment";
89 continue;
90 }
91
92 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
93 array_unshift( $columns, '' );
94
95 $testedChars[$columns[1]] = true;
96 $total++;
98 $success++;
99 } else {
100 $failure++;
101 # print "FAILED: $comment";
102 }
103 if( $total % 100 == 0 ) print "$total ";
104}
105fclose( $in );
106
108
109$in = fopen("UnicodeData.txt", "rt" );
110if( !$in ) {
111 print "Can't open UnicodeData.txt for reading.\n";
112 print "If necessary, fetch this file from the internet:\n";
113 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
114 exit(-1);
115}
116print "Now testing invariants...\n";
117while( false !== ($line = fgets( $in ) ) ) {
118 $cols = explode( ';', $line );
119 $char = codepointToUtf8( hexdec( $cols[0] ) );
120 $desc = $cols[0] . ": " . $cols[1];
121 if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
122 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
123 # Skip other control characters, as we strip them for XML safety.
124 # Surrogates are illegal on their own or in UTF-8, ignore.
125 continue;
126 }
127 if( empty( $testedChars[$char] ) ) {
128 $total++;
129 if( testInvariant( $normalizer, $char, $desc ) ) {
130 $success++;
131 } else {
132 $failure++;
133 }
134 if( $total % 100 == 0 ) print "$total ";
135 }
136}
137fclose( $in );
138
140
141if( $ok ) {
142 print "TEST SUCCEEDED!\n";
143 exit(0);
144} else {
145 print "TEST FAILED!\n";
146 exit(-1);
147}
148
149## ------
150
152 $percSucc = intval( $success * 100 / $total );
153 $percFail = intval( $failure * 100 / $total );
154 print "\n";
155 print "$success tests successful ($percSucc%)\n";
156 print "$failure tests failed ($percFail%)\n\n";
157 $ok = ($success > 0 && $failure == 0);
158 $total = 0;
159 $success = 0;
160 $failure = 0;
161 return $ok;
162}
163
164function testNormals( &$u, $c, $comment, $reportFailure = false ) {
165 $result = testNFC( $u, $c, $comment, $reportFailure );
166 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
167 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
168 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
169 $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
170
171 global $verbose;
172 if( $verbose && !$result && !$reportFailure ) {
174 testNormals( $u, $c, $comment, true );
175 }
176 return $result;
177}
178
179function verbosify( $a, $b, $col, $form, $verbose ) {
180 #$result = ($a === $b);
181 $result = (strcmp( $a, $b ) == 0);
182 if( $verbose ) {
183 $aa = pretty( $a );
184 $bb = pretty( $b );
185 $ok = $result ? "succeed" : " failed";
186 $eq = $result ? "==" : "!=";
187 print " $ok $form c$col '$aa' $eq '$bb'\n";
188 }
189 return $result;
190}
191
192function testNFC( &$u, $c, $comment, $verbose ) {
193 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
194 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
195 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
196 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
197 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
198 return $result;
199}
200
201function testCleanUp( &$u, $c, $comment, $verbose ) {
202 $x = $c[1];
203 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
204 $x = $c[2];
205 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
206 $x = $c[3];
207 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
208 $x = $c[4];
209 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
210 $x = $c[5];
211 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
212 return $result;
213}
214
215function testNFD( &$u, $c, $comment, $verbose ) {
216 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
217 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
218 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
219 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
220 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
221 return $result;
222}
223
224function 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;
230 return $result;
231}
232
233function testNFKD( &$u, $c, $comment, $verbose ) {
234 $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
235 $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
236 $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
237 $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
238 $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
239 return $result;
240}
241
242function testInvariant( &$u, $char, $desc, $reportFailure = false ) {
243 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
244 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
245 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
246 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
247 $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
248 global $verbose;
249 if( $verbose && !$result && !$reportFailure ) {
250 print $desc;
251 testInvariant( $u, $char, $desc, true );
252 }
253 return $result;
254}
255
256?>
sprintf('%.4f', $callTime)
$result
if(! $in) $columns
Definition: Utf8Test.php:45
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
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:65
const UTF8_SURROGATE_LAST
Definition: UtfNormal.php:66
$comment
Definition: buildRTE.php:83
An exception for terminatinating execution or to throw for unit testing.
$x
Definition: example_009.php:98
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']