1
# Python test set -- part 6, built-in types
3
from test.test_support import run_unittest, have_unicode, run_with_locale
8
class TypesTests(unittest.TestCase):
10
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')
13
if 0L: self.fail('0L is true instead of false')
14
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')
17
if not 1L: self.fail('1L is false instead of true')
18
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')
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')
30
def test_boolean_ops(self):
31
if 0 or 0: self.fail('0 or 0 is true instead of false')
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')
36
def test_comparisons(self):
37
if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
38
else: self.fail('int comparisons failed')
39
if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
40
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')
46
else: self.fail('identity test failed')
48
def test_float_constructor(self):
49
self.assertRaises(ValueError, float, '')
50
self.assertRaises(ValueError, float, '5\0')
52
def test_zero_division(self):
54
except ZeroDivisionError: pass
55
else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
58
except ZeroDivisionError: pass
59
else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
62
except ZeroDivisionError: pass
63
else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
66
except ZeroDivisionError: pass
67
else: self.fail("5 / 0L didn't raise ZeroDivisionError")
70
except ZeroDivisionError: pass
71
else: self.fail("5 // 0L didn't raise ZeroDivisionError")
74
except ZeroDivisionError: pass
75
else: self.fail("5 % 0L didn't raise ZeroDivisionError")
77
def test_numeric_types(self):
78
if 0 != 0L or 0 != 0.0 or 0L != 0.0: self.fail('mixed comparisons')
79
if 1 != 1L or 1 != 1.0 or 1L != 1.0: self.fail('mixed comparisons')
80
if -1 != -1L or -1 != -1.0 or -1L != -1.0:
81
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')
84
if long() != 0L: self.fail('long() does not return 0L')
85
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')
88
if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
89
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')
93
def test_float_to_string(self):
95
self.assertEqual(f.__format__('e'), result)
96
self.assertEqual('%e' % f, result)
98
# test all 2 digit exponents, both with __format__ and with
100
for i in range(-99, 100):
101
test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))
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')
107
self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
108
self.assertEqual('%e' % 1.5e101, '1.500000e+101')
110
self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
111
self.assertEqual('%e' % 1.5e-100, '1.500000e-100')
113
self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
114
self.assertEqual('%e' % 1.5e-101, '1.500000e-101')
116
def test_normal_integers(self):
117
# Ensure the first 256 integers are shared
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')
133
for divisor in 1, 2, 4, 8, 16, 32:
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" %
141
# Check for expected * overflow to long.
142
for divisor in 1, 2, 4, 8, 16, 32:
145
if type(prod) is not long:
146
self.fail("expected type(%r) to be long, not %r" %
148
# Check for expected * overflow to long.
150
for divisor in 1, 2, 4, 8, 16, 32:
153
if type(prod) is not long:
154
self.fail("expected type(%r) to be long, not %r" %
157
def test_long_integers(self):
158
if 12L + 24L != 36L: self.fail('long op')
159
if 12L + (-24L) != -12L: self.fail('long op')
160
if (-12L) + 24L != 12L: self.fail('long op')
161
if (-12L) + (-24L) != -36L: self.fail('long op')
162
if not 12L < 24L: self.fail('long op')
163
if not -24L < -12L: self.fail('long op')
165
if int(long(x)) != x: self.fail('long op')
166
try: y = int(long(x)+1L)
167
except OverflowError: self.fail('long op')
168
if not isinstance(y, long): self.fail('long op')
170
if int(long(x)) != x: self.fail('long op')
172
if int(long(x)) != x: self.fail('long op')
173
try: y = int(long(x)-1L)
174
except OverflowError: self.fail('long op')
175
if not isinstance(y, long): self.fail('long op')
178
except ValueError: pass
179
else: self.fail('int negative shift <<')
182
except ValueError: pass
183
else: self.fail('long negative shift <<')
186
except ValueError: pass
187
else: self.fail('int negative shift >>')
190
except ValueError: pass
191
else: self.fail('long negative shift >>')
193
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')
201
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')
212
if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
214
#extended slices for strings
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])
224
self.assertEqual(a[-100L:100L:2L], '02468')
227
a = unicode('0123456789', 'ascii')
228
self.assertEqual(a[::], a)
229
self.assertEqual(a[::2], unicode('02468', 'ascii'))
230
self.assertEqual(a[1::2], unicode('13579', 'ascii'))
231
self.assertEqual(a[::-1], unicode('9876543210', 'ascii'))
232
self.assertEqual(a[::-2], unicode('97531', 'ascii'))
233
self.assertEqual(a[3::-2], unicode('31', 'ascii'))
234
self.assertEqual(a[-100:100:], a)
235
self.assertEqual(a[100:-100:-1], a[::-1])
236
self.assertEqual(a[-100L:100L:2L], unicode('02468', 'ascii'))
239
def test_type_function(self):
240
self.assertRaises(TypeError, type, 1, 2)
241
self.assertRaises(TypeError, type, 1, 2, 3, 4)
243
def test_buffers(self):
244
self.assertRaises(ValueError, buffer, 'asdf', -1)
245
cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1
247
self.assertRaises(TypeError, buffer, None)
253
self.fail('buffers should not be equal')
254
if str(b) != ('asdf' * 5):
255
self.fail('repeated buffer has wrong content')
257
self.fail('repeated buffer zero times has wrong content')
258
if str(a + buffer('def')) != 'asdfdef':
259
self.fail('concatenation of buffers yields wrong content')
260
if str(buffer(a)) != 'asdf':
261
self.fail('composing buffers failed')
262
if str(buffer(a, 2)) != 'df':
263
self.fail('specifying buffer offset failed')
264
if str(buffer(a, 0, 2)) != 'as':
265
self.fail('specifying buffer size failed')
266
if str(buffer(a, 1, 2)) != 'sd':
267
self.fail('specifying buffer offset and size failed')
268
self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1)
269
if str(buffer(buffer('asdf', 0, 2), 0)) != 'as':
270
self.fail('composing length-specified buffer failed')
271
if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as':
272
self.fail('composing length-specified buffer failed')
273
if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as':
274
self.fail('composing length-specified buffer failed')
275
if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's':
276
self.fail('composing length-specified buffer failed')
279
except TypeError: pass
280
else: self.fail("buffer assignment should raise TypeError")
283
except TypeError: pass
284
else: self.fail("buffer slice assignment should raise TypeError")
286
# array.array() returns an object that does not implement a char buffer,
287
# something which int() uses for conversion.
289
try: int(buffer(array.array('c')))
290
except TypeError: pass
291
else: self.fail("char buffer (at C level) not working")
293
def test_int__format__(self):
294
def test(i, format_spec, result):
295
# just make sure I'm not accidentally checking longs
296
assert type(i) == int
297
assert type(format_spec) == str
298
self.assertEqual(i.__format__(format_spec), result)
299
self.assertEqual(i.__format__(unicode(format_spec)), result)
301
test(123456789, 'd', '123456789')
302
test(123456789, 'd', '123456789')
306
# sign and aligning are interdependent
310
test(-1, "-3", ' -1')
312
test(-1, "+3", ' -1')
314
test(-1, " 3", ' -1')
321
test(1234, "x", "4d2")
322
test(-1234, "x", "-4d2")
323
test(1234, "8x", " 4d2")
324
test(-1234, "8x", " -4d2")
325
test(1234, "x", "4d2")
326
test(-1234, "x", "-4d2")
329
test(int('be', 16), "x", "be")
330
test(int('be', 16), "X", "BE")
331
test(-int('be', 16), "x", "-be")
332
test(-int('be', 16), "X", "-BE")
338
test(-65, "o", "-101")
339
test(1234, "o", "2322")
340
test(-1234, "o", "-2322")
341
test(1234, "-o", "2322")
342
test(-1234, "-o", "-2322")
343
test(1234, " o", " 2322")
344
test(-1234, " o", "-2322")
345
test(1234, "+o", "+2322")
346
test(-1234, "+o", "-2322")
351
test(1234, "b", "10011010010")
352
test(-1234, "b", "-10011010010")
353
test(1234, "-b", "10011010010")
354
test(-1234, "-b", "-10011010010")
355
test(1234, " b", " 10011010010")
356
test(-1234, " b", "-10011010010")
357
test(1234, "+b", "+10011010010")
358
test(-1234, "+b", "-10011010010")
360
# alternate (#) formatting
362
test(0, "-#b", '0b0')
363
test(1, "-#b", '0b1')
364
test(-1, "-#b", '-0b1')
365
test(-1, "-#5b", ' -0b1')
366
test(1, "+#5b", ' +0b1')
367
test(100, "+#b", '+0b1100100')
368
test(100, "#012b", '0b0001100100')
369
test(-100, "#012b", '-0b001100100')
372
test(0, "-#o", '0o0')
373
test(1, "-#o", '0o1')
374
test(-1, "-#o", '-0o1')
375
test(-1, "-#5o", ' -0o1')
376
test(1, "+#5o", ' +0o1')
377
test(100, "+#o", '+0o144')
378
test(100, "#012o", '0o0000000144')
379
test(-100, "#012o", '-0o000000144')
382
test(0, "-#x", '0x0')
383
test(1, "-#x", '0x1')
384
test(-1, "-#x", '-0x1')
385
test(-1, "-#5x", ' -0x1')
386
test(1, "+#5x", ' +0x1')
387
test(100, "+#x", '+0x64')
388
test(100, "#012x", '0x0000000064')
389
test(-100, "#012x", '-0x000000064')
390
test(123456, "#012x", '0x000001e240')
391
test(-123456, "#012x", '-0x00001e240')
394
test(0, "-#X", '0X0')
395
test(1, "-#X", '0X1')
396
test(-1, "-#X", '-0X1')
397
test(-1, "-#5X", ' -0X1')
398
test(1, "+#5X", ' +0X1')
399
test(100, "+#X", '+0X64')
400
test(100, "#012X", '0X0000000064')
401
test(-100, "#012X", '-0X000000064')
402
test(123456, "#012X", '0X000001E240')
403
test(-123456, "#012X", '-0X00001E240')
405
# make sure these are errors
407
# precision disallowed
408
self.assertRaises(ValueError, 3 .__format__, "1.3")
409
# sign not allowed with 'c'
410
self.assertRaises(ValueError, 3 .__format__, "+c")
411
# format spec must be string
412
self.assertRaises(TypeError, 3 .__format__, None)
413
self.assertRaises(TypeError, 3 .__format__, 0)
415
# ensure that only int and float type specifiers work
416
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
417
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
418
if not format_spec in 'bcdoxXeEfFgGn%':
419
self.assertRaises(ValueError, 0 .__format__, format_spec)
420
self.assertRaises(ValueError, 1 .__format__, format_spec)
421
self.assertRaises(ValueError, (-1) .__format__, format_spec)
423
# ensure that float type specifiers work; format converts
425
for format_spec in 'eEfFgG%':
426
for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
427
self.assertEqual(value.__format__(format_spec),
428
float(value).__format__(format_spec))
430
def test_long__format__(self):
431
def test(i, format_spec, result):
432
# make sure we're not accidentally checking ints
433
assert type(i) == long
434
assert type(format_spec) == str
435
self.assertEqual(i.__format__(format_spec), result)
436
self.assertEqual(i.__format__(unicode(format_spec)), result)
438
test(10**100, 'd', '1' + '0' * 100)
439
test(10**100+100, 'd', '1' + '0' * 97 + '100')
441
test(123456789L, 'd', '123456789')
442
test(123456789L, 'd', '123456789')
444
# sign and aligning are interdependent
448
test(-1L, "-3", ' -1')
449
test(1L, "+3", ' +1')
450
test(-1L, "+3", ' -1')
452
test(-1L, " 3", ' -1')
461
test(1234L, "x", "4d2")
462
test(-1234L, "x", "-4d2")
463
test(1234L, "8x", " 4d2")
464
test(-1234L, "8x", " -4d2")
465
test(1234L, "x", "4d2")
466
test(-1234L, "x", "-4d2")
469
test(long('be', 16), "x", "be")
470
test(long('be', 16), "X", "BE")
471
test(-long('be', 16), "x", "-be")
472
test(-long('be', 16), "X", "-BE")
477
test(65L, "o", "101")
478
test(-65L, "o", "-101")
479
test(1234L, "o", "2322")
480
test(-1234L, "o", "-2322")
481
test(1234L, "-o", "2322")
482
test(-1234L, "-o", "-2322")
483
test(1234L, " o", " 2322")
484
test(-1234L, " o", "-2322")
485
test(1234L, "+o", "+2322")
486
test(-1234L, "+o", "-2322")
490
test(-3L, "b", "-11")
491
test(1234L, "b", "10011010010")
492
test(-1234L, "b", "-10011010010")
493
test(1234L, "-b", "10011010010")
494
test(-1234L, "-b", "-10011010010")
495
test(1234L, " b", " 10011010010")
496
test(-1234L, " b", "-10011010010")
497
test(1234L, "+b", "+10011010010")
498
test(-1234L, "+b", "-10011010010")
500
# make sure these are errors
502
# precision disallowed
503
self.assertRaises(ValueError, 3L .__format__, "1.3")
504
# sign not allowed with 'c'
505
self.assertRaises(ValueError, 3L .__format__, "+c")
506
# format spec must be string
507
self.assertRaises(TypeError, 3L .__format__, None)
508
self.assertRaises(TypeError, 3L .__format__, 0)
509
# alternate specifier in wrong place
510
self.assertRaises(ValueError, 1L .__format__, "#+5x")
511
self.assertRaises(ValueError, 1L .__format__, "+5#x")
513
# ensure that only int and float type specifiers work
514
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
515
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
516
if not format_spec in 'bcdoxXeEfFgGn%':
517
self.assertRaises(ValueError, 0L .__format__, format_spec)
518
self.assertRaises(ValueError, 1L .__format__, format_spec)
519
self.assertRaises(ValueError, (-1L) .__format__, format_spec)
521
# ensure that float type specifiers work; format converts
522
# the long to a float
523
for format_spec in 'eEfFgG%':
524
for value in [0L, 1L, -1L, 100L, -100L, 1234567890L, -1234567890L]:
525
self.assertEqual(value.__format__(format_spec),
526
float(value).__format__(format_spec))
528
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
529
def test_float__format__locale(self):
530
# test locale support for __format__ code 'n'
532
for i in range(-10, 10):
533
x = 1234567890.0 * (10.0 ** i)
534
self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
535
self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
537
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
538
def test_int__format__locale(self):
539
# test locale support for __format__ code 'n' for integers
541
x = 123456789012345678901234567890
542
for i in range(0, 30):
543
self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
545
# move to the next integer to test
551
for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
552
self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
553
self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
554
self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
556
def test_float__format__(self):
557
# these should be rewritten to use both format(x, spec) and
560
def test(f, format_spec, result):
561
assert type(f) == float
562
assert type(format_spec) == str
563
self.assertEqual(f.__format__(format_spec), result)
564
self.assertEqual(f.__format__(unicode(format_spec)), result)
566
test(0.0, 'f', '0.000000')
568
# the default is 'g', except for empty format spec
570
test(0.01, '', '0.01')
571
test(0.01, 'g', '0.01')
573
# test for issue 3411
574
test(1.23, '1', '1.23')
575
test(-1.23, '1', '-1.23')
576
test(1.23, '1g', '1.23')
577
test(-1.23, '1g', '-1.23')
579
test( 1.0, ' g', ' 1')
580
test(-1.0, ' g', '-1')
581
test( 1.0, '+g', '+1')
582
test(-1.0, '+g', '-1')
583
test(1.1234e200, 'g', '1.1234e+200')
584
test(1.1234e200, 'G', '1.1234E+200')
587
test(1.0, 'f', '1.000000')
589
test(-1.0, 'f', '-1.000000')
591
test( 1.0, ' f', ' 1.000000')
592
test(-1.0, ' f', '-1.000000')
593
test( 1.0, '+f', '+1.000000')
594
test(-1.0, '+f', '-1.000000')
595
test(1.1234e90, 'f', '1.1234e+90')
596
test(1.1234e90, 'F', '1.1234e+90')
597
test(1.1234e200, 'f', '1.1234e+200')
598
test(1.1234e200, 'F', '1.1234e+200')
600
test( 1.0, 'e', '1.000000e+00')
601
test(-1.0, 'e', '-1.000000e+00')
602
test( 1.0, 'E', '1.000000E+00')
603
test(-1.0, 'E', '-1.000000E+00')
604
test(1.1234e20, 'e', '1.123400e+20')
605
test(1.1234e20, 'E', '1.123400E+20')
607
# No format code means use g, but must have a decimal
608
# and a number after the decimal. This is tricky, because
609
# a totaly empty format specifier means something else.
610
# So, just use a sign flag
611
test(1e200, '+g', '+1e+200')
612
test(1e200, '+', '+1.0e+200')
613
test(1.1e200, '+g', '+1.1e+200')
614
test(1.1e200, '+', '+1.1e+200')
617
test(-1.0, '%', '-100.000000%')
619
# format spec must be string
620
self.assertRaises(TypeError, 3.0.__format__, None)
621
self.assertRaises(TypeError, 3.0.__format__, 0)
623
# other format specifiers shouldn't work on floats,
624
# in particular int specifiers
625
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
626
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
627
if not format_spec in 'eEfFgGn%':
628
self.assertRaises(ValueError, format, 0.0, format_spec)
629
self.assertRaises(ValueError, format, 1.0, format_spec)
630
self.assertRaises(ValueError, format, -1.0, format_spec)
631
self.assertRaises(ValueError, format, 1e100, format_spec)
632
self.assertRaises(ValueError, format, -1e100, format_spec)
633
self.assertRaises(ValueError, format, 1e-100, format_spec)
634
self.assertRaises(ValueError, format, -1e-100, format_spec)
636
# Alternate formatting is not supported
637
self.assertRaises(ValueError, format, 0.0, '#')
638
self.assertRaises(ValueError, format, 0.0, '#20f')
642
run_unittest(TypesTests)
644
if __name__ == '__main__':