blob: 0d4d1b3edc9bdf8e287ae492b03745425f700c9f [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
Eric Smitha3b1ac82009-04-03 14:45:06 +0000341 test(123, ',', '123')
342 test(-123, ',', '-123')
343 test(1234, ',', '1,234')
344 test(-1234, ',', '-1,234')
345 test(123456, ',', '123,456')
346 test(-123456, ',', '-123,456')
347 test(1234567, ',', '1,234,567')
348 test(-1234567, ',', '-1,234,567')
349
Christian Heimes7131fd92008-02-19 14:21:46 +0000350 # make sure these are errors
351
352 # precision disallowed
353 self.assertRaises(ValueError, 3 .__format__, "1.3")
354 # sign not allowed with 'c'
355 self.assertRaises(ValueError, 3 .__format__, "+c")
356 # format spec must be string
357 self.assertRaises(TypeError, 3 .__format__, None)
358 self.assertRaises(TypeError, 3 .__format__, 0)
Eric Smitha3b1ac82009-04-03 14:45:06 +0000359 # can't have ',' with 'n'
360 self.assertRaises(ValueError, 3 .__format__, ",n")
Christian Heimes7131fd92008-02-19 14:21:46 +0000361
362 # ensure that only int and float type specifiers work
363 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
364 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
365 if not format_spec in 'bcdoxXeEfFgGn%':
366 self.assertRaises(ValueError, 0 .__format__, format_spec)
367 self.assertRaises(ValueError, 1 .__format__, format_spec)
368 self.assertRaises(ValueError, (-1) .__format__, format_spec)
369
370 # ensure that float type specifiers work; format converts
371 # the int to a float
Eric Smith5807c412008-05-11 21:00:57 +0000372 for format_spec in 'eEfFgG%':
Christian Heimes7131fd92008-02-19 14:21:46 +0000373 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
374 self.assertEqual(value.__format__(format_spec),
375 float(value).__format__(format_spec))
376
377 def test_long__format__(self):
378 def test(i, format_spec, result):
379 # make sure we're not accidentally checking ints
380 assert type(i) == int
381 assert type(format_spec) == str
382 self.assertEqual(i.__format__(format_spec), result)
383
384 test(10**100, 'd', '1' + '0' * 100)
385 test(10**100+100, 'd', '1' + '0' * 97 + '100')
386
387 test(123456789, 'd', '123456789')
388 test(123456789, 'd', '123456789')
389
390 # sign and aligning are interdependent
391 test(1, "-", '1')
392 test(-1, "-", '-1')
393 test(1, "-3", ' 1')
394 test(-1, "-3", ' -1')
395 test(1, "+3", ' +1')
396 test(-1, "+3", ' -1')
397 test(1, " 3", ' 1')
398 test(-1, " 3", ' -1')
399 test(1, " ", ' 1')
400 test(-1, " ", '-1')
401
402 test(1, 'c', '\01')
403
404 # hex
405 test(3, "x", "3")
406 test(3, "X", "3")
407 test(1234, "x", "4d2")
408 test(-1234, "x", "-4d2")
409 test(1234, "8x", " 4d2")
410 test(-1234, "8x", " -4d2")
411 test(1234, "x", "4d2")
412 test(-1234, "x", "-4d2")
413 test(-3, "x", "-3")
414 test(-3, "X", "-3")
415
416 # octal
417 test(3, "o", "3")
418 test(-3, "o", "-3")
419 test(65, "o", "101")
420 test(-65, "o", "-101")
421 test(1234, "o", "2322")
422 test(-1234, "o", "-2322")
423 test(1234, "-o", "2322")
424 test(-1234, "-o", "-2322")
425 test(1234, " o", " 2322")
426 test(-1234, " o", "-2322")
427 test(1234, "+o", "+2322")
428 test(-1234, "+o", "-2322")
429
430 # binary
431 test(3, "b", "11")
432 test(-3, "b", "-11")
433 test(1234, "b", "10011010010")
434 test(-1234, "b", "-10011010010")
435 test(1234, "-b", "10011010010")
436 test(-1234, "-b", "-10011010010")
437 test(1234, " b", " 10011010010")
438 test(-1234, " b", "-10011010010")
439 test(1234, "+b", "+10011010010")
440 test(-1234, "+b", "-10011010010")
441
442 # make sure these are errors
443
444 # precision disallowed
445 self.assertRaises(ValueError, 3 .__format__, "1.3")
446 # sign not allowed with 'c'
447 self.assertRaises(ValueError, 3 .__format__, "+c")
448 # format spec must be string
449 self.assertRaises(TypeError, 3 .__format__, None)
450 self.assertRaises(TypeError, 3 .__format__, 0)
451
452 # ensure that only int and float type specifiers work
453 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
454 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
455 if not format_spec in 'bcdoxXeEfFgGn%':
456 self.assertRaises(ValueError, 0 .__format__, format_spec)
457 self.assertRaises(ValueError, 1 .__format__, format_spec)
458 self.assertRaises(ValueError, (-1) .__format__, format_spec)
459
460 # ensure that float type specifiers work; format converts
461 # the long to a float
Eric Smith5807c412008-05-11 21:00:57 +0000462 for format_spec in 'eEfFgG%':
Christian Heimes7131fd92008-02-19 14:21:46 +0000463 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
464 self.assertEqual(value.__format__(format_spec),
465 float(value).__format__(format_spec))
466
Eric Smithb2c7af82008-04-30 02:12:09 +0000467 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
468 def test_float__format__locale(self):
469 # test locale support for __format__ code 'n'
470
471 for i in range(-10, 10):
472 x = 1234567890.0 * (10.0 ** i)
473 self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
474 self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
475
Eric Smith5807c412008-05-11 21:00:57 +0000476 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
477 def test_int__format__locale(self):
478 # test locale support for __format__ code 'n' for integers
479
480 x = 123456789012345678901234567890
481 for i in range(0, 30):
482 self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
483
484 # move to the next integer to test
485 x = x // 10
486
Eric Smithb151a452008-06-24 11:21:04 +0000487 rfmt = ">20n"
488 lfmt = "<20n"
489 cfmt = "^20n"
490 for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
491 self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
492 self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
493 self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
494
Christian Heimes7131fd92008-02-19 14:21:46 +0000495 def test_float__format__(self):
496 # these should be rewritten to use both format(x, spec) and
497 # x.__format__(spec)
498
499 def test(f, format_spec, result):
500 assert type(f) == float
501 assert type(format_spec) == str
502 self.assertEqual(f.__format__(format_spec), result)
503
504 test(0.0, 'f', '0.000000')
505
506 # the default is 'g', except for empty format spec
507 test(0.0, '', '0.0')
508 test(0.01, '', '0.01')
509 test(0.01, 'g', '0.01')
510
Eric Smith2ad79e82008-07-19 00:33:23 +0000511 # test for issue 3411
512 test(1.23, '1', '1.23')
513 test(-1.23, '1', '-1.23')
514 test(1.23, '1g', '1.23')
515 test(-1.23, '1g', '-1.23')
516
Christian Heimes7131fd92008-02-19 14:21:46 +0000517 test( 1.0, ' g', ' 1')
518 test(-1.0, ' g', '-1')
519 test( 1.0, '+g', '+1')
520 test(-1.0, '+g', '-1')
521 test(1.1234e200, 'g', '1.1234e+200')
522 test(1.1234e200, 'G', '1.1234E+200')
523
524
525 test(1.0, 'f', '1.000000')
526
527 test(-1.0, 'f', '-1.000000')
528
529 test( 1.0, ' f', ' 1.000000')
530 test(-1.0, ' f', '-1.000000')
531 test( 1.0, '+f', '+1.000000')
532 test(-1.0, '+f', '-1.000000')
Christian Heimesc3f30c42008-02-22 16:37:40 +0000533 test(1.1234e90, 'f', '1.1234e+90')
Eric Smith22b85b32008-07-17 19:18:29 +0000534 test(1.1234e90, 'F', '1.1234e+90')
Christian Heimes7131fd92008-02-19 14:21:46 +0000535 test(1.1234e200, 'f', '1.1234e+200')
Eric Smith22b85b32008-07-17 19:18:29 +0000536 test(1.1234e200, 'F', '1.1234e+200')
Christian Heimes7131fd92008-02-19 14:21:46 +0000537
Christian Heimesc3f30c42008-02-22 16:37:40 +0000538 test( 1.0, 'e', '1.000000e+00')
539 test(-1.0, 'e', '-1.000000e+00')
540 test( 1.0, 'E', '1.000000E+00')
541 test(-1.0, 'E', '-1.000000E+00')
542 test(1.1234e20, 'e', '1.123400e+20')
543 test(1.1234e20, 'E', '1.123400E+20')
Christian Heimes7131fd92008-02-19 14:21:46 +0000544
Christian Heimesb186d002008-03-18 15:15:01 +0000545 # No format code means use g, but must have a decimal
546 # and a number after the decimal. This is tricky, because
547 # a totaly empty format specifier means something else.
548 # So, just use a sign flag
549 test(1e200, '+g', '+1e+200')
550 test(1e200, '+', '+1.0e+200')
551 test(1.1e200, '+g', '+1.1e+200')
552 test(1.1e200, '+', '+1.1e+200')
553
Christian Heimes7131fd92008-02-19 14:21:46 +0000554 # % formatting
555 test(-1.0, '%', '-100.000000%')
556
557 # format spec must be string
558 self.assertRaises(TypeError, 3.0.__format__, None)
559 self.assertRaises(TypeError, 3.0.__format__, 0)
560
561 # other format specifiers shouldn't work on floats,
562 # in particular int specifiers
563 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
564 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
565 if not format_spec in 'eEfFgGn%':
566 self.assertRaises(ValueError, format, 0.0, format_spec)
567 self.assertRaises(ValueError, format, 1.0, format_spec)
568 self.assertRaises(ValueError, format, -1.0, format_spec)
569 self.assertRaises(ValueError, format, 1e100, format_spec)
570 self.assertRaises(ValueError, format, -1e100, format_spec)
571 self.assertRaises(ValueError, format, 1e-100, format_spec)
572 self.assertRaises(ValueError, format, -1e-100, format_spec)
573
Eric Smithb1ebcc62008-07-15 13:02:41 +0000574 # Alternate formatting is not supported
575 self.assertRaises(ValueError, format, 0.0, '#')
576 self.assertRaises(ValueError, format, 0.0, '#20f')
577
Christian Heimes7131fd92008-02-19 14:21:46 +0000578
Thomas Wouters89f507f2006-12-13 04:49:30 +0000579def test_main():
580 run_unittest(TypesTests)
Neil Schemenauereff72442002-03-24 01:24:54 +0000581
Thomas Wouters89f507f2006-12-13 04:49:30 +0000582if __name__ == '__main__':
583 test_main()