summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2024-03-20 13:30:22 +1100
committerDamien George <damien@micropython.org>2024-03-20 14:13:49 +1100
commit9d27183bde0a70521a0d189636e53975df2260eb (patch)
treecbc694aa0b1de01e2a5b1b7d63c280cdea633246
parentb80607ecafeb47664b35e4f255ed43171719ecf5 (diff)
downloadmicropython-9d27183bde0a70521a0d189636e53975df2260eb.tar.gz
micropython-9d27183bde0a70521a0d189636e53975df2260eb.zip
tests/float/float_struct_e.py: Add specific test for struct 'e' type.
Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--tests/float/float_struct_e.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/float/float_struct_e.py b/tests/float/float_struct_e.py
new file mode 100644
index 0000000000..403fbc5db4
--- /dev/null
+++ b/tests/float/float_struct_e.py
@@ -0,0 +1,43 @@
+# Test struct pack/unpack with 'e' typecode.
+
+try:
+ import struct
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+test_values = (
+ 1e-7,
+ 2e-7,
+ 1e-6,
+ 1e-5,
+ 1e-4,
+ 1e-3,
+ 1e-2,
+ 0.1,
+ 0,
+ 1,
+ 2,
+ 4,
+ 8,
+ 10,
+ 100,
+ 1e3,
+ 1e4,
+ 6e4,
+ float("inf"),
+)
+
+for j in test_values:
+ for i in (j, -j):
+ x = struct.pack("<e", i)
+ v = struct.unpack("<e", x)[0]
+ print("%.7f %s %.15f %s" % (i, x, v, i == v))
+
+# In CPython, packing a float that doesn't fit into a half-float raises OverflowError.
+# But in MicroPython it does not, but rather stores the value as inf.
+# This test is here for coverage.
+try:
+ struct.pack("e", 1e15)
+except OverflowError:
+ pass