import json import unittest from pathlib import Path from unittest.mock import patch import probe class BitmapNegationTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.evidence = probe.collect() def test_partition_counts(self): x = self.evidence["exhaustive"] self.assertEqual(x["truth_universe"]["configurations"], 4 ** 8) self.assertEqual(x["truth_universe"]["incorrect_if_unknown_mask_omitted"], 4 ** 8 - 3 ** 8) self.assertEqual(x["candidate_complement"]["configurations"], 3 ** 8) self.assertEqual(x["candidate_complement"]["incomplete_negative_result"], 3 ** 8 - 2 ** 8) self.assertEqual(x["dual_bounds"]["configurations"], 5 ** 6) def test_hardcoded_sql_oracles(self): expected = { "equal": ([0], [4], [2, 6]), "null_comparison": ([], [0, 2, 4, 6], []), "or_null": ([0, 4], [], [2, 6]), "and_false": ([], [], [0, 2, 4, 6]), "in_with_null": ([0], [2, 4, 6], []), "in_empty": ([], [], [0, 2, 4, 6]), "is_null": ([4], [], [0, 2, 6]), } self.assertEqual(len(self.evidence["native_predicates"]), 14) for row in self.evidence["native_predicates"]: with self.subTest(name=row["predicate"], indexed=row["index_present"]): self.assertEqual((row["true"], row["unknown"], row["sql_not"]), expected[row["predicate"]]) def test_index_presence_invariance(self): rows = self.evidence["native_predicates"] for first, second in zip(rows[:7], rows[7:]): self.assertEqual({k: v for k, v in first.items() if k != "index_present"}, {k: v for k, v in second.items() if k != "index_present"}) def test_native_snapshot_oracles(self): s = self.evidence["wal_snapshots"] self.assertEqual(s["before"]["sql_not"], [2, 6]) self.assertEqual(s["current"]["sql_not"], [3, 4]) self.assertEqual(s["before"], s["held_after_commit"]) self.assertEqual(s["current"], s["reader_after_release"]) mixed = next(m for m in s["mixed_masks"] if (m["universe_snapshot"], m["true_snapshot"], m["unknown_snapshot"]) == (1, 0, 0)) self.assertEqual(mixed["mixed_result"], [2, 3]) self.assertEqual(mixed["current_false_positives"], [2]) self.assertEqual(mixed["current_missing_rows"], [4]) self.assertEqual(mixed["after_current_predicate_recheck"], [3]) def test_minimal_candidate_counterexample(self): universe, true, candidate = {0, 1}, {0}, {0, 1} self.assertEqual(universe - candidate, set()) self.assertEqual(universe - true, {1}) self.assertEqual((universe - candidate) & (universe - true), set()) def test_native_candidate_inversion_and_refinement(self): c = self.evidence["native_candidate_complement"] self.assertEqual(c["native_negative"], [2, 6]) self.assertEqual(c["inverted_candidate_with_unknown_removed"], [6]) self.assertEqual(c["native_rechecked_survivors"], [6]) self.assertEqual(c["native_negative_after_refining_positive_candidate"], [2, 6]) def test_holes_and_unknown_are_distinct(self): self.assertEqual(probe.ids(probe.sql_not(0b01010101, 1, 0b00010000)), [2, 6]) self.assertEqual(probe.ids(0b11111111 & ~1), [1, 2, 3, 4, 5, 6, 7]) self.assertEqual(probe.ids(0b01010101 & ~1), [2, 4, 6]) def test_invalid_contracts_fail(self): for args in [(7, 1, 1), (7, 8, 0), (-1, 0, 0), (7, -1, 0), (7, True, 0), (7, 0.5, 0)]: with self.subTest(args=args), self.assertRaises(ValueError): probe.sql_not(*args) def test_empty_and_all_unknown(self): self.assertEqual(probe.sql_not(0, 0, 0), 0) self.assertEqual(probe.sql_not(7, 0, 7), 0) self.assertEqual(probe.sql_not(7, 7, 0), 0) self.assertEqual(probe.sql_not(7, 0, 0), 7) def test_negative_control_rejects_missing_unknown(self): with patch.object(probe, "sql_not", lambda e, t, n: e & ~t): with self.assertRaises(ValueError): probe.native_matrix() def test_recorded_evidence_matches(self): recorded = json.loads((Path(__file__).parent / "evidence.json").read_text()) self.assertEqual(self.evidence, recorded) def test_no_clock_or_path_in_deterministic_results(self): encoded = probe.serialize(self.evidence) self.assertNotIn("/srv/", encoded) self.assertNotIn("/tmp/", encoded) self.assertEqual(json.loads(encoded), self.evidence) if __name__ == "__main__": unittest.main()