3 #ini_set( 'memory_limit', '40M' );
5 require_once( 'PHPUnit.php' );
6 require_once( 'UtfNormal.php' );
8 class CleanUpTest
extends PHPUnit_TestCase
{
9 function CleanUpTest( $name ) {
10 $this->PHPUnit_TestCase( $name );
19 function testAscii() {
20 $text = 'This is plain ASCII text.';
21 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
25 $text = "a \x00 null";
26 $expect = "a \xef\xbf\xbd null";
29 bin2hex( UtfNormal
::cleanUp( $text ) ) );
32 function testLatin() {
33 $text = "L'\xc3\xa9cole";
34 $this->assertEquals( $text, UtfNormal
::cleanUp( $text ) );
37 function testLatinNormal() {
38 $text = "L'e\xcc\x81cole";
39 $expect = "L'\xc3\xa9cole";
40 $this->assertEquals( $expect, UtfNormal
::cleanUp( $text ) );
43 # This test is *very* expensive!
44 function XtestAllChars() {
45 $rep = UTF8_REPLACEMENT
;
46 global $utfCanonicalComp, $utfCanonicalDecomp;
47 for( $i = 0x0; $i < UNICODE_MAX
; $i++
) {
48 $char = codepointToUtf8( $i );
49 $clean = UtfNormal
::cleanUp( $char );
50 $x = sprintf( "%04X", $i );
51 if( $i %
0x1000 == 0 ) echo "U+$x\n";
55 ($i > 0x001f && $i < UNICODE_SURROGATE_FIRST
) ||
56 ($i > UNICODE_SURROGATE_LAST
&& $i < 0xfffe ) ||
57 ($i > 0xffff && $i <= UNICODE_MAX
) ) {
58 if( isset( $utfCanonicalComp[$char] ) ||
isset( $utfCanonicalDecomp[$char] ) ) {
59 $comp = UtfNormal
::NFC( $char );
63 "U+$x should be decomposed" );
68 "U+$x should be intact" );
71 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
76 function testAllBytes() {
77 $this->doTestBytes( '', '' );
78 $this->doTestBytes( 'x', '' );
79 $this->doTestBytes( '', 'x' );
80 $this->doTestBytes( 'x', 'x' );
83 function doTestBytes( $head, $tail ) {
84 for( $i = 0x0; $i < 256; $i++
) {
85 $char = $head . chr( $i ) . $tail;
86 $clean = UtfNormal
::cleanUp( $char );
87 $x = sprintf( "%02X", $i );
91 ($i > 0x001f && $i < 0x80) ) {
95 "ASCII byte $x should be intact" );
96 if( $char != $clean ) return;
98 $norm = $head . UTF8_REPLACEMENT
. $tail;
102 "Forbidden byte $x should be rejected" );
103 if( $norm != $clean ) return;
108 function testDoubleBytes() {
109 $this->doTestDoubleBytes( '', '' );
110 $this->doTestDoubleBytes( 'x', '' );
111 $this->doTestDoubleBytes( '', 'x' );
112 $this->doTestDoubleBytes( 'x', 'x' );
115 function doTestDoubleBytes( $head, $tail ) {
116 for( $first = 0xc0; $first < 0x100; $first++
) {
117 for( $second = 0x80; $second < 0x100; $second++
) {
118 $char = $head . chr( $first ) . chr( $second ) . $tail;
119 $clean = UtfNormal
::cleanUp( $char );
120 $x = sprintf( "%02X,%02X", $first, $second );
124 $norm = UtfNormal
::NFC( $char );
128 "Pair $x should be intact" );
129 if( $norm != $clean ) return;
130 } elseif( $first > 0xfd ||
$second > 0xbf ) {
131 # fe and ff are not legal head bytes -- expect two replacement chars
132 $norm = $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail;
136 "Forbidden pair $x should be rejected" );
137 if( $norm != $clean ) return;
139 $norm = $head . UTF8_REPLACEMENT
. $tail;
143 "Forbidden pair $x should be rejected" );
144 if( $norm != $clean ) return;
150 function testTripleBytes() {
151 $this->doTestTripleBytes( '', '' );
152 $this->doTestTripleBytes( 'x', '' );
153 $this->doTestTripleBytes( '', 'x' );
154 $this->doTestTripleBytes( 'x', 'x' );
157 function doTestTripleBytes( $head, $tail ) {
158 for( $first = 0xc0; $first < 0x100; $first++
) {
159 for( $second = 0x80; $second < 0x100; $second++
) {
160 #for( $third = 0x80; $third < 0x100; $third++ ) {
161 for( $third = 0x80; $third < 0x81; $third++
) {
162 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
163 $clean = UtfNormal
::cleanUp( $char );
164 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
165 if( $first >= 0xe0 &&
169 if( $first == 0xe0 && $second < 0xa0 ) {
171 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
173 "Overlong triplet $x should be rejected" );
174 } elseif( $first == 0xed &&
175 ( chr( $first ) . chr( $second ) . chr( $third )) >= UTF8_SURROGATE_FIRST
) {
177 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
179 "Surrogate triplet $x should be rejected" );
182 bin2hex( UtfNormal
::NFC( $char ) ),
184 "Triplet $x should be intact" );
186 } elseif( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
188 bin2hex( UtfNormal
::NFC( $head . chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
190 "Valid 2-byte $x + broken tail" );
191 } elseif( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
193 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) . $tail ) ),
195 "Broken head + valid 2-byte $x" );
196 } elseif( $first > 0xfd && ( ( $second > 0xbf && $third > 0xbf ) ||
($second < 0xc0 && $third < 0xc0 ) ||
($second > 0xfd ) ||
($third > 0xfd) ) ) {
197 # fe and ff are not legal head bytes -- expect three replacement chars
199 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
201 "Forbidden triplet $x should be rejected" );
202 } elseif( $second < 0xc0 && $second < 0xc0 ) {
204 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
206 "Forbidden triplet $x should be rejected" );
209 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
211 "Forbidden triplet $x should be rejected" );
221 $suite =& new PHPUnit_TestSuite( 'CleanUpTest' );
222 $result = PHPUnit
::run( $suite );
223 echo $result->toString();