commit cdafc18fc7b8d09b09573ec82a0f4526bcba577d
parent 45ffe6e016056478df3e105f6c75f66b6c4c6016
Author: lash <dev@holbrook.no>
Date: Mon, 21 Feb 2022 09:15:45 +0000
Add int tests file, test runner
Diffstat:
2 files changed, 74 insertions(+), 0 deletions(-)
diff --git a/run_tests.sh b/run_tests.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+set -a
+set -e
+set -x
+default_pythonpath=$PYTHONPATH:.
+export PYTHONPATH=${default_pythonpath:-.}
+>&2 echo using pythonpath $PYTHONPATH
+for f in `ls tests/*.py`; do
+ python $f
+done
+set +x
+set +e
+set +a
diff --git a/tests/test_int.py b/tests/test_int.py
@@ -0,0 +1,60 @@
+# standard imports
+import unittest
+
+# local imports
+import hexathon
+
+class TestInt(unittest.TestCase):
+
+ def test_1_bytes_min(self):
+
+ v = 129
+ r = hexathon.int_to_minbytes(v)
+ self.assertEqual(r, b'\x81')
+
+ v = 1025
+ r = hexathon.int_to_minbytes(v)
+ self.assertEqual(r, b'\x04\x01')
+
+ v = 65536
+ r = hexathon.int_to_minbytes(v)
+ self.assertEqual(r, b'\x01\x00\x00')
+
+
+ def test_2_hex_min(self):
+
+ v = 129
+ r = hexathon.int_to_minhex(v)
+ self.assertEqual(r, '81')
+
+ v = 1025
+ r = hexathon.int_to_minhex(v)
+ self.assertEqual(r, '0401')
+
+ v = 65536
+ r = hexathon.int_to_minhex(v)
+ self.assertEqual(r, '010000')
+
+
+ def test_to_int(self):
+
+ v = '123'
+ r = hexathon.to_int(v)
+ self.assertEqual(r, 291)
+
+ v = '0123'
+ r = hexathon.to_int(v)
+ self.assertEqual(r, 291)
+
+ with self.assertRaises(ValueError):
+ hexathon.to_int(v, need_prefix=True)
+
+ r = hexathon.to_int(hexathon.add_0x(v), need_prefix=True)
+ self.assertEqual(r, 291)
+
+ with self.assertRaises(ValueError):
+ hexathon.to_int('foo')
+
+
+if __name__ == '__main__':
+ unittest.main()