"""Host arithmetic model only, not firmware or circuit validation."""
import unittest, random
class CountTests(unittest.TestCase):
 def test_wrap(self):
  for old in [0,1,65534,65535]:
   for step in [0,1,1000,65535]:
    self.assertEqual((((old+step)%65536)-old)%65536,step)
 def test_long_runs(self):
  rng=random.Random(35)
  for _ in range(100):
   count=last=total=0
   for _ in range(1000):
    count+=rng.randrange(1001); now=count%65536; total+=(now-last)%65536;last=now
   self.assertEqual(total,count)
 def test_ambiguity(self):
  self.assertEqual(65536%65536,0) # a full wrap cannot be detected from samples
 def test_gate_boundary(self):
  # ideal square waves; AND gate adds at most one opening edge
  for a in range(20):
   for b in range(a+1,40):
    edges=sum(i%10==0 for i in range(a,b)); gated=edges+int(a%10 in range(1,5));self.assertLessEqual(abs(gated-edges),1)
if __name__=='__main__':unittest.main()
