3 * Implements the conformance test at:
4 * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
6 * Copyright © 2004 Brion Vibber <brion@pobox.com>
7 * https://www.mediawiki.org/
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.
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.
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
29 class UtfNormalTest
extends PHPUnit_Framework_TestCase
{
31 protected static $testedChars = array();
33 public static function provideNormalizationTest() {
35 $in = fopen( "$IP/tests/phpunit/data/normal/NormalizationTest.txt", "rt" );
38 while ( false !== ( $line = fgets( $in ) ) ) {
39 list( $data, $comment ) = explode( '#', $line );
40 if ( $data === '' ) continue;
42 if ( preg_match( '/@Part([\d])/', $data, $matches ) ) {
46 $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
47 array_unshift( $columns, '' );
49 self
::$testedChars[$columns[1]] = true;
50 $testCases[] = array( $columns, $comment );
54 return array( array( $testCases ) );
57 function assertStringEquals( $a, $b, $desc ) {
58 $this->assertEquals( 0, strcmp( $a, $b ), $desc );
61 function assertNFC( $c, $desc ) {
62 $this->assertStringEquals( $c[2], UtfNormal
::toNFC( $c[1] ), $desc );
63 $this->assertStringEquals( $c[2], UtfNormal
::toNFC( $c[2] ), $desc );
64 $this->assertStringEquals( $c[2], UtfNormal
::toNFC( $c[3] ), $desc );
65 $this->assertStringEquals( $c[4], UtfNormal
::toNFC( $c[4] ), $desc );
66 $this->assertStringEquals( $c[4], UtfNormal
::toNFC( $c[5] ), $desc );
69 function assertNFD( $c, $desc ) {
70 $this->assertStringEquals( $c[3], UtfNormal
::toNFD( $c[1] ), $desc );
71 $this->assertStringEquals( $c[3], UtfNormal
::toNFD( $c[2] ), $desc );
72 $this->assertStringEquals( $c[3], UtfNormal
::toNFD( $c[3] ), $desc );
73 $this->assertStringEquals( $c[5], UtfNormal
::toNFD( $c[4] ), $desc );
74 $this->assertStringEquals( $c[5], UtfNormal
::toNFD( $c[5] ), $desc );
77 function assertNFKC( $c, $desc ) {
78 $this->assertStringEquals( $c[4], UtfNormal
::toNFKC( $c[1] ), $desc );
79 $this->assertStringEquals( $c[4], UtfNormal
::toNFKC( $c[2] ), $desc );
80 $this->assertStringEquals( $c[4], UtfNormal
::toNFKC( $c[3] ), $desc );
81 $this->assertStringEquals( $c[4], UtfNormal
::toNFKC( $c[4] ), $desc );
82 $this->assertStringEquals( $c[4], UtfNormal
::toNFKC( $c[5] ), $desc );
85 function assertNFKD( $c, $desc ) {
86 $this->assertStringEquals( $c[5], UtfNormal
::toNFKD( $c[1] ), $desc );
87 $this->assertStringEquals( $c[5], UtfNormal
::toNFKD( $c[2] ), $desc );
88 $this->assertStringEquals( $c[5], UtfNormal
::toNFKD( $c[3] ), $desc );
89 $this->assertStringEquals( $c[5], UtfNormal
::toNFKD( $c[4] ), $desc );
90 $this->assertStringEquals( $c[5], UtfNormal
::toNFKD( $c[5] ), $desc );
93 function assertCleanUp( $c, $desc ) {
94 $this->assertStringEquals( $c[2], UtfNormal
::cleanUp( $c[1] ), $desc );
95 $this->assertStringEquals( $c[2], UtfNormal
::cleanUp( $c[2] ), $desc );
96 $this->assertStringEquals( $c[2], UtfNormal
::cleanUp( $c[3] ), $desc );
97 $this->assertStringEquals( $c[4], UtfNormal
::cleanUp( $c[4] ), $desc );
98 $this->assertStringEquals( $c[4], UtfNormal
::cleanUp( $c[5] ), $desc );
102 * The data provider for this intentionally returns all the
103 * test cases as one since PHPUnit is too slow otherwise
105 * @dataProvider provideNormalizationTest
107 function testNormals( $testCases ) {
108 foreach ( $testCases as $case ) {
111 $this->assertNFC( $c, $desc );
112 $this->assertNFD( $c, $desc );
113 $this->assertNFKC( $c, $desc );
114 $this->assertNFKD( $c, $desc );
115 $this->assertCleanUp( $c, $desc );
119 public static function provideUnicodeData() {
121 $in = fopen( "$IP/tests/phpunit/data/normal/UnicodeData.txt", "rt" );
122 $testCases = array();
123 while ( false !== ( $line = fgets( $in ) ) ) {
124 $cols = explode( ';', $line );
125 $char = codepointToUtf8( hexdec( $cols[0] ) );
126 $desc = $cols[0] . ": " . $cols[1];
127 if ( $char < "\x20" ||
$char >= UTF8_SURROGATE_FIRST
&& $char <= UTF8_SURROGATE_LAST
) {
128 # Can't check NULL with the ICU plugin, as null bytes fail in C land.
129 # Skip other control characters, as we strip them for XML safety.
130 # Surrogates are illegal on their own or in UTF-8, ignore.
133 if ( empty( self
::$testedChars[$char] ) ) {
134 $testCases[] = array( $char, $desc );
139 return array( array( $testCases ) );
143 * The data provider for this intentionally returns all the
144 * test cases as one since PHPUnit is too slow otherwise
146 * @depends testNormals
147 * @dataProvider provideUnicodeData
149 public function testInvariant( $testCases ) {
150 foreach ( $testCases as $case ) {
153 $this->assertStringEquals( $char, UtfNormal
::toNFC( $char ), $desc );
154 $this->assertStringEquals( $char, UtfNormal
::toNFD( $char ), $desc );
155 $this->assertStringEquals( $char, UtfNormal
::toNFKC( $char ), $desc );
156 $this->assertStringEquals( $char, UtfNormal
::toNFKD( $char ), $desc );
157 $this->assertStringEquals( $char, UtfNormal
::cleanUp( $char ), $desc );