blob: 7fa950d9ef5e44c6b86b149e177c31292aa9d3b5 [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test.support import verbose, TestFailed
Eric S. Raymondd8c628b2001-02-09 11:46:37 +00002import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003import test.support as support
Christian Heimesf6cd9672008-03-26 13:45:42 +00004import unittest
5
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006maxsize = support.MAX_Py_ssize_t
Marc-André Lemburgd70141a2000-06-30 10:26:29 +00007
8# test string formatting operator (I am not sure if this is being tested
9# elsewhere but, surely, some of the given cases are *not* tested because
10# they crash python)
11# test on unicode strings as well
12
Mark Dickinsonb7be5702010-02-23 13:20:58 +000013def testformat(formatstr, args, output=None, limit=None, overflowok=False):
Tim Peters38fd5b62000-09-21 05:43:11 +000014 if verbose:
15 if output:
Walter Dörwald38e43c22007-06-09 16:13:23 +000016 print("%r %% %r =? %r ..." %\
17 (formatstr, args, output), end=' ')
Tim Peters38fd5b62000-09-21 05:43:11 +000018 else:
Walter Dörwald38e43c22007-06-09 16:13:23 +000019 print("%r %% %r works? ..." % (formatstr, args), end=' ')
Tim Peters38fd5b62000-09-21 05:43:11 +000020 try:
21 result = formatstr % args
22 except OverflowError:
23 if not overflowok:
24 raise
25 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000026 print('overflow (this is fine)')
Tim Peters38fd5b62000-09-21 05:43:11 +000027 else:
Mark Dickinson5354a1f2010-02-07 13:15:37 +000028 if output and limit is None and result != output:
Guido van Rossumb5a755e2007-07-18 18:15:48 +000029 if verbose:
30 print('no')
Christian Heimesf6cd9672008-03-26 13:45:42 +000031 raise AssertionError("%r %% %r == %r != %r" %
32 (formatstr, args, result, output))
Christian Heimesa612dc02008-02-24 13:08:18 +000033 # when 'limit' is specified, it determines how many characters
34 # must match exactly; lengths must always match.
35 # ex: limit=5, '12345678' matches '12345___'
36 # (mainly for floating point format tests for which an exact match
37 # can't be guaranteed due to rounding and representation errors)
38 elif output and limit is not None and (
39 len(result)!=len(output) or result[:limit]!=output[:limit]):
40 if verbose:
41 print('no')
42 print("%s %% %s == %s != %s" % \
43 (repr(formatstr), repr(args), repr(result), repr(output)))
Tim Peters38fd5b62000-09-21 05:43:11 +000044 else:
45 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000046 print('yes')
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000047
Tim Peters38fd5b62000-09-21 05:43:11 +000048
Christian Heimesf6cd9672008-03-26 13:45:42 +000049class FormatTest(unittest.TestCase):
50 def test_format(self):
51 testformat("%.1d", (1,), "1")
Mark Dickinsonb7be5702010-02-23 13:20:58 +000052 testformat("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow
53 testformat("%.100d", (1,), '00000000000000000000000000000000000000'
54 '000000000000000000000000000000000000000000000000000000'
55 '00000001', overflowok=True)
56 testformat("%#.117x", (1,), '0x00000000000000000000000000000000000'
57 '000000000000000000000000000000000000000000000000000000'
58 '0000000000000000000000000001',
59 overflowok=True)
60 testformat("%#.118x", (1,), '0x00000000000000000000000000000000000'
61 '000000000000000000000000000000000000000000000000000000'
62 '00000000000000000000000000001',
63 overflowok=True)
Marc-André Lemburgd70141a2000-06-30 10:26:29 +000064
Christian Heimesf6cd9672008-03-26 13:45:42 +000065 testformat("%f", (1.0,), "1.000000")
66 # these are trying to test the limits of the internal magic-number-length
67 # formatting buffer, if that number changes then these tests are less
68 # effective
69 testformat("%#.*g", (109, -1.e+49/3.))
70 testformat("%#.*g", (110, -1.e+49/3.))
71 testformat("%#.*g", (110, -1.e+100/3.))
72 # test some ridiculously large precision, expect overflow
73 testformat('%12.*f', (123456, 1.0))
Mark Dickinson5354a1f2010-02-07 13:15:37 +000074
Christian Heimesf6cd9672008-03-26 13:45:42 +000075 # check for internal overflow validation on length of precision
Mark Dickinson5354a1f2010-02-07 13:15:37 +000076 # these tests should no longer cause overflow in Python
77 # 2.7/3.1 and later.
Christian Heimesf6cd9672008-03-26 13:45:42 +000078 testformat("%#.*g", (110, -1.e+100/3.))
79 testformat("%#.*G", (110, -1.e+100/3.))
80 testformat("%#.*f", (110, -1.e+100/3.))
81 testformat("%#.*F", (110, -1.e+100/3.))
Christian Heimesf6cd9672008-03-26 13:45:42 +000082 # Formatting of integers. Overflow is not ok
Christian Heimesf6cd9672008-03-26 13:45:42 +000083 testformat("%x", 10, "a")
84 testformat("%x", 100000000000, "174876e800")
85 testformat("%o", 10, "12")
86 testformat("%o", 100000000000, "1351035564000")
87 testformat("%d", 10, "10")
88 testformat("%d", 100000000000, "100000000000")
89 big = 123456789012345678901234567890
90 testformat("%d", big, "123456789012345678901234567890")
91 testformat("%d", -big, "-123456789012345678901234567890")
92 testformat("%5d", -big, "-123456789012345678901234567890")
93 testformat("%31d", -big, "-123456789012345678901234567890")
94 testformat("%32d", -big, " -123456789012345678901234567890")
95 testformat("%-32d", -big, "-123456789012345678901234567890 ")
96 testformat("%032d", -big, "-0123456789012345678901234567890")
97 testformat("%-032d", -big, "-123456789012345678901234567890 ")
98 testformat("%034d", -big, "-000123456789012345678901234567890")
99 testformat("%034d", big, "0000123456789012345678901234567890")
100 testformat("%0+34d", big, "+000123456789012345678901234567890")
101 testformat("%+34d", big, " +123456789012345678901234567890")
102 testformat("%34d", big, " 123456789012345678901234567890")
103 testformat("%.2d", big, "123456789012345678901234567890")
104 testformat("%.30d", big, "123456789012345678901234567890")
105 testformat("%.31d", big, "0123456789012345678901234567890")
106 testformat("%32.31d", big, " 0123456789012345678901234567890")
107 testformat("%d", float(big), "123456________________________", 6)
108 big = 0x1234567890abcdef12345 # 21 hex digits
109 testformat("%x", big, "1234567890abcdef12345")
110 testformat("%x", -big, "-1234567890abcdef12345")
111 testformat("%5x", -big, "-1234567890abcdef12345")
112 testformat("%22x", -big, "-1234567890abcdef12345")
113 testformat("%23x", -big, " -1234567890abcdef12345")
114 testformat("%-23x", -big, "-1234567890abcdef12345 ")
115 testformat("%023x", -big, "-01234567890abcdef12345")
116 testformat("%-023x", -big, "-1234567890abcdef12345 ")
117 testformat("%025x", -big, "-0001234567890abcdef12345")
118 testformat("%025x", big, "00001234567890abcdef12345")
119 testformat("%0+25x", big, "+0001234567890abcdef12345")
120 testformat("%+25x", big, " +1234567890abcdef12345")
121 testformat("%25x", big, " 1234567890abcdef12345")
122 testformat("%.2x", big, "1234567890abcdef12345")
123 testformat("%.21x", big, "1234567890abcdef12345")
124 testformat("%.22x", big, "01234567890abcdef12345")
125 testformat("%23.22x", big, " 01234567890abcdef12345")
126 testformat("%-23.22x", big, "01234567890abcdef12345 ")
127 testformat("%X", big, "1234567890ABCDEF12345")
128 testformat("%#X", big, "0X1234567890ABCDEF12345")
129 testformat("%#x", big, "0x1234567890abcdef12345")
130 testformat("%#x", -big, "-0x1234567890abcdef12345")
131 testformat("%#.23x", -big, "-0x001234567890abcdef12345")
132 testformat("%#+.23x", big, "+0x001234567890abcdef12345")
133 testformat("%# .23x", big, " 0x001234567890abcdef12345")
134 testformat("%#+.23X", big, "+0X001234567890ABCDEF12345")
135 testformat("%#-+.23X", big, "+0X001234567890ABCDEF12345")
136 testformat("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
137 testformat("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
138 testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
139 # next one gets two leading zeroes from precision, and another from the
140 # 0 flag and the width
141 testformat("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
142 # same, except no 0 flag
143 testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
144 testformat("%x", float(big), "123456_______________", 6)
145 big = 0o12345670123456701234567012345670 # 32 octal digits
146 testformat("%o", big, "12345670123456701234567012345670")
147 testformat("%o", -big, "-12345670123456701234567012345670")
148 testformat("%5o", -big, "-12345670123456701234567012345670")
149 testformat("%33o", -big, "-12345670123456701234567012345670")
150 testformat("%34o", -big, " -12345670123456701234567012345670")
151 testformat("%-34o", -big, "-12345670123456701234567012345670 ")
152 testformat("%034o", -big, "-012345670123456701234567012345670")
153 testformat("%-034o", -big, "-12345670123456701234567012345670 ")
154 testformat("%036o", -big, "-00012345670123456701234567012345670")
155 testformat("%036o", big, "000012345670123456701234567012345670")
156 testformat("%0+36o", big, "+00012345670123456701234567012345670")
157 testformat("%+36o", big, " +12345670123456701234567012345670")
158 testformat("%36o", big, " 12345670123456701234567012345670")
159 testformat("%.2o", big, "12345670123456701234567012345670")
160 testformat("%.32o", big, "12345670123456701234567012345670")
161 testformat("%.33o", big, "012345670123456701234567012345670")
162 testformat("%34.33o", big, " 012345670123456701234567012345670")
163 testformat("%-34.33o", big, "012345670123456701234567012345670 ")
164 testformat("%o", big, "12345670123456701234567012345670")
165 testformat("%#o", big, "0o12345670123456701234567012345670")
166 testformat("%#o", -big, "-0o12345670123456701234567012345670")
167 testformat("%#.34o", -big, "-0o0012345670123456701234567012345670")
168 testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
169 testformat("%# .34o", big, " 0o0012345670123456701234567012345670")
170 testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
171 testformat("%#-+.34o", big, "+0o0012345670123456701234567012345670")
172 testformat("%#-+37.34o", big, "+0o0012345670123456701234567012345670")
173 testformat("%#+37.34o", big, "+0o0012345670123456701234567012345670")
174 # next one gets one leading zero from precision
175 testformat("%.33o", big, "012345670123456701234567012345670")
176 # base marker shouldn't change that, since "0" is redundant
177 testformat("%#.33o", big, "0o012345670123456701234567012345670")
178 # but reduce precision, and base marker should add a zero
179 testformat("%#.32o", big, "0o12345670123456701234567012345670")
180 # one leading zero from precision, and another from "0" flag & width
181 testformat("%034.33o", big, "0012345670123456701234567012345670")
182 # base marker shouldn't change that
183 testformat("%0#34.33o", big, "0o012345670123456701234567012345670")
184 testformat("%o", float(big), "123456__________________________", 6)
185 # Some small ints, in both Python int and flavors).
186 testformat("%d", 42, "42")
187 testformat("%d", -42, "-42")
188 testformat("%d", 42, "42")
189 testformat("%d", -42, "-42")
190 testformat("%d", 42.0, "42")
191 testformat("%#x", 1, "0x1")
192 testformat("%#x", 1, "0x1")
193 testformat("%#X", 1, "0X1")
194 testformat("%#X", 1, "0X1")
195 testformat("%#x", 1.0, "0x1")
196 testformat("%#o", 1, "0o1")
197 testformat("%#o", 1, "0o1")
198 testformat("%#o", 0, "0o0")
199 testformat("%#o", 0, "0o0")
200 testformat("%o", 0, "0")
201 testformat("%o", 0, "0")
202 testformat("%d", 0, "0")
203 testformat("%d", 0, "0")
204 testformat("%#x", 0, "0x0")
205 testformat("%#x", 0, "0x0")
206 testformat("%#X", 0, "0X0")
207 testformat("%#X", 0, "0X0")
208 testformat("%x", 0x42, "42")
209 testformat("%x", -0x42, "-42")
210 testformat("%x", 0x42, "42")
211 testformat("%x", -0x42, "-42")
212 testformat("%x", float(0x42), "42")
213 testformat("%o", 0o42, "42")
214 testformat("%o", -0o42, "-42")
215 testformat("%o", 0o42, "42")
216 testformat("%o", -0o42, "-42")
217 testformat("%o", float(0o42), "42")
Amaury Forgeot d'Arca083f1e2008-09-10 23:51:42 +0000218 testformat("%r", "\u0378", "'\\u0378'") # non printable
219 testformat("%a", "\u0378", "'\\u0378'") # non printable
Amaury Forgeot d'Arca819caa2008-07-04 17:57:09 +0000220 testformat("%r", "\u0374", "'\u0374'") # printable
221 testformat("%a", "\u0374", "'\\u0374'") # printable
Eric Smith0923d1d2009-04-16 20:16:10 +0000222
223 # alternate float formatting
224 testformat('%g', 1.1, '1.1')
225 testformat('%#g', 1.1, '1.10000')
226
Christian Heimesf6cd9672008-03-26 13:45:42 +0000227 # Test exception for unknown format characters
228 if verbose:
229 print('Testing exceptions')
230 def test_exc(formatstr, args, exception, excmsg):
231 try:
232 testformat(formatstr, args)
233 except exception as exc:
234 if str(exc) == excmsg:
235 if verbose:
236 print("yes")
237 else:
238 if verbose: print('no')
239 print('Unexpected ', exception, ':', repr(str(exc)))
240 except:
241 if verbose: print('no')
242 print('Unexpected exception')
243 raise
244 else:
245 raise TestFailed('did not get expected exception: %s' % excmsg)
Georg Brandl559e5d72008-06-11 18:37:52 +0000246 test_exc('abc %b', 1, ValueError,
247 "unsupported format character 'b' (0x62) at index 5")
Christian Heimesf6cd9672008-03-26 13:45:42 +0000248 #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
249 # "unsupported format character '?' (0x3000) at index 5")
250 test_exc('%d', '1', TypeError, "%d format: a number is required, not str")
251 test_exc('%g', '1', TypeError, "a float is required")
252 test_exc('no format', '1', TypeError,
253 "not all arguments converted during string formatting")
254 test_exc('no format', '1', TypeError,
255 "not all arguments converted during string formatting")
Marc-André Lemburgd70141a2000-06-30 10:26:29 +0000256
Christian Heimesf6cd9672008-03-26 13:45:42 +0000257 if maxsize == 2**31-1:
258 # crashes 2.2.1 and earlier:
259 try:
260 "%*d"%(maxsize, -127)
261 except MemoryError:
262 pass
263 else:
264 raise TestFailed('"%*d"%(maxsize, -127) should fail')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000265
Christian Heimesf6cd9672008-03-26 13:45:42 +0000266def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000267 support.run_unittest(FormatTest)
Tim Peters38fd5b62000-09-21 05:43:11 +0000268
Tim Peters38fd5b62000-09-21 05:43:11 +0000269
Christian Heimesf6cd9672008-03-26 13:45:42 +0000270if __name__ == "__main__":
271 unittest.main()