blob: 266cbb6f0d27c330b68fcb7a63c1c3415bda9e63 [file] [log] [blame]
Marc-André Lemburgd70141a2000-06-30 10:26:29 +00001from test_support import verbose
2import string, sys
3
4# test string formatting operator (I am not sure if this is being tested
5# elsewhere but, surely, some of the given cases are *not* tested because
6# they crash python)
7# test on unicode strings as well
8
Tim Peters38fd5b62000-09-21 05:43:11 +00009overflowok = 1
10
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000011def testformat(formatstr, args, output=None):
Tim Peters38fd5b62000-09-21 05:43:11 +000012 if verbose:
13 if output:
14 print "%s %% %s =? %s ..." %\
15 (repr(formatstr), repr(args), repr(output)),
16 else:
17 print "%s %% %s works? ..." % (repr(formatstr), repr(args)),
18 try:
19 result = formatstr % args
20 except OverflowError:
21 if not overflowok:
22 raise
23 if verbose:
24 print 'overflow (this is fine)'
25 else:
26 if output and result != output:
27 if verbose:
28 print 'no'
29 print "%s %% %s == %s != %s" %\
30 (repr(formatstr), repr(args), repr(result), repr(output))
31 else:
32 if verbose:
33 print 'yes'
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000034
35def testboth(formatstr, *args):
Tim Peters38fd5b62000-09-21 05:43:11 +000036 testformat(formatstr, *args)
37 testformat(unicode(formatstr), *args)
38
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000039
40testboth("%.1d", (1,), "1")
41testboth("%.*d", (sys.maxint,1)) # expect overflow
42testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
43testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
44testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
45
46testboth("%f", (1.0,), "1.000000")
47# these are trying to test the limits of the internal magic-number-length
48# formatting buffer, if that number changes then these tests are less
49# effective
50testboth("%#.*g", (109, -1.e+49/3.))
51testboth("%#.*g", (110, -1.e+49/3.))
52testboth("%#.*g", (110, -1.e+100/3.))
53
54# test some ridiculously large precision, expect overflow
55testboth('%12.*f', (123456, 1.0))
56
Tim Peters38fd5b62000-09-21 05:43:11 +000057# Formatting of long integers. Overflow is not ok
58overflowok = 0
59testboth("%x", 10L, "a")
60testboth("%x", 100000000000L, "174876e800")
61testboth("%o", 10L, "12")
62testboth("%o", 100000000000L, "1351035564000")
63testboth("%d", 10L, "10")
64testboth("%d", 100000000000L, "100000000000")
65
66# Make sure big is too big to fit in a 64-bit int, else the unbounded
67# int formatting will be sidestepped on some machines. That's vital,
68# because bitwise (x, X, o) formats of regular Python ints never
69# produce a sign ("+" or "-").
70
71big = 123456789012345678901234567890L
72testboth("%d", big, "123456789012345678901234567890")
73testboth("%d", -big, "-123456789012345678901234567890")
74testboth("%5d", -big, "-123456789012345678901234567890")
75testboth("%31d", -big, "-123456789012345678901234567890")
76testboth("%32d", -big, " -123456789012345678901234567890")
77testboth("%-32d", -big, "-123456789012345678901234567890 ")
78testboth("%032d", -big, "-0123456789012345678901234567890")
79testboth("%-032d", -big, "-123456789012345678901234567890 ")
80testboth("%034d", -big, "-000123456789012345678901234567890")
81testboth("%034d", big, "0000123456789012345678901234567890")
82testboth("%0+34d", big, "+000123456789012345678901234567890")
83testboth("%+34d", big, " +123456789012345678901234567890")
84testboth("%34d", big, " 123456789012345678901234567890")
85testboth("%.2d", big, "123456789012345678901234567890")
86testboth("%.30d", big, "123456789012345678901234567890")
87testboth("%.31d", big, "0123456789012345678901234567890")
88testboth("%32.31d", big, " 0123456789012345678901234567890")
89
90big = 0x1234567890abcdef12345L # 21 hex digits
91testboth("%x", big, "1234567890abcdef12345")
92testboth("%x", -big, "-1234567890abcdef12345")
93testboth("%5x", -big, "-1234567890abcdef12345")
94testboth("%22x", -big, "-1234567890abcdef12345")
95testboth("%23x", -big, " -1234567890abcdef12345")
96testboth("%-23x", -big, "-1234567890abcdef12345 ")
97testboth("%023x", -big, "-01234567890abcdef12345")
98testboth("%-023x", -big, "-1234567890abcdef12345 ")
99testboth("%025x", -big, "-0001234567890abcdef12345")
100testboth("%025x", big, "00001234567890abcdef12345")
101testboth("%0+25x", big, "+0001234567890abcdef12345")
102testboth("%+25x", big, " +1234567890abcdef12345")
103testboth("%25x", big, " 1234567890abcdef12345")
104testboth("%.2x", big, "1234567890abcdef12345")
105testboth("%.21x", big, "1234567890abcdef12345")
106testboth("%.22x", big, "01234567890abcdef12345")
107testboth("%23.22x", big, " 01234567890abcdef12345")
108testboth("%-23.22x", big, "01234567890abcdef12345 ")
109testboth("%X", big, "1234567890ABCDEF12345")
110testboth("%#X", big, "0X1234567890ABCDEF12345")
111testboth("%#x", big, "0x1234567890abcdef12345")
112testboth("%#x", -big, "-0x1234567890abcdef12345")
113testboth("%#.23x", -big, "-0x001234567890abcdef12345")
114testboth("%#+.23x", big, "+0x001234567890abcdef12345")
115testboth("%# .23x", big, " 0x001234567890abcdef12345")
116testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
117testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
118testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
119testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
120testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
121# next one gets two leading zeroes from precision, and another from the
122# 0 flag and the width
123testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
124# same, except no 0 flag
125testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
126
127big = 012345670123456701234567012345670L # 32 octal digits
128testboth("%o", big, "12345670123456701234567012345670")
129testboth("%o", -big, "-12345670123456701234567012345670")
130testboth("%5o", -big, "-12345670123456701234567012345670")
131testboth("%33o", -big, "-12345670123456701234567012345670")
132testboth("%34o", -big, " -12345670123456701234567012345670")
133testboth("%-34o", -big, "-12345670123456701234567012345670 ")
134testboth("%034o", -big, "-012345670123456701234567012345670")
135testboth("%-034o", -big, "-12345670123456701234567012345670 ")
136testboth("%036o", -big, "-00012345670123456701234567012345670")
137testboth("%036o", big, "000012345670123456701234567012345670")
138testboth("%0+36o", big, "+00012345670123456701234567012345670")
139testboth("%+36o", big, " +12345670123456701234567012345670")
140testboth("%36o", big, " 12345670123456701234567012345670")
141testboth("%.2o", big, "12345670123456701234567012345670")
142testboth("%.32o", big, "12345670123456701234567012345670")
143testboth("%.33o", big, "012345670123456701234567012345670")
144testboth("%34.33o", big, " 012345670123456701234567012345670")
145testboth("%-34.33o", big, "012345670123456701234567012345670 ")
146testboth("%o", big, "12345670123456701234567012345670")
147testboth("%#o", big, "012345670123456701234567012345670")
148testboth("%#o", -big, "-012345670123456701234567012345670")
149testboth("%#.34o", -big, "-0012345670123456701234567012345670")
150testboth("%#+.34o", big, "+0012345670123456701234567012345670")
151testboth("%# .34o", big, " 0012345670123456701234567012345670")
152testboth("%#+.34o", big, "+0012345670123456701234567012345670")
153testboth("%#-+.34o", big, "+0012345670123456701234567012345670")
154testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ")
155testboth("%#+37.34o", big, " +0012345670123456701234567012345670")
156# next one gets one leading zero from precision
157testboth("%.33o", big, "012345670123456701234567012345670")
158# base marker shouldn't change that, since "0" is redundant
159testboth("%#.33o", big, "012345670123456701234567012345670")
160# but reduce precision, and base marker should add a zero
161testboth("%#.32o", big, "012345670123456701234567012345670")
162# one leading zero from precision, and another from "0" flag & width
163testboth("%034.33o", big, "0012345670123456701234567012345670")
164# base marker shouldn't change that
165testboth("%0#34.33o", big, "0012345670123456701234567012345670")