UtfNormalTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * Implements the conformance test at:
  21. * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
  22. * @ingroup UtfNormal
  23. */
  24. /** */
  25. $verbose = true;
  26. #define( 'PRETTY_UTF8', true );
  27. if( defined( 'PRETTY_UTF8' ) ) {
  28. function pretty( $string ) {
  29. return preg_replace( '/([\x00-\xff])/e',
  30. 'sprintf("%02X", ord("$1"))',
  31. $string );
  32. }
  33. } else {
  34. /**
  35. * @ignore
  36. */
  37. function pretty( $string ) {
  38. return trim( preg_replace( '/(.)/use',
  39. 'sprintf("%04X ", utf8ToCodepoint("$1"))',
  40. $string ) );
  41. }
  42. }
  43. if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
  44. dl( 'php_utfnormal.so' );
  45. }
  46. require_once 'UtfNormalUtil.php';
  47. require_once 'UtfNormal.php';
  48. if( php_sapi_name() != 'cli' ) {
  49. die( "Run me from the command line please.\n" );
  50. }
  51. $in = fopen("NormalizationTest.txt", "rt");
  52. if( !$in ) {
  53. print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
  54. print "If necessary, manually download this file. It can be obtained at\n";
  55. print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
  56. exit(-1);
  57. }
  58. $normalizer = new UtfNormal;
  59. $total = 0;
  60. $success = 0;
  61. $failure = 0;
  62. $ok = true;
  63. $testedChars = array();
  64. while( false !== ( $line = fgets( $in ) ) ) {
  65. list( $data, $comment ) = explode( '#', $line );
  66. if( $data === '' ) continue;
  67. $matches = array();
  68. if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
  69. if( $matches[1] > 0 ) {
  70. $ok = reportResults( $total, $success, $failure ) && $ok;
  71. }
  72. print "Part {$matches[1]}: $comment";
  73. continue;
  74. }
  75. $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
  76. array_unshift( $columns, '' );
  77. $testedChars[$columns[1]] = true;
  78. $total++;
  79. if( testNormals( $normalizer, $columns, $comment ) ) {
  80. $success++;
  81. } else {
  82. $failure++;
  83. # print "FAILED: $comment";
  84. }
  85. if( $total % 100 == 0 ) print "$total ";
  86. }
  87. fclose( $in );
  88. $ok = reportResults( $total, $success, $failure ) && $ok;
  89. $in = fopen("UnicodeData.txt", "rt" );
  90. if( !$in ) {
  91. print "Can't open UnicodeData.txt for reading.\n";
  92. print "If necessary, fetch this file from the internet:\n";
  93. print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
  94. exit(-1);
  95. }
  96. print "Now testing invariants...\n";
  97. while( false !== ($line = fgets( $in ) ) ) {
  98. $cols = explode( ';', $line );
  99. $char = codepointToUtf8( hexdec( $cols[0] ) );
  100. $desc = $cols[0] . ": " . $cols[1];
  101. if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
  102. # Can't check NULL with the ICU plugin, as null bytes fail in C land.
  103. # Skip other control characters, as we strip them for XML safety.
  104. # Surrogates are illegal on their own or in UTF-8, ignore.
  105. continue;
  106. }
  107. if( empty( $testedChars[$char] ) ) {
  108. $total++;
  109. if( testInvariant( $normalizer, $char, $desc ) ) {
  110. $success++;
  111. } else {
  112. $failure++;
  113. }
  114. if( $total % 100 == 0 ) print "$total ";
  115. }
  116. }
  117. fclose( $in );
  118. $ok = reportResults( $total, $success, $failure ) && $ok;
  119. if( $ok ) {
  120. print "TEST SUCCEEDED!\n";
  121. exit(0);
  122. } else {
  123. print "TEST FAILED!\n";
  124. exit(-1);
  125. }
  126. ## ------
  127. function reportResults( &$total, &$success, &$failure ) {
  128. $percSucc = intval( $success * 100 / $total );
  129. $percFail = intval( $failure * 100 / $total );
  130. print "\n";
  131. print "$success tests successful ($percSucc%)\n";
  132. print "$failure tests failed ($percFail%)\n\n";
  133. $ok = ($success > 0 && $failure == 0);
  134. $total = 0;
  135. $success = 0;
  136. $failure = 0;
  137. return $ok;
  138. }
  139. function testNormals( &$u, $c, $comment, $reportFailure = false ) {
  140. $result = testNFC( $u, $c, $comment, $reportFailure );
  141. $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
  142. $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
  143. $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
  144. $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
  145. global $verbose;
  146. if( $verbose && !$result && !$reportFailure ) {
  147. print $comment;
  148. testNormals( $u, $c, $comment, true );
  149. }
  150. return $result;
  151. }
  152. function verbosify( $a, $b, $col, $form, $verbose ) {
  153. #$result = ($a === $b);
  154. $result = (strcmp( $a, $b ) == 0);
  155. if( $verbose ) {
  156. $aa = pretty( $a );
  157. $bb = pretty( $b );
  158. $ok = $result ? "succeed" : " failed";
  159. $eq = $result ? "==" : "!=";
  160. print " $ok $form c$col '$aa' $eq '$bb'\n";
  161. }
  162. return $result;
  163. }
  164. function testNFC( &$u, $c, $comment, $verbose ) {
  165. $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
  166. $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
  167. $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
  168. $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
  169. $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
  170. return $result;
  171. }
  172. function testCleanUp( &$u, $c, $comment, $verbose ) {
  173. $x = $c[1];
  174. $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
  175. $x = $c[2];
  176. $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
  177. $x = $c[3];
  178. $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
  179. $x = $c[4];
  180. $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
  181. $x = $c[5];
  182. $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
  183. return $result;
  184. }
  185. function testNFD( &$u, $c, $comment, $verbose ) {
  186. $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
  187. $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
  188. $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
  189. $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
  190. $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
  191. return $result;
  192. }
  193. function testNFKC( &$u, $c, $comment, $verbose ) {
  194. $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
  195. $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
  196. $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
  197. $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
  198. $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
  199. return $result;
  200. }
  201. function testNFKD( &$u, $c, $comment, $verbose ) {
  202. $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
  203. $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
  204. $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
  205. $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
  206. $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
  207. return $result;
  208. }
  209. function testInvariant( &$u, $char, $desc, $reportFailure = false ) {
  210. $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
  211. $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
  212. $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
  213. $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
  214. $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
  215. global $verbose;
  216. if( $verbose && !$result && !$reportFailure ) {
  217. print $desc;
  218. testInvariant( $u, $char, $desc, true );
  219. }
  220. return $result;
  221. }