diff options
author | Barry Warsaw <barry@python.org> | 1996-12-12 23:34:06 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 1996-12-12 23:34:06 (GMT) |
commit | 07a0eeceaafd2b57a2024b471a1d34c879464427 (patch) | |
tree | a9f73b354ee2d9b70be8cfb8fbad4f13e24625e8 /Lib | |
parent | 30695fa3cde9826fec67037fef528d196449a5c3 (diff) | |
download | cpython-07a0eeceaafd2b57a2024b471a1d34c879464427.zip cpython-07a0eeceaafd2b57a2024b471a1d34c879464427.tar.gz cpython-07a0eeceaafd2b57a2024b471a1d34c879464427.tar.bz2 |
A test of the struct module
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_struct.py | 44 | ||||
-rw-r--r-- | Lib/test/testall.py | 1 |
2 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py new file mode 100644 index 0000000..77cda29 --- /dev/null +++ b/Lib/test/test_struct.py @@ -0,0 +1,44 @@ +import struct +## import pdb + +def simple_err(func, *args): + try: + apply(func, args) + except struct.error: + pass + else: + print 'expected struct.error not caught' +## pdb.set_trace() + +simple_err(struct.calcsize, 'Q') + +sz = struct.calcsize('i') +if sz * 3 <> struct.calcsize('iii'): + print 'inconsistent sizes' + +sz = struct.calcsize('cbhilfd') +if sz * 3 <> struct.calcsize('3c3b3h3i3l3f3d'): + print 'inconsistent sizes' + +simple_err(struct.pack, 'iii', 3) +simple_err(struct.pack, 'i', 3, 3, 3) +simple_err(struct.pack, 'i', 'foo') +simple_err(struct.unpack, 'd', 'flap') +s = struct.pack('ii', 1, 2) +simple_err(struct.unpack, 'iii', s) +simple_err(struct.unpack, 'i', s) + +c = 'a' +b = -1 +h = 255 +i = 65535 +l = 65536 +f = 3.1415 +d = 3.1415 + +s = struct.pack('xcbhilfd', c, b, h, i, l, f, d) +cp, bp, hp, ip, lp, fp, dp = struct.unpack('xcbhilfd', s) +if cp <> c or bp <> b or hp <> h or ip <> i or lp <> l or \ + int(100 * fp) <> int(100 * f) or int(100 * dp) <> int(100 * d): + # ^^^ calculate only to two decimal places + print 'unpack/pack not transitive' diff --git a/Lib/test/testall.py b/Lib/test/testall.py index 7ad7b88..c9470cd 100644 --- a/Lib/test/testall.py +++ b/Lib/test/testall.py @@ -21,6 +21,7 @@ tests = ['test_grammar', 'test_rgbimg', 'test_select', 'test_strftime', + 'test_struct', ] if __name__ == '__main__': |