c3ab2498e11178930291467935decf32d354698d
[lhc/web/wiklou.git] / includes / normal / Utf8Test.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 # Runs the UTF-8 decoder test at:
21 # http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
22
23 require_once 'UtfNormalUtil.php';
24 require_once 'UtfNormal.php';
25 mb_internal_encoding( "utf-8" );
26
27 #$verbose = true;
28 if( php_sapi_name() != 'cli' ) {
29 die( "Run me from the command line please.\n" );
30 }
31
32 $in = fopen( "UTF-8-test.txt", "rt" );
33 if( !$in ) {
34 print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
35 print "If necessary, manually download this file. It can be obtained at\n";
36 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
37 exit(-1);
38 }
39
40 $columns = 0;
41 while( false !== ( $line = fgets( $in ) ) ) {
42 if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
43 $columns = strpos( $line, '|' );
44 break;
45 }
46 }
47
48 if( !$columns ) {
49 print "Something seems to be wrong; couldn't extract line length.\n";
50 print "Check that UTF-8-test.txt was downloaded correctly from\n";
51 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
52 exit(-1);
53 }
54
55 # print "$columns\n";
56
57 $ignore = array(
58 # These two lines actually seem to be corrupt
59 '2.1.1', '2.2.1' );
60
61 $exceptions = array(
62 # Tests that should mark invalid characters due to using long
63 # sequences beyond what is now considered legal.
64 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
65
66 # Literal 0xffff, which is illegal
67 '2.2.3' );
68
69 $longTests = array(
70 # These tests span multiple lines
71 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
72 '3.4' );
73
74 # These tests are not in proper subsections
75 $sectionTests = array( '3.4' );
76
77 $section = NULL;
78 $test = '';
79 $failed = 0;
80 $success = 0;
81 $total = 0;
82 while( false !== ( $line = fgets( $in ) ) ) {
83 if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
84 $section = $matches[1];
85 print $line;
86 continue;
87 }
88 if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
89 $test = $matches[1];
90
91 if( in_array( $test, $ignore ) ) {
92 continue;
93 }
94 if( in_array( $test, $longTests ) ) {
95 $line = fgets( $in );
96 for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
97 testLine( $test, $line, $total, $success, $failed );
98 }
99 } else {
100 testLine( $test, $line, $total, $success, $failed );
101 }
102 }
103 }
104
105 if( $failed ) {
106 echo "\nFailed $failed tests.\n";
107 echo "UTF-8 DECODER TEST FAILED\n";
108 exit (-1);
109 }
110
111 echo "UTF-8 DECODER TEST SUCCESS!\n";
112 exit (0);
113
114
115 function testLine( $test, $line, &$total, &$success, &$failed ) {
116 $stripped = $line;
117 UtfNormal::quickisNFCVerify( $stripped );
118
119 $same = ( $line == $stripped );
120 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
121 if( $len == 0 ) {
122 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
123 }
124
125 global $columns;
126 $ok = $same ^ ($test >= 3 );
127
128 global $exceptions;
129 $ok ^= in_array( $test, $exceptions );
130
131 $ok &= ($columns == $len);
132
133 $total++;
134 if( $ok ) {
135 $success++;
136 } else {
137 $failed++;
138 }
139 global $verbose;
140 if( $verbose || !$ok ) {
141 print str_replace( "\n", "$len\n", $stripped );
142 }
143 }
144
145 ?>