Merge "Leave table names with numbers untouched in DatabaseBase::generalizeSQL"
[lhc/web/wiklou.git] / includes / normal / Utf8Test.php
1 <?php
2 /**
3 * Runs the UTF-8 decoder test at:
4 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
5 *
6 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * https://www.mediawiki.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup UtfNormal
26 */
27
28 /** */
29
30 if ( PHP_SAPI != 'cli' ) {
31 die( "Run me from the command line please.\n" );
32 }
33
34 require_once 'UtfNormalDefines.php';
35 require_once 'UtfNormalUtil.php';
36 require_once 'UtfNormal.php';
37 mb_internal_encoding( "utf-8" );
38
39 $verbose = false;
40 #$verbose = true;
41
42 $in = fopen( "UTF-8-test.txt", "rt" );
43 if ( !$in ) {
44 print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
45 print "If necessary, manually download this file. It can be obtained at\n";
46 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
47 exit( -1 );
48 }
49
50 $columns = 0;
51 while ( false !== ( $line = fgets( $in ) ) ) {
52 $matches = array();
53 if ( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
54 $columns = strpos( $line, '|' );
55 break;
56 }
57 }
58
59 if ( !$columns ) {
60 print "Something seems to be wrong; couldn't extract line length.\n";
61 print "Check that UTF-8-test.txt was downloaded correctly from\n";
62 print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
63 exit( -1 );
64 }
65
66 # print "$columns\n";
67
68 $ignore = array(
69 # These two lines actually seem to be corrupt
70 '2.1.1', '2.2.1' );
71
72 $exceptions = array(
73 # Tests that should mark invalid characters due to using long
74 # sequences beyond what is now considered legal.
75 '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
76
77 # Literal 0xffff, which is illegal
78 '2.2.3' );
79
80 $longTests = array(
81 # These tests span multiple lines
82 '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
83 '3.4' );
84
85 # These tests are not in proper subsections
86 $sectionTests = array( '3.4' );
87
88 $section = null;
89 $test = '';
90 $failed = 0;
91 $success = 0;
92 $total = 0;
93 while ( false !== ( $line = fgets( $in ) ) ) {
94 $matches = array();
95 if ( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
96 $section = $matches[1];
97 print $line;
98 continue;
99 }
100 if ( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
101 $test = $matches[1];
102
103 if ( in_array( $test, $ignore ) ) {
104 continue;
105 }
106 if ( in_array( $test, $longTests ) ) {
107 $line = fgets( $in );
108
109 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
110 for ( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
111 // @codingStandardsIgnoreEnd
112
113 testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
114 }
115 } else {
116 testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
117 }
118 }
119 }
120
121 if ( $failed ) {
122 echo "\nFailed $failed tests.\n";
123 echo "UTF-8 DECODER TEST FAILED\n";
124 exit ( -1 );
125 }
126
127 echo "UTF-8 DECODER TEST SUCCESS!\n";
128 exit ( 0 );
129
130 function testLine( $test, $line, &$total, &$success, &$failed, $columns, $exceptions, $verbose ) {
131 $stripped = $line;
132 UtfNormal::quickisNFCVerify( $stripped );
133
134 $same = ( $line == $stripped );
135 $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
136 if ( $len == 0 ) {
137 $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
138 }
139
140 $ok = $same ^ ( $test >= 3 );
141
142 $ok ^= in_array( $test, $exceptions );
143
144 $ok &= ( $columns == $len );
145
146 $total++;
147 if ( $ok ) {
148 $success++;
149 } else {
150 $failed++;
151 }
152
153 if ( $verbose || !$ok ) {
154 print str_replace( "\n", "$len\n", $stripped );
155 }
156 }