From 6684ff8f9112b8dd7166b34ea0f916695fe7a26c Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Wed, 23 Sep 2020 14:15:05 +0200 Subject: explicit test of unaligned pointer reads/writes --- c/test_c.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'c') diff --git a/c/test_c.py b/c/test_c.py index fa24676..95ca812 100644 --- a/c/test_c.py +++ b/c/test_c.py @@ -4496,3 +4496,24 @@ def test_type_available_with_correct_names(): tp = getattr(_cffi_backend, name) assert isinstance(tp, type) assert (tp.__module__, tp.__name__) == ('_cffi_backend', name) + +def test_unaligned_types(): + BByteArray = new_array_type( + new_pointer_type(new_primitive_type("unsigned char")), None) + pbuf = newp(BByteArray, 40) + buf = buffer(pbuf) + # + for name in ['short', 'int', 'long', 'long long', 'float', 'double', + 'float _Complex', 'double _Complex']: + p = new_primitive_type(name) + if name.endswith(' _Complex'): + num = cast(p, 1.23 - 4.56j) + else: + num = cast(p, 0x0123456789abcdef) + size = sizeof(p) + buf[0:40] = b"\x00" * 40 + pbuf1 = cast(new_pointer_type(p), pbuf + 1) + pbuf1[0] = num + assert pbuf1[0] == num + assert buf[0] == '\x00' + assert buf[1 + size] == '\x00' -- cgit v1.2.1