blob: c200e07b2a346e485f26a57d53e190ed032c142d [file] [log] [blame]
Guido van Rossum85f18201992-11-27 22:53:50 +00001# Python test set -- part 6, built-in types
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test.support import run_unittest, run_with_locale
Thomas Wouters89f507f2006-12-13 04:49:30 +00004import unittest
Guido van Rossum85f18201992-11-27 22:53:50 +00005import sys
Eric Smithb2c7af82008-04-30 02:12:09 +00006import locale
Guido van Rossum85f18201992-11-27 22:53:50 +00007
Thomas Wouters89f507f2006-12-13 04:49:30 +00008class TypesTests(unittest.TestCase):
Guido van Rossum85f18201992-11-27 22:53:50 +00009
Thomas Wouters89f507f2006-12-13 04:49:30 +000010 def test_truth_values(self):
11 if None: self.fail('None is true instead of false')
12 if 0: self.fail('0 is true instead of false')
Guido van Rossume2a383d2007-01-15 16:59:06 +000013 if 0: self.fail('0L is true instead of false')
Thomas Wouters89f507f2006-12-13 04:49:30 +000014 if 0.0: self.fail('0.0 is true instead of false')
15 if '': self.fail('\'\' is true instead of false')
16 if not 1: self.fail('1 is false instead of true')
Guido van Rossume2a383d2007-01-15 16:59:06 +000017 if not 1: self.fail('1L is false instead of true')
Thomas Wouters89f507f2006-12-13 04:49:30 +000018 if not 1.0: self.fail('1.0 is false instead of true')
19 if not 'x': self.fail('\'x\' is false instead of true')
20 if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
21 def f(): pass
22 class C: pass
23 import sys
24 x = C()
25 if not f: self.fail('f is false instead of true')
26 if not C: self.fail('C is false instead of true')
27 if not sys: self.fail('sys is false instead of true')
28 if not x: self.fail('x is false instead of true')
Guido van Rossum85f18201992-11-27 22:53:50 +000029
Thomas Wouters89f507f2006-12-13 04:49:30 +000030 def test_boolean_ops(self):
31 if 0 or 0: self.fail('0 or 0 is true instead of false')
32 if 1 and 1: pass
33 else: self.fail('1 and 1 is false instead of true')
34 if not 1: self.fail('not 1 is true instead of false')
Neil Schemenauereff72442002-03-24 01:24:54 +000035
Thomas Wouters89f507f2006-12-13 04:49:30 +000036 def test_comparisons(self):
37 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
38 else: self.fail('int comparisons failed')
Guido van Rossume2a383d2007-01-15 16:59:06 +000039 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +000040 else: self.fail('long int comparisons failed')
41 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
42 else: self.fail('float comparisons failed')
43 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
44 else: self.fail('string comparisons failed')
45 if None is None: pass
46 else: self.fail('identity test failed')
Neil Schemenauereff72442002-03-24 01:24:54 +000047
Thomas Wouters89f507f2006-12-13 04:49:30 +000048 def test_float_constructor(self):
49 self.assertRaises(ValueError, float, '')
50 self.assertRaises(ValueError, float, '5\0')
Neil Schemenauereff72442002-03-24 01:24:54 +000051
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 def test_zero_division(self):
53 try: 5.0 / 0.0
54 except ZeroDivisionError: pass
55 else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000056
Thomas Wouters89f507f2006-12-13 04:49:30 +000057 try: 5.0 // 0.0
58 except ZeroDivisionError: pass
59 else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000060
Thomas Wouters89f507f2006-12-13 04:49:30 +000061 try: 5.0 % 0.0
62 except ZeroDivisionError: pass
63 else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000064
Guido van Rossume2a383d2007-01-15 16:59:06 +000065 try: 5 / 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000066 except ZeroDivisionError: pass
Christian Heimes7131fd92008-02-19 14:21:46 +000067 else: self.fail("5 / 0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000068
Guido van Rossume2a383d2007-01-15 16:59:06 +000069 try: 5 // 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000070 except ZeroDivisionError: pass
Christian Heimes7131fd92008-02-19 14:21:46 +000071 else: self.fail("5 // 0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000072
Guido van Rossume2a383d2007-01-15 16:59:06 +000073 try: 5 % 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000074 except ZeroDivisionError: pass
Christian Heimes7131fd92008-02-19 14:21:46 +000075 else: self.fail("5 % 0 didn't raise ZeroDivisionError")
Tim Petersa3c01ce2001-12-04 23:05:10 +000076
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 def test_numeric_types(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000078 if 0 != 0 or 0 != 0.0 or 0 != 0.0: self.fail('mixed comparisons')
79 if 1 != 1 or 1 != 1.0 or 1 != 1.0: self.fail('mixed comparisons')
80 if -1 != -1 or -1 != -1.0 or -1 != -1.0:
Thomas Wouters89f507f2006-12-13 04:49:30 +000081 self.fail('int/long/float value not equal')
82 # calling built-in types without argument must return 0
83 if int() != 0: self.fail('int() does not return 0')
Christian Heimes7131fd92008-02-19 14:21:46 +000084 if int() != 0: self.fail('long() does not return 0')
Thomas Wouters89f507f2006-12-13 04:49:30 +000085 if float() != 0.0: self.fail('float() does not return 0.0')
86 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
87 else: self.fail('int() does not round properly')
Guido van Rossume2a383d2007-01-15 16:59:06 +000088 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
Thomas Wouters89f507f2006-12-13 04:49:30 +000089 else: self.fail('long() does not round properly')
90 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
91 else: self.fail('float() does not work properly')
Neil Schemenauereff72442002-03-24 01:24:54 +000092
Christian Heimesc3f30c42008-02-22 16:37:40 +000093 def test_float_to_string(self):
94 def test(f, result):
95 self.assertEqual(f.__format__('e'), result)
96 self.assertEqual('%e' % f, result)
97
98 # test all 2 digit exponents, both with __format__ and with
99 # '%' formatting
100 for i in range(-99, 100):
101 test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))
102
103 # test some 3 digit exponents
104 self.assertEqual(1.5e100.__format__('e'), '1.500000e+100')
105 self.assertEqual('%e' % 1.5e100, '1.500000e+100')
106
107 self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
108 self.assertEqual('%e' % 1.5e101, '1.500000e+101')
109
110 self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
111 self.assertEqual('%e' % 1.5e-100, '1.500000e-100')
112
113 self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
114 self.assertEqual('%e' % 1.5e-101, '1.500000e-101')
115
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 def test_normal_integers(self):
117 # Ensure the first 256 integers are shared
118 a = 256
119 b = 128*2
120 if a is not b: self.fail('256 is not shared')
121 if 12 + 24 != 36: self.fail('int op')
122 if 12 + (-24) != -12: self.fail('int op')
123 if (-12) + 24 != 12: self.fail('int op')
124 if (-12) + (-24) != -36: self.fail('int op')
125 if not 12 < 24: self.fail('int op')
126 if not -24 < -12: self.fail('int op')
127 # Test for a particular bug in integer multiply
128 xsize, ysize, zsize = 238, 356, 4
129 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
130 self.fail('int mul commutativity')
131 # And another.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000132 m = -sys.maxsize - 1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000133 for divisor in 1, 2, 4, 8, 16, 32:
134 j = m // divisor
135 prod = divisor * j
136 if prod != m:
137 self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
138 if type(prod) is not int:
139 self.fail("expected type(prod) to be int, not %r" %
140 type(prod))
141 # Check for expected * overflow to long.
142 for divisor in 1, 2, 4, 8, 16, 32:
143 j = m // divisor - 1
144 prod = divisor * j
Guido van Rossume2a383d2007-01-15 16:59:06 +0000145 if type(prod) is not int:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146 self.fail("expected type(%r) to be long, not %r" %
147 (prod, type(prod)))
148 # Check for expected * overflow to long.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000149 m = sys.maxsize
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 for divisor in 1, 2, 4, 8, 16, 32:
151 j = m // divisor + 1
152 prod = divisor * j
Guido van Rossume2a383d2007-01-15 16:59:06 +0000153 if type(prod) is not int:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000154 self.fail("expected type(%r) to be long, not %r" %
155 (prod, type(prod)))
Neil Schemenauereff72442002-03-24 01:24:54 +0000156
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157 def test_long_integers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000158 if 12 + 24 != 36: self.fail('long op')
159 if 12 + (-24) != -12: self.fail('long op')
160 if (-12) + 24 != 12: self.fail('long op')
161 if (-12) + (-24) != -36: self.fail('long op')
162 if not 12 < 24: self.fail('long op')
163 if not -24 < -12: self.fail('long op')
Christian Heimesa37d4c62007-12-04 23:02:19 +0000164 x = sys.maxsize
Guido van Rossume2a383d2007-01-15 16:59:06 +0000165 if int(int(x)) != x: self.fail('long op')
166 try: y = int(int(x)+1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167 except OverflowError: self.fail('long op')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000168 if not isinstance(y, int): self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000169 x = -x
Guido van Rossume2a383d2007-01-15 16:59:06 +0000170 if int(int(x)) != x: self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000171 x = x-1
Guido van Rossume2a383d2007-01-15 16:59:06 +0000172 if int(int(x)) != x: self.fail('long op')
173 try: y = int(int(x)-1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000174 except OverflowError: self.fail('long op')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000175 if not isinstance(y, int): self.fail('long op')
Neil Schemenauereff72442002-03-24 01:24:54 +0000176
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177 try: 5 << -5
178 except ValueError: pass
179 else: self.fail('int negative shift <<')
Neil Schemenauereff72442002-03-24 01:24:54 +0000180
Guido van Rossume2a383d2007-01-15 16:59:06 +0000181 try: 5 << -5
Thomas Wouters89f507f2006-12-13 04:49:30 +0000182 except ValueError: pass
183 else: self.fail('long negative shift <<')
Neil Schemenauereff72442002-03-24 01:24:54 +0000184
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185 try: 5 >> -5
186 except ValueError: pass
187 else: self.fail('int negative shift >>')
Guido van Rossum85f18201992-11-27 22:53:50 +0000188
Guido van Rossume2a383d2007-01-15 16:59:06 +0000189 try: 5 >> -5
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190 except ValueError: pass
191 else: self.fail('long negative shift >>')
Guido van Rossum85f18201992-11-27 22:53:50 +0000192
Thomas Wouters89f507f2006-12-13 04:49:30 +0000193 def test_floats(self):
194 if 12.0 + 24.0 != 36.0: self.fail('float op')
195 if 12.0 + (-24.0) != -12.0: self.fail('float op')
196 if (-12.0) + 24.0 != 12.0: self.fail('float op')
197 if (-12.0) + (-24.0) != -36.0: self.fail('float op')
198 if not 12.0 < 24.0: self.fail('float op')
199 if not -24.0 < -12.0: self.fail('float op')
Guido van Rossum85f18201992-11-27 22:53:50 +0000200
Thomas Wouters89f507f2006-12-13 04:49:30 +0000201 def test_strings(self):
202 if len('') != 0: self.fail('len(\'\')')
203 if len('a') != 1: self.fail('len(\'a\')')
204 if len('abcdef') != 6: self.fail('len(\'abcdef\')')
205 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
206 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
207 if 0*'abcde' != '': self.fail('string repetition 0*')
208 if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
209 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
210 else: self.fail('in/not in string')
211 x = 'x'*103
212 if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000213
Thomas Wouters89f507f2006-12-13 04:49:30 +0000214 #extended slices for strings
215 a = '0123456789'
216 self.assertEqual(a[::], a)
217 self.assertEqual(a[::2], '02468')
218 self.assertEqual(a[1::2], '13579')
219 self.assertEqual(a[::-1],'9876543210')
220 self.assertEqual(a[::-2], '97531')
221 self.assertEqual(a[3::-2], '31')
222 self.assertEqual(a[-100:100:], a)
223 self.assertEqual(a[100:-100:-1], a[::-1])
Guido van Rossume2a383d2007-01-15 16:59:06 +0000224 self.assertEqual(a[-100:100:2], '02468')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000225
Thomas Wouters89f507f2006-12-13 04:49:30 +0000226 def test_type_function(self):
227 self.assertRaises(TypeError, type, 1, 2)
228 self.assertRaises(TypeError, type, 1, 2, 3, 4)
Guido van Rossum85f18201992-11-27 22:53:50 +0000229
Christian Heimes7131fd92008-02-19 14:21:46 +0000230 def test_int__format__(self):
231 def test(i, format_spec, result):
232 # just make sure I'm not accidentally checking longs
233 assert type(i) == int
234 assert type(format_spec) == str
235 self.assertEqual(i.__format__(format_spec), result)
236
237 test(123456789, 'd', '123456789')
238 test(123456789, 'd', '123456789')
239
240 test(1, 'c', '\01')
241
242 # sign and aligning are interdependent
243 test(1, "-", '1')
244 test(-1, "-", '-1')
245 test(1, "-3", ' 1')
246 test(-1, "-3", ' -1')
247 test(1, "+3", ' +1')
248 test(-1, "+3", ' -1')
249 test(1, " 3", ' 1')
250 test(-1, " 3", ' -1')
251 test(1, " ", ' 1')
252 test(-1, " ", '-1')
253
254 # hex
255 test(3, "x", "3")
256 test(3, "X", "3")
257 test(1234, "x", "4d2")
258 test(-1234, "x", "-4d2")
259 test(1234, "8x", " 4d2")
260 test(-1234, "8x", " -4d2")
261 test(1234, "x", "4d2")
262 test(-1234, "x", "-4d2")
263 test(-3, "x", "-3")
264 test(-3, "X", "-3")
265 test(int('be', 16), "x", "be")
266 test(int('be', 16), "X", "BE")
267 test(-int('be', 16), "x", "-be")
268 test(-int('be', 16), "X", "-BE")
269
270 # octal
271 test(3, "o", "3")
272 test(-3, "o", "-3")
273 test(65, "o", "101")
274 test(-65, "o", "-101")
275 test(1234, "o", "2322")
276 test(-1234, "o", "-2322")
277 test(1234, "-o", "2322")
278 test(-1234, "-o", "-2322")
279 test(1234, " o", " 2322")
280 test(-1234, " o", "-2322")
281 test(1234, "+o", "+2322")
282 test(-1234, "+o", "-2322")
283
284 # binary
285 test(3, "b", "11")
286 test(-3, "b", "-11")
287 test(1234, "b", "10011010010")
288 test(-1234, "b", "-10011010010")
289 test(1234, "-b", "10011010010")
290 test(-1234, "-b", "-10011010010")
291 test(1234, " b", " 10011010010")
292 test(-1234, " b", "-10011010010")
293 test(1234, "+b", "+10011010010")
294 test(-1234, "+b", "-10011010010")
295
Eric Smithb1ebcc62008-07-15 13:02:41 +0000296 # alternate (#) formatting
297 test(0, "#b", '0b0')
298 test(0, "-#b", '0b0')
299 test(1, "-#b", '0b1')
300 test(-1, "-#b", '-0b1')
301 test(-1, "-#5b", ' -0b1')
302 test(1, "+#5b", ' +0b1')
303 test(100, "+#b", '+0b1100100')
Eric Smithd68af8f2008-07-16 00:15:35 +0000304 test(100, "#012b", '0b0001100100')
305 test(-100, "#012b", '-0b001100100')
Eric Smithb1ebcc62008-07-15 13:02:41 +0000306
307 test(0, "#o", '0o0')
308 test(0, "-#o", '0o0')
309 test(1, "-#o", '0o1')
310 test(-1, "-#o", '-0o1')
311 test(-1, "-#5o", ' -0o1')
312 test(1, "+#5o", ' +0o1')
313 test(100, "+#o", '+0o144')
Eric Smithd68af8f2008-07-16 00:15:35 +0000314 test(100, "#012o", '0o0000000144')
315 test(-100, "#012o", '-0o000000144')
Eric Smithb1ebcc62008-07-15 13:02:41 +0000316
317 test(0, "#x", '0x0')
318 test(0, "-#x", '0x0')
319 test(1, "-#x", '0x1')
320 test(-1, "-#x", '-0x1')
321 test(-1, "-#5x", ' -0x1')
322 test(1, "+#5x", ' +0x1')
323 test(100, "+#x", '+0x64')
Eric Smithd68af8f2008-07-16 00:15:35 +0000324 test(100, "#012x", '0x0000000064')
325 test(-100, "#012x", '-0x000000064')
326 test(123456, "#012x", '0x000001e240')
327 test(-123456, "#012x", '-0x00001e240')
Eric Smithb1ebcc62008-07-15 13:02:41 +0000328
329 test(0, "#X", '0X0')
330 test(0, "-#X", '0X0')
331 test(1, "-#X", '0X1')
332 test(-1, "-#X", '-0X1')
333 test(-1, "-#5X", ' -0X1')
334 test(1, "+#5X", ' +0X1')
335 test(100, "+#X", '+0X64')
Eric Smithd68af8f2008-07-16 00:15:35 +0000336 test(100, "#012X", '0X0000000064')
337 test(-100, "#012X", '-0X000000064')
338 test(123456, "#012X", '0X000001E240')
339 test(-123456, "#012X", '-0X00001E240')
Eric Smithb1ebcc62008-07-15 13:02:41 +0000340
Christian Heimes7131fd92008-02-19 14:21:46 +0000341 # make sure these are errors
342
343 # precision disallowed
344 self.assertRaises(ValueError, 3 .__format__, "1.3")
345 # sign not allowed with 'c'
346 self.assertRaises(ValueError, 3 .__format__, "+c")
347 # format spec must be string
348 self.assertRaises(TypeError, 3 .__format__, None)
349 self.assertRaises(TypeError, 3 .__format__, 0)
350
351 # ensure that only int and float type specifiers work
352 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
353 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
354 if not format_spec in 'bcdoxXeEfFgGn%':
355 self.assertRaises(ValueError, 0 .__format__, format_spec)
356 self.assertRaises(ValueError, 1 .__format__, format_spec)
357 self.assertRaises(ValueError, (-1) .__format__, format_spec)
358
359 # ensure that float type specifiers work; format converts
360 # the int to a float
Eric Smith5807c412008-05-11 21:00:57 +0000361 for format_spec in 'eEfFgG%':
Christian Heimes7131fd92008-02-19 14:21:46 +0000362 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
363 self.assertEqual(value.__format__(format_spec),
364 float(value).__format__(format_spec))
365
366 def test_long__format__(self):
367 def test(i, format_spec, result):
368 # make sure we're not accidentally checking ints
369 assert type(i) == int
370 assert type(format_spec) == str
371 self.assertEqual(i.__format__(format_spec), result)
372
373 test(10**100, 'd', '1' + '0' * 100)
374 test(10**100+100, 'd', '1' + '0' * 97 + '100')
375
376 test(123456789, 'd', '123456789')
377 test(123456789, 'd', '123456789')
378
379 # sign and aligning are interdependent
380 test(1, "-", '1')
381 test(-1, "-", '-1')
382 test(1, "-3", ' 1')
383 test(-1, "-3", ' -1')
384 test(1, "+3", ' +1')
385 test(-1, "+3", ' -1')
386 test(1, " 3", ' 1')
387 test(-1, " 3", ' -1')
388 test(1, " ", ' 1')
389 test(-1, " ", '-1')
390
391 test(1, 'c', '\01')
392
393 # hex
394 test(3, "x", "3")
395 test(3, "X", "3")
396 test(1234, "x", "4d2")
397 test(-1234, "x", "-4d2")
398 test(1234, "8x", " 4d2")
399 test(-1234, "8x", " -4d2")
400 test(1234, "x", "4d2")
401 test(-1234, "x", "-4d2")
402 test(-3, "x", "-3")
403 test(-3, "X", "-3")
404
405 # octal
406 test(3, "o", "3")
407 test(-3, "o", "-3")
408 test(65, "o", "101")
409 test(-65, "o", "-101")
410 test(1234, "o", "2322")
411 test(-1234, "o", "-2322")
412 test(1234, "-o", "2322")
413 test(-1234, "-o", "-2322")
414 test(1234, " o", " 2322")
415 test(-1234, " o", "-2322")
416 test(1234, "+o", "+2322")
417 test(-1234, "+o", "-2322")
418
419 # binary
420 test(3, "b", "11")
421 test(-3, "b", "-11")
422 test(1234, "b", "10011010010")
423 test(-1234, "b", "-10011010010")
424 test(1234, "-b", "10011010010")
425 test(-1234, "-b", "-10011010010")
426 test(1234, " b", " 10011010010")
427 test(-1234, " b", "-10011010010")
428 test(1234, "+b", "+10011010010")
429 test(-1234, "+b", "-10011010010")
430
431 # make sure these are errors
432
433 # precision disallowed
434 self.assertRaises(ValueError, 3 .__format__, "1.3")
435 # sign not allowed with 'c'
436 self.assertRaises(ValueError, 3 .__format__, "+c")
437 # format spec must be string
438 self.assertRaises(TypeError, 3 .__format__, None)
439 self.assertRaises(TypeError, 3 .__format__, 0)
440
441 # ensure that only int and float type specifiers work
442 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
443 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
444 if not format_spec in 'bcdoxXeEfFgGn%':
445 self.assertRaises(ValueError, 0 .__format__, format_spec)
446 self.assertRaises(ValueError, 1 .__format__, format_spec)
447 self.assertRaises(ValueError, (-1) .__format__, format_spec)
448
449 # ensure that float type specifiers work; format converts
450 # the long to a float
Eric Smith5807c412008-05-11 21:00:57 +0000451 for format_spec in 'eEfFgG%':
Christian Heimes7131fd92008-02-19 14:21:46 +0000452 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
453 self.assertEqual(value.__format__(format_spec),
454 float(value).__format__(format_spec))
455
Eric Smithb2c7af82008-04-30 02:12:09 +0000456 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
457 def test_float__format__locale(self):
458 # test locale support for __format__ code 'n'
459
460 for i in range(-10, 10):
461 x = 1234567890.0 * (10.0 ** i)
462 self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
463 self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
464
Eric Smith5807c412008-05-11 21:00:57 +0000465 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
466 def test_int__format__locale(self):
467 # test locale support for __format__ code 'n' for integers
468
469 x = 123456789012345678901234567890
470 for i in range(0, 30):
471 self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
472
473 # move to the next integer to test
474 x = x // 10
475
Eric Smithb151a452008-06-24 11:21:04 +0000476 rfmt = ">20n"
477 lfmt = "<20n"
478 cfmt = "^20n"
479 for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
480 self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
481 self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
482 self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
483
Christian Heimes7131fd92008-02-19 14:21:46 +0000484 def test_float__format__(self):
485 # these should be rewritten to use both format(x, spec) and
486 # x.__format__(spec)
487
488 def test(f, format_spec, result):
489 assert type(f) == float
490 assert type(format_spec) == str
491 self.assertEqual(f.__format__(format_spec), result)
492
493 test(0.0, 'f', '0.000000')
494
495 # the default is 'g', except for empty format spec
496 test(0.0, '', '0.0')
497 test(0.01, '', '0.01')
498 test(0.01, 'g', '0.01')
499
Eric Smith2ad79e82008-07-19 00:33:23 +0000500 # test for issue 3411
501 test(1.23, '1', '1.23')
502 test(-1.23, '1', '-1.23')
503 test(1.23, '1g', '1.23')
504 test(-1.23, '1g', '-1.23')
505
Christian Heimes7131fd92008-02-19 14:21:46 +0000506 test( 1.0, ' g', ' 1')
507 test(-1.0, ' g', '-1')
508 test( 1.0, '+g', '+1')
509 test(-1.0, '+g', '-1')
510 test(1.1234e200, 'g', '1.1234e+200')
511 test(1.1234e200, 'G', '1.1234E+200')
512
513
514 test(1.0, 'f', '1.000000')
515
516 test(-1.0, 'f', '-1.000000')
517
518 test( 1.0, ' f', ' 1.000000')
519 test(-1.0, ' f', '-1.000000')
520 test( 1.0, '+f', '+1.000000')
521 test(-1.0, '+f', '-1.000000')
Christian Heimesc3f30c42008-02-22 16:37:40 +0000522 test(1.1234e90, 'f', '1.1234e+90')
Eric Smith22b85b32008-07-17 19:18:29 +0000523 test(1.1234e90, 'F', '1.1234e+90')
Christian Heimes7131fd92008-02-19 14:21:46 +0000524 test(1.1234e200, 'f', '1.1234e+200')
Eric Smith22b85b32008-07-17 19:18:29 +0000525 test(1.1234e200, 'F', '1.1234e+200')
Christian Heimes7131fd92008-02-19 14:21:46 +0000526
Christian Heimesc3f30c42008-02-22 16:37:40 +0000527 test( 1.0, 'e', '1.000000e+00')
528 test(-1.0, 'e', '-1.000000e+00')
529 test( 1.0, 'E', '1.000000E+00')
530 test(-1.0, 'E', '-1.000000E+00')
531 test(1.1234e20, 'e', '1.123400e+20')
532 test(1.1234e20, 'E', '1.123400E+20')
Christian Heimes7131fd92008-02-19 14:21:46 +0000533
Christian Heimesb186d002008-03-18 15:15:01 +0000534 # No format code means use g, but must have a decimal
535 # and a number after the decimal. This is tricky, because
536 # a totaly empty format specifier means something else.
537 # So, just use a sign flag
538 test(1e200, '+g', '+1e+200')
539 test(1e200, '+', '+1.0e+200')
540 test(1.1e200, '+g', '+1.1e+200')
541 test(1.1e200, '+', '+1.1e+200')
542
Christian Heimes7131fd92008-02-19 14:21:46 +0000543 # % formatting
544 test(-1.0, '%', '-100.000000%')
545
546 # format spec must be string
547 self.assertRaises(TypeError, 3.0.__format__, None)
548 self.assertRaises(TypeError, 3.0.__format__, 0)
549
550 # other format specifiers shouldn't work on floats,
551 # in particular int specifiers
552 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
553 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
554 if not format_spec in 'eEfFgGn%':
555 self.assertRaises(ValueError, format, 0.0, format_spec)
556 self.assertRaises(ValueError, format, 1.0, format_spec)
557 self.assertRaises(ValueError, format, -1.0, format_spec)
558 self.assertRaises(ValueError, format, 1e100, format_spec)
559 self.assertRaises(ValueError, format, -1e100, format_spec)
560 self.assertRaises(ValueError, format, 1e-100, format_spec)
561 self.assertRaises(ValueError, format, -1e-100, format_spec)
562
Eric Smithb1ebcc62008-07-15 13:02:41 +0000563 # Alternate formatting is not supported
564 self.assertRaises(ValueError, format, 0.0, '#')
565 self.assertRaises(ValueError, format, 0.0, '#20f')
566
Christian Heimes7131fd92008-02-19 14:21:46 +0000567
Thomas Wouters89f507f2006-12-13 04:49:30 +0000568def test_main():
569 run_unittest(TypesTests)
Neil Schemenauereff72442002-03-24 01:24:54 +0000570
Thomas Wouters89f507f2006-12-13 04:49:30 +0000571if __name__ == '__main__':
572 test_main()