Browse Source

split out test code

Rob Austein 7 months ago
parent
commit
f2976c2d08
2 changed files with 27 additions and 10 deletions
  1. 12 10
      rfc1982_serial_number.py
  2. 15 0
      test.py

+ 12 - 10
rfc1982_serial_number.py

@@ -47,16 +47,18 @@ def step(start, finish):
         assert start < midpoint and midpoint < finish
         return start, midpoint, finish
 
-if __name__ == "__main__":
-    from random import randint
+def show(steps):
+    print(" => ".join(str(s) for s in steps))
+
+def main():
+    from argparse import ArgumentParser
 
-    for test in range(10000):
-        i1 = Serial(randint(0, Serial.modulus - 1))
-        i2 = Serial(randint(0, Serial.modulus - 1))
+    ap = ArgumentParser()
+    ap.add_argument("start", type = Serial)
+    ap.add_argument("finish", type = Serial)
+    args = ap.parse_args()
 
-        assert i1 == i2 or \
-            (i1 < i2 and not (i1 > i2)) or \
-            (i1 > i2 and not (i1 < i2)) or \
-            int(i1) & int(i2) == Serial.modulus >> 1
+    show(step(args.start, args.finish))
 
-        print(f"{i1} => {i2}: {', '.join(str(s) for s in step(i1, i2))}")
+if __name__ == "__main__":
+    main()

+ 15 - 0
test.py

@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+
+from rfc1982_serial_number import Serial, step, show
+from random import randint
+
+for test in range(10000):
+    i1 = Serial(randint(0, Serial.modulus - 1))
+    i2 = Serial(randint(0, Serial.modulus - 1))
+
+    assert i1 == i2 or \
+        (i1 < i2 and not (i1 > i2)) or \
+        (i1 > i2 and not (i1 < i2)) or \
+        int(i1) & int(i2) == Serial.modulus >> 1
+    
+    show(step(i1, i2))