562ead7b3da6ec569a0a98bf4555e750370eb0bc
[lhc/web/wiklou.git] / includes / normal / RandomTest.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * Test feeds random 16-byte strings to both the pure PHP and ICU-based
22 * UtfNormal::cleanUp() code paths, and checks to see if there's a
23 * difference. Will run forever until it finds one or you kill it.
24 *
25 * @package UtfNormal
26 * @access private
27 */
28
29 if( php_sapi_name() != 'cli' ) {
30 die( "Run me from the command line please.\n" );
31 }
32
33 require_once( 'UtfNormal.php' );
34 require_once( '../DifferenceEngine.php' );
35
36 dl('php_utfnormal.so' );
37
38 # mt_srand( 99999 );
39
40 function randomString( $length, $nullOk, $ascii = false ) {
41 $out = '';
42 for( $i = 0; $i < $length; $i++ )
43 $out .= chr( mt_rand( $nullOk ? 0 : 1, $ascii ? 127 : 255 ) );
44 return $out;
45 }
46
47 /* Duplicate of the cleanUp() path for ICU usage */
48 function donorm( $str ) {
49 # We exclude a few chars that ICU would not.
50 $str = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str );
51 $str = str_replace( UTF8_FFFE, UTF8_REPLACEMENT, $str );
52 $str = str_replace( UTF8_FFFF, UTF8_REPLACEMENT, $str );
53
54 # UnicodeString constructor fails if the string ends with a head byte.
55 # Add a junk char at the end, we'll strip it off
56 return rtrim( utf8_normalize( $str . "\x01", UNORM_NFC ), "\x01" );
57 }
58
59 function wfMsg($x) {
60 return $x;
61 }
62
63 function showDiffs( $a, $b ) {
64 $ota = explode( "\n", str_replace( "\r\n", "\n", $a ) );
65 $nta = explode( "\n", str_replace( "\r\n", "\n", $b ) );
66
67 $diffs =& new Diff( $ota, $nta );
68 $formatter =& new TableDiffFormatter();
69 $funky = $formatter->format( $diffs );
70 preg_match_all( '/<span class="diffchange">(.*?)<\/span>/', $funky, $matches );
71 foreach( $matches[1] as $bit ) {
72 $hex = bin2hex( $bit );
73 echo "\t$hex\n";
74 }
75 }
76
77 $size = 16;
78 $n = 0;
79 while( true ) {
80 $n++;
81 echo "$n\n";
82
83 $str = randomString( $size, true);
84 $clean = UtfNormal::cleanUp( $str );
85 $norm = donorm( $str );
86
87 echo strlen( $clean ) . ", " . strlen( $norm );
88 if( $clean == $norm ) {
89 echo " (match)\n";
90 } else {
91 echo " (FAIL)\n";
92 echo "\traw: " . bin2hex( $str ) . "\n" .
93 "\tphp: " . bin2hex( $clean ) . "\n" .
94 "\ticu: " . bin2hex( $norm ) . "\n";
95 echo "\n\tdiffs:\n";
96 showDiffs( $clean, $norm );
97 die();
98 }
99
100
101 $str = '';
102 $clean = '';
103 $norm = '';
104 }
105
106 ?>