diff options
author | Rob Austein <sra@hactrn.net> | 2024-04-13 22:44:16 -0400 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2024-04-13 22:45:10 -0400 |
commit | cca4a4ce8f986a72deddc797a9cdf314ab07ab1f (patch) | |
tree | 359c623707ecc6881b9b0b7045085d5f8b616965 | |
parent | d2c08cfed1d0d545da42a80c6deaf85fcd844cc6 (diff) |
Cleanuptrunk
-rw-r--r-- | README.md | 4 | ||||
-rwxr-xr-x | rfc1982_serial_number.py | 5 | ||||
-rwxr-xr-x | test2.py | 19 |
3 files changed, 12 insertions, 16 deletions
@@ -13,8 +13,8 @@ addition and comparision, both within a restricted range specified by the RFC. The default modulus is `2**32`, you can change this by subclassing the -`Serial` and overriding the class's `modulus` variable. The modulus -must be a power of two. +`Serial` class and overriding the `modulus` variable in the subclass. +The modulus must be a power of two. See RFC 1982 for discussion of the ways in which serial numbers do not work like normal integers. In particular, note that there's a corner diff --git a/rfc1982_serial_number.py b/rfc1982_serial_number.py index bb9988c..7b9b845 100755 --- a/rfc1982_serial_number.py +++ b/rfc1982_serial_number.py @@ -19,15 +19,14 @@ class Serial: by the RFC. The default modulus is 2**32, you can change this by subclassing - the Serial class and overriding the class's modulus variable. The - modulus must be a power of two. + the Serial class and overriding the subclass's modulus variable. + The modulus must be a power of two. See RFC 1982 for discussion of the ways in which Serial numbers do not work like normal integers. In particular, note that there's a corner case in which one can have a pair of Serial numbers I1 and I2 where I1 is neither equal to, less than, nor greater than I2. This is deliberate and is not a bug in the code. See the RFC. - """ modulus = 2 ** 32 @@ -4,21 +4,18 @@ from rfc1982_serial_number import Serial from random import randint from subprocess import run -def test(n, cmd): - print(f"\nTest: {test}: {cmd[0]} {cmd[1]} {cmd[2]}") +def test(n, i1, i2): + cmd = ("./rfc1982_serial_number.py", str(i1), str(i2)) + print(f"\nTest: {n}: {cmd[0]} {cmd[1]} {cmd[2]}") run(cmd) for n in range(100): - i1 = randint(0, Serial.modulus - 1) - i2 = randint(0, Serial.modulus - 1) - test(n, ("./rfc1982_serial_number.py", str(i1), str(i2))) + test(n, randint(0, Serial.modulus - 1), randint(0, Serial.modulus - 1)) for n in range(10): - i1 = randint(0, Serial.modulus - 1) - i2 = i1 ^ (Serial.modulus >> 1) - test(n, ("./rfc1982_serial_number.py", str(i1), str(i2))) + t = randint(0, Serial.modulus - 1) + test(n, t, t ^ (Serial.modulus >> 1)) for n in range(10): - i1 = randint(0, Serial.modulus - 1) - i2 = (i1 + Serial.modulus - 1) & (Serial.modulus - 1) - test(n, ("./rfc1982_serial_number.py", str(i1), str(i2))) + t = randint(0, Serial.modulus - 1) + test(n, t, (t + Serial.modulus - 1) & (Serial.modulus - 1)) |