Commit e0aa8c82 authored by YOU's avatar YOU Committed by Dylan Trotter

Add list, slice tests (#194)

parent 39954183
......@@ -78,11 +78,10 @@ STDLIB_TESTS := \
re_tests \
sys_test \
tempfile_test \
test/seq_tests \
test/test_tuple \
test/mapping_tests \
test/test_dict \
test/string_tests \
test/test_list \
test/test_slice \
test/test_string \
threading_test \
time_test \
......
This diff is collapsed.
......@@ -198,7 +198,7 @@ class BasicTestMappingProtocol(unittest.TestCase):
self.assertRaises((TypeError, AttributeError), d.update, 42)
outerself = self
class SimpleUserDict:
class SimpleUserDict(object):
def __init__(self):
self.d = outerself.reference
def keys(self):
......@@ -220,16 +220,16 @@ class BasicTestMappingProtocol(unittest.TestCase):
class Exc(Exception): pass
d = self._empty_mapping()
class FailingUserDict:
class FailingUserDict(object):
def keys(self):
raise Exc
self.assertRaises(Exc, d.update, FailingUserDict())
d.clear()
class FailingUserDict:
class FailingUserDict(object):
def keys(self):
class BogonIter:
class BogonIter(object):
def __init__(self):
self.i = 1
def __iter__(self):
......@@ -244,9 +244,9 @@ class BasicTestMappingProtocol(unittest.TestCase):
return key
self.assertRaises(Exc, d.update, FailingUserDict())
class FailingUserDict:
class FailingUserDict(object):
def keys(self):
class BogonIter:
class BogonIter(object):
def __init__(self):
self.i = ord('a')
def __iter__(self):
......@@ -425,7 +425,7 @@ class TestMappingProtocol(BasicTestMappingProtocol):
d.update(self._full_mapping({1:2, 3:4, 5:6}).iteritems())
self.assertEqual(d, {1:2, 2:4, 3:4, 5:6})
class SimpleUserDict:
class SimpleUserDict(object):
def __init__(self):
self.d = {1:1, 2:2, 3:3}
def keys(self):
......
import sys
import unittest
from test import test_support, list_tests
class ListTest(list_tests.CommonTest):
type2test = list
def test_basic(self):
self.assertEqual(list([]), [])
l0_3 = [0, 1, 2, 3]
l0_3_bis = list(l0_3)
self.assertEqual(l0_3, l0_3_bis)
self.assertTrue(l0_3 is not l0_3_bis)
self.assertEqual(list(()), [])
self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
self.assertEqual(list(''), [])
self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
if sys.maxsize == 0x7fffffff:
# This test can currently only work on 32-bit machines.
# XXX If/when PySequence_Length() returns a ssize_t, it should be
# XXX re-enabled.
# Verify clearing of bug #556025.
# This assumes that the max data size (sys.maxint) == max
# address size this also assumes that the address size is at
# least 4 bytes with 8 byte addresses, the bug is not well
# tested
#
# Note: This test is expected to SEGV under Cygwin 1.3.12 or
# earlier due to a newlib bug. See the following mailing list
# thread for the details:
# http://sources.redhat.com/ml/newlib/2002/msg00369.html
self.assertRaises(MemoryError, list, xrange(sys.maxint // 2))
# This code used to segfault in Py2.4a3
x = []
x.extend(-y for y in x)
self.assertEqual(x, [])
def test_truth(self):
super(ListTest, self).test_truth()
self.assertTrue(not [])
self.assertTrue([42])
def test_identity(self):
self.assertTrue([] is not [])
def test_len(self):
super(ListTest, self).test_len()
self.assertEqual(len([]), 0)
self.assertEqual(len([0]), 1)
self.assertEqual(len([0, 1, 2]), 3)
@unittest.expectedFailure
def test_overflow(self):
lst = [4, 5, 6, 7]
n = int((sys.maxsize*2+2) // len(lst))
def mul(a, b): return a * b
def imul(a, b): a *= b
self.assertRaises((MemoryError, OverflowError), mul, lst, n)
self.assertRaises((MemoryError, OverflowError), imul, lst, n)
def test_main(verbose=None):
test_support.run_unittest(ListTest)
# verify reference counting
# import sys
# if verbose and hasattr(sys, "gettotalrefcount"):
# import gc
# counts = [None] * 5
# for i in xrange(len(counts)):
# test_support.run_unittest(ListTest)
# gc.collect()
# counts[i] = sys.gettotalrefcount()
# print counts
if __name__ == "__main__":
test_main(verbose=True)
# tests for slice objects; in particular the indices method.
import unittest
import weakref
# from cPickle import loads, dumps
from test import test_support
import sys
class SliceTest(unittest.TestCase):
def test_constructor(self):
self.assertRaises(TypeError, slice)
self.assertRaises(TypeError, slice, 1, 2, 3, 4)
def test_repr(self):
self.assertEqual(repr(slice(1, 2, 3)), "slice(1, 2, 3)")
def test_hash(self):
# Verify clearing of SF bug #800796
self.assertRaises(TypeError, hash, slice(5))
with self.assertRaises(TypeError):
slice(5).__hash__()
@unittest.expectedFailure
def test_cmp(self):
s1 = slice(1, 2, 3)
s2 = slice(1, 2, 3)
s3 = slice(1, 2, 4)
self.assertEqual(s1, s2)
self.assertNotEqual(s1, s3)
class Exc(Exception):
pass
class BadCmp(object):
def __eq__(self, other):
raise Exc
__hash__ = None # Silence Py3k warning
s1 = slice(BadCmp())
s2 = slice(BadCmp())
self.assertRaises(Exc, cmp, s1, s2)
self.assertEqual(s1, s1)
s1 = slice(1, BadCmp())
s2 = slice(1, BadCmp())
self.assertEqual(s1, s1)
self.assertRaises(Exc, cmp, s1, s2)
s1 = slice(1, 2, BadCmp())
s2 = slice(1, 2, BadCmp())
self.assertEqual(s1, s1)
self.assertRaises(Exc, cmp, s1, s2)
def test_members(self):
s = slice(1)
self.assertEqual(s.start, None)
self.assertEqual(s.stop, 1)
self.assertEqual(s.step, None)
s = slice(1, 2)
self.assertEqual(s.start, 1)
self.assertEqual(s.stop, 2)
self.assertEqual(s.step, None)
s = slice(1, 2, 3)
self.assertEqual(s.start, 1)
self.assertEqual(s.stop, 2)
self.assertEqual(s.step, 3)
class AnyClass(object):
pass
obj = AnyClass()
s = slice(obj)
self.assertTrue(s.stop is obj)
@unittest.expectedFailure
def test_indices(self):
self.assertEqual(slice(None ).indices(10), (0, 10, 1))
self.assertEqual(slice(None, None, 2).indices(10), (0, 10, 2))
self.assertEqual(slice(1, None, 2).indices(10), (1, 10, 2))
self.assertEqual(slice(None, None, -1).indices(10), (9, -1, -1))
self.assertEqual(slice(None, None, -2).indices(10), (9, -1, -2))
self.assertEqual(slice(3, None, -2).indices(10), (3, -1, -2))
# issue 3004 tests
self.assertEqual(slice(None, -9).indices(10), (0, 1, 1))
self.assertEqual(slice(None, -10).indices(10), (0, 0, 1))
self.assertEqual(slice(None, -11).indices(10), (0, 0, 1))
self.assertEqual(slice(None, -10, -1).indices(10), (9, 0, -1))
self.assertEqual(slice(None, -11, -1).indices(10), (9, -1, -1))
self.assertEqual(slice(None, -12, -1).indices(10), (9, -1, -1))
self.assertEqual(slice(None, 9).indices(10), (0, 9, 1))
self.assertEqual(slice(None, 10).indices(10), (0, 10, 1))
self.assertEqual(slice(None, 11).indices(10), (0, 10, 1))
self.assertEqual(slice(None, 8, -1).indices(10), (9, 8, -1))
self.assertEqual(slice(None, 9, -1).indices(10), (9, 9, -1))
self.assertEqual(slice(None, 10, -1).indices(10), (9, 9, -1))
self.assertEqual(
slice(-100, 100 ).indices(10),
slice(None).indices(10)
)
self.assertEqual(
slice(100, -100, -1).indices(10),
slice(None, None, -1).indices(10)
)
self.assertEqual(slice(-100L, 100L, 2L).indices(10), (0, 10, 2))
self.assertEqual(range(10)[::sys.maxint - 1], [0])
self.assertRaises(OverflowError, slice(None).indices, 1L<<100)
@unittest.expectedFailure
def test_setslice_without_getslice(self):
tmp = []
class X(object):
def __setslice__(self, i, j, k):
tmp.append((i, j, k))
x = X()
with test_support.check_py3k_warnings():
x[1:2] = 42
self.assertEqual(tmp, [(1, 2, 42)])
# def test_pickle(self):
# s = slice(10, 20, 3)
# for protocol in (0,1,2):
# t = loads(dumps(s, protocol))
# self.assertEqual(s, t)
# self.assertEqual(s.indices(15), t.indices(15))
# self.assertNotEqual(id(s), id(t))
@unittest.expectedFailure
def test_cycle(self):
class myobj(object): pass
o = myobj()
o.s = slice(o)
w = weakref.ref(o)
o = None
test_support.gc_collect()
self.assertIsNone(w())
def test_main():
test_support.run_unittest(SliceTest)
if __name__ == "__main__":
test_main()
......@@ -45,7 +45,7 @@ SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
"""
__all__ = ['TestResult', 'TestCase', 'TestSuite',
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
'TextTestRunner', 'TestLoader', 'FunctionTestCase',
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
'expectedFailure', 'TextTestResult', 'installHandler',
'registerResult', 'removeResult', 'removeHandler']
......
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