Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame^] | 1 | from test_support import TestFailed |
| 2 | import marshal |
| 3 | import sys |
| 4 | |
| 5 | # XXX Much more needed here. |
| 6 | |
| 7 | # Test the full range of Python ints. |
| 8 | n = sys.maxint |
| 9 | while n: |
| 10 | for expected in (-n, n): |
| 11 | s = marshal.dumps(expected) |
| 12 | got = marshal.loads(s) |
| 13 | if expected != got: |
| 14 | raise TestFailed("for int %d, marshal string is %r, loaded " |
| 15 | "back as %d" % (expected, s, got)) |
| 16 | n = n >> 1 |
| 17 | |
| 18 | # Simulate int marshaling on a 64-bit box. This is most interesting if |
| 19 | # we're running the test on a 32-bit box, of course. |
| 20 | |
| 21 | def to_little_endian_string(value, nbytes): |
| 22 | bytes = [] |
| 23 | for i in range(nbytes): |
| 24 | bytes.append(chr(value & 0xff)) |
| 25 | value >>= 8 |
| 26 | return ''.join(bytes) |
| 27 | |
| 28 | maxint64 = (1L << 63) - 1 |
| 29 | minint64 = -maxint64-1 |
| 30 | |
| 31 | for base in maxint64, minint64, -maxint64, -(minint64 >> 1): |
| 32 | while base: |
| 33 | s = 'I' + to_little_endian_string(base, 8) |
| 34 | got = marshal.loads(s) |
| 35 | if base != got: |
| 36 | raise TestFailed("for int %d, simulated marshal string is %r, " |
| 37 | "loaded back as %d" % (base, s, got)) |
| 38 | if base == -1: # a fixed-point for shifting right 1 |
| 39 | base = 0 |
| 40 | else: |
| 41 | base >>= 1 |