blob: 654bc1171b8a1e7d6b22a99e37ba8fbac13b12af [file] [log] [blame]
Guido van Rossum85f18201992-11-27 22:53:50 +00001# Python test set -- part 6, built-in types
2
Walter Dörwald9b775532007-06-08 14:30:53 +00003from test.test_support import run_unittest
Thomas Wouters89f507f2006-12-13 04:49:30 +00004import unittest
Guido van Rossum85f18201992-11-27 22:53:50 +00005import sys
Guido van Rossum85f18201992-11-27 22:53:50 +00006
Thomas Wouters89f507f2006-12-13 04:49:30 +00007class TypesTests(unittest.TestCase):
Guido van Rossum85f18201992-11-27 22:53:50 +00008
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 def test_truth_values(self):
10 if None: self.fail('None is true instead of false')
11 if 0: self.fail('0 is true instead of false')
Guido van Rossume2a383d2007-01-15 16:59:06 +000012 if 0: self.fail('0L is true instead of false')
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 if 0.0: self.fail('0.0 is true instead of false')
14 if '': self.fail('\'\' is true instead of false')
15 if not 1: self.fail('1 is false instead of true')
Guido van Rossume2a383d2007-01-15 16:59:06 +000016 if not 1: self.fail('1L is false instead of true')
Thomas Wouters89f507f2006-12-13 04:49:30 +000017 if not 1.0: self.fail('1.0 is false instead of true')
18 if not 'x': self.fail('\'x\' is false instead of true')
19 if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
20 def f(): pass
21 class C: pass
22 import sys
23 x = C()
24 if not f: self.fail('f is false instead of true')
25 if not C: self.fail('C is false instead of true')
26 if not sys: self.fail('sys is false instead of true')
27 if not x: self.fail('x is false instead of true')
Guido van Rossum85f18201992-11-27 22:53:50 +000028
Thomas Wouters89f507f2006-12-13 04:49:30 +000029 def test_boolean_ops(self):
30 if 0 or 0: self.fail('0 or 0 is true instead of false')
31 if 1 and 1: pass
32 else: self.fail('1 and 1 is false instead of true')
33 if not 1: self.fail('not 1 is true instead of false')
Neil Schemenauereff72442002-03-24 01:24:54 +000034
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 def test_comparisons(self):
36 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
37 else: self.fail('int comparisons failed')
Guido van Rossume2a383d2007-01-15 16:59:06 +000038 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 else: self.fail('long int comparisons failed')
40 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
41 else: self.fail('float comparisons failed')
42 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
43 else: self.fail('string comparisons failed')
44 if None is None: pass
45 else: self.fail('identity test failed')
Neil Schemenauereff72442002-03-24 01:24:54 +000046
Thomas Wouters89f507f2006-12-13 04:49:30 +000047 def test_float_constructor(self):
48 self.assertRaises(ValueError, float, '')
49 self.assertRaises(ValueError, float, '5\0')
Neil Schemenauereff72442002-03-24 01:24:54 +000050
Thomas Wouters89f507f2006-12-13 04:49:30 +000051 def test_zero_division(self):
52 try: 5.0 / 0.0
53 except ZeroDivisionError: pass
54 else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000055
Thomas Wouters89f507f2006-12-13 04:49:30 +000056 try: 5.0 // 0.0
57 except ZeroDivisionError: pass
58 else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000059
Thomas Wouters89f507f2006-12-13 04:49:30 +000060 try: 5.0 % 0.0
61 except ZeroDivisionError: pass
62 else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000063
Guido van Rossume2a383d2007-01-15 16:59:06 +000064 try: 5 / 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000065 except ZeroDivisionError: pass
Christian Heimes7131fd92008-02-19 14:21:46 +000066 else: self.fail("5 / 0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000067
Guido van Rossume2a383d2007-01-15 16:59:06 +000068 try: 5 // 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 except ZeroDivisionError: pass
Christian Heimes7131fd92008-02-19 14:21:46 +000070 else: self.fail("5 // 0 didn't raise ZeroDivisionError")
Neil Schemenauereff72442002-03-24 01:24:54 +000071
Guido van Rossume2a383d2007-01-15 16:59:06 +000072 try: 5 % 0
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 except ZeroDivisionError: pass
Christian Heimes7131fd92008-02-19 14:21:46 +000074 else: self.fail("5 % 0 didn't raise ZeroDivisionError")
Tim Petersa3c01ce2001-12-04 23:05:10 +000075
Thomas Wouters89f507f2006-12-13 04:49:30 +000076 def test_numeric_types(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000077 if 0 != 0 or 0 != 0.0 or 0 != 0.0: self.fail('mixed comparisons')
78 if 1 != 1 or 1 != 1.0 or 1 != 1.0: self.fail('mixed comparisons')
79 if -1 != -1 or -1 != -1.0 or -1 != -1.0:
Thomas Wouters89f507f2006-12-13 04:49:30 +000080 self.fail('int/long/float value not equal')
81 # calling built-in types without argument must return 0
82 if int() != 0: self.fail('int() does not return 0')
Christian Heimes7131fd92008-02-19 14:21:46 +000083 if int() != 0: self.fail('long() does not return 0')
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 if float() != 0.0: self.fail('float() does not return 0.0')
85 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
86 else: self.fail('int() does not round properly')
Guido van Rossume2a383d2007-01-15 16:59:06 +000087 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 else: self.fail('long() does not round properly')
89 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
90 else: self.fail('float() does not work properly')
Neil Schemenauereff72442002-03-24 01:24:54 +000091
Christian Heimesc3f30c42008-02-22 16:37:40 +000092 def test_float_to_string(self):
93 def test(f, result):
94 self.assertEqual(f.__format__('e'), result)
95 self.assertEqual('%e' % f, result)
96
97 # test all 2 digit exponents, both with __format__ and with
98 # '%' formatting
99 for i in range(-99, 100):
100 test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))
101
102 # test some 3 digit exponents
103 self.assertEqual(1.5e100.__format__('e'), '1.500000e+100')
104 self.assertEqual('%e' % 1.5e100, '1.500000e+100')
105
106 self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
107 self.assertEqual('%e' % 1.5e101, '1.500000e+101')
108
109 self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
110 self.assertEqual('%e' % 1.5e-100, '1.500000e-100')
111
112 self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
113 self.assertEqual('%e' % 1.5e-101, '1.500000e-101')
114
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 def test_normal_integers(self):
116 # Ensure the first 256 integers are shared
117 a = 256
118 b = 128*2
119 if a is not b: self.fail('256 is not shared')
120 if 12 + 24 != 36: self.fail('int op')
121 if 12 + (-24) != -12: self.fail('int op')
122 if (-12) + 24 != 12: self.fail('int op')
123 if (-12) + (-24) != -36: self.fail('int op')
124 if not 12 < 24: self.fail('int op')
125 if not -24 < -12: self.fail('int op')
126 # Test for a particular bug in integer multiply
127 xsize, ysize, zsize = 238, 356, 4
128 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
129 self.fail('int mul commutativity')
130 # And another.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000131 m = -sys.maxsize - 1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132 for divisor in 1, 2, 4, 8, 16, 32:
133 j = m // divisor
134 prod = divisor * j
135 if prod != m:
136 self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
137 if type(prod) is not int:
138 self.fail("expected type(prod) to be int, not %r" %
139 type(prod))
140 # Check for expected * overflow to long.
141 for divisor in 1, 2, 4, 8, 16, 32:
142 j = m // divisor - 1
143 prod = divisor * j
Guido van Rossume2a383d2007-01-15 16:59:06 +0000144 if type(prod) is not int:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000145 self.fail("expected type(%r) to be long, not %r" %
146 (prod, type(prod)))
147 # Check for expected * overflow to long.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000148 m = sys.maxsize
Thomas Wouters89f507f2006-12-13 04:49:30 +0000149 for divisor in 1, 2, 4, 8, 16, 32:
150 j = m // divisor + 1
151 prod = divisor * j
Guido van Rossume2a383d2007-01-15 16:59:06 +0000152 if type(prod) is not int:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 self.fail("expected type(%r) to be long, not %r" %
154 (prod, type(prod)))
Neil Schemenauereff72442002-03-24 01:24:54 +0000155
Thomas Wouters89f507f2006-12-13 04:49:30 +0000156 def test_long_integers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000157 if 12 + 24 != 36: self.fail('long op')
158 if 12 + (-24) != -12: self.fail('long op')
159 if (-12) + 24 != 12: self.fail('long op')
160 if (-12) + (-24) != -36: self.fail('long op')
161 if not 12 < 24: self.fail('long op')
162 if not -24 < -12: self.fail('long op')
Christian Heimesa37d4c62007-12-04 23:02:19 +0000163 x = sys.maxsize
Guido van Rossume2a383d2007-01-15 16:59:06 +0000164 if int(int(x)) != x: self.fail('long op')
165 try: y = int(int(x)+1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000166 except OverflowError: self.fail('long op')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000167 if not isinstance(y, int): self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168 x = -x
Guido van Rossume2a383d2007-01-15 16:59:06 +0000169 if int(int(x)) != x: self.fail('long op')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170 x = x-1
Guido van Rossume2a383d2007-01-15 16:59:06 +0000171 if int(int(x)) != x: self.fail('long op')
172 try: y = int(int(x)-1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000173 except OverflowError: self.fail('long op')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000174 if not isinstance(y, int): self.fail('long op')
Neil Schemenauereff72442002-03-24 01:24:54 +0000175
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176 try: 5 << -5
177 except ValueError: pass
178 else: self.fail('int negative shift <<')
Neil Schemenauereff72442002-03-24 01:24:54 +0000179
Guido van Rossume2a383d2007-01-15 16:59:06 +0000180 try: 5 << -5
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181 except ValueError: pass
182 else: self.fail('long negative shift <<')
Neil Schemenauereff72442002-03-24 01:24:54 +0000183
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184 try: 5 >> -5
185 except ValueError: pass
186 else: self.fail('int negative shift >>')
Guido van Rossum85f18201992-11-27 22:53:50 +0000187
Guido van Rossume2a383d2007-01-15 16:59:06 +0000188 try: 5 >> -5
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 except ValueError: pass
190 else: self.fail('long negative shift >>')
Guido van Rossum85f18201992-11-27 22:53:50 +0000191
Thomas Wouters89f507f2006-12-13 04:49:30 +0000192 def test_floats(self):
193 if 12.0 + 24.0 != 36.0: self.fail('float op')
194 if 12.0 + (-24.0) != -12.0: self.fail('float op')
195 if (-12.0) + 24.0 != 12.0: self.fail('float op')
196 if (-12.0) + (-24.0) != -36.0: self.fail('float op')
197 if not 12.0 < 24.0: self.fail('float op')
198 if not -24.0 < -12.0: self.fail('float op')
Guido van Rossum85f18201992-11-27 22:53:50 +0000199
Thomas Wouters89f507f2006-12-13 04:49:30 +0000200 def test_strings(self):
201 if len('') != 0: self.fail('len(\'\')')
202 if len('a') != 1: self.fail('len(\'a\')')
203 if len('abcdef') != 6: self.fail('len(\'abcdef\')')
204 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
205 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
206 if 0*'abcde' != '': self.fail('string repetition 0*')
207 if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
208 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
209 else: self.fail('in/not in string')
210 x = 'x'*103
211 if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000212
Thomas Wouters89f507f2006-12-13 04:49:30 +0000213 #extended slices for strings
214 a = '0123456789'
215 self.assertEqual(a[::], a)
216 self.assertEqual(a[::2], '02468')
217 self.assertEqual(a[1::2], '13579')
218 self.assertEqual(a[::-1],'9876543210')
219 self.assertEqual(a[::-2], '97531')
220 self.assertEqual(a[3::-2], '31')
221 self.assertEqual(a[-100:100:], a)
222 self.assertEqual(a[100:-100:-1], a[::-1])
Guido van Rossume2a383d2007-01-15 16:59:06 +0000223 self.assertEqual(a[-100:100:2], '02468')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000224
Thomas Wouters89f507f2006-12-13 04:49:30 +0000225 def test_type_function(self):
226 self.assertRaises(TypeError, type, 1, 2)
227 self.assertRaises(TypeError, type, 1, 2, 3, 4)
Guido van Rossum85f18201992-11-27 22:53:50 +0000228
Christian Heimes7131fd92008-02-19 14:21:46 +0000229 def test_int__format__(self):
230 def test(i, format_spec, result):
231 # just make sure I'm not accidentally checking longs
232 assert type(i) == int
233 assert type(format_spec) == str
234 self.assertEqual(i.__format__(format_spec), result)
235
236 test(123456789, 'd', '123456789')
237 test(123456789, 'd', '123456789')
238
239 test(1, 'c', '\01')
240
241 # sign and aligning are interdependent
242 test(1, "-", '1')
243 test(-1, "-", '-1')
244 test(1, "-3", ' 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, " ", ' 1')
251 test(-1, " ", '-1')
252
253 # hex
254 test(3, "x", "3")
255 test(3, "X", "3")
256 test(1234, "x", "4d2")
257 test(-1234, "x", "-4d2")
258 test(1234, "8x", " 4d2")
259 test(-1234, "8x", " -4d2")
260 test(1234, "x", "4d2")
261 test(-1234, "x", "-4d2")
262 test(-3, "x", "-3")
263 test(-3, "X", "-3")
264 test(int('be', 16), "x", "be")
265 test(int('be', 16), "X", "BE")
266 test(-int('be', 16), "x", "-be")
267 test(-int('be', 16), "X", "-BE")
268
269 # octal
270 test(3, "o", "3")
271 test(-3, "o", "-3")
272 test(65, "o", "101")
273 test(-65, "o", "-101")
274 test(1234, "o", "2322")
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
283 # binary
284 test(3, "b", "11")
285 test(-3, "b", "-11")
286 test(1234, "b", "10011010010")
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
295 # make sure these are errors
296
297 # precision disallowed
298 self.assertRaises(ValueError, 3 .__format__, "1.3")
299 # sign not allowed with 'c'
300 self.assertRaises(ValueError, 3 .__format__, "+c")
301 # format spec must be string
302 self.assertRaises(TypeError, 3 .__format__, None)
303 self.assertRaises(TypeError, 3 .__format__, 0)
304
305 # ensure that only int and float type specifiers work
306 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
307 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
308 if not format_spec in 'bcdoxXeEfFgGn%':
309 self.assertRaises(ValueError, 0 .__format__, format_spec)
310 self.assertRaises(ValueError, 1 .__format__, format_spec)
311 self.assertRaises(ValueError, (-1) .__format__, format_spec)
312
313 # ensure that float type specifiers work; format converts
314 # the int to a float
315 for format_spec in 'eEfFgGn%':
316 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
317 self.assertEqual(value.__format__(format_spec),
318 float(value).__format__(format_spec))
319
320 def test_long__format__(self):
321 def test(i, format_spec, result):
322 # make sure we're not accidentally checking ints
323 assert type(i) == int
324 assert type(format_spec) == str
325 self.assertEqual(i.__format__(format_spec), result)
326
327 test(10**100, 'd', '1' + '0' * 100)
328 test(10**100+100, 'd', '1' + '0' * 97 + '100')
329
330 test(123456789, 'd', '123456789')
331 test(123456789, 'd', '123456789')
332
333 # sign and aligning are interdependent
334 test(1, "-", '1')
335 test(-1, "-", '-1')
336 test(1, "-3", ' 1')
337 test(-1, "-3", ' -1')
338 test(1, "+3", ' +1')
339 test(-1, "+3", ' -1')
340 test(1, " 3", ' 1')
341 test(-1, " 3", ' -1')
342 test(1, " ", ' 1')
343 test(-1, " ", '-1')
344
345 test(1, 'c', '\01')
346
347 # hex
348 test(3, "x", "3")
349 test(3, "X", "3")
350 test(1234, "x", "4d2")
351 test(-1234, "x", "-4d2")
352 test(1234, "8x", " 4d2")
353 test(-1234, "8x", " -4d2")
354 test(1234, "x", "4d2")
355 test(-1234, "x", "-4d2")
356 test(-3, "x", "-3")
357 test(-3, "X", "-3")
358
359 # octal
360 test(3, "o", "3")
361 test(-3, "o", "-3")
362 test(65, "o", "101")
363 test(-65, "o", "-101")
364 test(1234, "o", "2322")
365 test(-1234, "o", "-2322")
366 test(1234, "-o", "2322")
367 test(-1234, "-o", "-2322")
368 test(1234, " o", " 2322")
369 test(-1234, " o", "-2322")
370 test(1234, "+o", "+2322")
371 test(-1234, "+o", "-2322")
372
373 # binary
374 test(3, "b", "11")
375 test(-3, "b", "-11")
376 test(1234, "b", "10011010010")
377 test(-1234, "b", "-10011010010")
378 test(1234, "-b", "10011010010")
379 test(-1234, "-b", "-10011010010")
380 test(1234, " b", " 10011010010")
381 test(-1234, " b", "-10011010010")
382 test(1234, "+b", "+10011010010")
383 test(-1234, "+b", "-10011010010")
384
385 # make sure these are errors
386
387 # precision disallowed
388 self.assertRaises(ValueError, 3 .__format__, "1.3")
389 # sign not allowed with 'c'
390 self.assertRaises(ValueError, 3 .__format__, "+c")
391 # format spec must be string
392 self.assertRaises(TypeError, 3 .__format__, None)
393 self.assertRaises(TypeError, 3 .__format__, 0)
394
395 # ensure that only int and float type specifiers work
396 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
397 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
398 if not format_spec in 'bcdoxXeEfFgGn%':
399 self.assertRaises(ValueError, 0 .__format__, format_spec)
400 self.assertRaises(ValueError, 1 .__format__, format_spec)
401 self.assertRaises(ValueError, (-1) .__format__, format_spec)
402
403 # ensure that float type specifiers work; format converts
404 # the long to a float
405 for format_spec in 'eEfFgGn%':
406 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
407 self.assertEqual(value.__format__(format_spec),
408 float(value).__format__(format_spec))
409
410 def test_float__format__(self):
411 # these should be rewritten to use both format(x, spec) and
412 # x.__format__(spec)
413
414 def test(f, format_spec, result):
415 assert type(f) == float
416 assert type(format_spec) == str
417 self.assertEqual(f.__format__(format_spec), result)
418
419 test(0.0, 'f', '0.000000')
420
421 # the default is 'g', except for empty format spec
422 test(0.0, '', '0.0')
423 test(0.01, '', '0.01')
424 test(0.01, 'g', '0.01')
425
426 test( 1.0, ' g', ' 1')
427 test(-1.0, ' g', '-1')
428 test( 1.0, '+g', '+1')
429 test(-1.0, '+g', '-1')
430 test(1.1234e200, 'g', '1.1234e+200')
431 test(1.1234e200, 'G', '1.1234E+200')
432
433
434 test(1.0, 'f', '1.000000')
435
436 test(-1.0, 'f', '-1.000000')
437
438 test( 1.0, ' f', ' 1.000000')
439 test(-1.0, ' f', '-1.000000')
440 test( 1.0, '+f', '+1.000000')
441 test(-1.0, '+f', '-1.000000')
Christian Heimesc3f30c42008-02-22 16:37:40 +0000442 test(1.1234e90, 'f', '1.1234e+90')
443 test(1.1234e90, 'F', '1.1234e+90')
Christian Heimes7131fd92008-02-19 14:21:46 +0000444 test(1.1234e200, 'f', '1.1234e+200')
445 test(1.1234e200, 'F', '1.1234e+200')
446
Christian Heimesc3f30c42008-02-22 16:37:40 +0000447 test( 1.0, 'e', '1.000000e+00')
448 test(-1.0, 'e', '-1.000000e+00')
449 test( 1.0, 'E', '1.000000E+00')
450 test(-1.0, 'E', '-1.000000E+00')
451 test(1.1234e20, 'e', '1.123400e+20')
452 test(1.1234e20, 'E', '1.123400E+20')
Christian Heimes7131fd92008-02-19 14:21:46 +0000453
454 # % formatting
455 test(-1.0, '%', '-100.000000%')
456
457 # format spec must be string
458 self.assertRaises(TypeError, 3.0.__format__, None)
459 self.assertRaises(TypeError, 3.0.__format__, 0)
460
461 # other format specifiers shouldn't work on floats,
462 # in particular int specifiers
463 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
464 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
465 if not format_spec in 'eEfFgGn%':
466 self.assertRaises(ValueError, format, 0.0, format_spec)
467 self.assertRaises(ValueError, format, 1.0, format_spec)
468 self.assertRaises(ValueError, format, -1.0, format_spec)
469 self.assertRaises(ValueError, format, 1e100, format_spec)
470 self.assertRaises(ValueError, format, -1e100, format_spec)
471 self.assertRaises(ValueError, format, 1e-100, format_spec)
472 self.assertRaises(ValueError, format, -1e-100, format_spec)
473
474
Thomas Wouters89f507f2006-12-13 04:49:30 +0000475def test_main():
476 run_unittest(TypesTests)
Neil Schemenauereff72442002-03-24 01:24:54 +0000477
Thomas Wouters89f507f2006-12-13 04:49:30 +0000478if __name__ == '__main__':
479 test_main()