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