|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the Zephir. |
| 7 | + * |
| 8 | + * (c) Phalcon Team <team@zephir-lang.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Extension\Optimizers; |
| 15 | + |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Stub\Optimizers\Substr; |
| 18 | + |
| 19 | +/** |
| 20 | + * Exhaustive differential gate for zephir_substr(). |
| 21 | + * |
| 22 | + * The hand-written cases in SubstrTest document intent; this one proves |
| 23 | + * totality by comparing every (subject, from, length) combination against |
| 24 | + * native substr(). It exists because the kernel spent ten years diverging |
| 25 | + * from PHP on ~24% of its input space without a single test noticing. |
| 26 | + * |
| 27 | + * Not a data-provider test on purpose: 20k PHPUnit cases would dominate the |
| 28 | + * suite's runtime and output. One assertion, full divergence list on failure. |
| 29 | + */ |
| 30 | +final class SubstrParityTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Subjects PHP accepts and coerces. Arrays and bare objects are excluded: |
| 34 | + * PHP 8 raises TypeError for those, while Zephir routes them through |
| 35 | + * zend_make_printable_zval. That delta is pre-existing and out of scope. |
| 36 | + */ |
| 37 | + private const SUBJECTS = [ |
| 38 | + "''" => '', |
| 39 | + "'a'" => 'a', |
| 40 | + "'abcdef'" => 'abcdef', |
| 41 | + "'GetPosts'" => 'GetPosts', |
| 42 | + 'binary' => "\x00ab", |
| 43 | + 'long20' => 'abcdefghijklmnopqrst', |
| 44 | + 'null' => null, |
| 45 | + 'true' => true, |
| 46 | + 'false' => false, |
| 47 | + 'int0' => 0, |
| 48 | + 'int12345' => 12345, |
| 49 | + 'float1.5' => 1.5, |
| 50 | + ]; |
| 51 | + |
| 52 | + public function testMatchesNativeSubstrForEveryCombination(): void |
| 53 | + { |
| 54 | + $test = new Substr(); |
| 55 | + $divergent = []; |
| 56 | + $combos = 0; |
| 57 | + |
| 58 | + foreach (self::SUBJECTS as $name => $subject) { |
| 59 | + // This file declares strict_types, so native substr() would reject a |
| 60 | + // non-string subject outright. Cast first: (string) reproduces exactly |
| 61 | + // the weak-mode coercion PHP applies to a string parameter |
| 62 | + // (null -> "", false -> "", true -> "1", 1.5 -> "1.5"), which is the |
| 63 | + // behaviour the kernel is being held to. |
| 64 | + $native = (string) $subject; |
| 65 | + |
| 66 | + // Span is driven by the coerced length so the sweep always straddles |
| 67 | + // the len-1 / len / len+1 boundary where the off-by-one lived. |
| 68 | + $span = strlen($native) + 4; |
| 69 | + $froms = array_merge(range(-$span, $span), [PHP_INT_MIN, PHP_INT_MAX]); |
| 70 | + $lens = array_merge(range(-$span, $span), [PHP_INT_MIN, PHP_INT_MAX]); |
| 71 | + |
| 72 | + foreach ($froms as $from) { |
| 73 | + ++$combos; |
| 74 | + $this->collect( |
| 75 | + $divergent, |
| 76 | + $name, |
| 77 | + $from, |
| 78 | + null, |
| 79 | + $test->testTwoArguments($subject, $from), |
| 80 | + substr($native, $from) |
| 81 | + ); |
| 82 | + |
| 83 | + foreach ($lens as $len) { |
| 84 | + ++$combos; |
| 85 | + $this->collect( |
| 86 | + $divergent, |
| 87 | + $name, |
| 88 | + $from, |
| 89 | + $len, |
| 90 | + $test->testThreeArguments($subject, $from, $len), |
| 91 | + substr($native, $from, $len) |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + $this->assertSame( |
| 98 | + [], |
| 99 | + $divergent, |
| 100 | + sprintf( |
| 101 | + "zephir_substr() diverges from native substr() in %d of %d combinations:\n%s", |
| 102 | + count($divergent), |
| 103 | + $combos, |
| 104 | + implode("\n", array_slice($divergent, 0, 40)) |
| 105 | + ) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param list<string> $divergent |
| 111 | + */ |
| 112 | + private function collect(array &$divergent, string $name, int $from, ?int $len, $actual, $expected): void |
| 113 | + { |
| 114 | + if ($actual === $expected) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $divergent[] = sprintf( |
| 119 | + ' %s from=%s length=%s -> zephir=%s php=%s', |
| 120 | + $name, |
| 121 | + $from, |
| 122 | + $len ?? '-', |
| 123 | + var_export($actual, true), |
| 124 | + var_export($expected, true) |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments