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 < 0xfdd0 ) ||
57 ($i > 0xfdef && $i < 0xfffe ) ||
58 ($i > 0xffff && $i <= UNICODE_MAX
) ) {
59 if( isset( $utfCanonicalComp[$char] ) ||
isset( $utfCanonicalDecomp[$char] ) ) {
60 $comp = UtfNormal
::NFC( $char );
64 "U+$x should be decomposed" );
69 "U+$x should be intact" );
72 $this->assertEquals( bin2hex( $rep ), bin2hex( $clean ), $x );
77 function testAllBytes() {
78 $this->doTestBytes( '', '' );
79 $this->doTestBytes( 'x', '' );
80 $this->doTestBytes( '', 'x' );
81 $this->doTestBytes( 'x', 'x' );
84 function doTestBytes( $head, $tail ) {
85 for( $i = 0x0; $i < 256; $i++
) {
86 $char = $head . chr( $i ) . $tail;
87 $clean = UtfNormal
::cleanUp( $char );
88 $x = sprintf( "%02X", $i );
92 ($i > 0x001f && $i < 0x80) ) {
96 "ASCII byte $x should be intact" );
99 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
101 "Forbidden byte $x should be rejected" );
106 function testDoubleBytes() {
107 $this->doTestDoubleBytes( '', '' );
108 $this->doTestDoubleBytes( 'x', '' );
109 $this->doTestDoubleBytes( '', 'x' );
110 $this->doTestDoubleBytes( 'x', 'x' );
113 function doTestDoubleBytes( $head, $tail ) {
114 for( $first = 0xc0; $first < 0x100; $first++
) {
115 for( $second = 0x80; $second < 0x100; $second++
) {
116 $char = $head . chr( $first ) . chr( $second ) . $tail;
117 $clean = UtfNormal
::cleanUp( $char );
118 $x = sprintf( "%02X,%02X", $first, $second );
123 bin2hex( UtfNormal
::NFC( $char ) ),
125 "Pair $x should be intact" );
126 } elseif( $first > 0xfd ||
$second > 0xbf ) {
127 # fe and ff are not legal head bytes -- expect two replacement chars
129 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
131 "Forbidden pair $x should be rejected" );
134 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
136 "Forbidden pair $x should be rejected" );
142 function testTripleBytes() {
143 $this->doTestTripleBytes( '', '' );
144 #$this->doTestTripleBytes( 'x', '' );
145 #$this->doTestTripleBytes( '', 'x' );
146 #$this->doTestTripleBytes( 'x', 'x' );
149 function doTestTripleBytes( $head, $tail ) {
150 for( $first = 0xc0; $first < 0x100; $first++
) {
151 for( $second = 0x80; $second < 0x100; $second++
) {
152 #for( $third = 0x80; $third < 0x100; $third++ ) {
153 for( $third = 0x80; $third < 0x81; $third++
) {
154 $char = $head . chr( $first ) . chr( $second ) . chr( $third ) . $tail;
155 $clean = UtfNormal
::cleanUp( $char );
156 $x = sprintf( "%02X,%02X,%02X", $first, $second, $third );
157 if( $first >= 0xe0 &&
161 if( $first == 0xe0 && $second < 0xa0 ) {
163 bin2hex( UTF8_REPLACEMENT
),
165 "Overlong triplet $x should be rejected" );
166 } elseif( $first == 0xed &&
167 ( chr( $first ) . chr( $second ) . chr( $third )) >= UTF8_SURROGATE_FIRST
) {
169 bin2hex( UTF8_REPLACEMENT
),
171 "Surrogate triplet $x should be rejected" );
174 bin2hex( UtfNormal
::NFC( $char ) ),
176 "Triplet $x should be intact" );
178 } elseif( $first > 0xc1 && $first < 0xe0 && $second < 0xc0 ) {
180 bin2hex( $head . UtfNormal
::NFC( chr( $first ) . chr( $second ) ) . UTF8_REPLACEMENT
. $tail ),
182 "Valid 2-byte $x + broken tail" );
183 } elseif( $second > 0xc1 && $second < 0xe0 && $third < 0xc0 ) {
185 bin2hex( $head . UTF8_REPLACEMENT
. UtfNormal
::NFC( chr( $second ) . chr( $third ) ) . $tail ),
187 "Broken head + valid 2-byte $x" );
188 } elseif( $first > 0xfd && ( ( $second > 0xbf && $third > 0xbf ) ||
($second < 0xc0 && $third < 0xc0 ) ||
($second > 0xfd ) ||
($third > 0xfd) ) ) {
189 # fe and ff are not legal head bytes -- expect three replacement chars
191 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
193 "Forbidden triplet $x should be rejected" );
194 } elseif( $second < 0xc0 && $second < 0xc0 ) {
196 bin2hex( $head . UTF8_REPLACEMENT
. $tail ),
198 "Forbidden triplet $x should be rejected" );
201 bin2hex( $head . UTF8_REPLACEMENT
. UTF8_REPLACEMENT
. $tail ),
203 "Forbidden triplet $x should be rejected" );
213 $suite =& new PHPUnit_TestSuite( 'CleanUpTest' );
214 $result = PHPUnit
::run( $suite );
215 echo $result->toString();