Skip to content

Commit 9d27183

Browse files
committed
tests/float/float_struct_e.py: Add specific test for struct 'e' type.
Signed-off-by: Damien George <[email protected]>
1 parent b80607e commit 9d27183

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/float/float_struct_e.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Test struct pack/unpack with 'e' typecode.
2+
3+
try:
4+
import struct
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
test_values = (
10+
1e-7,
11+
2e-7,
12+
1e-6,
13+
1e-5,
14+
1e-4,
15+
1e-3,
16+
1e-2,
17+
0.1,
18+
0,
19+
1,
20+
2,
21+
4,
22+
8,
23+
10,
24+
100,
25+
1e3,
26+
1e4,
27+
6e4,
28+
float("inf"),
29+
)
30+
31+
for j in test_values:
32+
for i in (j, -j):
33+
x = struct.pack("<e", i)
34+
v = struct.unpack("<e", x)[0]
35+
print("%.7f %s %.15f %s" % (i, x, v, i == v))
36+
37+
# In CPython, packing a float that doesn't fit into a half-float raises OverflowError.
38+
# But in MicroPython it does not, but rather stores the value as inf.
39+
# This test is here for coverage.
40+
try:
41+
struct.pack("e", 1e15)
42+
except OverflowError:
43+
pass

0 commit comments

Comments
 (0)