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

/includes/normal/UtfNormalTest.php

https://github.com/daevid/MWFork
PHP | 248 lines | 188 code | 26 blank | 34 comment | 63 complexity | 711e62fa50e71a28d513e6e362df93f3 MD5 | raw file
  1. <?php
  2. /**
  3. * Implements the conformance test at:
  4. * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
  5. *
  6. * Copyright © 2004 Brion Vibber <brion@pobox.com>
  7. * http://www.mediawiki.org/
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. * @ingroup UtfNormal
  26. */
  27. $verbose = true;
  28. #define( 'PRETTY_UTF8', true );
  29. if( defined( 'PRETTY_UTF8' ) ) {
  30. function pretty( $string ) {
  31. return preg_replace( '/([\x00-\xff])/e',
  32. 'sprintf("%02X", ord("$1"))',
  33. $string );
  34. }
  35. } else {
  36. /**
  37. * @ignore
  38. */
  39. function pretty( $string ) {
  40. return trim( preg_replace( '/(.)/use',
  41. 'sprintf("%04X ", utf8ToCodepoint("$1"))',
  42. $string ) );
  43. }
  44. }
  45. if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
  46. dl( 'php_utfnormal.so' );
  47. }
  48. require_once 'UtfNormalDefines.php';
  49. require_once 'UtfNormalUtil.php';
  50. require_once 'UtfNormal.php';
  51. if( php_sapi_name() != 'cli' ) {
  52. die( "Run me from the command line please.\n" );
  53. }
  54. $in = fopen("NormalizationTest.txt", "rt");
  55. if( !$in ) {
  56. print "Couldn't open NormalizationTest.txt -- can't run tests.\n";
  57. print "If necessary, manually download this file. It can be obtained at\n";
  58. print "http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt";
  59. exit(-1);
  60. }
  61. $normalizer = new UtfNormal;
  62. $total = 0;
  63. $success = 0;
  64. $failure = 0;
  65. $ok = true;
  66. $testedChars = array();
  67. while( false !== ( $line = fgets( $in ) ) ) {
  68. list( $data, $comment ) = explode( '#', $line );
  69. if( $data === '' ) continue;
  70. $matches = array();
  71. if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
  72. if( $matches[1] > 0 ) {
  73. $ok = reportResults( $total, $success, $failure ) && $ok;
  74. }
  75. print "Part {$matches[1]}: $comment";
  76. continue;
  77. }
  78. $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
  79. array_unshift( $columns, '' );
  80. $testedChars[$columns[1]] = true;
  81. $total++;
  82. if( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
  83. $success++;
  84. } else {
  85. $failure++;
  86. # print "FAILED: $comment";
  87. }
  88. if( $total % 100 == 0 ) print "$total ";
  89. }
  90. fclose( $in );
  91. $ok = reportResults( $total, $success, $failure ) && $ok;
  92. $in = fopen("UnicodeData.txt", "rt" );
  93. if( !$in ) {
  94. print "Can't open UnicodeData.txt for reading.\n";
  95. print "If necessary, fetch this file from the internet:\n";
  96. print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
  97. exit(-1);
  98. }
  99. print "Now testing invariants...\n";
  100. while( false !== ($line = fgets( $in ) ) ) {
  101. $cols = explode( ';', $line );
  102. $char = codepointToUtf8( hexdec( $cols[0] ) );
  103. $desc = $cols[0] . ": " . $cols[1];
  104. if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
  105. # Can't check NULL with the ICU plugin, as null bytes fail in C land.
  106. # Skip other control characters, as we strip them for XML safety.
  107. # Surrogates are illegal on their own or in UTF-8, ignore.
  108. continue;
  109. }
  110. if( empty( $testedChars[$char] ) ) {
  111. $total++;
  112. if( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
  113. $success++;
  114. } else {
  115. $failure++;
  116. }
  117. if( $total % 100 == 0 ) print "$total ";
  118. }
  119. }
  120. fclose( $in );
  121. $ok = reportResults( $total, $success, $failure ) && $ok;
  122. if( $ok ) {
  123. print "TEST SUCCEEDED!\n";
  124. exit(0);
  125. } else {
  126. print "TEST FAILED!\n";
  127. exit(-1);
  128. }
  129. ## ------
  130. function reportResults( &$total, &$success, &$failure ) {
  131. $percSucc = intval( $success * 100 / $total );
  132. $percFail = intval( $failure * 100 / $total );
  133. print "\n";
  134. print "$success tests successful ($percSucc%)\n";
  135. print "$failure tests failed ($percFail%)\n\n";
  136. $ok = ($success > 0 && $failure == 0);
  137. $total = 0;
  138. $success = 0;
  139. $failure = 0;
  140. return $ok;
  141. }
  142. function testNormals( &$u, $c, $comment, $verbose, $reportFailure = false ) {
  143. $result = testNFC( $u, $c, $comment, $reportFailure );
  144. $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
  145. $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
  146. $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
  147. $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
  148. if( $verbose && !$result && !$reportFailure ) {
  149. print $comment;
  150. testNormals( $u, $c, $comment, $verbose, true );
  151. }
  152. return $result;
  153. }
  154. function verbosify( $a, $b, $col, $form, $verbose ) {
  155. #$result = ($a === $b);
  156. $result = (strcmp( $a, $b ) == 0);
  157. if( $verbose ) {
  158. $aa = pretty( $a );
  159. $bb = pretty( $b );
  160. $ok = $result ? "succeed" : " failed";
  161. $eq = $result ? "==" : "!=";
  162. print " $ok $form c$col '$aa' $eq '$bb'\n";
  163. }
  164. return $result;
  165. }
  166. function testNFC( &$u, $c, $comment, $verbose ) {
  167. $result = verbosify( $c[2], $u->toNFC( $c[1] ), 1, 'NFC', $verbose );
  168. $result = verbosify( $c[2], $u->toNFC( $c[2] ), 2, 'NFC', $verbose ) && $result;
  169. $result = verbosify( $c[2], $u->toNFC( $c[3] ), 3, 'NFC', $verbose ) && $result;
  170. $result = verbosify( $c[4], $u->toNFC( $c[4] ), 4, 'NFC', $verbose ) && $result;
  171. $result = verbosify( $c[4], $u->toNFC( $c[5] ), 5, 'NFC', $verbose ) && $result;
  172. return $result;
  173. }
  174. function testCleanUp( &$u, $c, $comment, $verbose ) {
  175. $x = $c[1];
  176. $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
  177. $x = $c[2];
  178. $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
  179. $x = $c[3];
  180. $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
  181. $x = $c[4];
  182. $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
  183. $x = $c[5];
  184. $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
  185. return $result;
  186. }
  187. function testNFD( &$u, $c, $comment, $verbose ) {
  188. $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
  189. $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
  190. $result = verbosify( $c[3], $u->toNFD( $c[3] ), 3, 'NFD', $verbose ) && $result;
  191. $result = verbosify( $c[5], $u->toNFD( $c[4] ), 4, 'NFD', $verbose ) && $result;
  192. $result = verbosify( $c[5], $u->toNFD( $c[5] ), 5, 'NFD', $verbose ) && $result;
  193. return $result;
  194. }
  195. function testNFKC( &$u, $c, $comment, $verbose ) {
  196. $result = verbosify( $c[4], $u->toNFKC( $c[1] ), 1, 'NFKC', $verbose );
  197. $result = verbosify( $c[4], $u->toNFKC( $c[2] ), 2, 'NFKC', $verbose ) && $result;
  198. $result = verbosify( $c[4], $u->toNFKC( $c[3] ), 3, 'NFKC', $verbose ) && $result;
  199. $result = verbosify( $c[4], $u->toNFKC( $c[4] ), 4, 'NFKC', $verbose ) && $result;
  200. $result = verbosify( $c[4], $u->toNFKC( $c[5] ), 5, 'NFKC', $verbose ) && $result;
  201. return $result;
  202. }
  203. function testNFKD( &$u, $c, $comment, $verbose ) {
  204. $result = verbosify( $c[5], $u->toNFKD( $c[1] ), 1, 'NFKD', $verbose );
  205. $result = verbosify( $c[5], $u->toNFKD( $c[2] ), 2, 'NFKD', $verbose ) && $result;
  206. $result = verbosify( $c[5], $u->toNFKD( $c[3] ), 3, 'NFKD', $verbose ) && $result;
  207. $result = verbosify( $c[5], $u->toNFKD( $c[4] ), 4, 'NFKD', $verbose ) && $result;
  208. $result = verbosify( $c[5], $u->toNFKD( $c[5] ), 5, 'NFKD', $verbose ) && $result;
  209. return $result;
  210. }
  211. function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
  212. $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
  213. $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
  214. $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
  215. $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
  216. $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
  217. if( $verbose && !$result && !$reportFailure ) {
  218. print $desc;
  219. testInvariant( $u, $char, $desc, $verbose, true );
  220. }
  221. return $result;
  222. }