summaryrefslogtreecommitdiff
path: root/tests/basics/struct_overflow.py
diff options
context:
space:
mode:
authorMatt Wozniski <godlygeek+git@gmail.com>2019-05-09 03:19:29 -0400
committerMatt Wozniski <godlygeek+git@gmail.com>2019-05-09 03:22:24 -0400
commite041df73bb7bf424bcf571f25e3459359c4f36ed (patch)
treef70962c41b51ee7bee03af3e37960ec7ab18f63e /tests/basics/struct_overflow.py
parent9ce042ad07cabbba27f4db90f9c95d0b057b9f51 (diff)
Add tests for overflows converting ints to bytes
Diffstat (limited to 'tests/basics/struct_overflow.py')
-rw-r--r--tests/basics/struct_overflow.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/basics/struct_overflow.py b/tests/basics/struct_overflow.py
new file mode 100644
index 000000000..aee0c6b9d
--- /dev/null
+++ b/tests/basics/struct_overflow.py
@@ -0,0 +1,47 @@
+import skip_if
+skip_if.no_bigint()
+
+try:
+ import ustruct as struct
+except:
+ try:
+ import struct
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+def test_struct_overflow(typecode, val):
+ try:
+ print(struct.pack(typecode, val))
+ except OverflowError:
+ print('OverflowError')
+ except struct.error:
+ print('OverflowError')
+
+# small int -1
+test_struct_overflow('>Q', -1)
+test_struct_overflow('>L', -1)
+test_struct_overflow('>I', -1)
+test_struct_overflow('>H', -1)
+test_struct_overflow('>B', -1)
+
+# big int -1
+test_struct_overflow('>Q', -2**64 // 2**64)
+test_struct_overflow('>L', -2**64 // 2**64)
+test_struct_overflow('>I', -2**64 // 2**64)
+test_struct_overflow('>H', -2**64 // 2**64)
+test_struct_overflow('>B', -2**64 // 2**64)
+
+# possibly small ints
+test_struct_overflow('>q', 2**63)
+test_struct_overflow('>l', 2**31)
+test_struct_overflow('>i', 2**31)
+test_struct_overflow('>h', 2**15)
+test_struct_overflow('>b', 2**7)
+
+# definitely big ints
+test_struct_overflow('>q', 2**64 // 2**1)
+test_struct_overflow('>l', 2**64 // 2**33)
+test_struct_overflow('>i', 2**64 // 2**33)
+test_struct_overflow('>h', 2**64 // 2**49)
+test_struct_overflow('>b', 2**64 // 2**57)