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