Skip to content

Commit 4e8d8ba

Browse files
committed
GR-23856: add simple test_struct
1 parent c0ccf02 commit 4e8d8ba

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

graalpython/com.oracle.graal.python.test/src/graalpytest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def assertGreater(self, expected, actual, msg=None):
213213
msg = "Expected '%r' to be greater than '%r'" % (actual, expected)
214214
assert expected > actual, msg
215215

216+
def assertGreaterEqual(self, expected, actual, msg=None):
217+
if not msg:
218+
msg = "Expected '%r' to be greater than or equal to '%r'" % (actual, expected)
219+
assert expected >= actual, msg
220+
216221
def assertSequenceEqual(self, expected, actual, msg=None):
217222
if not msg:
218223
msg = "Expected '%r' to be equal to '%r'" % (actual, expected)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates.
2+
# Copyright (C) 1996-2017 Python Software Foundation
3+
#
4+
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5+
import sys
6+
import struct
7+
8+
ISBIGENDIAN = sys.byteorder == "big"
9+
10+
11+
def test_new_features():
12+
tests = [
13+
('c', b'a', b'a', b'a', 0),
14+
('xc', b'a', b'\0a', b'\0a', 0),
15+
('cx', b'a', b'a\0', b'a\0', 0),
16+
('s', b'a', b'a', b'a', 0),
17+
('0s', b'helloworld', b'', b'', 1),
18+
('1s', b'helloworld', b'h', b'h', 1),
19+
('9s', b'helloworld', b'helloworl', b'helloworl', 1),
20+
('10s', b'helloworld', b'helloworld', b'helloworld', 0),
21+
('11s', b'helloworld', b'helloworld\0', b'helloworld\0', 1),
22+
('20s', b'helloworld', b'helloworld'+10*b'\0', b'helloworld'+10*b'\0', 1),
23+
('b', 7, b'\7', b'\7', 0),
24+
('b', -7, b'\371', b'\371', 0),
25+
('B', 7, b'\7', b'\7', 0),
26+
('B', 249, b'\371', b'\371', 0),
27+
('h', 700, b'\002\274', b'\274\002', 0),
28+
('h', -700, b'\375D', b'D\375', 0),
29+
('H', 700, b'\002\274', b'\274\002', 0),
30+
('H', 0x10000-700, b'\375D', b'D\375', 0),
31+
('i', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
32+
('i', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
33+
('I', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
34+
('I', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
35+
('l', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
36+
('l', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
37+
('L', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
38+
('L', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
39+
('f', 2.0, b'@\000\000\000', b'\000\000\000@', 0),
40+
('d', 2.0, b'@\000\000\000\000\000\000\000',
41+
b'\000\000\000\000\000\000\000@', 0),
42+
('f', -2.0, b'\300\000\000\000', b'\000\000\000\300', 0),
43+
('d', -2.0, b'\300\000\000\000\000\000\000\000',
44+
b'\000\000\000\000\000\000\000\300', 0),
45+
('?', 0, b'\0', b'\0', 0),
46+
('?', 3, b'\1', b'\1', 1),
47+
('?', True, b'\1', b'\1', 0),
48+
('?', [], b'\0', b'\0', 1),
49+
('?', (1,), b'\1', b'\1', 1),
50+
]
51+
52+
for fmt, arg, big, lil, asy in tests:
53+
for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
54+
('='+fmt, ISBIGENDIAN and big or lil)]:
55+
res = struct.pack(xfmt, arg)
56+
assert res == exp
57+
assert struct.calcsize(xfmt) == len(res)
58+
rev = struct.unpack(xfmt, res)[0]
59+
if rev != arg:
60+
assert asy
61+
62+
63+
def test_pack_unpack():
64+
# numbers
65+
cases = [
66+
((60, 61, 62, 12365, 3454353, 75, 76), '3BHI2B'),
67+
((9223372036854775806,), 'Q'),
68+
]
69+
70+
alignment = ['>', '<', '!', '=', '@']
71+
72+
for vals, fmt in cases:
73+
for align in alignment:
74+
_fmt = align + fmt
75+
result = struct.pack(_fmt, *vals)
76+
assert vals == struct.unpack(_fmt, result)
77+
assert struct.calcsize(_fmt) == len(result), "calcsize('{}')={} != len({})={}".format(
78+
_fmt, struct.calcsize(_fmt), result, len(result))
79+
80+
# bytes / strings
81+
long_str = b'hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-hello-' \
82+
b'graal-python-hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-' \
83+
b'python-hello-graal-python-hello-graal-python-hello-graal-p'
84+
85+
pascal_str = b'\xffhello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-' \
86+
b'hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-python-' \
87+
b'hello-graal-python-hello-graal-python-hello-graal-python-hello-graal-'
88+
89+
for align in alignment:
90+
for (res, fmt) in [(long_str, 's'), (pascal_str, 'p')]:
91+
_fmt = align + '260' + fmt
92+
assert struct.calcsize(_fmt) == 260
93+
assert struct.pack(_fmt, b'hello-graal-python-'*20) == res
94+
95+
# floats
96+
cases = [
97+
('f', 1.12123123, 1.121231198310852),
98+
('d', 1.12123123, 1.12123123),
99+
('e', 1.12345678912345, 1.123046875),
100+
('e', -145.12345678912345, -145.125),
101+
]
102+
103+
for align in alignment:
104+
for (fmt, val, res) in cases:
105+
_fmt = align + fmt
106+
_bytes = struct.pack(_fmt, val)
107+
_res = struct.unpack(_fmt, _bytes)[0]
108+
assert _res == res, "({}), {} != {}".format(_fmt, _res, res)

0 commit comments

Comments
 (0)