rfc1982_serial_number.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. class Serial:
  3. _modulus = 2 ** 32
  4. _bitmask = _modulus - 1
  5. _signbit = _modulus >> 1
  6. def __init__(self, val):
  7. self._val = int(val) & self._bitmask
  8. def __add__(self, val):
  9. return self.__class__(self._val + int(val))
  10. def __le__(self, other):
  11. return (self._modulus - int(self) + int(other)) & self._signbit == 0
  12. def __ge__(self, other):
  13. return (self._modulus + int(self) - int(other)) & self._signbit == 0
  14. def __eq__(self, other):
  15. return int(self) == int(other)
  16. def __ne__(self, other):
  17. return int(self) != int(other)
  18. def __lt__(self, other):
  19. return self != other and self <= other
  20. def __gt__(self, other):
  21. return self != other and self >= other
  22. def __int__(self):
  23. return self._val
  24. def __str__(self):
  25. return str(int(self))
  26. if __name__ == "__main__":
  27. from random import randint
  28. for test in range(100):
  29. i1 = Serial(randint(0, 2**32-1))
  30. i2 = Serial(randint(0, 2**32-1))
  31. assert i1 == i2 or \
  32. (i1 < i2 and not (i1 > i2)) or \
  33. (i1 > i2 and not (i1 < i2)) or \
  34. int(i1) & int(i2) == 0x80000000
  35. if i1 == i2:
  36. print(f"{int(i1):10d} == {int(i2):10d}, how did that happen?")
  37. elif i1 < i2:
  38. print(f"{int(i1):10d} => {int(i2):10d}, can do it in one jump")
  39. else:
  40. i3 = i1 + (2**31 - 2)
  41. print(f"{int(i1):10d} => {int(i2):10d}, need to wrap, i3 value {i3}")
  42. assert i1 < i3 and i3 < i2