ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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( '/([\x00-\xff])/e',
33 'sprintf("%02X", ord("$1"))',
34 $string );
35 }
36} else {
40 function pretty( $string ) {
41 return trim( preg_replace( '/(.)/use',
42 'sprintf("%04X ", utf8ToCodepoint("$1"))',
43 $string ) );
44 }
45}
46
47if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
48 dl( 'php_utfnormal.so' );
49}
50
51require_once 'UtfNormalUtil.php';
52require_once 'UtfNormal.php';
53
54if( php_sapi_name() != 'cli' ) {
55 die( "Run me from the command line please.\n" );
56}
57
58$in = fopen("NormalizationTest.txt", "rt");
59if( !$in ) {
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";
63 exit(-1);
64}
65
67
71$ok = true;
72$testedChars = array();
73while( false !== ( $line = fgets( $in ) ) ) {
74 list( $data, $comment ) = explode( '#', $line );
75 if( $data === '' ) continue;
76 $matches = array();
77 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
78 if( $matches[1] > 0 ) {
80 }
81 print "Part {$matches[1]}: $comment";
82 continue;
83 }
84
85 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
86 array_unshift( $columns, '' );
87
88 $testedChars[$columns[1]] = true;
89 $total++;
91 $success++;
92 } else {
93 $failure++;
94 # print "FAILED: $comment";
95 }
96 if( $total % 100 == 0 ) print "$total ";
97}
98fclose( $in );
99
101
102$in = fopen("UnicodeData.txt", "rt" );
103if( !$in ) {
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";
107 exit(-1);
108}
109print "Now testing invariants...\n";
110while( 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.
118 continue;
119 }
120 if( empty( $testedChars[$char] ) ) {
121 $total++;
122 if( testInvariant( $normalizer, $char, $desc ) ) {
123 $success++;
124 } else {
125 $failure++;
126 }
127 if( $total % 100 == 0 ) print "$total ";
128 }
129}
130fclose( $in );
131
133
134if( $ok ) {
135 print "TEST SUCCEEDED!\n";
136 exit(0);
137} else {
138 print "TEST FAILED!\n";
139 exit(-1);
140}
141
142## ------
143
145 $percSucc = intval( $success * 100 / $total );
146 $percFail = intval( $failure * 100 / $total );
147 print "\n";
148 print "$success tests successful ($percSucc%)\n";
149 print "$failure tests failed ($percFail%)\n\n";
150 $ok = ($success > 0 && $failure == 0);
151 $total = 0;
152 $success = 0;
153 $failure = 0;
154 return $ok;
155}
156
157function 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;
163
164 global $verbose;
165 if( $verbose && !$result && !$reportFailure ) {
167 testNormals( $u, $c, $comment, true );
168 }
169 return $result;
170}
171
172function verbosify( $a, $b, $col, $form, $verbose ) {
173 #$result = ($a === $b);
174 $result = (strcmp( $a, $b ) == 0);
175 if( $verbose ) {
176 $aa = pretty( $a );
177 $bb = pretty( $b );
178 $ok = $result ? "succeed" : " failed";
179 $eq = $result ? "==" : "!=";
180 print " $ok $form c$col '$aa' $eq '$bb'\n";
181 }
182 return $result;
183}
184
185function 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;
191 return $result;
192}
193
194function testCleanUp( &$u, $c, $comment, $verbose ) {
195 $x = $c[1];
196 $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
197 $x = $c[2];
198 $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
199 $x = $c[3];
200 $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
201 $x = $c[4];
202 $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
203 $x = $c[5];
204 $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
205 return $result;
206}
207
208function 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;
214 return $result;
215}
216
217function 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;
223 return $result;
224}
225
226function 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;
232 return $result;
233}
234
235function 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;
241 global $verbose;
242 if( $verbose && !$result && !$reportFailure ) {
243 print $desc;
244 testInvariant( $u, $char, $desc, true );
245 }
246 return $result;
247}
248
249?>
$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
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.