2004-08-29 10:30:23 +00:00
|
|
|
<?php
|
2004-09-03 23:00:01 +00:00
|
|
|
/**
|
|
|
|
|
* Implements the conformance test at:
|
|
|
|
|
* http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
|
2010-08-15 07:47:23 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup UtfNormal
|
2004-09-03 23:00:01 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
|
|
|
|
|
$verbose = true;
|
|
|
|
|
#define( 'PRETTY_UTF8', true );
|
|
|
|
|
|
|
|
|
|
if( defined( 'PRETTY_UTF8' ) ) {
|
|
|
|
|
function pretty( $string ) {
|
|
|
|
|
return preg_replace( '/([\x00-\xff])/e',
|
|
|
|
|
'sprintf("%02X", ord("$1"))',
|
|
|
|
|
$string );
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
} else {
|
2004-09-03 23:00:01 +00:00
|
|
|
/**
|
|
|
|
|
* @ignore
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2004-09-03 23:00:01 +00:00
|
|
|
*/
|
2004-08-29 10:30:23 +00:00
|
|
|
function pretty( $string ) {
|
|
|
|
|
return trim( preg_replace( '/(.)/use',
|
2004-09-02 07:39:06 +00:00
|
|
|
'sprintf("%04X ", utf8ToCodepoint("$1"))',
|
2004-08-29 10:30:23 +00:00
|
|
|
$string ) );
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
2004-10-07 05:59:10 +00:00
|
|
|
if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
|
|
|
|
|
dl( 'php_utfnormal.so' );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-15 13:36:59 +00:00
|
|
|
require_once 'UtfNormalDefines.php';
|
2004-08-29 10:30:23 +00:00
|
|
|
require_once 'UtfNormalUtil.php';
|
|
|
|
|
require_once 'UtfNormal.php';
|
|
|
|
|
|
|
|
|
|
if( php_sapi_name() != 'cli' ) {
|
|
|
|
|
die( "Run me from the command line please.\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$in = fopen("NormalizationTest.txt", "rt");
|
|
|
|
|
if( !$in ) {
|
|
|
|
|
print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
|
|
|
|
|
print "If necessary, manually download this file. It can be obtained at\n";
|
|
|
|
|
print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$normalizer = new UtfNormal;
|
|
|
|
|
|
|
|
|
|
$total = 0;
|
|
|
|
|
$success = 0;
|
|
|
|
|
$failure = 0;
|
|
|
|
|
$ok = true;
|
|
|
|
|
$testedChars = array();
|
|
|
|
|
while( false !== ( $line = fgets( $in ) ) ) {
|
|
|
|
|
list( $data, $comment ) = explode( '#', $line );
|
|
|
|
|
if( $data === '' ) continue;
|
2006-11-23 08:25:56 +00:00
|
|
|
$matches = array();
|
2004-08-29 10:30:23 +00:00
|
|
|
if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
|
|
|
|
|
if( $matches[1] > 0 ) {
|
|
|
|
|
$ok = reportResults( $total, $success, $failure ) && $ok;
|
|
|
|
|
}
|
|
|
|
|
print "Part {$matches[1]}: $comment";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
$columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
|
|
|
|
|
array_unshift( $columns, '' );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
$testedChars[$columns[1]] = true;
|
|
|
|
|
$total++;
|
2010-07-26 21:01:34 +00:00
|
|
|
if( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$success++;
|
|
|
|
|
} else {
|
|
|
|
|
$failure++;
|
|
|
|
|
# print "FAILED: $comment";
|
|
|
|
|
}
|
2004-09-02 07:39:06 +00:00
|
|
|
if( $total % 100 == 0 ) print "$total ";
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
fclose( $in );
|
|
|
|
|
|
|
|
|
|
$ok = reportResults( $total, $success, $failure ) && $ok;
|
|
|
|
|
|
|
|
|
|
$in = fopen("UnicodeData.txt", "rt" );
|
|
|
|
|
if( !$in ) {
|
|
|
|
|
print "Can't open UnicodeData.txt for reading.\n";
|
|
|
|
|
print "If necessary, fetch this file from the internet:\n";
|
|
|
|
|
print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
print "Now testing invariants...\n";
|
|
|
|
|
while( false !== ($line = fgets( $in ) ) ) {
|
|
|
|
|
$cols = explode( ';', $line );
|
|
|
|
|
$char = codepointToUtf8( hexdec( $cols[0] ) );
|
|
|
|
|
$desc = $cols[0] . ": " . $cols[1];
|
2004-11-06 03:00:29 +00:00
|
|
|
if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
|
2004-10-07 05:59:10 +00:00
|
|
|
# Can't check NULL with the ICU plugin, as null bytes fail in C land.
|
2004-11-06 03:00:29 +00:00
|
|
|
# Skip other control characters, as we strip them for XML safety.
|
2004-09-03 05:39:30 +00:00
|
|
|
# Surrogates are illegal on their own or in UTF-8, ignore.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2004-08-29 10:30:23 +00:00
|
|
|
if( empty( $testedChars[$char] ) ) {
|
|
|
|
|
$total++;
|
2010-07-26 21:01:34 +00:00
|
|
|
if( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$success++;
|
|
|
|
|
} else {
|
|
|
|
|
$failure++;
|
|
|
|
|
}
|
2004-09-02 07:39:06 +00:00
|
|
|
if( $total % 100 == 0 ) print "$total ";
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose( $in );
|
|
|
|
|
|
|
|
|
|
$ok = reportResults( $total, $success, $failure ) && $ok;
|
|
|
|
|
|
|
|
|
|
if( $ok ) {
|
|
|
|
|
print "TEST SUCCEEDED!\n";
|
|
|
|
|
exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
print "TEST FAILED!\n";
|
|
|
|
|
exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## ------
|
|
|
|
|
|
|
|
|
|
function reportResults( &$total, &$success, &$failure ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$percSucc = intval( $success * 100 / $total );
|
|
|
|
|
$percFail = intval( $failure * 100 / $total );
|
2004-09-02 07:39:06 +00:00
|
|
|
print "\n";
|
2004-08-29 10:30:23 +00:00
|
|
|
print "$success tests successful ($percSucc%)\n";
|
|
|
|
|
print "$failure tests failed ($percFail%)\n\n";
|
|
|
|
|
$ok = ($success > 0 && $failure == 0);
|
|
|
|
|
$total = 0;
|
|
|
|
|
$success = 0;
|
|
|
|
|
$failure = 0;
|
|
|
|
|
return $ok;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 21:01:34 +00:00
|
|
|
function testNormals( &$u, $c, $comment, $verbose, $reportFailure = false ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$result = testNFC( $u, $c, $comment, $reportFailure );
|
|
|
|
|
$result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
|
|
|
|
|
$result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
|
|
|
|
|
$result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
|
2004-10-30 05:24:24 +00:00
|
|
|
$result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
if( $verbose && !$result && !$reportFailure ) {
|
|
|
|
|
print $comment;
|
2010-07-26 21:01:34 +00:00
|
|
|
testNormals( $u, $c, $comment, $verbose, true );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function verbosify( $a, $b, $col, $form, $verbose ) {
|
|
|
|
|
#$result = ($a === $b);
|
|
|
|
|
$result = (strcmp( $a, $b ) == 0);
|
|
|
|
|
if( $verbose ) {
|
|
|
|
|
$aa = pretty( $a );
|
|
|
|
|
$bb = pretty( $b );
|
|
|
|
|
$ok = $result ? "succeed" : " failed";
|
|
|
|
|
$eq = $result ? "==" : "!=";
|
|
|
|
|
print " $ok $form c$col '$aa' $eq '$bb'\n";
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testNFC( &$u, $c, $comment, $verbose ) {
|
|
|
|
|
$result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
|
|
|
|
|
$result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-30 05:24:24 +00:00
|
|
|
function testCleanUp( &$u, $c, $comment, $verbose ) {
|
|
|
|
|
$x = $c[1];
|
|
|
|
|
$result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
|
|
|
|
|
$x = $c[2];
|
|
|
|
|
$result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
|
|
|
|
|
$x = $c[3];
|
|
|
|
|
$result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
|
|
|
|
|
$x = $c[4];
|
|
|
|
|
$result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
|
|
|
|
|
$x = $c[5];
|
|
|
|
|
$result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
function testNFD( &$u, $c, $comment, $verbose ) {
|
|
|
|
|
$result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
|
|
|
|
|
$result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testNFKC( &$u, $c, $comment, $verbose ) {
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testNFKD( &$u, $c, $comment, $verbose ) {
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
|
|
|
|
|
$result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 21:01:34 +00:00
|
|
|
function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
|
2004-08-29 10:30:23 +00:00
|
|
|
$result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
|
|
|
|
|
$result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
|
|
|
|
|
$result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
|
|
|
|
|
$result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
|
2004-10-30 05:24:24 +00:00
|
|
|
$result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
|
2010-07-26 21:01:34 +00:00
|
|
|
|
2004-08-29 10:30:23 +00:00
|
|
|
if( $verbose && !$result && !$reportFailure ) {
|
|
|
|
|
print $desc;
|
2010-07-26 21:01:34 +00:00
|
|
|
testInvariant( $u, $char, $desc, $verbose, true );
|
2004-08-29 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|