summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2004-05-31 16:29:04 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2004-05-31 16:29:04 (GMT)
commit0a6d0ff8d9ad27d66eb8195b9366e5b49dbb69b6 (patch)
treeac27098581c6e50a7776b6853950670aaa48473f
parentcbd0b365c1f27d390827e74a88a96f3a13034e0e (diff)
downloadcpython-0a6d0ff8d9ad27d66eb8195b9366e5b49dbb69b6.zip
cpython-0a6d0ff8d9ad27d66eb8195b9366e5b49dbb69b6.tar.gz
cpython-0a6d0ff8d9ad27d66eb8195b9366e5b49dbb69b6.tar.bz2
Port the dictionary tests from test_types.py to unittest. Collect as much
mapping tests as possible in mapping_test.py and reuse the tests in test_dict.py, test_userdict.py, test_weakref.py, test_os.py and test_shelve.py. From SF patch #736962.
-rw-r--r--Lib/test/mapping_tests.py672
-rw-r--r--Lib/test/output/test_types2
-rw-r--r--Lib/test/test_os.py4
-rw-r--r--Lib/test/test_shelve.py6
-rw-r--r--Lib/test/test_types.py228
-rw-r--r--Lib/test/test_userdict.py148
-rw-r--r--Lib/test/test_weakref.py6
7 files changed, 705 insertions, 361 deletions
diff --git a/Lib/test/mapping_tests.py b/Lib/test/mapping_tests.py
new file mode 100644
index 0000000..c0b70a7
--- /dev/null
+++ b/Lib/test/mapping_tests.py
@@ -0,0 +1,672 @@
+# tests common to dict and UserDict
+import unittest
+import UserDict
+
+
+class BasicTestMappingProtocol(unittest.TestCase):
+ # This base class can be used to check that an object conforms to the
+ # mapping protocol
+
+ # Functions that can be useful to override to adapt to dictionary
+ # semantics
+ _tested_class = None # which class is being tested (overwrite in subclasses)
+
+ def _reference(self):
+ """Return a dictionary of values which are invariant by storage
+ in the object under test."""
+ return {1:2, "key1":"value1", "key2":(1,2,3)}
+ def _empty_mapping(self):
+ """Return an empty mapping object"""
+ return self._tested_class()
+ def _full_mapping(self, data):
+ """Return a mapping object with the value contained in data
+ dictionary"""
+ x = self._empty_mapping()
+ for key, value in data.items():
+ x[key] = value
+ return x
+
+ def __init__(self, *args, **kw):
+ unittest.TestCase.__init__(self, *args, **kw)
+ self.reference = self._reference().copy()
+
+ # A (key, value) pair not in the mapping
+ key, value = self.reference.popitem()
+ self.other = {key:value}
+
+ # A (key, value) pair in the mapping
+ key, value = self.reference.popitem()
+ self.inmapping = {key:value}
+ self.reference[key] = value
+
+ def test_read(self):
+ # Test for read only operations on mapping
+ p = self._empty_mapping()
+ p1 = dict(p) #workaround for singleton objects
+ d = self._full_mapping(self.reference)
+ if d is p:
+ p = p1
+ #Indexing
+ for key, value in self.reference.items():
+ self.assertEqual(d[key], value)
+ knownkey = self.other.keys()[0]
+ self.failUnlessRaises(KeyError, lambda:d[knownkey])
+ #len
+ self.assertEqual(len(p), 0)
+ self.assertEqual(len(d), len(self.reference))
+ #has_key
+ for k in self.reference:
+ self.assert_(d.has_key(k))
+ self.assert_(k in d)
+ for k in self.other:
+ self.failIf(d.has_key(k))
+ self.failIf(k in d)
+ #cmp
+ self.assertEqual(cmp(p,p), 0)
+ self.assertEqual(cmp(d,d), 0)
+ self.assertEqual(cmp(p,d), -1)
+ self.assertEqual(cmp(d,p), 1)
+ #__non__zero__
+ if p: self.fail("Empty mapping must compare to False")
+ if not d: self.fail("Full mapping must compare to True")
+ # keys(), items(), iterkeys() ...
+ def check_iterandlist(iter, lst, ref):
+ self.assert_(hasattr(iter, 'next'))
+ self.assert_(hasattr(iter, '__iter__'))
+ x = list(iter)
+ self.assert_(set(x)==set(lst)==set(ref))
+ check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys())
+ check_iterandlist(iter(d), d.keys(), self.reference.keys())
+ check_iterandlist(d.itervalues(), d.values(), self.reference.values())
+ check_iterandlist(d.iteritems(), d.items(), self.reference.items())
+ #get
+ key, value = d.iteritems().next()
+ knownkey, knownvalue = self.other.iteritems().next()
+ self.assertEqual(d.get(key, knownvalue), value)
+ self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
+ self.failIf(knownkey in d)
+
+ def test_write(self):
+ # Test for write operations on mapping
+ p = self._empty_mapping()
+ #Indexing
+ for key, value in self.reference.items():
+ p[key] = value
+ self.assertEqual(p[key], value)
+ for key in self.reference.keys():
+ del p[key]
+ self.failUnlessRaises(KeyError, lambda:p[key])
+ p = self._empty_mapping()
+ #update
+ p.update(self.reference)
+ self.assertEqual(dict(p), self.reference)
+ items = p.items()
+ p = self._empty_mapping()
+ p.update(items)
+ self.assertEqual(dict(p), self.reference)
+ d = self._full_mapping(self.reference)
+ #setdefault
+ key, value = d.iteritems().next()
+ knownkey, knownvalue = self.other.iteritems().next()
+ self.assertEqual(d.setdefault(key, knownvalue), value)
+ self.assertEqual(d[key], value)
+ self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
+ self.assertEqual(d[knownkey], knownvalue)
+ #pop
+ self.assertEqual(d.pop(knownkey), knownvalue)
+ self.failIf(knownkey in d)
+ self.assertRaises(KeyError, d.pop, knownkey)
+ default = 909
+ d[knownkey] = knownvalue
+ self.assertEqual(d.pop(knownkey, default), knownvalue)
+ self.failIf(knownkey in d)
+ self.assertEqual(d.pop(knownkey, default), default)
+ #popitem
+ key, value = d.popitem()
+ self.failIf(key in d)
+ self.assertEqual(value, self.reference[key])
+ p=self._empty_mapping()
+ self.assertRaises(KeyError, p.popitem)
+
+ def test_constructor(self):
+ self.assertEqual(self._empty_mapping(), self._empty_mapping())
+
+ def test_bool(self):
+ self.assert_(not self._empty_mapping())
+ self.assert_(self.reference)
+ self.assert_(bool(self._empty_mapping()) is False)
+ self.assert_(bool(self.reference) is True)
+
+ def test_keys(self):
+ d = self._empty_mapping()
+ self.assertEqual(d.keys(), [])
+ d = self.reference
+ self.assert_(self.inmapping.keys()[0] in d.keys())
+ self.assert_(self.other.keys()[0] not in d.keys())
+ self.assertRaises(TypeError, d.keys, None)
+
+ def test_values(self):
+ d = self._empty_mapping()
+ self.assertEqual(d.values(), [])
+
+ self.assertRaises(TypeError, d.values, None)
+
+ def test_items(self):
+ d = self._empty_mapping()
+ self.assertEqual(d.items(), [])
+
+ self.assertRaises(TypeError, d.items, None)
+
+ def test_len(self):
+ d = self._empty_mapping()
+ self.assertEqual(len(d), 0)
+
+ def test_getitem(self):
+ d = self.reference
+ self.assertEqual(d[self.inmapping.keys()[0]], self.inmapping.values()[0])
+
+ self.assertRaises(TypeError, d.__getitem__)
+
+ def test_update(self):
+ # mapping argument
+ d = self._empty_mapping()
+ d.update(self.other)
+ self.assertEqual(d.items(), self.other.items())
+
+ # No argument
+ d = self._empty_mapping()
+ d.update()
+ self.assertEqual(d, self._empty_mapping())
+
+ # item sequence
+ d = self._empty_mapping()
+ d.update(self.other.items())
+ self.assertEqual(d.items(), self.other.items())
+
+ # Iterator
+ d = self._empty_mapping()
+ d.update(self.other.iteritems())
+ self.assertEqual(d.items(), self.other.items())
+
+ # FIXME: Doesn't work with UserDict
+ # self.assertRaises((TypeError, AttributeError), d.update, None)
+ self.assertRaises((TypeError, AttributeError), d.update, 42)
+
+ outerself = self
+ class SimpleUserDict:
+ def __init__(self):
+ self.d = outerself.reference
+ def keys(self):
+ return self.d.keys()
+ def __getitem__(self, i):
+ return self.d[i]
+ d.clear()
+ d.update(SimpleUserDict())
+ i1 = d.items()
+ i2 = self.reference.items()
+ i1.sort()
+ i2.sort()
+ self.assertEqual(i1, i2)
+
+ class Exc(Exception): pass
+
+ d = self._empty_mapping()
+ class FailingUserDict:
+ def keys(self):
+ raise Exc
+ self.assertRaises(Exc, d.update, FailingUserDict())
+
+ d.clear()
+
+ class FailingUserDict:
+ def keys(self):
+ class BogonIter:
+ def __init__(self):
+ self.i = 1
+ def __iter__(self):
+ return self
+ def next(self):
+ if self.i:
+ self.i = 0
+ return 'a'
+ raise Exc
+ return BogonIter()
+ def __getitem__(self, key):
+ return key
+ self.assertRaises(Exc, d.update, FailingUserDict())
+
+ class FailingUserDict:
+ def keys(self):
+ class BogonIter:
+ def __init__(self):
+ self.i = ord('a')
+ def __iter__(self):
+ return self
+ def next(self):
+ if self.i <= ord('z'):
+ rtn = chr(self.i)
+ self.i += 1
+ return rtn
+ raise StopIteration
+ return BogonIter()
+ def __getitem__(self, key):
+ raise Exc
+ self.assertRaises(Exc, d.update, FailingUserDict())
+
+ d = self._empty_mapping()
+ class badseq(object):
+ def __iter__(self):
+ return self
+ def next(self):
+ raise Exc()
+
+ self.assertRaises(Exc, d.update, badseq())
+
+ self.assertRaises(ValueError, d.update, [(1, 2, 3)])
+
+ # no test_fromkeys or test_copy as both os.environ and selves don't support it
+
+ def test_get(self):
+ d = self._empty_mapping()
+ self.assert_(d.get(self.other.keys()[0]) is None)
+ self.assertEqual(d.get(self.other.keys()[0], 3), 3)
+ d = self.reference
+ self.assert_(d.get(self.other.keys()[0]) is None)
+ self.assertEqual(d.get(self.other.keys()[0], 3), 3)
+ self.assertEqual(d.get(self.inmapping.keys()[0]), self.inmapping.values()[0])
+ self.assertEqual(d.get(self.inmapping.keys()[0], 3), self.inmapping.values()[0])
+ self.assertRaises(TypeError, d.get)
+ self.assertRaises(TypeError, d.get, None, None, None)
+
+ def test_setdefault(self):
+ d = self._empty_mapping()
+ self.assertRaises(TypeError, d.setdefault)
+
+ def test_popitem(self):
+ d = self._empty_mapping()
+ self.assertRaises(KeyError, d.popitem)
+ self.assertRaises(TypeError, d.popitem, 42)
+
+ def test_pop(self):
+ d = self._empty_mapping()
+ k, v = self.inmapping.items()[0]
+ d[k] = v
+ self.assertRaises(KeyError, d.pop, self.other.keys()[0])
+
+ self.assertEqual(d.pop(k), v)
+ self.assertEqual(len(d), 0)
+
+ self.assertRaises(KeyError, d.pop, k)
+
+
+class TestMappingProtocol(BasicTestMappingProtocol):
+ def test_constructor(self):
+ BasicTestMappingProtocol.test_constructor(self)
+ self.assert_(self._empty_mapping() is not self._empty_mapping())
+ self.assertEqual(self._tested_class(x=1, y=2), {"x": 1, "y": 2})
+
+ def test_bool(self):
+ BasicTestMappingProtocol.test_bool(self)
+ self.assert_(not self._empty_mapping())
+ self.assert_(self._full_mapping({"x": "y"}))
+ self.assert_(bool(self._empty_mapping()) is False)
+ self.assert_(bool(self._full_mapping({"x": "y"})) is True)
+
+ def test_keys(self):
+ BasicTestMappingProtocol.test_keys(self)
+ d = self._empty_mapping()
+ self.assertEqual(d.keys(), [])
+ d = self._full_mapping({'a': 1, 'b': 2})
+ k = d.keys()
+ self.assert_('a' in k)
+ self.assert_('b' in k)
+ self.assert_('c' not in k)
+
+ def test_values(self):
+ BasicTestMappingProtocol.test_values(self)
+ d = self._full_mapping({1:2})
+ self.assertEqual(d.values(), [2])
+
+ def test_items(self):
+ BasicTestMappingProtocol.test_items(self)
+
+ d = self._full_mapping({1:2})
+ self.assertEqual(d.items(), [(1, 2)])
+
+ def test_has_key(self):
+ d = self._empty_mapping()
+ self.assert_(not d.has_key('a'))
+ d = self._full_mapping({'a': 1, 'b': 2})
+ k = d.keys()
+ k.sort()
+ self.assertEqual(k, ['a', 'b'])
+
+ self.assertRaises(TypeError, d.has_key)
+
+ def test_contains(self):
+ d = self._empty_mapping()
+ self.assert_(not ('a' in d))
+ self.assert_('a' not in d)
+ d = self._full_mapping({'a': 1, 'b': 2})
+ self.assert_('a' in d)
+ self.assert_('b' in d)
+ self.assert_('c' not in d)
+
+ self.assertRaises(TypeError, d.__contains__)
+
+ def test_len(self):
+ BasicTestMappingProtocol.test_len(self)
+ d = self._full_mapping({'a': 1, 'b': 2})
+ self.assertEqual(len(d), 2)
+
+ def test_getitem(self):
+ BasicTestMappingProtocol.test_getitem(self)
+ d = self._full_mapping({'a': 1, 'b': 2})
+ self.assertEqual(d['a'], 1)
+ self.assertEqual(d['b'], 2)
+ d['c'] = 3
+ d['a'] = 4
+ self.assertEqual(d['c'], 3)
+ self.assertEqual(d['a'], 4)
+ del d['b']
+ self.assertEqual(d, {'a': 4, 'c': 3})
+
+ self.assertRaises(TypeError, d.__getitem__)
+
+ def test_clear(self):
+ d = self._full_mapping({1:1, 2:2, 3:3})
+ d.clear()
+ self.assertEqual(d, {})
+
+ self.assertRaises(TypeError, d.clear, None)
+
+ def test_update(self):
+ BasicTestMappingProtocol.test_update(self)
+ # mapping argument
+ d = self._empty_mapping()
+ d.update({1:100})
+ d.update({2:20})
+ d.update({1:1, 2:2, 3:3})
+ self.assertEqual(d, {1:1, 2:2, 3:3})
+
+ # no argument
+ d.update()
+ self.assertEqual(d, {1:1, 2:2, 3:3})
+
+ # keyword arguments
+ d = self._empty_mapping()
+ d.update(x=100)
+ d.update(y=20)
+ d.update(x=1, y=2, z=3)
+ self.assertEqual(d, {"x":1, "y":2, "z":3})
+
+ # item sequence
+ d = self._empty_mapping()
+ d.update([("x", 100), ("y", 20)])
+ self.assertEqual(d, {"x":100, "y":20})
+
+ # Both item sequence and keyword arguments
+ d = self._empty_mapping()
+ d.update([("x", 100), ("y", 20)], x=1, y=2)
+ self.assertEqual(d, {"x":1, "y":2})
+
+ # iterator
+ d = self._full_mapping({1:3, 2:4})
+ 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:
+ def __init__(self):
+ self.d = {1:1, 2:2, 3:3}
+ def keys(self):
+ return self.d.keys()
+ def __getitem__(self, i):
+ return self.d[i]
+ d.clear()
+ d.update(SimpleUserDict())
+ self.assertEqual(d, {1:1, 2:2, 3:3})
+
+ def test_fromkeys(self):
+ self.assertEqual(self._tested_class.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
+ d = self._empty_mapping()
+ self.assert_(not(d.fromkeys('abc') is d))
+ self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
+ self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
+ self.assertEqual(d.fromkeys([]), {})
+ def g():
+ yield 1
+ self.assertEqual(d.fromkeys(g()), {1:None})
+ self.assertRaises(TypeError, {}.fromkeys, 3)
+ class dictlike(self._tested_class): pass
+ self.assertEqual(dictlike.fromkeys('a'), {'a':None})
+ self.assertEqual(dictlike().fromkeys('a'), {'a':None})
+ self.assert_(dictlike.fromkeys('a').__class__ is dictlike)
+ self.assert_(dictlike().fromkeys('a').__class__ is dictlike)
+ # FIXME: the following won't work with UserDict, because it's an old style class
+ # self.assert_(type(dictlike.fromkeys('a')) is dictlike)
+ class mydict(self._tested_class):
+ def __new__(cls):
+ return UserDict.UserDict()
+ ud = mydict.fromkeys('ab')
+ self.assertEqual(ud, {'a':None, 'b':None})
+ # FIXME: the following won't work with UserDict, because it's an old style class
+ # self.assert_(isinstance(ud, UserDict.UserDict))
+ self.assertRaises(TypeError, dict.fromkeys)
+
+ class Exc(Exception): pass
+
+ class baddict1(self._tested_class):
+ def __init__(self):
+ raise Exc()
+
+ self.assertRaises(Exc, baddict1.fromkeys, [1])
+
+ class BadSeq(object):
+ def __iter__(self):
+ return self
+ def next(self):
+ raise Exc()
+
+ self.assertRaises(Exc, self._tested_class.fromkeys, BadSeq())
+
+ class baddict2(self._tested_class):
+ def __setitem__(self, key, value):
+ raise Exc()
+
+ self.assertRaises(Exc, baddict2.fromkeys, [1])
+
+ def test_copy(self):
+ d = self._full_mapping({1:1, 2:2, 3:3})
+ self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
+ d = self._empty_mapping()
+ self.assertEqual(d.copy(), d)
+ self.assert_(isinstance(d.copy(), d.__class__))
+ self.assertRaises(TypeError, d.copy, None)
+
+ def test_get(self):
+ BasicTestMappingProtocol.test_get(self)
+ d = self._empty_mapping()
+ self.assert_(d.get('c') is None)
+ self.assertEqual(d.get('c', 3), 3)
+ d = self._full_mapping({'a' : 1, 'b' : 2})
+ self.assert_(d.get('c') is None)
+ self.assertEqual(d.get('c', 3), 3)
+ self.assertEqual(d.get('a'), 1)
+ self.assertEqual(d.get('a', 3), 1)
+
+ def test_setdefault(self):
+ BasicTestMappingProtocol.test_setdefault(self)
+ d = self._empty_mapping()
+ self.assert_(d.setdefault('key0') is None)
+ d.setdefault('key0', [])
+ self.assert_(d.setdefault('key0') is None)
+ d.setdefault('key', []).append(3)
+ self.assertEqual(d['key'][0], 3)
+ d.setdefault('key', []).append(4)
+ self.assertEqual(len(d['key']), 2)
+
+ def test_popitem(self):
+ BasicTestMappingProtocol.test_popitem(self)
+ for copymode in -1, +1:
+ # -1: b has same structure as a
+ # +1: b is a.copy()
+ for log2size in range(12):
+ size = 2**log2size
+ a = self._empty_mapping()
+ b = self._empty_mapping()
+ for i in range(size):
+ a[repr(i)] = i
+ if copymode < 0:
+ b[repr(i)] = i
+ if copymode > 0:
+ b = a.copy()
+ for i in range(size):
+ ka, va = ta = a.popitem()
+ self.assertEqual(va, int(ka))
+ kb, vb = tb = b.popitem()
+ self.assertEqual(vb, int(kb))
+ self.assert_(not(copymode < 0 and ta != tb))
+ self.assert_(not a)
+ self.assert_(not b)
+
+ def test_pop(self):
+ BasicTestMappingProtocol.test_pop(self)
+
+ # Tests for pop with specified key
+ d = self._empty_mapping()
+ k, v = 'abc', 'def'
+
+ # verify longs/ints get same value when key > 32 bits (for 64-bit archs)
+ # see SF bug #689659
+ x = 4503599627370496L
+ y = 4503599627370496
+ h = self._full_mapping({x: 'anything', y: 'something else'})
+ self.assertEqual(h[x], h[y])
+
+ self.assertEqual(d.pop(k, v), v)
+ d[k] = v
+ self.assertEqual(d.pop(k, 1), v)
+
+
+class TestHashMappingProtocol(TestMappingProtocol):
+
+ def test_getitem(self):
+ TestMappingProtocol.test_getitem(self)
+ class Exc(Exception): pass
+
+ class BadEq(object):
+ def __eq__(self, other):
+ raise Exc()
+
+ d = self._empty_mapping()
+ d[BadEq()] = 42
+ self.assertRaises(KeyError, d.__getitem__, 23)
+
+ class BadHash(object):
+ fail = False
+ def __hash__(self):
+ if self.fail:
+ raise Exc()
+ else:
+ return 42
+
+ d = self._empty_mapping()
+ x = BadHash()
+ d[x] = 42
+ x.fail = True
+ self.assertRaises(Exc, d.__getitem__, x)
+
+ def test_fromkeys(self):
+ TestMappingProtocol.test_fromkeys(self)
+ class mydict(self._tested_class):
+ def __new__(cls):
+ return UserDict.UserDict()
+ ud = mydict.fromkeys('ab')
+ self.assertEqual(ud, {'a':None, 'b':None})
+ self.assert_(isinstance(ud, UserDict.UserDict))
+
+ def test_pop(self):
+ TestMappingProtocol.test_pop(self)
+
+ class Exc(Exception): pass
+
+ class BadHash(object):
+ fail = False
+ def __hash__(self):
+ if self.fail:
+ raise Exc()
+ else:
+ return 42
+
+ d = self._empty_mapping()
+ x = BadHash()
+ d[x] = 42
+ x.fail = True
+ self.assertRaises(Exc, d.pop, x)
+
+ def test_mutatingiteration(self):
+ d = self._empty_mapping()
+ d[1] = 1
+ try:
+ for i in d:
+ d[i+1] = 1
+ except RuntimeError:
+ pass
+ else:
+ self.fail("changing dict size during iteration doesn't raise Error")
+
+ def test_repr(self):
+ d = self._empty_mapping()
+ self.assertEqual(repr(d), '{}')
+ d[1] = 2
+ self.assertEqual(repr(d), '{1: 2}')
+ d = self._empty_mapping()
+ d[1] = d
+ self.assertEqual(repr(d), '{1: {...}}')
+
+ class Exc(Exception): pass
+
+ class BadRepr(object):
+ def __repr__(self):
+ raise Exc()
+
+ d = self._full_mapping({1: BadRepr()})
+ self.assertRaises(Exc, repr, d)
+
+ def test_le(self):
+ self.assert_(not (self._empty_mapping() < self._empty_mapping()))
+ self.assert_(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
+
+ class Exc(Exception): pass
+
+ class BadCmp(object):
+ def __eq__(self, other):
+ raise Exc()
+
+ d1 = self._full_mapping({BadCmp(): 1})
+ d2 = self._full_mapping({1: 1})
+ try:
+ d1 < d2
+ except Exc:
+ pass
+ else:
+ self.fail("< didn't raise Exc")
+
+ def test_setdefault(self):
+ TestMappingProtocol.test_setdefault(self)
+
+ class Exc(Exception): pass
+
+ class BadHash(object):
+ fail = False
+ def __hash__(self):
+ if self.fail:
+ raise Exc()
+ else:
+ return 42
+
+ d = self._empty_mapping()
+ x = BadHash()
+ d[x] = 42
+ x.fail = True
+ self.assertRaises(Exc, d.setdefault, x, [])
diff --git a/Lib/test/output/test_types b/Lib/test/output/test_types
index b2f9429..b49ce0d 100644
--- a/Lib/test/output/test_types
+++ b/Lib/test/output/test_types
@@ -11,5 +11,5 @@ test_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
+6.6 Mappings == Dictionaries [see test_dict.py]
Buffers
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index db8d99e..1e283cd 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -205,9 +205,9 @@ class StatAttributeTests(unittest.TestCase):
except TypeError:
pass
-from test_userdict import TestMappingProtocol
+from test import mapping_tests
-class EnvironTests(TestMappingProtocol):
+class EnvironTests(mapping_tests.BasicTestMappingProtocol):
"""check that os.environ object conform to mapping protocol"""
_tested_class = None
def _reference(self):
diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py
index f852be5..fda02da 100644
--- a/Lib/test/test_shelve.py
+++ b/Lib/test/test_shelve.py
@@ -74,14 +74,14 @@ class TestCase(unittest.TestCase):
self.assertEqual(len(d2), 1)
-from test_userdict import TestMappingProtocol
+from test import mapping_tests
-class TestShelveBase(TestMappingProtocol):
+class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
fn = "shelftemp.db"
counter = 0
def __init__(self, *args, **kw):
self._db = []
- TestMappingProtocol.__init__(self, *args, **kw)
+ mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
_tested_class = shelve.Shelf
def _reference(self):
return {"key1":"value1", "key2":2, "key3":(1,2,3)}
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 4b4e19c..31466b7 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -10,7 +10,6 @@ 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 {}: 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'
@@ -215,233 +214,8 @@ print '6.5.2 Tuples [see test_tuple.py]'
print '6.5.3 Lists [see test_list.py]'
+print '6.6 Mappings == Dictionaries [see test_dict.py]'
-print '6.6 Mappings == Dictionaries'
-# calling built-in types without argument must return empty
-if dict() != {}: raise TestFailed,'dict() does not return {}'
-d = {}
-if d.keys() != []: raise TestFailed, '{}.keys()'
-if d.values() != []: raise TestFailed, '{}.values()'
-if d.items() != []: raise TestFailed, '{}.items()'
-if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')'
-if ('a' in d) != 0: raise TestFailed, "'a' in {}"
-if ('a' not in d) != 1: raise TestFailed, "'a' not in {}"
-if len(d) != 0: raise TestFailed, 'len({})'
-d = {'a': 1, 'b': 2}
-if len(d) != 2: raise TestFailed, 'len(dict)'
-k = d.keys()
-k.sort()
-if k != ['a', 'b']: raise TestFailed, 'dict keys()'
-if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
-else: raise TestFailed, 'dict keys()'
-if 'a' in d and 'b' in d and 'c' not in d: pass
-else: raise TestFailed, 'dict keys() # in/not in version'
-if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item'
-d['c'] = 3
-d['a'] = 4
-if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment'
-del d['b']
-if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
-# dict.clear()
-d = {1:1, 2:2, 3:3}
-d.clear()
-if d != {}: raise TestFailed, 'dict clear'
-# dict.update()
-d.update({1:100})
-d.update({2:20})
-d.update({1:1, 2:2, 3:3})
-if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
-d.clear()
-try: d.update(None)
-except (TypeError, AttributeError): pass
-else: raise TestFailed, 'dict.update(None), AttributeError expected'
-class SimpleUserDict:
- def __init__(self):
- self.d = {1:1, 2:2, 3:3}
- def keys(self):
- return self.d.keys()
- def __getitem__(self, i):
- return self.d[i]
-d.update(SimpleUserDict())
-if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)'
-d.clear()
-class FailingUserDict:
- def keys(self):
- raise ValueError
-try: d.update(FailingUserDict())
-except ValueError: pass
-else: raise TestFailed, 'dict.keys() expected ValueError'
-class FailingUserDict:
- def keys(self):
- class BogonIter:
- def __iter__(self):
- raise ValueError
- return BogonIter()
-try: d.update(FailingUserDict())
-except ValueError: pass
-else: raise TestFailed, 'iter(dict.keys()) expected ValueError'
-class FailingUserDict:
- def keys(self):
- class BogonIter:
- def __init__(self):
- self.i = 1
- def __iter__(self):
- return self
- def next(self):
- if self.i:
- self.i = 0
- return 'a'
- raise ValueError
- return BogonIter()
- def __getitem__(self, key):
- return key
-try: d.update(FailingUserDict())
-except ValueError: pass
-else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError'
-class FailingUserDict:
- def keys(self):
- class BogonIter:
- def __init__(self):
- self.i = ord('a')
- def __iter__(self):
- return self
- def next(self):
- if self.i <= ord('z'):
- rtn = chr(self.i)
- self.i += 1
- return rtn
- raise StopIteration
- return BogonIter()
- def __getitem__(self, key):
- raise ValueError
-try: d.update(FailingUserDict())
-except ValueError: pass
-else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError'
-# dict.fromkeys()
-if dict.fromkeys('abc') != {'a':None, 'b':None, 'c':None}:
- raise TestFailed, 'dict.fromkeys did not work as a class method'
-d = {}
-if d.fromkeys('abc') is d:
- raise TestFailed, 'dict.fromkeys did not return a new dict'
-if d.fromkeys('abc') != {'a':None, 'b':None, 'c':None}:
- raise TestFailed, 'dict.fromkeys failed with default value'
-if d.fromkeys((4,5),0) != {4:0, 5:0}:
- raise TestFailed, 'dict.fromkeys failed with specified value'
-if d.fromkeys([]) != {}:
- raise TestFailed, 'dict.fromkeys failed with null sequence'
-def g():
- yield 1
-if d.fromkeys(g()) != {1:None}:
- raise TestFailed, 'dict.fromkeys failed with a generator'
-try: {}.fromkeys(3)
-except TypeError: pass
-else: raise TestFailed, 'dict.fromkeys failed to raise TypeError'
-class dictlike(dict): pass
-if dictlike.fromkeys('a') != {'a':None}:
- raise TestFailed, 'dictsubclass.fromkeys did not inherit'
-if dictlike().fromkeys('a') != {'a':None}:
- raise TestFailed, 'dictsubclass.fromkeys did not inherit'
-if type(dictlike.fromkeys('a')) is not dictlike:
- raise TestFailed, 'dictsubclass.fromkeys created wrong type'
-if type(dictlike().fromkeys('a')) is not dictlike:
- raise TestFailed, 'dictsubclass.fromkeys created wrong type'
-from UserDict import UserDict
-class mydict(dict):
- def __new__(cls):
- return UserDict()
-ud = mydict.fromkeys('ab')
-if ud != {'a':None, 'b':None} or not isinstance(ud,UserDict):
- raise TestFailed, 'fromkeys did not instantiate using __new__'
-# dict.copy()
-d = {1:1, 2:2, 3:3}
-if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
-if {}.copy() != {}: raise TestFailed, 'empty dict copy'
-# dict.get()
-d = {}
-if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg'
-if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
-d = {'a' : 1, 'b' : 2}
-if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg'
-if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
-if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
-if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
-# dict.setdefault()
-d = {}
-if d.setdefault('key0') is not None:
- raise TestFailed, 'missing {} setdefault, no 2nd arg'
-if d.setdefault('key0') is not None:
- raise TestFailed, 'present {} setdefault, no 2nd arg'
-d.setdefault('key', []).append(3)
-if d['key'][0] != 3:
- raise TestFailed, 'missing {} setdefault, w/ 2nd arg'
-d.setdefault('key', []).append(4)
-if len(d['key']) != 2:
- raise TestFailed, 'present {} setdefault, w/ 2nd arg'
-# dict.popitem()
-for copymode in -1, +1:
- # -1: b has same structure as a
- # +1: b is a.copy()
- for log2size in range(12):
- size = 2**log2size
- a = {}
- b = {}
- for i in range(size):
- a[repr(i)] = i
- if copymode < 0:
- b[repr(i)] = i
- if copymode > 0:
- b = a.copy()
- for i in range(size):
- ka, va = ta = a.popitem()
- if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta)
- kb, vb = tb = b.popitem()
- if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb)
- if copymode < 0 and ta != tb:
- raise TestFailed, "a.popitem != b.popitem: %s, %s" % (
- str(ta), str(tb))
- if a: raise TestFailed, 'a not empty after popitems: %s' % str(a)
- if b: raise TestFailed, 'b not empty after popitems: %s' % str(b)
-
-d.clear()
-try: d.popitem()
-except KeyError: pass
-else: raise TestFailed, "{}.popitem doesn't raise KeyError"
-
-# Tests for pop with specified key
-d.clear()
-k, v = 'abc', 'def'
-d[k] = v
-try: d.pop('ghi')
-except KeyError: pass
-else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when k not in dictionary"
-
-if d.pop(k) != v: raise TestFailed, "{}.pop(k) doesn't find known key/value pair"
-if len(d) > 0: raise TestFailed, "{}.pop(k) failed to remove the specified pair"
-
-try: d.pop(k)
-except KeyError: pass
-else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when dictionary is empty"
-
-# verify longs/ints get same value when key > 32 bits (for 64-bit archs)
-# see SF bug #689659
-x = 4503599627370496L
-y = 4503599627370496
-h = {x: 'anything', y: 'something else'}
-if h[x] != h[y]:
- raise TestFailed, "long/int key should match"
-
-if d.pop(k, v) != v: raise TestFailed, "{}.pop(k, v) doesn't return default value"
-d[k] = v
-if d.pop(k, 1) != v: raise TestFailed, "{}.pop(k, v) doesn't find known key/value pair"
-
-d[1] = 1
-try:
- for i in d:
- d[i+1] = 1
-except RuntimeError:
- pass
-else:
- raise TestFailed, "changing dict size during iteration doesn't raise Error"
try: type(1, 2)
except TypeError: pass
diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py
index 0ed4369..f67e55b 100644
--- a/Lib/test/test_userdict.py
+++ b/Lib/test/test_userdict.py
@@ -1,126 +1,9 @@
# Check every path through every method of UserDict
-import test.test_support, unittest
+import unittest
+from test import test_support, mapping_tests
import UserDict
-class TestMappingProtocol(unittest.TestCase):
- # This base class can be used to check that an object conforms to the
- # mapping protocol
-
- # Functions that can be useful to override to adapt to dictionary
- # semantics
- _tested_class = dict # which class is being tested
-
- def _reference(self):
- """Return a dictionary of values which are invariant by storage
- in the object under test."""
- return {1:2, "key1":"value1", "key2":(1,2,3)}
- def _empty_mapping(self):
- """Return an empty mapping object"""
- return self._tested_class()
- def _full_mapping(self, data):
- """Return a mapping object with the value contained in data
- dictionary"""
- x = self._empty_mapping()
- for key, value in data.items():
- x[key] = value
- return x
-
- def __init__(self, *args, **kw):
- unittest.TestCase.__init__(self, *args, **kw)
- self.reference = self._reference().copy()
- key, value = self.reference.popitem()
- self.other = {key:value}
-
- def test_read(self):
- # Test for read only operations on mapping
- p = self._empty_mapping()
- p1 = dict(p) #workaround for singleton objects
- d = self._full_mapping(self.reference)
- if d is p:
- p = p1
- #Indexing
- for key, value in self.reference.items():
- self.assertEqual(d[key], value)
- knownkey = self.other.keys()[0]
- self.failUnlessRaises(KeyError, lambda:d[knownkey])
- #len
- self.assertEqual(len(p), 0)
- self.assertEqual(len(d), len(self.reference))
- #has_key
- for k in self.reference:
- self.assert_(d.has_key(k))
- self.assert_(k in d)
- for k in self.other:
- self.failIf(d.has_key(k))
- self.failIf(k in d)
- #cmp
- self.assertEqual(cmp(p,p), 0)
- self.assertEqual(cmp(d,d), 0)
- self.assertEqual(cmp(p,d), -1)
- self.assertEqual(cmp(d,p), 1)
- #__non__zero__
- if p: self.fail("Empty mapping must compare to False")
- if not d: self.fail("Full mapping must compare to True")
- # keys(), items(), iterkeys() ...
- def check_iterandlist(iter, lst, ref):
- self.assert_(hasattr(iter, 'next'))
- self.assert_(hasattr(iter, '__iter__'))
- x = list(iter)
- self.assert_(set(x)==set(lst)==set(ref))
- check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys())
- check_iterandlist(iter(d), d.keys(), self.reference.keys())
- check_iterandlist(d.itervalues(), d.values(), self.reference.values())
- check_iterandlist(d.iteritems(), d.items(), self.reference.items())
- #get
- key, value = d.iteritems().next()
- knownkey, knownvalue = self.other.iteritems().next()
- self.assertEqual(d.get(key, knownvalue), value)
- self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
- self.failIf(knownkey in d)
-
- def test_write(self):
- # Test for write operations on mapping
- p = self._empty_mapping()
- #Indexing
- for key, value in self.reference.items():
- p[key] = value
- self.assertEqual(p[key], value)
- for key in self.reference.keys():
- del p[key]
- self.failUnlessRaises(KeyError, lambda:p[key])
- p = self._empty_mapping()
- #update
- p.update(self.reference)
- self.assertEqual(dict(p), self.reference)
- items = p.items()
- p = self._empty_mapping()
- p.update(items)
- self.assertEqual(dict(p), self.reference)
- d = self._full_mapping(self.reference)
- #setdefault
- key, value = d.iteritems().next()
- knownkey, knownvalue = self.other.iteritems().next()
- self.assertEqual(d.setdefault(key, knownvalue), value)
- self.assertEqual(d[key], value)
- self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
- self.assertEqual(d[knownkey], knownvalue)
- #pop
- self.assertEqual(d.pop(knownkey), knownvalue)
- self.failIf(knownkey in d)
- self.assertRaises(KeyError, d.pop, knownkey)
- default = 909
- d[knownkey] = knownvalue
- self.assertEqual(d.pop(knownkey, default), knownvalue)
- self.failIf(knownkey in d)
- self.assertEqual(d.pop(knownkey, default), default)
- #popitem
- key, value = d.popitem()
- self.failIf(key in d)
- self.assertEqual(value, self.reference[key])
- p=self._empty_mapping()
- self.assertRaises(KeyError, p.popitem)
-
d0 = {}
d1 = {"one": 1}
d2 = {"one": 1, "two": 2}
@@ -128,7 +11,7 @@ d3 = {"one": 1, "two": 3, "three": 5}
d4 = {"one": None, "two": None}
d5 = {"one": 1, "two": 1}
-class UserDictTest(TestMappingProtocol):
+class UserDictTest(mapping_tests.TestHashMappingProtocol):
_tested_class = UserDict.IterableUserDict
def test_all(self):
@@ -273,9 +156,14 @@ class SeqDict(UserDict.DictMixin):
Used to test and demonstrate DictMixin
"""
- def __init__(self):
+ def __init__(self, other=None, **kwargs):
self.keylist = []
self.valuelist = []
+ if other is not None:
+ for (key, value) in other:
+ self[key] = value
+ for (key, value) in kwargs.iteritems():
+ self[key] = value
def __getitem__(self, key):
try:
i = self.keylist.index(key)
@@ -298,8 +186,19 @@ class SeqDict(UserDict.DictMixin):
self.valuelist.pop(i)
def keys(self):
return list(self.keylist)
-
-class UserDictMixinTest(TestMappingProtocol):
+ def copy(self):
+ d = self.__class__()
+ for key, value in self.iteritems():
+ d[key] = value
+ return d
+ def fromkeys(cls, keys, value=None):
+ d = cls()
+ for key in keys:
+ d[key] = value
+ return d
+ fromkeys = classmethod(fromkeys)
+
+class UserDictMixinTest(mapping_tests.TestMappingProtocol):
_tested_class = SeqDict
def test_all(self):
@@ -393,8 +292,7 @@ class UserDictMixinTest(TestMappingProtocol):
self.assertEqual(s, t)
def test_main():
- test.test_support.run_unittest(
- TestMappingProtocol,
+ test_support.run_unittest(
UserDictTest,
UserDictMixinTest
)
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index 2924313..cf5d0a4 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -912,16 +912,16 @@ class MappingTestCase(TestBase):
self.assertEqual(len(d), 0)
self.assertEqual(count, 2)
-from test_userdict import TestMappingProtocol
+from test import mapping_tests
-class WeakValueDictionaryTestCase(TestMappingProtocol):
+class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
"""Check that WeakValueDictionary conforms to the mapping protocol"""
__ref = {"key1":Object(1), "key2":Object(2), "key3":Object(3)}
_tested_class = weakref.WeakValueDictionary
def _reference(self):
return self.__ref.copy()
-class WeakKeyDictionaryTestCase(TestMappingProtocol):
+class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
"""Check that WeakKeyDictionary conforms to the mapping protocol"""
__ref = {Object("key1"):1, Object("key2"):2, Object("key3"):3}
_tested_class = weakref.WeakKeyDictionary