import unittest from fractions import Fraction from functools import lru_cache from itertools import combinations, combinations_with_replacement, permutations from model import capacity, example, placement_ceiling, results @lru_cache(None) def integer_oracle(state, shards): """Try every legal one-unit stripe, without using the bound formula.""" best = 0 for indices in combinations(range(len(state)), shards): if all(state[index] > 0 for index in indices): remaining = list(state) for index in indices: remaining[index] -= 1 best = max(best, 1 + integer_oracle(tuple(sorted(remaining)), shards)) return best class PlacementTests(unittest.TestCase): def test_all_small_integer_allocations(self): for domains in range(1, 6): for caps in combinations_with_replacement(range(4), domains): for shards in range(1, domains + 2): with self.subTest(capacities=caps, shards=shards): bound = placement_ceiling(caps, shards) self.assertEqual(bound.numerator // bound.denominator, integer_oracle(caps, shards)) def test_fractional_optimum_scaled_oracle(self): for caps, shards in [([1, 1, 1], 2), ([3, 1, 1, 1], 3), ([3, 2, 2, 2], 4)]: bound = placement_ceiling(caps, shards) scale = bound.denominator scaled = tuple(sorted(value * scale for value in caps)) self.assertEqual(integer_oracle(scaled, shards), bound * scale) def test_published_examples(self): rows = results()["examples"] self.assertEqual([row["logical_TiB"] for row in rows], ["20", "20", "30", "40", "40", "57/2", "18"]) self.assertEqual([row["stranded_within_budget_TiB"] for row in rows], ["40", "100", "30", "20", "40", "57/2", "0"]) self.assertEqual(rows[-1]["reserved_by_threshold_TiB"], "6") def test_four_domain_constructive_witness(self): loads = [0] * 4 stripes = [((0, 1, 2), 10), ((0, 1, 3), 10), ((0, 2, 3), 10)] for domains, amount in stripes: for domain in domains: loads[domain] += amount self.assertEqual(loads, [30, 20, 20, 20]) self.assertEqual(sum(amount for _, amount in stripes), placement_ceiling([60, 20, 20, 20], 3)) def test_all_single_domain_losses(self): caps = [60, 20, 20, 20] bounds = [] for lost in range(4): survivor = [Fraction(c * 9, 10) if i != lost else 0 for i, c in enumerate(caps)] bounds.append(placement_ceiling(survivor, 3)) self.assertEqual(bounds, [18] * 4) def test_permutation_and_scale(self): for caps in permutations([60, 20, 20, 20]): self.assertEqual(placement_ceiling(caps, 3), 30) self.assertEqual(placement_ceiling(["0.6", "0.2", "0.2"], 3), Fraction(1, 5)) self.assertEqual(placement_ceiling([10**30, 10**30, 10**30], 3), 10**30) def test_missing_domains_and_zeros(self): self.assertEqual(placement_ceiling([], 3), 0) self.assertEqual(placement_ceiling([60, 20], 3), 0) self.assertEqual(placement_ceiling([60, 20, 0], 3), 0) self.assertEqual(placement_ceiling([60, 0, 20], 1), 80) def test_reject_invalid_inputs(self): for bad in [-1, True, 0.5, float("nan"), float("inf"), "nan", "1/0", None]: with self.subTest(value=bad), self.assertRaises(ValueError): capacity(bad) for shards in [0, -1, True, 1.5, "3"]: with self.assertRaises(ValueError): placement_ceiling([1, 1, 1], shards) for ratio in ["0", "1.01"]: with self.assertRaises(ValueError): example("bad", [1, 1, 1], ratio=ratio) with self.assertRaises(ValueError): example("bad", [1, 1, 1], data_shards=True) if __name__ == "__main__": unittest.main()