rfc1982_serial_number.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python3
  2. class Serial:
  3. def __init__(self, val):
  4. self._val = int(val) & (2**32)
  5. def __int__(self):
  6. return self._val
  7. def __add__(self, val):
  8. return Serial(self._val + int(val))
  9. def __eq__(self, other):
  10. return int(self) == int(other)
  11. def __ne__(self, other):
  12. return int(self) != int(other)
  13. def __lt__(self, other):
  14. return self != other and (
  15. (self._val < other._val and other._val - self._val < 2**31) or
  16. (self._val > other._val and self._val - other._val > 2**31))
  17. def __gt__(self, other):
  18. return self != other and (
  19. (self._val < other._val and other._val - self._val > 2**31) or
  20. (self._val > other._val and self._val - other._val < 2**31))
  21. if __name__ == "__main__":
  22. from random import randint
  23. for test in range(100):
  24. i1 = randint(0, 2**32-1)
  25. i2 = randint(0, 2**32-1)
  26. print(i1, i2)
  27. assert i1 == i2 or \
  28. (i1 < i2 and not (i1 > i2)) or \
  29. (i1 > i2 and not (i1 < i2)) or \
  30. int(i1) & int(i2) == 0x80000000