Commit d5046ff1 authored by Georg Brandl's avatar Georg Brandl

Convert test_types to unittest.

parent 06e5c05d
test_types
6. Built-in types
6.1 Truth value testing
6.2 Boolean operations
6.3 Comparisons
6.4 Numeric types (mostly conversions)
6.4.1 32-bit integers
6.4.2 Long integers
6.4.3 Floating point numbers
6.5 Sequence types
6.5.1 Strings
6.5.2 Tuples [see test_tuple.py]
6.5.3 Lists [see test_list.py]
6.6 Mappings == Dictionaries [see test_dict.py]
Buffers
# Python test set -- part 6, built-in types # Python test set -- part 6, built-in types
from test.test_support import * from test.test_support import run_unittest, have_unicode
import unittest
print '6. Built-in types'
print '6.1 Truth value testing'
if None: raise TestFailed, 'None is true instead of false'
if 0: raise TestFailed, '0 is true instead of false'
if 0L: raise TestFailed, '0L is true instead of false'
if 0.0: raise TestFailed, '0.0 is true instead of false'
if '': raise TestFailed, '\'\' is true instead of false'
if not 1: raise TestFailed, '1 is false instead of true'
if not 1L: raise TestFailed, '1L is false instead of true'
if not 1.0: raise TestFailed, '1.0 is false instead of true'
if not 'x': raise TestFailed, '\'x\' is false instead of true'
if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true'
def f(): pass
class C: pass
import sys import sys
x = C()
if not f: raise TestFailed, 'f is false instead of true' class TypesTests(unittest.TestCase):
if not C: raise TestFailed, 'C is false instead of true'
if not sys: raise TestFailed, 'sys is false instead of true' def test_truth_values(self):
if not x: raise TestFailed, 'x is false instead of true' if None: self.fail('None is true instead of false')
if 0: self.fail('0 is true instead of false')
print '6.2 Boolean operations' if 0L: self.fail('0L is true instead of false')
if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' if 0.0: self.fail('0.0 is true instead of false')
if 1 and 1: pass if '': self.fail('\'\' is true instead of false')
else: raise TestFailed, '1 and 1 is false instead of true' if not 1: self.fail('1 is false instead of true')
if not 1: raise TestFailed, 'not 1 is true instead of false' if not 1L: self.fail('1L is false instead of true')
if not 1.0: self.fail('1.0 is false instead of true')
print '6.3 Comparisons' if not 'x': self.fail('\'x\' is false instead of true')
if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
else: raise TestFailed, 'int comparisons failed' def f(): pass
if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass class C: pass
else: raise TestFailed, 'long int comparisons failed' import sys
if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass x = C()
else: raise TestFailed, 'float comparisons failed' if not f: self.fail('f is false instead of true')
if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass if not C: self.fail('C is false instead of true')
else: raise TestFailed, 'string comparisons failed' if not sys: self.fail('sys is false instead of true')
if None is None: pass if not x: self.fail('x is false instead of true')
else: raise TestFailed, 'identity test failed'
def test_boolean_ops(self):
try: float('') if 0 or 0: self.fail('0 or 0 is true instead of false')
except ValueError: pass if 1 and 1: pass
else: raise TestFailed, "float('') didn't raise ValueError" else: self.fail('1 and 1 is false instead of true')
if not 1: self.fail('not 1 is true instead of false')
try: float('5\0')
except ValueError: pass def test_comparisons(self):
else: raise TestFailed, "float('5\0') didn't raise ValueError" if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
else: self.fail('int comparisons failed')
try: 5.0 / 0.0 if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
except ZeroDivisionError: pass else: self.fail('long int comparisons failed')
else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError" if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
else: self.fail('float comparisons failed')
try: 5.0 // 0.0 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
except ZeroDivisionError: pass else: self.fail('string comparisons failed')
else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError" if None is None: pass
else: self.fail('identity test failed')
try: 5.0 % 0.0
except ZeroDivisionError: pass def test_float_constructor(self):
else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError" self.assertRaises(ValueError, float, '')
self.assertRaises(ValueError, float, '5\0')
try: 5 / 0L
except ZeroDivisionError: pass def test_zero_division(self):
else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError" try: 5.0 / 0.0
except ZeroDivisionError: pass
try: 5 // 0L else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
except ZeroDivisionError: pass
else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError" try: 5.0 // 0.0
except ZeroDivisionError: pass
try: 5 % 0L else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
except ZeroDivisionError: pass
else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError" try: 5.0 % 0.0
except ZeroDivisionError: pass
print '6.4 Numeric types (mostly conversions)' else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons'
if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons' try: 5 / 0L
if -1 != -1L or -1 != -1.0 or -1L != -1.0: except ZeroDivisionError: pass
raise TestFailed, 'int/long/float value not equal' else: self.fail("5 / 0L didn't raise ZeroDivisionError")
# calling built-in types without argument must return 0
if int() != 0: raise TestFailed, 'int() does not return 0' try: 5 // 0L
if long() != 0L: raise TestFailed, 'long() does not return 0L' except ZeroDivisionError: pass
if float() != 0.0: raise TestFailed, 'float() does not return 0.0' else: self.fail("5 // 0L didn't raise ZeroDivisionError")
if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
else: raise TestFailed, 'int() does not round properly' try: 5 % 0L
if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass except ZeroDivisionError: pass
else: raise TestFailed, 'long() does not round properly' else: self.fail("5 % 0L didn't raise ZeroDivisionError")
if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
else: raise TestFailed, 'float() does not work properly' def test_numeric_types(self):
print '6.4.1 32-bit integers' if 0 != 0L or 0 != 0.0 or 0L != 0.0: self.fail('mixed comparisons')
# Ensure the first 256 integers are shared if 1 != 1L or 1 != 1.0 or 1L != 1.0: self.fail('mixed comparisons')
a = 256 if -1 != -1L or -1 != -1.0 or -1L != -1.0:
b = 128*2 self.fail('int/long/float value not equal')
if a is not b: raise TestFailed, '256 is not shared' # calling built-in types without argument must return 0
if 12 + 24 != 36: raise TestFailed, 'int op' if int() != 0: self.fail('int() does not return 0')
if 12 + (-24) != -12: raise TestFailed, 'int op' if long() != 0L: self.fail('long() does not return 0L')
if (-12) + 24 != 12: raise TestFailed, 'int op' if float() != 0.0: self.fail('float() does not return 0.0')
if (-12) + (-24) != -36: raise TestFailed, 'int op' if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
if not 12 < 24: raise TestFailed, 'int op' else: self.fail('int() does not round properly')
if not -24 < -12: raise TestFailed, 'int op' if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
# Test for a particular bug in integer multiply else: self.fail('long() does not round properly')
xsize, ysize, zsize = 238, 356, 4 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): else: self.fail('float() does not work properly')
raise TestFailed, 'int mul commutativity'
# And another. def test_normal_integers(self):
m = -sys.maxint - 1 # Ensure the first 256 integers are shared
for divisor in 1, 2, 4, 8, 16, 32: a = 256
j = m // divisor b = 128*2
prod = divisor * j if a is not b: self.fail('256 is not shared')
if prod != m: if 12 + 24 != 36: self.fail('int op')
raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m) if 12 + (-24) != -12: self.fail('int op')
if type(prod) is not int: if (-12) + 24 != 12: self.fail('int op')
raise TestFailed, ("expected type(prod) to be int, not %r" % if (-12) + (-24) != -36: self.fail('int op')
type(prod)) if not 12 < 24: self.fail('int op')
# Check for expected * overflow to long. if not -24 < -12: self.fail('int op')
for divisor in 1, 2, 4, 8, 16, 32: # Test for a particular bug in integer multiply
j = m // divisor - 1 xsize, ysize, zsize = 238, 356, 4
prod = divisor * j if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
if type(prod) is not long: self.fail('int mul commutativity')
raise TestFailed, ("expected type(%r) to be long, not %r" % # And another.
(prod, type(prod))) m = -sys.maxint - 1
# Check for expected * overflow to long. for divisor in 1, 2, 4, 8, 16, 32:
m = sys.maxint j = m // divisor
for divisor in 1, 2, 4, 8, 16, 32: prod = divisor * j
j = m // divisor + 1 if prod != m:
prod = divisor * j self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
if type(prod) is not long: if type(prod) is not int:
raise TestFailed, ("expected type(%r) to be long, not %r" % self.fail("expected type(prod) to be int, not %r" %
(prod, type(prod))) type(prod))
# Check for expected * overflow to long.
print '6.4.2 Long integers' for divisor in 1, 2, 4, 8, 16, 32:
if 12L + 24L != 36L: raise TestFailed, 'long op' j = m // divisor - 1
if 12L + (-24L) != -12L: raise TestFailed, 'long op' prod = divisor * j
if (-12L) + 24L != 12L: raise TestFailed, 'long op' if type(prod) is not long:
if (-12L) + (-24L) != -36L: raise TestFailed, 'long op' self.fail("expected type(%r) to be long, not %r" %
if not 12L < 24L: raise TestFailed, 'long op' (prod, type(prod)))
if not -24L < -12L: raise TestFailed, 'long op' # Check for expected * overflow to long.
x = sys.maxint m = sys.maxint
if int(long(x)) != x: raise TestFailed, 'long op' for divisor in 1, 2, 4, 8, 16, 32:
try: y = int(long(x)+1L) j = m // divisor + 1
except OverflowError: raise TestFailed, 'long op' prod = divisor * j
if not isinstance(y, long): raise TestFailed, 'long op' if type(prod) is not long:
x = -x self.fail("expected type(%r) to be long, not %r" %
if int(long(x)) != x: raise TestFailed, 'long op' (prod, type(prod)))
x = x-1
if int(long(x)) != x: raise TestFailed, 'long op' def test_long_integers(self):
try: y = int(long(x)-1L) if 12L + 24L != 36L: self.fail('long op')
except OverflowError: raise TestFailed, 'long op' if 12L + (-24L) != -12L: self.fail('long op')
if not isinstance(y, long): raise TestFailed, 'long op' if (-12L) + 24L != 12L: self.fail('long op')
if (-12L) + (-24L) != -36L: self.fail('long op')
try: 5 << -5 if not 12L < 24L: self.fail('long op')
except ValueError: pass if not -24L < -12L: self.fail('long op')
else: raise TestFailed, 'int negative shift <<' x = sys.maxint
if int(long(x)) != x: self.fail('long op')
try: 5L << -5L try: y = int(long(x)+1L)
except ValueError: pass except OverflowError: self.fail('long op')
else: raise TestFailed, 'long negative shift <<' if not isinstance(y, long): self.fail('long op')
x = -x
try: 5 >> -5 if int(long(x)) != x: self.fail('long op')
except ValueError: pass x = x-1
else: raise TestFailed, 'int negative shift >>' if int(long(x)) != x: self.fail('long op')
try: y = int(long(x)-1L)
try: 5L >> -5L except OverflowError: self.fail('long op')
except ValueError: pass if not isinstance(y, long): self.fail('long op')
else: raise TestFailed, 'long negative shift >>'
try: 5 << -5
print '6.4.3 Floating point numbers' except ValueError: pass
if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op' else: self.fail('int negative shift <<')
if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op'
if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op' try: 5L << -5L
if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op' except ValueError: pass
if not 12.0 < 24.0: raise TestFailed, 'float op' else: self.fail('long negative shift <<')
if not -24.0 < -12.0: raise TestFailed, 'float op'
try: 5 >> -5
print '6.5 Sequence types' except ValueError: pass
else: self.fail('int negative shift >>')
print '6.5.1 Strings'
if len('') != 0: raise TestFailed, 'len(\'\')' try: 5L >> -5L
if len('a') != 1: raise TestFailed, 'len(\'a\')' except ValueError: pass
if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')' else: self.fail('long negative shift >>')
if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation'
if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3' def test_floats(self):
if 0*'abcde' != '': raise TestFailed, 'string repetition 0*' if 12.0 + 24.0 != 36.0: self.fail('float op')
if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string' if 12.0 + (-24.0) != -12.0: self.fail('float op')
if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass if (-12.0) + 24.0 != 12.0: self.fail('float op')
else: raise TestFailed, 'in/not in string' if (-12.0) + (-24.0) != -36.0: self.fail('float op')
x = 'x'*103 if not 12.0 < 24.0: self.fail('float op')
if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' if not -24.0 < -12.0: self.fail('float op')
#extended slices for strings def test_strings(self):
a = '0123456789' if len('') != 0: self.fail('len(\'\')')
vereq(a[::], a) if len('a') != 1: self.fail('len(\'a\')')
vereq(a[::2], '02468') if len('abcdef') != 6: self.fail('len(\'abcdef\')')
vereq(a[1::2], '13579') if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
vereq(a[::-1],'9876543210') if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
vereq(a[::-2], '97531') if 0*'abcde' != '': self.fail('string repetition 0*')
vereq(a[3::-2], '31') if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
vereq(a[-100:100:], a) if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
vereq(a[100:-100:-1], a[::-1]) else: self.fail('in/not in string')
vereq(a[-100L:100L:2L], '02468') x = 'x'*103
if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
if have_unicode:
a = unicode('0123456789', 'ascii') #extended slices for strings
vereq(a[::], a) a = '0123456789'
vereq(a[::2], unicode('02468', 'ascii')) self.assertEqual(a[::], a)
vereq(a[1::2], unicode('13579', 'ascii')) self.assertEqual(a[::2], '02468')
vereq(a[::-1], unicode('9876543210', 'ascii')) self.assertEqual(a[1::2], '13579')
vereq(a[::-2], unicode('97531', 'ascii')) self.assertEqual(a[::-1],'9876543210')
vereq(a[3::-2], unicode('31', 'ascii')) self.assertEqual(a[::-2], '97531')
vereq(a[-100:100:], a) self.assertEqual(a[3::-2], '31')
vereq(a[100:-100:-1], a[::-1]) self.assertEqual(a[-100:100:], a)
vereq(a[-100L:100L:2L], unicode('02468', 'ascii')) self.assertEqual(a[100:-100:-1], a[::-1])
self.assertEqual(a[-100L:100L:2L], '02468')
print '6.5.2 Tuples [see test_tuple.py]' if have_unicode:
a = unicode('0123456789', 'ascii')
print '6.5.3 Lists [see test_list.py]' self.assertEqual(a[::], a)
self.assertEqual(a[::2], unicode('02468', 'ascii'))
print '6.6 Mappings == Dictionaries [see test_dict.py]' self.assertEqual(a[1::2], unicode('13579', 'ascii'))
self.assertEqual(a[::-1], unicode('9876543210', 'ascii'))
self.assertEqual(a[::-2], unicode('97531', 'ascii'))
try: type(1, 2) self.assertEqual(a[3::-2], unicode('31', 'ascii'))
except TypeError: pass self.assertEqual(a[-100:100:], a)
else: raise TestFailed, 'type(), w/2 args expected TypeError' self.assertEqual(a[100:-100:-1], a[::-1])
self.assertEqual(a[-100L:100L:2L], unicode('02468', 'ascii'))
try: type(1, 2, 3, 4)
except TypeError: pass
else: raise TestFailed, 'type(), w/4 args expected TypeError' def test_type_function(self):
self.assertRaises(TypeError, type, 1, 2)
print 'Buffers' self.assertRaises(TypeError, type, 1, 2, 3, 4)
try: buffer('asdf', -1)
except ValueError: pass def test_buffers(self):
else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" self.assertRaises(ValueError, buffer, 'asdf', -1)
cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1 cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1
try: buffer(None) self.assertRaises(TypeError, buffer, None)
except TypeError: pass
else: raise TestFailed, "buffer(None) should raise TypeError" a = buffer('asdf')
hash(a)
a = buffer('asdf') b = a * 5
hash(a) if a == b:
b = a * 5 self.fail('buffers should not be equal')
if a == b: if str(b) != ('asdf' * 5):
raise TestFailed, 'buffers should not be equal' self.fail('repeated buffer has wrong content')
if str(b) != ('asdf' * 5): if str(a * 0) != '':
raise TestFailed, 'repeated buffer has wrong content' self.fail('repeated buffer zero times has wrong content')
if str(a * 0) != '': if str(a + buffer('def')) != 'asdfdef':
raise TestFailed, 'repeated buffer zero times has wrong content' self.fail('concatenation of buffers yields wrong content')
if str(a + buffer('def')) != 'asdfdef': if str(buffer(a)) != 'asdf':
raise TestFailed, 'concatenation of buffers yields wrong content' self.fail('composing buffers failed')
if str(buffer(a)) != 'asdf': if str(buffer(a, 2)) != 'df':
raise TestFailed, 'composing buffers failed' self.fail('specifying buffer offset failed')
if str(buffer(a, 2)) != 'df': if str(buffer(a, 0, 2)) != 'as':
raise TestFailed, 'specifying buffer offset failed' self.fail('specifying buffer size failed')
if str(buffer(a, 0, 2)) != 'as': if str(buffer(a, 1, 2)) != 'sd':
raise TestFailed, 'specifying buffer size failed' self.fail('specifying buffer offset and size failed')
if str(buffer(a, 1, 2)) != 'sd': self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1)
raise TestFailed, 'specifying buffer offset and size failed' if str(buffer(buffer('asdf', 0, 2), 0)) != 'as':
try: buffer(buffer('asdf', 1), -1) self.fail('composing length-specified buffer failed')
except ValueError: pass if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as':
else: raise TestFailed, "buffer(buffer('asdf', 1), -1) should raise ValueError" self.fail('composing length-specified buffer failed')
if str(buffer(buffer('asdf', 0, 2), 0)) != 'as': if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as':
raise TestFailed, 'composing length-specified buffer failed' self.fail('composing length-specified buffer failed')
if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as': if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's':
raise TestFailed, 'composing length-specified buffer failed' self.fail('composing length-specified buffer failed')
if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as':
raise TestFailed, 'composing length-specified buffer failed' try: a[1] = 'g'
if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's': except TypeError: pass
raise TestFailed, 'composing length-specified buffer failed' else: self.fail("buffer assignment should raise TypeError")
try: a[1] = 'g' try: a[0:1] = 'g'
except TypeError: pass except TypeError: pass
else: raise TestFailed, "buffer assignment should raise TypeError" else: self.fail("buffer slice assignment should raise TypeError")
try: a[0:1] = 'g' # array.array() returns an object that does not implement a char buffer,
except TypeError: pass # something which int() uses for conversion.
else: raise TestFailed, "buffer slice assignment should raise TypeError" import array
try: int(buffer(array.array('c')))
# array.array() returns an object that does not implement a char buffer, except TypeError: pass
# something which int() uses for conversion. else: self.fail("char buffer (at C level) not working")
import array
try: int(buffer(array.array('c'))) def test_main():
except TypeError :pass run_unittest(TypesTests)
else: raise TestFailed, "char buffer (at C level) not working"
if __name__ == '__main__':
test_main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment