import itertools import json from fractions import Fraction as F from pathlib import Path import unittest from model import decode, interval, results, shortest_prefix, phrase_rate_control class ArithmeticIntervals(unittest.TestCase): def test_known_endpoints(self): self.assertEqual(interval("AA"), (F(0), F(9, 16))) self.assertEqual(interval("AB"), (F(9, 16), F(3, 4))) self.assertEqual(interval("BA"), (F(3, 4), F(15, 16))) self.assertEqual(interval("BB"), (F(15, 16), F(1))) self.assertEqual([r["prefix"] for r in results()["rows"]], ["0", "101", "110", "1111"]) def test_roundtrip_and_minimality(self): for weights in ((3, 1), (1, 3), (1, 1), (2, 5)): for length in range(8): for symbols in itertools.product("AB", repeat=length): message = "".join(symbols) low, high = interval(message, weights) prefix, start, end = shortest_prefix(low, high) self.assertLessEqual(low, start) self.assertLessEqual(end, high) self.assertEqual(decode(start, length, weights), message) self.assertEqual(decode((start + end) / 2, length, weights), message) # For shorter grids, inspect the first cell whose start is in the interval. for bits in range(len(prefix)): scale = 1 << bits first = (low.numerator * scale + low.denominator - 1) // low.denominator self.assertGreater(F(first + 1, scale), high) def test_probability_and_accounting(self): rows = results()["rows"] self.assertEqual(sum(F(r["probability"]) for r in rows), 1) self.assertEqual(sum(F(r["probability"]) * r["payload_bits"] for r in rows), F(31, 16)) self.assertEqual(results()["expected_payload_bits"], "31/16") self.assertTrue(all(r["packed_bytes"] == 1 for r in rows)) self.assertGreater(rows[1]["payload_bits"], rows[1]["fixed_width_bits"]) def test_wrong_model_and_length(self): self.assertEqual(decode(F(5, 8), 2), "AB") self.assertEqual(decode(F(5, 8), 2, (1, 3)), "BB") self.assertEqual(decode(F(5, 8), 1), "A") self.assertEqual(decode(F(3, 4), 1), "B") self.assertEqual(decode(F(0), 0), "") def test_invalid_inputs(self): for weights in ((0, 1), (-1, 3), (True, 1), (1.5, 1), (1,)): with self.assertRaises(ValueError): interval("A", weights) for message in ("C", "A" * 65, None): with self.assertRaises(ValueError): interval(message) for tag, length in ((F(1), 1), (F(-1), 1), (F(0), -1), (F(0), True)): with self.assertRaises(ValueError): decode(tag, length) with self.assertRaises(ValueError): shortest_prefix(F(1), F(1)) def test_phrase_rate_exact(self): r = phrase_rate_control() self.assertEqual([row["count"] for row in r["rows"]], [27, 9, 12, 16]) self.assertEqual((r["phrase_count"], r["source_symbols"], r["payload_bits"]), (64, 148, 128)) self.assertEqual(r["expected_phrase_length"], "37/16") self.assertEqual(r["aggregate_bits_per_symbol"], "32/37") self.assertEqual(r["phrase_weighted_bits_per_symbol"], "17/16") def test_phrase_dictionary_and_probability(self): rows = phrase_rate_control()["rows"] self.assertEqual(sum(F(r["probability"]) for r in rows), 1) for a, b in itertools.permutations(rows, 2): self.assertFalse(a["phrase"].startswith(b["phrase"])) for row in rows: word = row["phrase"] self.assertEqual(F(row["probability"]), F(3, 4)**word.count("A") * F(1, 4)**word.count("B")) def test_symbol_weighting_and_repetition(self): r = phrase_rate_control() length = F(r["expected_phrase_length"]) weights = [F(row["probability"]) * len(row["phrase"]) / length for row in r["rows"]] self.assertEqual(sum(weights), 1) rate = sum(w * F(2, len(row["phrase"])) for w, row in zip(weights, r["rows"])) self.assertEqual(rate, F(r["aggregate_bits_per_symbol"])) self.assertNotEqual(rate, F(r["phrase_weighted_bits_per_symbol"])) for scale in (1, 2, 7, 64): words = [row["phrase"] for row in r["rows"] for _ in range(row["count"] * scale)] self.assertEqual(F(2 * len(words), len("".join(words))), rate) self.assertEqual(len("AA"), 2) self.assertFalse(any("AA".startswith(row["phrase"]) for row in r["rows"])) def test_frozen_json(self): self.assertEqual(json.loads(Path(__file__).with_name("results.json").read_text()), results()) if __name__ == "__main__": unittest.main()