summaryrefslogtreecommitdiff
path: root/includes/normal/Utf8Test.php
blob: 6eae6e7275266d3a68f55fe578afe221a8a11290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
 * Runs the UTF-8 decoder test at:
 * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
 *
 * Copyright © 2004 Brion Vibber <brion@pobox.com>
 * http://www.mediawiki.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup UtfNormal
 */

/** */

require_once 'UtfNormalDefines.php';
require_once 'UtfNormalUtil.php';
require_once 'UtfNormal.php';
mb_internal_encoding( "utf-8" );

$verbose = false;
#$verbose = true;
if( php_sapi_name() != 'cli' ) {
	die( "Run me from the command line please.\n" );
}

$in = fopen( "UTF-8-test.txt", "rt" );
if( !$in ) {
	print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
	print "If necessary, manually download this file. It can be obtained at\n";
	print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
	exit(-1);
}

$columns = 0;
while( false !== ( $line = fgets( $in ) ) ) {
	$matches = array();
	if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
		$columns = strpos( $line, '|' );
		break;
	}
}

if( !$columns ) {
	print "Something seems to be wrong; couldn't extract line length.\n";
	print "Check that UTF-8-test.txt was downloaded correctly from\n";
	print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt\n";
	exit(-1);
}

# print "$columns\n";

$ignore = array(
	# These two lines actually seem to be corrupt
	'2.1.1', '2.2.1' );

$exceptions = array(
	# Tests that should mark invalid characters due to using long
	# sequences beyond what is now considered legal.
	'2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',

	# Literal 0xffff, which is illegal
	'2.2.3' );

$longTests = array(
	# These tests span multiple lines
	'3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
	'3.4' );

# These tests are not in proper subsections
$sectionTests = array( '3.4' );

$section = null;
$test = '';
$failed = 0;
$success = 0;
$total = 0;
while( false !== ( $line = fgets( $in ) ) ) {
	$matches = array();
	if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
		$section = $matches[1];
		print $line;
		continue;
	}
	if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
		$test = $matches[1];

		if( in_array( $test, $ignore ) ) {
			continue;
		}
		if( in_array( $test, $longTests ) ) {
			$line = fgets( $in );
			for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
				testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
			}
		} else {
			testLine( $test, $line, $total, $success, $failed, $columns, $exceptions, $verbose );
		}
	}
}

if( $failed ) {
	echo "\nFailed $failed tests.\n";
	echo "UTF-8 DECODER TEST FAILED\n";
	exit (-1);
}

echo "UTF-8 DECODER TEST SUCCESS!\n";
exit (0);


function testLine( $test, $line, &$total, &$success, &$failed, $columns, $exceptions, $verbose ) {
	$stripped = $line;
	UtfNormal::quickisNFCVerify( $stripped );

	$same = ( $line == $stripped );
	$len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
	if( $len == 0 ) {
		$len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
	}

	$ok = $same ^ ($test >= 3 );

	$ok ^= in_array( $test, $exceptions );

	$ok &= ($columns == $len);

	$total++;
	if( $ok ) {
		$success++;
	} else {
		$failed++;
	}

	if( $verbose || !$ok ) {
		print str_replace( "\n", "$len\n", $stripped );
	}
}