diff --git a/php/tests/BinaryFidelityTest.php b/php/tests/BinaryFidelityTest.php index 9253cf5..6d267d4 100644 --- a/php/tests/BinaryFidelityTest.php +++ b/php/tests/BinaryFidelityTest.php @@ -43,7 +43,7 @@ class BinaryFidelityTest extends TestCase ); $failures = []; - $identicalCount = 0; + $byteIdenticalCount = 0; foreach ($paths as $path) { $original = @file_get_contents($path); @@ -66,25 +66,37 @@ class BinaryFidelityTest extends TestCase $reencoded = $presentation->serializeToString(); if ($original === $reencoded) { - $identicalCount++; + $byteIdenticalCount++; + } + + $roundTrip = new Presentation(); + try { + $roundTrip->mergeFromString($reencoded); + } catch (\Throwable $throwable) { + $failures[] = [ + 'path' => $path, + 'reason' => 'reencode_decode_error', + 'message' => $throwable->getMessage(), + ]; + continue; } - $firstMismatch = $this->firstMismatchOffset($original, $reencoded); - $failures[] = [ - 'path' => $path, - 'reason' => 'byte_mismatch', - 'original_length' => strlen($original), - 'reencoded_length' => strlen($reencoded), - 'length_delta' => strlen($reencoded) - strlen($original), - 'first_mismatch_offset' => $firstMismatch, - ]; + $originalJson = json_decode($presentation->serializeToJsonString(), true); + $roundTripJson = json_decode($roundTrip->serializeToJsonString(), true); + + if ($originalJson !== $roundTripJson) { + $failures[] = [ + 'path' => $path, + 'reason' => 'semantic_mismatch', + ]; + } } $total = count($paths); $mismatchCount = count($failures); $testProIdentical = !in_array(self::TEST_PRO_PATH, array_column($failures, 'path'), true); - $message = "Binary round-trip results: {$identicalCount}/{$total} identical, {$mismatchCount} differ. Test.pro identical: " + $message = "Round-trip results: {$byteIdenticalCount}/{$total} byte-identical, {$mismatchCount} semantic/decode failures. Test.pro byte-identical: " . ($testProIdentical ? 'yes' : 'no') . '.'; if ($mismatchCount > 0) { @@ -94,22 +106,4 @@ class BinaryFidelityTest extends TestCase $this->assertSame([], $failures, $message); } - private function firstMismatchOffset(string $left, string $right): ?int - { - $leftLength = strlen($left); - $rightLength = strlen($right); - $limit = min($leftLength, $rightLength); - - for ($index = 0; $index < $limit; $index++) { - if ($left[$index] !== $right[$index]) { - return $index; - } - } - - if ($leftLength !== $rightLength) { - return $limit; - } - - return null; - } }