bless_tests.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env php
  2. <?php
  3. if ($argc < 2) {
  4. die("Usage: php bless_tests.php dir/\n");
  5. }
  6. $files = getFiles(array_slice($argv, 1));
  7. foreach ($files as $path) {
  8. if (!preg_match('/^(.*)\.phpt$/', $path, $matches)) {
  9. // Not a phpt test
  10. continue;
  11. }
  12. $outPath = $matches[1] . '.out';
  13. if (!file_exists($outPath)) {
  14. // Test did not fail
  15. continue;
  16. }
  17. $phpt = file_get_contents($path);
  18. $out = file_get_contents($outPath);
  19. if (false !== strpos($phpt, '--XFAIL--')) {
  20. // Don't modify expected output of XFAIL tests
  21. continue;
  22. }
  23. // Don't update EXPECTREGEX tests
  24. if (!preg_match('/--EXPECT(F?)--(.*)$/s', $phpt, $matches)) {
  25. continue;
  26. }
  27. $oldExpect = trim($matches[2]);
  28. $isFormat = $matches[1] == 'F';
  29. if ($isFormat) {
  30. $out = generateMinimallyDifferingOutput($out, $oldExpect);
  31. } else {
  32. $out = normalizeOutput($out);
  33. }
  34. $phpt = insertOutput($phpt, $out);
  35. file_put_contents($path, $phpt);
  36. }
  37. function getFiles(array $dirsOrFiles): \Iterator {
  38. foreach ($dirsOrFiles as $dirOrFile) {
  39. if (is_dir($dirOrFile)) {
  40. $it = new RecursiveIteratorIterator(
  41. new RecursiveDirectoryIterator($dirOrFile),
  42. RecursiveIteratorIterator::LEAVES_ONLY
  43. );
  44. foreach ($it as $file) {
  45. yield $file->getPathName();
  46. }
  47. } else if (is_file($dirOrFile)) {
  48. yield $dirOrFile;
  49. } else {
  50. die("$dirOrFile is not a directory or file\n");
  51. }
  52. }
  53. }
  54. function normalizeOutput(string $out): string {
  55. $out = preg_replace('/in (\/|[A-Z]:\\\\).+ on line \d+$/m', 'in %s on line %d', $out);
  56. $out = preg_replace('/in (\/|[A-Z]:\\\\).+:\d+$/m', 'in %s:%d', $out);
  57. $out = preg_replace('/^#(\d+) (\/|[A-Z]:\\\\).+\(\d+\):/m', '#$1 %s(%d):', $out);
  58. $out = preg_replace('/Resource id #\d+/', 'Resource id #%d', $out);
  59. $out = preg_replace('/resource\(\d+\) of type/', 'resource(%d) of type', $out);
  60. $out = preg_replace(
  61. '/Resource ID#\d+ used as offset, casting to integer \(\d+\)/',
  62. 'Resource ID#%d used as offset, casting to integer (%d)',
  63. $out);
  64. $out = preg_replace('/string\(\d+\) "([^"]*%d)/', 'string(%d) "$1', $out);
  65. $out = str_replace("\0", '%0', $out);
  66. return $out;
  67. }
  68. function formatToRegex(string $format): string {
  69. $result = preg_quote($format, '/');
  70. $result = str_replace('%e', '\\' . DIRECTORY_SEPARATOR, $result);
  71. $result = str_replace('%s', '[^\r\n]+', $result);
  72. $result = str_replace('%S', '[^\r\n]*', $result);
  73. $result = str_replace('%w', '\s*', $result);
  74. $result = str_replace('%i', '[+-]?\d+', $result);
  75. $result = str_replace('%d', '\d+', $result);
  76. $result = str_replace('%x', '[0-9a-fA-F]+', $result);
  77. $result = str_replace('%f', '[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?', $result);
  78. $result = str_replace('%c', '.', $result);
  79. $result = str_replace('%0', '\0', $result);
  80. return "/^$result$/s";
  81. }
  82. function generateMinimallyDifferingOutput(string $out, string $oldExpect) {
  83. $outLines = explode("\n", $out);
  84. $oldExpectLines = explode("\n", $oldExpect);
  85. $differ = new Differ(function($oldExpect, $new) {
  86. if (strpos($oldExpect, '%') === false) {
  87. return $oldExpect === $new;
  88. }
  89. return preg_match(formatToRegex($oldExpect), $new);
  90. });
  91. $diff = $differ->diff($oldExpectLines, $outLines);
  92. $result = [];
  93. foreach ($diff as $elem) {
  94. if ($elem->type == DiffElem::TYPE_KEEP) {
  95. $result[] = $elem->old;
  96. } else if ($elem->type == DiffElem::TYPE_ADD) {
  97. $result[] = normalizeOutput($elem->new);
  98. }
  99. }
  100. return implode("\n", $result);
  101. }
  102. function insertOutput(string $phpt, string $out): string {
  103. return preg_replace_callback('/--EXPECTF?--.*?(--CLEAN--|$)/sD', function($matches) use($out) {
  104. $hasWildcard = preg_match('/%[resSaAwidxfc0]/', $out);
  105. $F = $hasWildcard ? 'F' : '';
  106. return "--EXPECT$F--\n" . $out . "\n" . $matches[1];
  107. }, $phpt);
  108. }
  109. /**
  110. * Implementation of the the Myers diff algorithm.
  111. *
  112. * Myers, Eugene W. "An O (ND) difference algorithm and its variations."
  113. * Algorithmica 1.1 (1986): 251-266.
  114. */
  115. class DiffElem
  116. {
  117. const TYPE_KEEP = 0;
  118. const TYPE_REMOVE = 1;
  119. const TYPE_ADD = 2;
  120. /** @var int One of the TYPE_* constants */
  121. public $type;
  122. /** @var mixed Is null for add operations */
  123. public $old;
  124. /** @var mixed Is null for remove operations */
  125. public $new;
  126. public function __construct(int $type, $old, $new) {
  127. $this->type = $type;
  128. $this->old = $old;
  129. $this->new = $new;
  130. }
  131. }
  132. class Differ
  133. {
  134. private $isEqual;
  135. /**
  136. * Create differ over the given equality relation.
  137. *
  138. * @param callable $isEqual Equality relation with signature function($a, $b) : bool
  139. */
  140. public function __construct(callable $isEqual) {
  141. $this->isEqual = $isEqual;
  142. }
  143. /**
  144. * Calculate diff (edit script) from $old to $new.
  145. *
  146. * @param array $old Original array
  147. * @param array $new New array
  148. *
  149. * @return DiffElem[] Diff (edit script)
  150. */
  151. public function diff(array $old, array $new) {
  152. list($trace, $x, $y) = $this->calculateTrace($old, $new);
  153. return $this->extractDiff($trace, $x, $y, $old, $new);
  154. }
  155. private function calculateTrace(array $a, array $b) {
  156. $n = \count($a);
  157. $m = \count($b);
  158. $max = $n + $m;
  159. $v = [1 => 0];
  160. $trace = [];
  161. for ($d = 0; $d <= $max; $d++) {
  162. $trace[] = $v;
  163. for ($k = -$d; $k <= $d; $k += 2) {
  164. if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
  165. $x = $v[$k+1];
  166. } else {
  167. $x = $v[$k-1] + 1;
  168. }
  169. $y = $x - $k;
  170. while ($x < $n && $y < $m && ($this->isEqual)($a[$x], $b[$y])) {
  171. $x++;
  172. $y++;
  173. }
  174. $v[$k] = $x;
  175. if ($x >= $n && $y >= $m) {
  176. return [$trace, $x, $y];
  177. }
  178. }
  179. }
  180. throw new \Exception('Should not happen');
  181. }
  182. private function extractDiff(array $trace, int $x, int $y, array $a, array $b) {
  183. $result = [];
  184. for ($d = \count($trace) - 1; $d >= 0; $d--) {
  185. $v = $trace[$d];
  186. $k = $x - $y;
  187. if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
  188. $prevK = $k + 1;
  189. } else {
  190. $prevK = $k - 1;
  191. }
  192. $prevX = $v[$prevK];
  193. $prevY = $prevX - $prevK;
  194. while ($x > $prevX && $y > $prevY) {
  195. $result[] = new DiffElem(DiffElem::TYPE_KEEP, $a[$x-1], $b[$y-1]);
  196. $x--;
  197. $y--;
  198. }
  199. if ($d === 0) {
  200. break;
  201. }
  202. while ($x > $prevX) {
  203. $result[] = new DiffElem(DiffElem::TYPE_REMOVE, $a[$x-1], null);
  204. $x--;
  205. }
  206. while ($y > $prevY) {
  207. $result[] = new DiffElem(DiffElem::TYPE_ADD, null, $b[$y-1]);
  208. $y--;
  209. }
  210. }
  211. return array_reverse($result);
  212. }
  213. }