blob: 38b3cd4698c6bdf81f701f68d801ae54533ebda6 [file] [log] [blame]
Tim Peters82112372001-08-29 02:28:42 +00001from test_support import TestFailed
2import marshal
3import sys
4
5# XXX Much more needed here.
6
7# Test the full range of Python ints.
8n = sys.maxint
9while 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
21def 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
28maxint64 = (1L << 63) - 1
29minint64 = -maxint64-1
30
31for 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