| 1 |
import pytest |
| 2 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 3 |
from rpython.rlib.rstruct.runpack import runpack |
| 4 |
from rpython.rlib.rstruct import standardfmttable |
| 5 |
from rpython.rlib.rstruct.error import StructError |
| 6 |
from rpython.rlib.rarithmetic import LONG_BIT, long_typecode |
| 7 |
import struct |
| 8 |
|
| 9 |
class TestRStruct(BaseRtypingTest): |
| 10 |
def test_unpack(self): |
| 11 |
import sys |
| 12 |
pad = '\x00' * (LONG_BIT//8-1) |
| 13 |
fmt = 's' + long_typecode + long_typecode |
| 14 |
def fn(): |
| 15 |
return runpack(fmt, 'a'+pad+'\x03'+pad+'\x04'+pad)[1] |
| 16 |
result = 3 if sys.byteorder == 'little' else 3 << (LONG_BIT-8) |
| 17 |
assert fn() == result |
| 18 |
assert self.interpret(fn, []) == result |
| 19 |
|
| 20 |
def test_unpack_2(self): |
| 21 |
data = struct.pack('iiii', 0, 1, 2, 4) |
| 22 |
def fn(): |
| 23 |
a, b, c, d = runpack('iiii', data) |
| 24 |
return a * 1000 + b * 100 + c * 10 + d |
| 25 |
assert fn() == 124 |
| 26 |
assert self.interpret(fn, []) == 124 |
| 27 |
|
| 28 |
def test_unpack_error(self): |
| 29 |
data = '123' |
| 30 |
def fn(): |
| 31 |
try: |
| 32 |
runpack('i', data) |
| 33 |
except StructError: |
| 34 |
return True |
| 35 |
else: |
| 36 |
return False |
| 37 |
assert fn() |
| 38 |
assert self.interpret(fn, []) |
| 39 |
|
| 40 |
def test_unpack_single(self): |
| 41 |
data = struct.pack('i', 123) |
| 42 |
def fn(): |
| 43 |
return runpack('i', data) |
| 44 |
assert fn() == 123 |
| 45 |
assert self.interpret(fn, []) == 123 |
| 46 |
|
| 47 |
def test_unpack_big_endian(self): |
| 48 |
def fn(): |
| 49 |
return runpack(">i", "\x01\x02\x03\x04") |
| 50 |
assert fn() == 0x01020304 |
| 51 |
assert self.interpret(fn, []) == 0x01020304 |
| 52 |
|
| 53 |
def test_unpack_double_big_endian(self): |
| 54 |
def fn(): |
| 55 |
return runpack(">d", "testtest") |
| 56 |
assert fn() == struct.unpack(">d", "testtest")[0] |
| 57 |
assert self.interpret(fn, []) == struct.unpack(">d", "testtest")[0] |
| 58 |
|
| 59 |
def test_native_floats(self): |
| 60 |
""" |
| 61 |
Check the 'd' and 'f' format characters on native packing. |
| 62 |
""" |
| 63 |
d_data = struct.pack("df", 12.34, 12.34) |
| 64 |
def fn(): |
| 65 |
d, f = runpack("@df", d_data) |
| 66 |
return d, f |
| 67 |
|
| 68 |
|
| 69 |
d, f = fn() |
| 70 |
assert d == 12.34 |
| 71 |
assert f != 12.34 |
| 72 |
assert abs(f - 12.34) < 1E-6 |
| 73 |
|
| 74 |
|
| 75 |
res = self.interpret(fn, []) |
| 76 |
d = res.item0 |
| 77 |
f = res.item1 |
| 78 |
assert d == 12.34 |
| 79 |
assert f != 12.34 |
| 80 |
assert abs(f - 12.34) < 1E-6 |
| 81 |
|
| 82 |
def test_unpack_halffloat(self): |
| 83 |
assert runpack(">e", b"\x7b\xef") == 64992.0 |
| 84 |
assert runpack("<e", b"\xef\x7b") == 64992.0 |
| 85 |
|
| 86 |
def test_unpack_standard_little(self): |
| 87 |
def unpack(fmt, data): |
| 88 |
def fn(): |
| 89 |
return runpack(fmt, data) |
| 90 |
return self.interpret(fn, []) |
| 91 |
|
| 92 |
assert unpack("<i", 'DCBA') == 0x41424344 |
| 93 |
assert unpack("<i", '\xfd\xff\xff\xff') == -3 |
| 94 |
assert unpack("<i", '\x00\x00\x00\x80') == -2147483648 |
| 95 |
assert unpack("<I", 'DCB\x81') == 0x81424344 |
| 96 |
assert unpack("<q", 'HGFEDCBA') == 0x4142434445464748 |
| 97 |
assert unpack("<q", 'HHIJKLM\xbe') == -0x41B2B3B4B5B6B7B8 |
| 98 |
assert unpack("<Q", 'HGFEDCB\x81') == 0x8142434445464748 |
| 99 |
|
| 100 |
def test_unpack_standard_big(self): |
| 101 |
def unpack(fmt, data): |
| 102 |
def fn(): |
| 103 |
return runpack(fmt, data) |
| 104 |
return self.interpret(fn, []) |
| 105 |
|
| 106 |
assert unpack(">i", 'ABCD') == 0x41424344 |
| 107 |
assert unpack(">i", '\xff\xff\xff\xfd') == -3 |
| 108 |
assert unpack(">i", '\x80\x00\x00\x00') == -2147483648 |
| 109 |
assert unpack(">I", '\x81BCD') == 0x81424344 |
| 110 |
assert unpack(">q", 'ABCDEFGH') == 0x4142434445464748 |
| 111 |
assert unpack(">q", '\xbeMLKJIHH') == -0x41B2B3B4B5B6B7B8 |
| 112 |
assert unpack(">Q", '\x81BCDEFGH') == 0x8142434445464748 |
| 113 |
|
| 114 |
def test_align(self): |
| 115 |
data = struct.pack('BBhi', 1, 2, 3, 4) |
| 116 |
def fn(): |
| 117 |
a, b, c, d = runpack('BBhi', data) |
| 118 |
return a + (b << 4) + (c << 8) + (d << 12) |
| 119 |
assert fn() == 0x4321 |
| 120 |
assert self.interpret(fn, []) == 0x4321 |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
class TestNoFastPath(TestRStruct): |
| 125 |
|
| 126 |
def setup_method(self, meth): |
| 127 |
standardfmttable.USE_FASTPATH = False |
| 128 |
|
| 129 |
def teardown_method(self, meth): |
| 130 |
standardfmttable.USE_FASTPATH = True |