diff --git a/tests/test_rust_parity.py b/tests/test_rust_parity.py index 3efc9de1c..b14723c16 100644 --- a/tests/test_rust_parity.py +++ b/tests/test_rust_parity.py @@ -37,6 +37,36 @@ class ParityRunnerTests(unittest.TestCase): right.write_text(left.read_text()) self.assertEqual(parity.compare(left, right), 2) + def test_chunk_comparison_preserves_newlines_unicode_and_unterminated_lines(self): + cases = [ + ("one", "one", 1), + ("\n\n", "\n\n", 2), + ("one\r\ntwo\r", "one\ntwo\n", 2), + ("α" * (1024 * 1024 + 1) + "\r\nlast", "α" * (1024 * 1024 + 1) + "\nlast", 2), + ("same\n" * 220000 + "tail", "same\n" * 220000 + "tail", 220001), + ] + with tempfile.TemporaryDirectory() as temp: + left, right = Path(temp) / "reference", Path(temp) / "candidate" + for reference, candidate, count in cases: + left.write_bytes(reference.encode("utf-8")) + right.write_bytes(candidate.encode("utf-8")) + self.assertEqual(parity.compare(left, right), count) + + def test_chunk_mismatch_retains_exact_first_line_diagnostic(self): + prefix = "same\n" * 220000 + cases = [("expected\n", "actual\n", "'expected\\n'", "'actual\\n'"), + ("tail", "tail\n", "'tail'", "'tail\\n'"), + ("extra\n", "", "'extra\\n'", "None")] + with tempfile.TemporaryDirectory() as temp: + left, right = Path(temp) / "reference", Path(temp) / "candidate" + for reference, candidate, expected, actual in cases: + left.write_text(prefix + reference) + right.write_text(prefix + candidate) + with self.assertRaises(AssertionError) as caught: + parity.compare(left, right) + self.assertEqual(str(caught.exception), + f"Parity mismatch at output line 220001: vanilla={expected}, Rust={actual}") + def test_failed_artifact_verification_removes_previous_success(self): with tempfile.TemporaryDirectory() as temp: local = Path(temp) diff --git a/tools/test_rust_parity.py b/tools/test_rust_parity.py index 21b013b57..beffc2baa 100644 --- a/tools/test_rust_parity.py +++ b/tools/test_rust_parity.py @@ -103,15 +103,28 @@ def verify_artifacts(): def compare(reference, candidate): + # Text mode retains cross-platform universal-newline behavior. Equal chunks + # avoid a Python loop for every line in multi-gigabyte differential traces. count = 0 - with reference.open(encoding="utf-8") as expected, candidate.open(encoding="utf-8") as actual: - for count, (left, right) in enumerate(itertools.zip_longest(expected, actual), 1): + last = '' + with reference.open(encoding='utf-8') as expected, candidate.open(encoding='utf-8') as actual: + while True: + left, right = expected.read(1024 * 1024), actual.read(1024 * 1024) if left != right: - raise AssertionError(f"Parity mismatch at output line {count}: vanilla={left!r}, Rust={right!r}") - if count == 0: - raise AssertionError("Empty output cannot establish parity") - return count - + break + if not left: + if not last: + raise AssertionError('Empty output cannot establish parity') + return count + (last != '\n') + count += left.count('\n') + last = left[-1] + # Preserve the existing exact first-line mismatch diagnostic. Mismatches are + # exceptional; successful runs use only the bounded chunk buffers above. + with reference.open(encoding='utf-8') as expected, candidate.open(encoding='utf-8') as actual: + for line, (left, right) in enumerate(itertools.zip_longest(expected, actual), 1): + if left != right: + raise AssertionError(f'Parity mismatch at output line {line}: vanilla={left!r}, Rust={right!r}') + raise AssertionError('Parity outputs changed during comparison') def run(command, **kwargs): subprocess.run(command, cwd=lab.ROOT, check=True, timeout=300, **kwargs)