RandomTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  20. * Test feeds random 16-byte strings to both the pure PHP and ICU-based
  21. * UtfNormal::cleanUp() code paths, and checks to see if there's a
  22. * difference. Will run forever until it finds one or you kill it.
  23. *
  24. * @ingroup UtfNormal
  25. * @access private
  26. */
  27. if( php_sapi_name() != 'cli' ) {
  28. die( "Run me from the command line please.\n" );
  29. }
  30. /** */
  31. require_once( 'UtfNormal.php' );
  32. require_once( '../DifferenceEngine.php' );
  33. dl('php_utfnormal.so' );
  34. # mt_srand( 99999 );
  35. function randomString( $length, $nullOk, $ascii = false ) {
  36. $out = '';
  37. for( $i = 0; $i < $length; $i++ )
  38. $out .= chr( mt_rand( $nullOk ? 0 : 1, $ascii ? 127 : 255 ) );
  39. return $out;
  40. }
  41. /* Duplicate of the cleanUp() path for ICU usage */
  42. function donorm( $str ) {
  43. # We exclude a few chars that ICU would not.
  44. $str = preg_replace( '/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str );
  45. $str = str_replace( UTF8_FFFE, UTF8_REPLACEMENT, $str );
  46. $str = str_replace( UTF8_FFFF, UTF8_REPLACEMENT, $str );
  47. # UnicodeString constructor fails if the string ends with a head byte.
  48. # Add a junk char at the end, we'll strip it off
  49. return rtrim( utf8_normalize( $str . "\x01", UNORM_NFC ), "\x01" );
  50. }
  51. function wfMsg($x) {
  52. return $x;
  53. }
  54. function showDiffs( $a, $b ) {
  55. $ota = explode( "\n", str_replace( "\r\n", "\n", $a ) );
  56. $nta = explode( "\n", str_replace( "\r\n", "\n", $b ) );
  57. $diffs = new Diff( $ota, $nta );
  58. $formatter = new TableDiffFormatter();
  59. $funky = $formatter->format( $diffs );
  60. $matches = array();
  61. preg_match_all( '/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches );
  62. foreach( $matches[1] as $bit ) {
  63. $hex = bin2hex( $bit );
  64. echo "\t$hex\n";
  65. }
  66. }
  67. $size = 16;
  68. $n = 0;
  69. while( true ) {
  70. $n++;
  71. echo "$n\n";
  72. $str = randomString( $size, true);
  73. $clean = UtfNormal::cleanUp( $str );
  74. $norm = donorm( $str );
  75. echo strlen( $clean ) . ", " . strlen( $norm );
  76. if( $clean == $norm ) {
  77. echo " (match)\n";
  78. } else {
  79. echo " (FAIL)\n";
  80. echo "\traw: " . bin2hex( $str ) . "\n" .
  81. "\tphp: " . bin2hex( $clean ) . "\n" .
  82. "\ticu: " . bin2hex( $norm ) . "\n";
  83. echo "\n\tdiffs:\n";
  84. showDiffs( $clean, $norm );
  85. die();
  86. }
  87. $str = '';
  88. $clean = '';
  89. $norm = '';
  90. }