Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame^] | 1 | from test_support import TestFailed, verbose, verify |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 2 | import struct |
| 3 | ## import pdb |
| 4 | |
| 5 | def simple_err(func, *args): |
| 6 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 7 | apply(func, args) |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 8 | except struct.error: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 9 | pass |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 10 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 11 | raise TestFailed, "%s%s did not raise struct.error" % ( |
| 12 | func.__name__, args) |
| 13 | ## pdb.set_trace() |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 14 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame^] | 15 | simple_err(struct.calcsize, 'Z') |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 16 | |
| 17 | sz = struct.calcsize('i') |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 18 | if sz * 3 != struct.calcsize('iii'): |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 19 | raise TestFailed, 'inconsistent sizes' |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 04ebf5c | 1997-01-03 19:00:37 +0000 | [diff] [blame] | 21 | fmt = 'cbxxxxxxhhhhiillffd' |
| 22 | fmt3 = '3c3b18x12h6i6l6f3d' |
| 23 | sz = struct.calcsize(fmt) |
| 24 | sz3 = struct.calcsize(fmt3) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 25 | if sz * 3 != sz3: |
Guido van Rossum | 04ebf5c | 1997-01-03 19:00:37 +0000 | [diff] [blame] | 26 | raise TestFailed, 'inconsistent sizes (3*%s -> 3*%d = %d, %s -> %d)' % ( |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 27 | `fmt`, sz, 3*sz, `fmt3`, sz3) |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 28 | |
| 29 | simple_err(struct.pack, 'iii', 3) |
| 30 | simple_err(struct.pack, 'i', 3, 3, 3) |
| 31 | simple_err(struct.pack, 'i', 'foo') |
| 32 | simple_err(struct.unpack, 'd', 'flap') |
| 33 | s = struct.pack('ii', 1, 2) |
| 34 | simple_err(struct.unpack, 'iii', s) |
| 35 | simple_err(struct.unpack, 'i', s) |
| 36 | |
| 37 | c = 'a' |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 38 | b = 1 |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 39 | h = 255 |
| 40 | i = 65535 |
| 41 | l = 65536 |
| 42 | f = 3.1415 |
| 43 | d = 3.1415 |
| 44 | |
Guido van Rossum | 420c11c | 1997-01-03 00:09:46 +0000 | [diff] [blame] | 45 | for prefix in ('', '@', '<', '>', '=', '!'): |
| 46 | for format in ('xcbhilfd', 'xcBHILfd'): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 47 | format = prefix + format |
| 48 | if verbose: |
| 49 | print "trying:", format |
| 50 | s = struct.pack(format, c, b, h, i, l, f, d) |
| 51 | cp, bp, hp, ip, lp, fp, dp = struct.unpack(format, s) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 52 | if (cp != c or bp != b or hp != h or ip != i or lp != l or |
| 53 | int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d)): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 54 | # ^^^ calculate only to two decimal places |
| 55 | raise TestFailed, "unpack/pack not transitive (%s, %s)" % ( |
| 56 | str(format), str((cp, bp, hp, ip, lp, fp, dp))) |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 420c11c | 1997-01-03 00:09:46 +0000 | [diff] [blame] | 58 | # Test some of the new features in detail |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 59 | |
| 60 | # (format, argument, big-endian result, little-endian result, asymmetric) |
| 61 | tests = [ |
| 62 | ('c', 'a', 'a', 'a', 0), |
| 63 | ('xc', 'a', '\0a', '\0a', 0), |
| 64 | ('cx', 'a', 'a\0', 'a\0', 0), |
| 65 | ('s', 'a', 'a', 'a', 0), |
| 66 | ('0s', 'helloworld', '', '', 1), |
| 67 | ('1s', 'helloworld', 'h', 'h', 1), |
| 68 | ('9s', 'helloworld', 'helloworl', 'helloworl', 1), |
| 69 | ('10s', 'helloworld', 'helloworld', 'helloworld', 0), |
| 70 | ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1), |
| 71 | ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1), |
| 72 | ('b', 7, '\7', '\7', 0), |
| 73 | ('b', -7, '\371', '\371', 0), |
| 74 | ('B', 7, '\7', '\7', 0), |
| 75 | ('B', 249, '\371', '\371', 0), |
| 76 | ('h', 700, '\002\274', '\274\002', 0), |
| 77 | ('h', -700, '\375D', 'D\375', 0), |
| 78 | ('H', 700, '\002\274', '\274\002', 0), |
| 79 | ('H', 0x10000-700, '\375D', 'D\375', 0), |
| 80 | ('i', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 81 | ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 82 | ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0), |
| 83 | ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 84 | ('l', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 85 | ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 86 | ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0), |
| 87 | ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
Guido van Rossum | 420c11c | 1997-01-03 00:09:46 +0000 | [diff] [blame] | 88 | ('f', 2.0, '@\000\000\000', '\000\000\000@', 0), |
| 89 | ('d', 2.0, '@\000\000\000\000\000\000\000', |
| 90 | '\000\000\000\000\000\000\000@', 0), |
| 91 | ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0), |
| 92 | ('d', -2.0, '\300\000\000\000\000\000\000\000', |
| 93 | '\000\000\000\000\000\000\000\300', 0), |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 94 | ] |
| 95 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame^] | 96 | isbigendian = struct.pack('=i', 1)[0] == chr(0) |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 97 | |
| 98 | for fmt, arg, big, lil, asy in tests: |
| 99 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 100 | print `fmt`, `arg`, `big`, `lil` |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 101 | for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil), |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 102 | ('='+fmt, isbigendian and big or lil)]: |
| 103 | res = struct.pack(xfmt, arg) |
| 104 | if res != exp: |
| 105 | raise TestFailed, "pack(%s, %s) -> %s # expected %s" % ( |
| 106 | `fmt`, `arg`, `res`, `exp`) |
| 107 | n = struct.calcsize(xfmt) |
| 108 | if n != len(res): |
| 109 | raise TestFailed, "calcsize(%s) -> %d # expected %d" % ( |
| 110 | `xfmt`, n, len(res)) |
| 111 | rev = struct.unpack(xfmt, res)[0] |
| 112 | if rev != arg and not asy: |
| 113 | raise TestFailed, "unpack(%s, %s) -> (%s,) # expected (%s,)" % ( |
| 114 | `fmt`, `res`, `rev`, `arg`) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame^] | 115 | |
| 116 | # Some q/Q sanity checks. |
| 117 | |
| 118 | has_native_qQ = 1 |
| 119 | try: |
| 120 | struct.pack("q", 5) |
| 121 | except struct.error: |
| 122 | has_native_qQ = 0 |
| 123 | |
| 124 | if verbose: |
| 125 | print "Platform has native q/Q?", has_native_qQ and "Yes." or "No." |
| 126 | |
| 127 | simple_err(struct.pack, "Q", -1) # can't pack -1 as unsigned regardless |
| 128 | simple_err(struct.pack, "q", "a") # can't pack string as 'q' regardless |
| 129 | simple_err(struct.pack, "Q", "a") # ditto, but 'Q' |
| 130 | |
| 131 | def force_bigendian(value): |
| 132 | if isbigendian: |
| 133 | return value |
| 134 | chars = list(value) |
| 135 | chars.reverse() |
| 136 | return "".join(chars) |
| 137 | |
| 138 | if has_native_qQ: |
| 139 | bytes = struct.calcsize('q') |
| 140 | # The expected values here are in big-endian format, primarily because |
| 141 | # I'm on a little-endian machine and so this is the clearest way (for |
| 142 | # me) to force the code to get exercised. |
| 143 | for format, input, expected in ( |
| 144 | ('q', -1, '\xff' * bytes), |
| 145 | ('q', 0, '\x00' * bytes), |
| 146 | ('Q', 0, '\x00' * bytes), |
| 147 | ('q', 1L, '\x00' * (bytes-1) + '\x01'), |
| 148 | ('Q', (1L << (8*bytes))-1, '\xff' * bytes), |
| 149 | ('q', (1L << (8*bytes-1))-1, '\x7f' + '\xff' * (bytes - 1))): |
| 150 | got = struct.pack(format, input) |
| 151 | bigexpected = force_bigendian(expected) |
| 152 | verify(got == bigexpected, |
| 153 | "%r-pack of %r gave %r, not %r" % |
| 154 | (format, input, got, bigexpected)) |
| 155 | retrieved = struct.unpack(format, got)[0] |
| 156 | verify(retrieved == input, |
| 157 | "%r-unpack of %r gave %r, not %r" % |
| 158 | (format, got, retrieved, input)) |