diff options
author | Dave Hylands <dhylands@gmail.com> | 2015-12-23 19:11:27 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-01-19 12:25:28 +0000 |
commit | a17755ee8ba2982505d349e0f06acdabe943d33e (patch) | |
tree | d8033e99e23e780b20c6dfb86b04c2de5b3b3144 /tests/basics/struct1.py | |
parent | ac16cc9a358fff9383a70db531464578bffd2418 (diff) | |
download | micropython-a17755ee8ba2982505d349e0f06acdabe943d33e.tar.gz micropython-a17755ee8ba2982505d349e0f06acdabe943d33e.zip |
py: Add ustruct.pack_into and unpack_from
Diffstat (limited to 'tests/basics/struct1.py')
-rw-r--r-- | tests/basics/struct1.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py index 61c08b74e4..1afea5a96f 100644 --- a/tests/basics/struct1.py +++ b/tests/basics/struct1.py @@ -66,3 +66,44 @@ except TypeError: # but later were implemented for all. print(struct.unpack("<3B2h", b"foo\x12\x34\xff\xff")) print(struct.pack("<3B", 1, 2, 3)) + +# pack_into +buf = bytearray(b'>>>123<<<') +struct.pack_into('<bbb', buf, 3, 0x41, 0x42, 0x43) +print(buf) +struct.pack_into('<bbb', buf, -6, 0x44, 0x45, 0x46) +print(buf) + +try: + struct.pack_into('<bbb', buf, 7, 0x41, 0x42, 0x43) +except: + print('struct.error') +try: + struct.pack_into('<bbb', buf, -10, 0x41, 0x42, 0x43) +except: + print('struct.error') + +# unpack_from +buf = b'0123456789' +print(struct.unpack_from('<b', buf, 4)) +print(struct.unpack_from('<b', buf, -4)) +try: + print(struct.unpack_from('<b', buf, 10)) +except: + print('struct.error') +try: + print(struct.unpack_from('<b', buf, -11)) +except: + print('struct.error') + +# pack with too many args, not checked by uPy +#try: +# print(struct.pack('ii', 1, 2, 3)) +#except: +# print('struct.error') + +# pack with too few args, not checked by uPy +#try: +# print(struct.pack('ii', 1)) +#except: +# print('struct.error') |