import unittest from experiment import fixture, run, scenario, split_files class FramingTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.report = run() cls.rows = {row["id"]: row for row in cls.report["rows"]} def test_controls(self): for key in ("unchanged-files", "unchanged-stream", "reordered-files", "tiny-file", "zero-stream"): with self.subTest(key=key): self.assertEqual(self.rows[key]["reuseFraction"], 1) def test_reordering_is_not_framing_neutral(self): self.assertLess(self.rows["reordered-stream"]["reuseFraction"], 1) self.assertEqual(self.rows["reordered-files"]["reuseFraction"], 1) def test_prefix_observation(self): self.assertEqual(self.rows["insert-fixed"]["reusedLogicalBytes"], 0) self.assertGreater(self.rows["insert-cdc"]["reusedLogicalBytes"], 0) self.assertGreater(self.rows["insert-cdc"]["unmatchedLogicalBytes"], 1) def test_empty_is_not_a_zero_percent_failure(self): row = self.rows["empty-file"] self.assertEqual(row["candidateChunkCount"], 0) self.assertIsNone(row["reuseFraction"]) self.assertIsNone(row["observedMeanBytes"]) def test_tiny_file_is_not_dropped(self): row = self.rows["tiny-file"] self.assertEqual(row["reusedLogicalBytes"], 31) self.assertEqual(row["subMinimumTerminalChunks"], 1) def test_all_byte_denominators_and_manifests(self): for row in self.rows.values(): with self.subTest(key=row["id"]): self.assertEqual(sum(f["bytes"] for f in row["candidateFiles"]), row["candidateBytes"]) self.assertEqual(sum(c["length"] for c in row["candidateChunks"]), row["candidateBytes"]) self.assertEqual(sum(c["length"] for c in row["candidateChunks"] if c["matchesBaselineChunk"]), row["reusedLogicalBytes"]) self.assertEqual(row["unmatchedLogicalBytes"] + row["reusedLogicalBytes"], row["candidateBytes"]) def test_determinism(self): self.assertEqual(run(), self.report) def test_parameters_are_part_of_identity(self): self.assertLess(self.rows["changed-parameters"]["reuseFraction"], 1) def test_fixture_prefix_and_validation(self): self.assertEqual(fixture("a", 33)[:32], fixture("a", 32)) with self.assertRaises(ValueError): fixture("a", -1) with self.assertRaises(ValueError): split_files([], "unknown", (2048, 8192, 65536)) with self.assertRaises(ValueError): split_files([], "per-file", (8192, 2048, 65536)) def test_no_within_candidate_dedup_is_counted(self): row = scenario("new", "Repeated new bytes", [("old", b"a" * 31)], [("new1", b"b" * 31), ("new2", b"b" * 31)], "per-file") self.assertEqual(row["reusedLogicalBytes"], 0) self.assertEqual(row["candidateBytes"], 62) if __name__ == "__main__": unittest.main()