import unittest from fractions import Fraction from model import count_symbols, periodic_counts, lattice_mean, entity_loss class AlignmentTests(unittest.TestCase): def test_paper_generic_offset_examples(self): self.assertEqual(periodic_counts(Fraction(23,10),Fraction(1,20)), {3:7,4:3}) self.assertEqual(periodic_counts(Fraction(3,10),Fraction(1,20)), {1:7,2:3}) self.assertEqual(periodic_counts(Fraction(27,10),Fraction(1,20)), {3:3,4:7}) def test_exact_boundary_contact(self): self.assertEqual(periodic_counts(Fraction(23,10),Fraction(1,10)), {3:8,4:2}) self.assertEqual(periodic_counts(Fraction(3,10),Fraction(1,10)), {1:8,2:2}) self.assertEqual(count_symbols(Fraction(1), Fraction(2)), 2) self.assertEqual(count_symbols(Fraction(1,2), Fraction(1,2)), 1) def test_integer_microcell_oracle_and_uniform_grid_mean(self): for q in range(1,17): for a in range(1,65): values=[] for r in range(q): expected=len({u//q for u in range(r,r+a)}) actual=count_symbols(Fraction(r,q),Fraction(a,q)) self.assertEqual(actual,expected,(a,q,r)) values.append(actual) self.assertEqual(Fraction(sum(values),q),lattice_mean(a,q),(a,q)) def test_integer_translation_invariance(self): for i in range(20): self.assertEqual(count_symbols(Fraction(1,10)+i,Fraction(23,10)), count_symbols(Fraction(1,10),Fraction(23,10))) def test_period_has_denominator_many_shards(self): for a in range(1,41): for q in range(1,13): length=Fraction(a,q) self.assertEqual(sum(periodic_counts(length,Fraction(1,101)).values()),length.denominator) def test_entity_denominators(self): small = entity_loss([1, 999], [0]) large = entity_loss([1, 999], [1]) self.assertEqual(small["entityFraction"], large["entityFraction"]) self.assertEqual(small["entityFraction"], "1/2") self.assertEqual(small["effectiveByteFraction"], "1/1000") self.assertEqual(large["effectiveByteFraction"], "999/1000") self.assertEqual(entity_loss([1, 999], [0, 0]), small) self.assertEqual(entity_loss([1, 999], [])["effectiveByteFraction"], "0") self.assertEqual(entity_loss([1, 999], [0, 1])["effectiveByteFraction"], "1") def test_entity_inventory_validation(self): for sizes in [[], [0], [-1], [True], [0.5]]: with self.assertRaises(ValueError): entity_loss(sizes, []) for lost in [[-1], [2], [True], [0.0]]: with self.assertRaises(ValueError): entity_loss([1, 999], lost) def test_invalid_inputs(self): for start,length in [(Fraction(-1),Fraction(1)),(Fraction(0),Fraction(0)),(Fraction(0),Fraction(-1))]: with self.assertRaises(ValueError): count_symbols(start,length) with self.assertRaises(TypeError): count_symbols(0.1,Fraction(1)) with self.assertRaises(ValueError): periodic_counts(Fraction(1),Fraction(1)) with self.assertRaises(ValueError): lattice_mean(1,0) with self.assertRaises(TypeError): lattice_mean(True,10) if __name__ == "__main__": unittest.main()