ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RandomTest.php
Go to the documentation of this file.
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19 
29 if (php_sapi_name() != 'cli') {
30  die("Run me from the command line please.\n");
31 }
32 
34 require_once('include/Unicode/UtfNormal.php');
35 require_once('../DifferenceEngine.php');
36 
37 dl('php_utfnormal.so');
38 
39 # mt_srand( 99999 );
40 
41 function randomString($length, $nullOk, $ascii = false)
42 {
43  $out = '';
44  for ($i = 0; $i < $length; $i++) {
45  $out .= chr(mt_rand($nullOk ? 0 : 1, $ascii ? 127 : 255));
46  }
47  return $out;
48 }
49 
50 /* Duplicate of the cleanUp() path for ICU usage */
51 function donorm($str)
52 {
53  # We exclude a few chars that ICU would not.
54  $str = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str);
55  $str = str_replace(UTF8_FFFE, UTF8_REPLACEMENT, $str);
56  $str = str_replace(UTF8_FFFF, UTF8_REPLACEMENT, $str);
57 
58  # UnicodeString constructor fails if the string ends with a head byte.
59  # Add a junk char at the end, we'll strip it off
60  return rtrim(utf8_normalize($str . "\x01", UNORM_NFC), "\x01");
61 }
62 
63 function wfMsg($x)
64 {
65  return $x;
66 }
67 
68 function showDiffs($a, $b)
69 {
70  $ota = explode("\n", str_replace("\r\n", "\n", $a));
71  $nta = explode("\n", str_replace("\r\n", "\n", $b));
72 
73  $diffs = new Diff($ota, $nta);
74  $formatter = new TableDiffFormatter();
75  $funky = $formatter->format($diffs);
76  $matches = array();
77  preg_match_all('/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches);
78  foreach ($matches[1] as $bit) {
79  $hex = bin2hex($bit);
80  echo "\t$hex\n";
81  }
82 }
83 
84 $size = 16;
85 $n = 0;
86 while (true) {
87  $n++;
88  echo "$n\n";
89 
90  $str = randomString($size, true);
91  $clean = UtfNormal::cleanUp($str);
92  $norm = donorm($str);
93 
94  echo strlen($clean) . ", " . strlen($norm);
95  if ($clean == $norm) {
96  echo " (match)\n";
97  } else {
98  echo " (FAIL)\n";
99  echo "\traw: " . bin2hex($str) . "\n" .
100  "\tphp: " . bin2hex($clean) . "\n" .
101  "\ticu: " . bin2hex($norm) . "\n";
102  echo "\n\tdiffs:\n";
103  showDiffs($clean, $norm);
104  die();
105  }
106 
107 
108  $str = '';
109  $clean = '';
110  $norm = '';
111 }
const UTF8_FFFE
Definition: UtfNormal.php:78
const UNORM_NFC
Definition: UtfNormal.php:91
static cleanUp($string)
The ultimate convenience function! Clean up invalid UTF-8 sequences, and convert to normal form C...
Definition: UtfNormal.php:125
$size
Definition: RandomTest.php:84
const UTF8_FFFF
Definition: UtfNormal.php:79
showDiffs($a, $b)
Definition: RandomTest.php:68
wfMsg($x)
Definition: RandomTest.php:63
const UTF8_REPLACEMENT
Definition: UtfNormal.php:68
$n
Definition: RandomTest.php:85
donorm($str)
Definition: RandomTest.php:51
$i
Definition: disco.tpl.php:19
$x
Definition: complexTest.php:9
randomString($length, $nullOk, $ascii=false)
Definition: RandomTest.php:41