Split the data arrays for form KC, KD to a separate include file and load it on demand.
[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 # Implements the conformance test at:
21 # http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
22
23 $verbose = true;
24 #define( 'PRETTY_UTF8', true );
25
26 if( defined( 'PRETTY_UTF8' ) ) {
27 function pretty( $string ) {
28 return preg_replace( '/([\x00-\xff])/e',
29 'sprintf("%02X", ord("$1"))',
30 $string );
31 }
32 } else {
33 function pretty( $string ) {
34 return trim( preg_replace( '/(.)/use',
35 'sprintf("%04X ", utf8ToCodepoint("$1"))',
36 $string ) );
37 }
38 }
39
40 require_once 'UtfNormalUtil.php';
41 require_once 'UtfNormal.php';
42
43 if( php_sapi_name() != 'cli' ) {
44 die( "Run me from the command line please.\n" );
45 }
46
47 $in = fopen("NormalizationTest.txt", "rt");
48 if( !$in ) {
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";
52 exit(-1);
53 }
54
55 $normalizer = new UtfNormal;
56
57 $total = 0;
58 $success = 0;
59 $failure = 0;
60 $ok = true;
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;
68 }
69 print "Part {$matches[1]}: $comment";
70 continue;
71 }
72
73 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
74 array_unshift( $columns, '' );
75
76 $testedChars[$columns[1]] = true;
77 $total++;
78 if( testNormals( $normalizer, $columns, $comment ) ) {
79 $success++;
80 } else {
81 $failure++;
82 # print "FAILED: $comment";
83 }
84 if( $total % 100 == 0 ) print "$total ";
85 }
86 fclose( $in );
87
88 $ok = reportResults( $total, $success, $failure ) && $ok;
89
90 $in = fopen("UnicodeData.txt", "rt" );
91 if( !$in ) {
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";
95 exit(-1);
96 }
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] ) ) {
103 $total++;
104 if( testInvariant( $normalizer, $char, $desc ) ) {
105 $success++;
106 } else {
107 $failure++;
108 }
109 if( $total % 100 == 0 ) print "$total ";
110 }
111 }
112 fclose( $in );
113
114 $ok = reportResults( $total, $success, $failure ) && $ok;
115
116 if( $ok ) {
117 print "TEST SUCCEEDED!\n";
118 exit(0);
119 } else {
120 print "TEST FAILED!\n";
121 exit(-1);
122 }
123
124 ## ------
125
126 function reportResults( &$total, &$success, &$failure ) {
127 $percSucc = IntVal( $success * 100 / $total );
128 $percFail = IntVal( $failure * 100 / $total );
129 print "\n";
130 print "$success tests successful ($percSucc%)\n";
131 print "$failure tests failed ($percFail%)\n\n";
132 $ok = ($success > 0 && $failure == 0);
133 $total = 0;
134 $success = 0;
135 $failure = 0;
136 return $ok;
137 }
138
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;
144
145 global $verbose;
146 if( $verbose && !$result && !$reportFailure ) {
147 print $comment;
148 testNormals( $u, $c, $comment, true );
149 }
150 return $result;
151 }
152
153 function verbosify( $a, $b, $col, $form, $verbose ) {
154 #$result = ($a === $b);
155 $result = (strcmp( $a, $b ) == 0);
156 if( $verbose ) {
157 $aa = pretty( $a );
158 $bb = pretty( $b );
159 $ok = $result ? "succeed" : " failed";
160 $eq = $result ? "==" : "!=";
161 print " $ok $form c$col '$aa' $eq '$bb'\n";
162 }
163 return $result;
164 }
165
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;
172 return $result;
173 }
174
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;
181 return $result;
182 }
183
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;
190 return $result;
191 }
192
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;
199 return $result;
200 }
201
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;
207 global $verbose;
208 if( $verbose && !$result && !$reportFailure ) {
209 print $desc;
210 testInvariant( $u, $char, $desc, true );
211 }
212 return $result;
213 }
214
215 ?>