PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/wiki/mediawiki/normal/RandomTest.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 115 lines | 67 code | 17 blank | 31 comment | 6 complexity | 432e8e4b979c4e33c914b0192c02c592 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  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. {
  29. die("Run me from the command line please.\n");
  30. }
  31. /** */
  32. require_once ('UtfNormal.php');
  33. require_once ('../DifferenceEngine.php');
  34. dl('php_utfnormal.so');
  35. # mt_srand( 99999 );
  36. function randomString($length, $nullOk, $ascii = false)
  37. {
  38. $out = '';
  39. for($i = 0; $i < $length; $i ++)
  40. $out .= chr(mt_rand($nullOk ? 0 : 1, $ascii ? 127 : 255));
  41. return $out;
  42. }
  43. /* Duplicate of the cleanUp() path for ICU usage */
  44. function donorm($str)
  45. {
  46. # We exclude a few chars that ICU would not.
  47. $str = preg_replace('/[\x00-\x08\x0b\x0c\x0e-\x1f]/', UTF8_REPLACEMENT, $str);
  48. $str = str_replace(UTF8_FFFE, UTF8_REPLACEMENT, $str);
  49. $str = str_replace(UTF8_FFFF, UTF8_REPLACEMENT, $str);
  50. # UnicodeString constructor fails if the string ends with a head byte.
  51. # Add a junk char at the end, we'll strip it off
  52. return rtrim(utf8_normalize($str . "\x01", UNORM_NFC), "\x01");
  53. }
  54. function wfMsg($x)
  55. {
  56. return $x;
  57. }
  58. function showDiffs($a, $b)
  59. {
  60. $ota = explode("\n", str_replace("\r\n", "\n", $a));
  61. $nta = explode("\n", str_replace("\r\n", "\n", $b));
  62. $diffs = new Diff($ota, $nta);
  63. $formatter = new TableDiffFormatter();
  64. $funky = $formatter->format($diffs);
  65. $matches = array();
  66. preg_match_all('/<(?:ins|del) class="diffchange">(.*?)<\/(?:ins|del)>/', $funky, $matches);
  67. foreach ($matches[1] as $bit)
  68. {
  69. $hex = bin2hex($bit);
  70. echo "\t$hex\n";
  71. }
  72. }
  73. $size = 16;
  74. $n = 0;
  75. while (true)
  76. {
  77. $n ++;
  78. echo "$n\n";
  79. $str = randomString($size, true);
  80. $clean = UtfNormal :: cleanUp($str);
  81. $norm = donorm($str);
  82. echo strlen($clean) . ", " . strlen($norm);
  83. if ($clean == $norm)
  84. {
  85. echo " (match)\n";
  86. }
  87. else
  88. {
  89. echo " (FAIL)\n";
  90. echo "\traw: " . bin2hex($str) . "\n" . "\tphp: " . bin2hex($clean) . "\n" . "\ticu: " . bin2hex($norm) . "\n";
  91. echo "\n\tdiffs:\n";
  92. showDiffs($clean, $norm);
  93. die();
  94. }
  95. $str = '';
  96. $clean = '';
  97. $norm = '';
  98. }