16992be9fbcb9a151ff053b43292a81e382f81ad
[lhc/web/wiklou.git] / includes / normal / UtfNormalTest.php
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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Implements the conformance test at:
22 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
23 * @package MediaWiki
24 */
25
26 /** */
27 $verbose = true;
28 #define( 'PRETTY_UTF8', true );
29
30 if( defined( 'PRETTY_UTF8' ) ) {
31 function pretty( $string ) {
32 return preg_replace( '/([\x00-\xff])/e',
33 'sprintf("%02X", ord("$1"))',
34 $string );
35 }
36 } else {
37 /**
38 * @ignore
39 */
40 function pretty( $string ) {
41 return trim( preg_replace( '/(.)/use',
42 'sprintf("%04X ", utf8ToCodepoint("$1"))',
43 $string ) );
44 }
45 }
46
47 if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
48 dl( 'php_utfnormal.so' );
49 }
50
51 require_once 'UtfNormalUtil.php';
52 require_once 'UtfNormal.php';
53
54 if( php_sapi_name() != 'cli' ) {
55 die( "Run me from the command line please.\n" );
56 }
57
58 $in = fopen("NormalizationTest.txt", "rt");
59 if( !$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
66 $normalizer = new UtfNormal;
67
68 $total = 0;
69 $success = 0;
70 $failure = 0;
71 $ok = true;
72 $testedChars = array();
73 while( false !== ( $line = fgets( $in ) ) ) {
74 list( $data, $comment ) = explode( '#', $line );
75 if( $data === '' ) continue;
76 if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
77 if( $matches[1] > 0 ) {
78 $ok = reportResults( $total, $success, $failure ) && $ok;
79 }
80 print "Part {$matches[1]}: $comment";
81 continue;
82 }
83
84 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
85 array_unshift( $columns, '' );
86
87 $testedChars[$columns[1]] = true;
88 $total++;
89 if( testNormals( $normalizer, $columns, $comment ) ) {
90 $success++;
91 } else {
92 $failure++;
93 # print "FAILED: $comment";
94 }
95 if( $total % 100 == 0 ) print "$total ";
96 }
97 fclose( $in );
98
99 $ok = reportResults( $total, $success, $failure ) && $ok;
100
101 $in = fopen("UnicodeData.txt", "rt" );
102 if( !$in ) {
103 print "Can't open UnicodeData.txt for reading.\n";
104 print "If necessary, fetch this file from the internet:\n";
105 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
106 exit(-1);
107 }
108 print "Now testing invariants...\n";
109 while( false !== ($line = fgets( $in ) ) ) {
110 $cols = explode( ';', $line );
111 $char = codepointToUtf8( hexdec( $cols[0] ) );
112 $desc = $cols[0] . ": " . $cols[1];
113 if( $char === "\x00" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
114 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
115 # Surrogates are illegal on their own or in UTF-8, ignore.
116 continue;
117 }
118 if( empty( $testedChars[$char] ) ) {
119 $total++;
120 if( testInvariant( $normalizer, $char, $desc ) ) {
121 $success++;
122 } else {
123 $failure++;
124 }
125 if( $total % 100 == 0 ) print "$total ";
126 }
127 }
128 fclose( $in );
129
130 $ok = reportResults( $total, $success, $failure ) && $ok;
131
132 if( $ok ) {
133 print "TEST SUCCEEDED!\n";
134 exit(0);
135 } else {
136 print "TEST FAILED!\n";
137 exit(-1);
138 }
139
140 ## ------
141
142 function reportResults( &$total, &$success, &$failure ) {
143 $percSucc = IntVal( $success * 100 / $total );
144 $percFail = IntVal( $failure * 100 / $total );
145 print "\n";
146 print "$success tests successful ($percSucc%)\n";
147 print "$failure tests failed ($percFail%)\n\n";
148 $ok = ($success > 0 && $failure == 0);
149 $total = 0;
150 $success = 0;
151 $failure = 0;
152 return $ok;
153 }
154
155 function testNormals( &$u, $c, $comment, $reportFailure = false ) {
156 $result = testNFC( $u, $c, $comment, $reportFailure );
157 $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
158 $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
159 $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
160
161 global $verbose;
162 if( $verbose && !$result && !$reportFailure ) {
163 print $comment;
164 testNormals( $u, $c, $comment, true );
165 }
166 return $result;
167 }
168
169 function verbosify( $a, $b, $col, $form, $verbose ) {
170 #$result = ($a === $b);
171 $result = (strcmp( $a, $b ) == 0);
172 if( $verbose ) {
173 $aa = pretty( $a );
174 $bb = pretty( $b );
175 $ok = $result ? "succeed" : " failed";
176 $eq = $result ? "==" : "!=";
177 print " $ok $form c$col '$aa' $eq '$bb'\n";
178 }
179 return $result;
180 }
181
182 function testNFC( &$u, $c, $comment, $verbose ) {
183 $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
184 $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
185 $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
186 $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
187 $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
188 return $result;
189 }
190
191 function testNFD( &$u, $c, $comment, $verbose ) {
192 $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
193 $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
194 $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
195 $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
196 $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
197 return $result;
198 }
199
200 function testNFKC( &$u, $c, $comment, $verbose ) {
201 $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
202 $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
203 $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
204 $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
205 $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
206 return $result;
207 }
208
209 function testNFKD( &$u, $c, $comment, $verbose ) {
210 $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
211 $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
212 $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
213 $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
214 $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
215 return $result;
216 }
217
218 function testInvariant( &$u, $char, $desc, $reportFailure = false ) {
219 $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
220 $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
221 $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
222 $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
223 global $verbose;
224 if( $verbose && !$result && !$reportFailure ) {
225 print $desc;
226 testInvariant( $u, $char, $desc, true );
227 }
228 return $result;
229 }
230
231 ?>