Utf8Test.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * Runs the UTF-8 decoder test at:
  21. * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
  22. *
  23. * @ingroup UtfNormal
  24. * @access private
  25. */
  26. /** */
  27. require_once 'UtfNormalUtil.php';
  28. require_once 'UtfNormal.php';
  29. mb_internal_encoding( "utf-8" );
  30. #$verbose = true;
  31. if( php_sapi_name() != 'cli' ) {
  32. die( "Run me from the command line please.\n" );
  33. }
  34. $in = fopen( "UTF-8-test.txt", "rt" );
  35. if( !$in ) {
  36. print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
  37. print "If necessary, manually download this file. It can be obtained at\n";
  38. print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
  39. exit(-1);
  40. }
  41. $columns = 0;
  42. while( false !== ( $line = fgets( $in ) ) ) {
  43. $matches = array();
  44. if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
  45. $columns = strpos( $line, '|' );
  46. break;
  47. }
  48. }
  49. if( !$columns ) {
  50. print "Something seems to be wrong; couldn't extract line length.\n";
  51. print "Check that UTF-8-test.txt was downloaded correctly from\n";
  52. print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
  53. exit(-1);
  54. }
  55. # print "$columns\n";
  56. $ignore = array(
  57. # These two lines actually seem to be corrupt
  58. '2.1.1', '2.2.1' );
  59. $exceptions = array(
  60. # Tests that should mark invalid characters due to using long
  61. # sequences beyond what is now considered legal.
  62. '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
  63. # Literal 0xffff, which is illegal
  64. '2.2.3' );
  65. $longTests = array(
  66. # These tests span multiple lines
  67. '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
  68. '3.4' );
  69. # These tests are not in proper subsections
  70. $sectionTests = array( '3.4' );
  71. $section = NULL;
  72. $test = '';
  73. $failed = 0;
  74. $success = 0;
  75. $total = 0;
  76. while( false !== ( $line = fgets( $in ) ) ) {
  77. $matches = array();
  78. if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
  79. $section = $matches[1];
  80. print $line;
  81. continue;
  82. }
  83. if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
  84. $test = $matches[1];
  85. if( in_array( $test, $ignore ) ) {
  86. continue;
  87. }
  88. if( in_array( $test, $longTests ) ) {
  89. $line = fgets( $in );
  90. for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
  91. testLine( $test, $line, $total, $success, $failed );
  92. }
  93. } else {
  94. testLine( $test, $line, $total, $success, $failed );
  95. }
  96. }
  97. }
  98. if( $failed ) {
  99. echo "\nFailed $failed tests.\n";
  100. echo "UTF-8 DECODER TEST FAILED\n";
  101. exit (-1);
  102. }
  103. echo "UTF-8 DECODER TEST SUCCESS!\n";
  104. exit (0);
  105. function testLine( $test, $line, &$total, &$success, &$failed ) {
  106. $stripped = $line;
  107. UtfNormal::quickisNFCVerify( $stripped );
  108. $same = ( $line == $stripped );
  109. $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
  110. if( $len == 0 ) {
  111. $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
  112. }
  113. global $columns;
  114. $ok = $same ^ ($test >= 3 );
  115. global $exceptions;
  116. $ok ^= in_array( $test, $exceptions );
  117. $ok &= ($columns == $len);
  118. $total++;
  119. if( $ok ) {
  120. $success++;
  121. } else {
  122. $failed++;
  123. }
  124. global $verbose;
  125. if( $verbose || !$ok ) {
  126. print str_replace( "\n", "$len\n", $stripped );
  127. }
  128. }