2009-03-03 00:45:34 -04:00
|
|
|
|
2008-01-10 20:23:13 -04:00
|
|
|
import unittest, doctest
|
2009-03-03 00:45:34 -04:00
|
|
|
import inspect
|
2007-03-01 02:16:43 -04:00
|
|
|
from test import test_support
|
2009-03-03 00:45:34 -04:00
|
|
|
from collections import namedtuple, Counter, OrderedDict
|
|
|
|
from test import mapping_tests
|
2008-06-08 22:28:30 -03:00
|
|
|
import pickle, cPickle, copy
|
2009-03-03 00:45:34 -04:00
|
|
|
from random import randrange, shuffle
|
2009-01-19 21:19:26 -04:00
|
|
|
import operator
|
2009-05-26 23:24:45 -03:00
|
|
|
import keyword
|
|
|
|
import re
|
2007-11-21 20:55:51 -04:00
|
|
|
from collections import Hashable, Iterable, Iterator
|
|
|
|
from collections import Sized, Container, Callable
|
|
|
|
from collections import Set, MutableSet
|
|
|
|
from collections import Mapping, MutableMapping
|
|
|
|
from collections import Sequence, MutableSequence
|
|
|
|
|
2008-06-08 22:28:30 -03:00
|
|
|
TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests
|
2007-03-01 02:16:43 -04:00
|
|
|
|
|
|
|
class TestNamedTuple(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_factory(self):
|
2007-10-23 17:37:41 -03:00
|
|
|
Point = namedtuple('Point', 'x y')
|
2007-03-01 02:16:43 -04:00
|
|
|
self.assertEqual(Point.__name__, 'Point')
|
|
|
|
self.assertEqual(Point.__doc__, 'Point(x, y)')
|
|
|
|
self.assertEqual(Point.__slots__, ())
|
|
|
|
self.assertEqual(Point.__module__, __name__)
|
|
|
|
self.assertEqual(Point.__getitem__, tuple.__getitem__)
|
2008-01-03 23:22:53 -04:00
|
|
|
self.assertEqual(Point._fields, ('x', 'y'))
|
2007-10-16 18:28:32 -03:00
|
|
|
|
2007-10-23 17:37:41 -03:00
|
|
|
self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char
|
|
|
|
self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword
|
|
|
|
self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit
|
2007-10-16 18:28:32 -03:00
|
|
|
|
2007-10-23 17:37:41 -03:00
|
|
|
self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char
|
|
|
|
self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword
|
|
|
|
self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit
|
2007-12-13 22:49:47 -04:00
|
|
|
self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore
|
2007-10-23 17:37:41 -03:00
|
|
|
self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field
|
2007-10-16 18:28:32 -03:00
|
|
|
|
2007-10-23 17:37:41 -03:00
|
|
|
namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
|
2007-12-13 22:49:47 -04:00
|
|
|
namedtuple('_', 'a b c') # Test leading underscores in a typename
|
2007-03-01 02:16:43 -04:00
|
|
|
|
2008-09-25 20:31:52 -03:00
|
|
|
nt = namedtuple('nt', u'the quick brown fox') # check unicode input
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue("u'" not in repr(nt._fields))
|
2008-09-25 20:31:52 -03:00
|
|
|
nt = namedtuple('nt', (u'the', u'quick')) # check unicode input
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue("u'" not in repr(nt._fields))
|
2008-09-25 20:31:52 -03:00
|
|
|
|
2008-01-04 21:35:43 -04:00
|
|
|
self.assertRaises(TypeError, Point._make, [11]) # catch too few args
|
|
|
|
self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args
|
|
|
|
|
2009-02-09 21:24:05 -04:00
|
|
|
def test_name_fixer(self):
|
|
|
|
for spec, renamed in [
|
2009-04-02 19:25:40 -03:00
|
|
|
[('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char
|
|
|
|
[('abc', 'class'), ('abc', '_1')], # field has keyword
|
|
|
|
[('8efg', '9ghi'), ('_0', '_1')], # field starts with digit
|
|
|
|
[('abc', '_efg'), ('abc', '_1')], # field with leading underscore
|
|
|
|
[('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')], # duplicate field
|
|
|
|
[('abc', '', 'x'), ('abc', '_1', 'x')], # fieldname is a space
|
2009-02-09 21:24:05 -04:00
|
|
|
]:
|
|
|
|
self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed)
|
|
|
|
|
2007-03-01 02:16:43 -04:00
|
|
|
def test_instance(self):
|
2007-10-23 17:37:41 -03:00
|
|
|
Point = namedtuple('Point', 'x y')
|
2007-03-01 02:16:43 -04:00
|
|
|
p = Point(11, 22)
|
|
|
|
self.assertEqual(p, Point(x=11, y=22))
|
|
|
|
self.assertEqual(p, Point(11, y=22))
|
|
|
|
self.assertEqual(p, Point(y=22, x=11))
|
|
|
|
self.assertEqual(p, Point(*(11, 22)))
|
|
|
|
self.assertEqual(p, Point(**dict(x=11, y=22)))
|
|
|
|
self.assertRaises(TypeError, Point, 1) # too few args
|
|
|
|
self.assertRaises(TypeError, Point, 1, 2, 3) # too many args
|
|
|
|
self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument
|
|
|
|
self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument
|
|
|
|
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('__dict__' not in dir(p)) # verify instance has no dict
|
|
|
|
self.assertTrue('__weakref__' not in dir(p))
|
2008-01-04 21:35:43 -04:00
|
|
|
self.assertEqual(p, Point._make([11, 22])) # test _make classmethod
|
2007-12-13 22:49:47 -04:00
|
|
|
self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
|
|
|
|
self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
|
|
|
|
self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
|
2007-09-16 21:55:00 -03:00
|
|
|
|
2008-01-04 22:17:24 -04:00
|
|
|
try:
|
|
|
|
p._replace(x=1, error=2)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self._fail('Did not detect an incorrect fieldname')
|
|
|
|
|
2007-09-16 21:55:00 -03:00
|
|
|
# verify that field string can have commas
|
2007-10-23 17:37:41 -03:00
|
|
|
Point = namedtuple('Point', 'x, y')
|
2007-09-16 21:55:00 -03:00
|
|
|
p = Point(x=11, y=22)
|
|
|
|
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
2007-03-01 02:16:43 -04:00
|
|
|
|
2007-10-08 06:14:28 -03:00
|
|
|
# verify that fieldspec can be a non-string sequence
|
2007-10-23 17:37:41 -03:00
|
|
|
Point = namedtuple('Point', ('x', 'y'))
|
2007-10-08 06:14:28 -03:00
|
|
|
p = Point(x=11, y=22)
|
|
|
|
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
|
|
|
|
2007-03-01 02:16:43 -04:00
|
|
|
def test_tupleness(self):
|
2007-10-23 17:37:41 -03:00
|
|
|
Point = namedtuple('Point', 'x y')
|
2007-03-01 02:16:43 -04:00
|
|
|
p = Point(11, 22)
|
|
|
|
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(p, tuple))
|
2007-03-01 02:16:43 -04:00
|
|
|
self.assertEqual(p, (11, 22)) # matches a real tuple
|
|
|
|
self.assertEqual(tuple(p), (11, 22)) # coercable to a real tuple
|
|
|
|
self.assertEqual(list(p), [11, 22]) # coercable to a list
|
|
|
|
self.assertEqual(max(p), 22) # iterable
|
|
|
|
self.assertEqual(max(*p), 22) # star-able
|
|
|
|
x, y = p
|
|
|
|
self.assertEqual(p, (x, y)) # unpacks like a tuple
|
|
|
|
self.assertEqual((p[0], p[1]), (11, 22)) # indexable like a tuple
|
|
|
|
self.assertRaises(IndexError, p.__getitem__, 3)
|
|
|
|
|
|
|
|
self.assertEqual(p.x, x)
|
|
|
|
self.assertEqual(p.y, y)
|
|
|
|
self.assertRaises(AttributeError, eval, 'p.z', locals())
|
|
|
|
|
2007-09-18 00:33:19 -03:00
|
|
|
def test_odd_sizes(self):
|
2007-10-23 17:37:41 -03:00
|
|
|
Zero = namedtuple('Zero', '')
|
2007-09-18 00:33:19 -03:00
|
|
|
self.assertEqual(Zero(), ())
|
2008-01-04 21:35:43 -04:00
|
|
|
self.assertEqual(Zero._make([]), ())
|
2007-12-17 20:13:45 -04:00
|
|
|
self.assertEqual(repr(Zero()), 'Zero()')
|
|
|
|
self.assertEqual(Zero()._asdict(), {})
|
|
|
|
self.assertEqual(Zero()._fields, ())
|
|
|
|
|
2007-10-23 17:37:41 -03:00
|
|
|
Dot = namedtuple('Dot', 'd')
|
2007-09-18 00:33:19 -03:00
|
|
|
self.assertEqual(Dot(1), (1,))
|
2008-01-04 21:35:43 -04:00
|
|
|
self.assertEqual(Dot._make([1]), (1,))
|
2007-12-17 20:13:45 -04:00
|
|
|
self.assertEqual(Dot(1).d, 1)
|
|
|
|
self.assertEqual(repr(Dot(1)), 'Dot(d=1)')
|
|
|
|
self.assertEqual(Dot(1)._asdict(), {'d':1})
|
|
|
|
self.assertEqual(Dot(1)._replace(d=999), (999,))
|
|
|
|
self.assertEqual(Dot(1)._fields, ('d',))
|
|
|
|
|
2008-06-08 22:28:30 -03:00
|
|
|
n = 5000
|
2007-12-17 20:13:45 -04:00
|
|
|
import string, random
|
2008-05-18 07:39:26 -03:00
|
|
|
names = list(set(''.join([random.choice(string.ascii_letters)
|
|
|
|
for j in range(10)]) for i in range(n)))
|
|
|
|
n = len(names)
|
2007-12-17 20:13:45 -04:00
|
|
|
Big = namedtuple('Big', names)
|
|
|
|
b = Big(*range(n))
|
|
|
|
self.assertEqual(b, tuple(range(n)))
|
2008-01-04 21:35:43 -04:00
|
|
|
self.assertEqual(Big._make(range(n)), tuple(range(n)))
|
2007-12-17 20:13:45 -04:00
|
|
|
for pos, name in enumerate(names):
|
|
|
|
self.assertEqual(getattr(b, name), pos)
|
|
|
|
repr(b) # make sure repr() doesn't blow-up
|
|
|
|
d = b._asdict()
|
|
|
|
d_expected = dict(zip(names, range(n)))
|
|
|
|
self.assertEqual(d, d_expected)
|
|
|
|
b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)]))
|
|
|
|
b2_expected = range(n)
|
|
|
|
b2_expected[1] = 999
|
|
|
|
b2_expected[-5] = 42
|
|
|
|
self.assertEqual(b2, tuple(b2_expected))
|
|
|
|
self.assertEqual(b._fields, tuple(names))
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2008-06-08 22:28:30 -03:00
|
|
|
def test_pickle(self):
|
|
|
|
p = TestNT(x=10, y=20, z=30)
|
|
|
|
for module in pickle, cPickle:
|
|
|
|
loads = getattr(module, 'loads')
|
|
|
|
dumps = getattr(module, 'dumps')
|
|
|
|
for protocol in -1, 0, 1, 2:
|
|
|
|
q = loads(dumps(p, protocol))
|
|
|
|
self.assertEqual(p, q)
|
|
|
|
self.assertEqual(p._fields, q._fields)
|
|
|
|
|
|
|
|
def test_copy(self):
|
|
|
|
p = TestNT(x=10, y=20, z=30)
|
|
|
|
for copier in copy.copy, copy.deepcopy:
|
|
|
|
q = copier(p)
|
|
|
|
self.assertEqual(p, q)
|
|
|
|
self.assertEqual(p._fields, q._fields)
|
|
|
|
|
2009-05-26 23:24:45 -03:00
|
|
|
def test_name_conflicts(self):
|
|
|
|
# Some names like "self", "cls", "tuple", "itemgetter", and "property"
|
|
|
|
# failed when used as field names. Test to make sure these now work.
|
|
|
|
T = namedtuple('T', 'itemgetter property self cls tuple')
|
|
|
|
t = T(1, 2, 3, 4, 5)
|
|
|
|
self.assertEqual(t, (1,2,3,4,5))
|
|
|
|
newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50)
|
|
|
|
self.assertEqual(newt, (10,20,30,40,50))
|
|
|
|
|
|
|
|
# Broader test of all interesting names in a template
|
|
|
|
with test_support.captured_stdout() as template:
|
|
|
|
T = namedtuple('T', 'x', verbose=True)
|
|
|
|
words = set(re.findall('[A-Za-z]+', template.getvalue()))
|
|
|
|
words -= set(keyword.kwlist)
|
|
|
|
T = namedtuple('T', words)
|
|
|
|
# test __new__
|
|
|
|
values = tuple(range(len(words)))
|
|
|
|
t = T(*values)
|
|
|
|
self.assertEqual(t, values)
|
|
|
|
t = T(**dict(zip(T._fields, values)))
|
|
|
|
self.assertEqual(t, values)
|
|
|
|
# test _make
|
|
|
|
t = T._make(values)
|
|
|
|
self.assertEqual(t, values)
|
|
|
|
# exercise __repr__
|
|
|
|
repr(t)
|
|
|
|
# test _asdict
|
|
|
|
self.assertEqual(t._asdict(), dict(zip(T._fields, values)))
|
|
|
|
# test _replace
|
|
|
|
t = T._make(values)
|
|
|
|
newvalues = tuple(v*10 for v in values)
|
|
|
|
newt = t._replace(**dict(zip(T._fields, newvalues)))
|
|
|
|
self.assertEqual(newt, newvalues)
|
|
|
|
# test _fields
|
|
|
|
self.assertEqual(T._fields, tuple(words))
|
|
|
|
# test __getnewargs__
|
|
|
|
self.assertEqual(t.__getnewargs__(), values)
|
|
|
|
|
2009-01-28 19:02:26 -04:00
|
|
|
class ABCTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def validate_abstract_methods(self, abc, *names):
|
|
|
|
methodstubs = dict.fromkeys(names, lambda s, *args: 0)
|
|
|
|
|
|
|
|
# everything should work will all required methods are present
|
|
|
|
C = type('C', (abc,), methodstubs)
|
|
|
|
C()
|
|
|
|
|
|
|
|
# instantiation should fail if a required method is missing
|
|
|
|
for name in names:
|
|
|
|
stubs = methodstubs.copy()
|
|
|
|
del stubs[name]
|
|
|
|
C = type('C', (abc,), stubs)
|
|
|
|
self.assertRaises(TypeError, C, name)
|
|
|
|
|
|
|
|
|
|
|
|
class TestOneTrickPonyABCs(ABCTestCase):
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Hashable(self):
|
|
|
|
# Check some non-hashables
|
|
|
|
non_samples = [list(), set(), dict()]
|
|
|
|
for x in non_samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(x, Hashable), repr(x))
|
|
|
|
self.assertFalse(issubclass(type(x), Hashable), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
# Check some hashables
|
|
|
|
samples = [None,
|
|
|
|
int(), float(), complex(),
|
|
|
|
str(),
|
|
|
|
tuple(), frozenset(),
|
|
|
|
int, list, object, type,
|
|
|
|
]
|
|
|
|
for x in samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(x, Hashable), repr(x))
|
|
|
|
self.assertTrue(issubclass(type(x), Hashable), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
self.assertRaises(TypeError, Hashable)
|
|
|
|
# Check direct subclassing
|
|
|
|
class H(Hashable):
|
|
|
|
def __hash__(self):
|
|
|
|
return super(H, self).__hash__()
|
2008-08-11 12:45:58 -03:00
|
|
|
__eq__ = Hashable.__eq__ # Silence Py3k warning
|
2007-11-21 20:55:51 -04:00
|
|
|
self.assertEqual(hash(H()), 0)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(issubclass(int, H))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Hashable, '__hash__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Iterable(self):
|
|
|
|
# Check some non-iterables
|
|
|
|
non_samples = [None, 42, 3.14, 1j]
|
|
|
|
for x in non_samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(x, Iterable), repr(x))
|
|
|
|
self.assertFalse(issubclass(type(x), Iterable), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
# Check some iterables
|
|
|
|
samples = [str(),
|
|
|
|
tuple(), list(), set(), frozenset(), dict(),
|
|
|
|
dict().keys(), dict().items(), dict().values(),
|
|
|
|
(lambda: (yield))(),
|
|
|
|
(x for x in []),
|
|
|
|
]
|
|
|
|
for x in samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(x, Iterable), repr(x))
|
|
|
|
self.assertTrue(issubclass(type(x), Iterable), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
# Check direct subclassing
|
|
|
|
class I(Iterable):
|
|
|
|
def __iter__(self):
|
|
|
|
return super(I, self).__iter__()
|
|
|
|
self.assertEqual(list(I()), [])
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(issubclass(str, I))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Iterable, '__iter__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Iterator(self):
|
|
|
|
non_samples = [None, 42, 3.14, 1j, "".encode('ascii'), "", (), [],
|
|
|
|
{}, set()]
|
|
|
|
for x in non_samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(x, Iterator), repr(x))
|
|
|
|
self.assertFalse(issubclass(type(x), Iterator), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
samples = [iter(str()),
|
|
|
|
iter(tuple()), iter(list()), iter(dict()),
|
|
|
|
iter(set()), iter(frozenset()),
|
|
|
|
iter(dict().keys()), iter(dict().items()),
|
|
|
|
iter(dict().values()),
|
|
|
|
(lambda: (yield))(),
|
|
|
|
(x for x in []),
|
|
|
|
]
|
|
|
|
for x in samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(x, Iterator), repr(x))
|
|
|
|
self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Iterator, 'next')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Sized(self):
|
|
|
|
non_samples = [None, 42, 3.14, 1j,
|
|
|
|
(lambda: (yield))(),
|
|
|
|
(x for x in []),
|
|
|
|
]
|
|
|
|
for x in non_samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(x, Sized), repr(x))
|
|
|
|
self.assertFalse(issubclass(type(x), Sized), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
samples = [str(),
|
|
|
|
tuple(), list(), set(), frozenset(), dict(),
|
|
|
|
dict().keys(), dict().items(), dict().values(),
|
|
|
|
]
|
|
|
|
for x in samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(x, Sized), repr(x))
|
|
|
|
self.assertTrue(issubclass(type(x), Sized), repr(type(x)))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Sized, '__len__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Container(self):
|
|
|
|
non_samples = [None, 42, 3.14, 1j,
|
|
|
|
(lambda: (yield))(),
|
|
|
|
(x for x in []),
|
|
|
|
]
|
|
|
|
for x in non_samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(x, Container), repr(x))
|
|
|
|
self.assertFalse(issubclass(type(x), Container), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
samples = [str(),
|
|
|
|
tuple(), list(), set(), frozenset(), dict(),
|
|
|
|
dict().keys(), dict().items(),
|
|
|
|
]
|
|
|
|
for x in samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(x, Container), repr(x))
|
|
|
|
self.assertTrue(issubclass(type(x), Container), repr(type(x)))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Container, '__contains__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Callable(self):
|
|
|
|
non_samples = [None, 42, 3.14, 1j,
|
|
|
|
"", "".encode('ascii'), (), [], {}, set(),
|
|
|
|
(lambda: (yield))(),
|
|
|
|
(x for x in []),
|
|
|
|
]
|
|
|
|
for x in non_samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(x, Callable), repr(x))
|
|
|
|
self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
|
2007-11-21 20:55:51 -04:00
|
|
|
samples = [lambda: None,
|
|
|
|
type, int, object,
|
|
|
|
len,
|
|
|
|
list.append, [].append,
|
|
|
|
]
|
|
|
|
for x in samples:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(x, Callable), repr(x))
|
|
|
|
self.assertTrue(issubclass(type(x), Callable), repr(type(x)))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Callable, '__call__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_direct_subclassing(self):
|
|
|
|
for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
|
|
|
|
class C(B):
|
|
|
|
pass
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(issubclass(C, B))
|
|
|
|
self.assertFalse(issubclass(int, C))
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_registration(self):
|
|
|
|
for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
|
|
|
|
class C:
|
|
|
|
__metaclass__ = type
|
|
|
|
__hash__ = None # Make sure it isn't hashable by default
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(issubclass(C, B), B.__name__)
|
2007-11-21 20:55:51 -04:00
|
|
|
B.register(C)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(issubclass(C, B))
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2009-04-01 15:50:56 -03:00
|
|
|
class WithSet(MutableSet):
|
|
|
|
|
|
|
|
def __init__(self, it=()):
|
|
|
|
self.data = set(it)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.data)
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.data
|
|
|
|
|
|
|
|
def add(self, item):
|
|
|
|
self.data.add(item)
|
|
|
|
|
|
|
|
def discard(self, item):
|
|
|
|
self.data.discard(item)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2009-01-28 19:02:26 -04:00
|
|
|
class TestCollectionABCs(ABCTestCase):
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
# XXX For now, we only test some virtual inheritance properties.
|
|
|
|
# We should also test the proper behavior of the collection ABCs
|
|
|
|
# as real base classes or mix-in classes.
|
|
|
|
|
|
|
|
def test_Set(self):
|
|
|
|
for sample in [set, frozenset]:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(sample(), Set))
|
|
|
|
self.assertTrue(issubclass(sample, Set))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2008-06-23 00:29:28 -03:00
|
|
|
def test_hash_Set(self):
|
|
|
|
class OneTwoThreeSet(Set):
|
|
|
|
def __init__(self):
|
|
|
|
self.contents = [1, 2, 3]
|
|
|
|
def __contains__(self, x):
|
|
|
|
return x in self.contents
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.contents)
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.contents)
|
|
|
|
def __hash__(self):
|
|
|
|
return self._hash()
|
|
|
|
a, b = OneTwoThreeSet(), OneTwoThreeSet()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hash(a) == hash(b))
|
2008-06-23 00:29:28 -03:00
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def test_MutableSet(self):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(set(), MutableSet))
|
|
|
|
self.assertTrue(issubclass(set, MutableSet))
|
|
|
|
self.assertFalse(isinstance(frozenset(), MutableSet))
|
|
|
|
self.assertFalse(issubclass(frozenset, MutableSet))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__',
|
|
|
|
'add', 'discard')
|
|
|
|
|
2009-04-01 15:50:56 -03:00
|
|
|
def test_issue_5647(self):
|
|
|
|
# MutableSet.__iand__ mutated the set during iteration
|
|
|
|
s = WithSet('abcd')
|
|
|
|
s &= WithSet('cdef') # This used to fail
|
|
|
|
self.assertEqual(set(s), set('cd'))
|
|
|
|
|
2009-01-28 19:02:26 -04:00
|
|
|
def test_issue_4920(self):
|
|
|
|
# MutableSet.pop() method did not work
|
|
|
|
class MySet(collections.MutableSet):
|
|
|
|
__slots__=['__s']
|
|
|
|
def __init__(self,items=None):
|
|
|
|
if items is None:
|
|
|
|
items=[]
|
|
|
|
self.__s=set(items)
|
|
|
|
def __contains__(self,v):
|
|
|
|
return v in self.__s
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.__s)
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.__s)
|
|
|
|
def add(self,v):
|
|
|
|
result=v not in self.__s
|
|
|
|
self.__s.add(v)
|
|
|
|
return result
|
|
|
|
def discard(self,v):
|
|
|
|
result=v in self.__s
|
|
|
|
self.__s.discard(v)
|
|
|
|
return result
|
|
|
|
def __repr__(self):
|
|
|
|
return "MySet(%s)" % repr(list(self))
|
|
|
|
s = MySet([5,43,2,1])
|
|
|
|
self.assertEqual(s.pop(), 1)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Mapping(self):
|
|
|
|
for sample in [dict]:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(sample(), Mapping))
|
|
|
|
self.assertTrue(issubclass(sample, Mapping))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
|
|
|
|
'__getitem__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_MutableMapping(self):
|
|
|
|
for sample in [dict]:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(sample(), MutableMapping))
|
|
|
|
self.assertTrue(issubclass(sample, MutableMapping))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__',
|
|
|
|
'__getitem__', '__setitem__', '__delitem__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_Sequence(self):
|
|
|
|
for sample in [tuple, list, str]:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(sample(), Sequence))
|
|
|
|
self.assertTrue(issubclass(sample, Sequence))
|
|
|
|
self.assertTrue(issubclass(basestring, Sequence))
|
|
|
|
self.assertTrue(isinstance(range(10), Sequence))
|
|
|
|
self.assertTrue(issubclass(xrange, Sequence))
|
|
|
|
self.assertTrue(issubclass(str, Sequence))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__',
|
|
|
|
'__getitem__')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def test_MutableSequence(self):
|
|
|
|
for sample in [tuple, str]:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(isinstance(sample(), MutableSequence))
|
|
|
|
self.assertFalse(issubclass(sample, MutableSequence))
|
2007-11-21 20:55:51 -04:00
|
|
|
for sample in [list]:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(sample(), MutableSequence))
|
|
|
|
self.assertTrue(issubclass(sample, MutableSequence))
|
|
|
|
self.assertFalse(issubclass(basestring, MutableSequence))
|
2009-01-28 19:02:26 -04:00
|
|
|
self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
|
|
|
|
'__len__', '__getitem__', '__setitem__', '__delitem__', 'insert')
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2009-01-12 18:58:41 -04:00
|
|
|
class TestCounter(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_basics(self):
|
|
|
|
c = Counter('abcaba')
|
2009-01-19 21:19:26 -04:00
|
|
|
self.assertEqual(c, Counter({'a':3 , 'b': 2, 'c': 1}))
|
|
|
|
self.assertEqual(c, Counter(a=3, b=2, c=1))
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(c, dict))
|
|
|
|
self.assertTrue(isinstance(c, Mapping))
|
|
|
|
self.assertTrue(issubclass(Counter, dict))
|
|
|
|
self.assertTrue(issubclass(Counter, Mapping))
|
2009-01-12 18:58:41 -04:00
|
|
|
self.assertEqual(len(c), 3)
|
|
|
|
self.assertEqual(sum(c.values()), 6)
|
|
|
|
self.assertEqual(sorted(c.values()), [1, 2, 3])
|
|
|
|
self.assertEqual(sorted(c.keys()), ['a', 'b', 'c'])
|
|
|
|
self.assertEqual(sorted(c), ['a', 'b', 'c'])
|
|
|
|
self.assertEqual(sorted(c.items()),
|
|
|
|
[('a', 3), ('b', 2), ('c', 1)])
|
|
|
|
self.assertEqual(c['b'], 2)
|
|
|
|
self.assertEqual(c['z'], 0)
|
|
|
|
self.assertEqual(c.has_key('c'), True)
|
|
|
|
self.assertEqual(c.has_key('z'), False)
|
|
|
|
self.assertEqual(c.__contains__('c'), True)
|
|
|
|
self.assertEqual(c.__contains__('z'), False)
|
|
|
|
self.assertEqual(c.get('b', 10), 2)
|
|
|
|
self.assertEqual(c.get('z', 10), 10)
|
|
|
|
self.assertEqual(c, dict(a=3, b=2, c=1))
|
2009-01-12 21:05:03 -04:00
|
|
|
self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})")
|
2009-01-12 18:58:41 -04:00
|
|
|
self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)])
|
|
|
|
for i in range(5):
|
|
|
|
self.assertEqual(c.most_common(i),
|
|
|
|
[('a', 3), ('b', 2), ('c', 1)][:i])
|
|
|
|
self.assertEqual(''.join(sorted(c.elements())), 'aaabbc')
|
|
|
|
c['a'] += 1 # increment an existing value
|
|
|
|
c['b'] -= 2 # sub existing value to zero
|
|
|
|
del c['c'] # remove an entry
|
2009-01-19 21:19:26 -04:00
|
|
|
del c['c'] # make sure that del doesn't raise KeyError
|
2009-01-12 18:58:41 -04:00
|
|
|
c['d'] -= 2 # sub from a missing value
|
|
|
|
c['e'] = -5 # directly assign a missing value
|
|
|
|
c['f'] += 4 # add to a missing value
|
|
|
|
self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4))
|
|
|
|
self.assertEqual(''.join(sorted(c.elements())), 'aaaaffff')
|
|
|
|
self.assertEqual(c.pop('f'), 4)
|
|
|
|
self.assertEqual('f' in c, False)
|
|
|
|
for i in range(3):
|
|
|
|
elem, cnt = c.popitem()
|
|
|
|
self.assertEqual(elem in c, False)
|
|
|
|
c.clear()
|
|
|
|
self.assertEqual(c, {})
|
|
|
|
self.assertEqual(repr(c), 'Counter()')
|
|
|
|
self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc')
|
|
|
|
self.assertRaises(TypeError, hash, c)
|
2009-01-19 21:19:26 -04:00
|
|
|
c.update(dict(a=5, b=3))
|
|
|
|
c.update(c=1)
|
2009-01-12 21:05:03 -04:00
|
|
|
c.update(Counter('a' * 50 + 'b' * 30))
|
2009-01-13 21:15:06 -04:00
|
|
|
c.update() # test case with no args
|
2009-01-12 21:05:03 -04:00
|
|
|
c.__init__('a' * 500 + 'b' * 300)
|
2009-01-12 18:58:41 -04:00
|
|
|
c.__init__('cdc')
|
2009-01-13 21:15:06 -04:00
|
|
|
c.__init__()
|
2009-01-12 18:58:41 -04:00
|
|
|
self.assertEqual(c, dict(a=555, b=333, c=3, d=1))
|
|
|
|
self.assertEqual(c.setdefault('d', 5), 1)
|
|
|
|
self.assertEqual(c['d'], 1)
|
|
|
|
self.assertEqual(c.setdefault('e', 5), 5)
|
|
|
|
self.assertEqual(c['e'], 5)
|
|
|
|
|
|
|
|
def test_copying(self):
|
|
|
|
# Check that counters are copyable, deepcopyable, picklable, and
|
|
|
|
#have a repr/eval round-trip
|
|
|
|
words = Counter('which witch had which witches wrist watch'.split())
|
|
|
|
update_test = Counter()
|
|
|
|
update_test.update(words)
|
|
|
|
for i, dup in enumerate([
|
|
|
|
words.copy(),
|
|
|
|
copy.copy(words),
|
|
|
|
copy.deepcopy(words),
|
|
|
|
pickle.loads(pickle.dumps(words, 0)),
|
|
|
|
pickle.loads(pickle.dumps(words, 1)),
|
|
|
|
pickle.loads(pickle.dumps(words, 2)),
|
|
|
|
pickle.loads(pickle.dumps(words, -1)),
|
|
|
|
cPickle.loads(cPickle.dumps(words, 0)),
|
|
|
|
cPickle.loads(cPickle.dumps(words, 1)),
|
|
|
|
cPickle.loads(cPickle.dumps(words, 2)),
|
|
|
|
cPickle.loads(cPickle.dumps(words, -1)),
|
|
|
|
eval(repr(words)),
|
|
|
|
update_test,
|
2009-01-12 21:05:03 -04:00
|
|
|
Counter(words),
|
2009-01-12 18:58:41 -04:00
|
|
|
]):
|
|
|
|
msg = (i, dup, words)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(dup is not words)
|
2009-01-12 18:58:41 -04:00
|
|
|
self.assertEquals(dup, words)
|
|
|
|
self.assertEquals(len(dup), len(words))
|
|
|
|
self.assertEquals(type(dup), type(words))
|
|
|
|
|
|
|
|
def test_conversions(self):
|
|
|
|
# Convert to: set, list, dict
|
|
|
|
s = 'she sells sea shells by the sea shore'
|
|
|
|
self.assertEqual(sorted(Counter(s).elements()), sorted(s))
|
|
|
|
self.assertEqual(sorted(Counter(s)), sorted(set(s)))
|
|
|
|
self.assertEqual(dict(Counter(s)), dict(Counter(s).items()))
|
|
|
|
self.assertEqual(set(Counter(s)), set(s))
|
|
|
|
|
2009-01-21 19:12:51 -04:00
|
|
|
def test_invariant_for_the_in_operator(self):
|
|
|
|
c = Counter(a=10, b=-2, c=0)
|
|
|
|
for elem in c:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(elem in c)
|
2009-01-21 19:12:51 -04:00
|
|
|
|
2009-01-19 21:19:26 -04:00
|
|
|
def test_multiset_operations(self):
|
|
|
|
# Verify that adding a zero counter will strip zeros and negatives
|
|
|
|
c = Counter(a=10, b=-2, c=0) + Counter()
|
|
|
|
self.assertEqual(dict(c), dict(a=10))
|
|
|
|
|
|
|
|
elements = 'abcd'
|
|
|
|
for i in range(1000):
|
|
|
|
# test random pairs of multisets
|
|
|
|
p = Counter(dict((elem, randrange(-2,4)) for elem in elements))
|
2009-01-21 16:31:50 -04:00
|
|
|
p.update(e=1, f=-1, g=0)
|
2009-01-19 21:19:26 -04:00
|
|
|
q = Counter(dict((elem, randrange(-2,4)) for elem in elements))
|
2009-01-21 16:31:50 -04:00
|
|
|
q.update(h=1, i=-1, j=0)
|
|
|
|
for counterop, numberop in [
|
|
|
|
(Counter.__add__, lambda x, y: max(0, x+y)),
|
|
|
|
(Counter.__sub__, lambda x, y: max(0, x-y)),
|
|
|
|
(Counter.__or__, lambda x, y: max(0,x,y)),
|
|
|
|
(Counter.__and__, lambda x, y: max(0, min(x,y))),
|
2009-01-19 21:19:26 -04:00
|
|
|
]:
|
|
|
|
result = counterop(p, q)
|
|
|
|
for x in elements:
|
2009-01-21 16:31:50 -04:00
|
|
|
self.assertEqual(numberop(p[x], q[x]), result[x],
|
|
|
|
(counterop, x, p, q))
|
2009-01-19 21:19:26 -04:00
|
|
|
# verify that results exclude non-positive counts
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(x>0 for x in result.values())
|
2009-01-19 21:19:26 -04:00
|
|
|
|
|
|
|
elements = 'abcdef'
|
|
|
|
for i in range(100):
|
|
|
|
# verify that random multisets with no repeats are exactly like sets
|
|
|
|
p = Counter(dict((elem, randrange(0, 2)) for elem in elements))
|
|
|
|
q = Counter(dict((elem, randrange(0, 2)) for elem in elements))
|
|
|
|
for counterop, setop in [
|
|
|
|
(Counter.__sub__, set.__sub__),
|
|
|
|
(Counter.__or__, set.__or__),
|
|
|
|
(Counter.__and__, set.__and__),
|
|
|
|
]:
|
|
|
|
counter_result = counterop(p, q)
|
|
|
|
set_result = setop(set(p.elements()), set(q.elements()))
|
|
|
|
self.assertEqual(counter_result, dict.fromkeys(set_result, 1))
|
2009-01-12 18:58:41 -04:00
|
|
|
|
2009-03-03 00:45:34 -04:00
|
|
|
class TestOrderedDict(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
OrderedDict([('a', 1), ('b', 2)], None) # too many args
|
|
|
|
pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
|
|
|
|
self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs) # dict input
|
|
|
|
self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs) # kwds input
|
|
|
|
self.assertEqual(list(OrderedDict(pairs).items()), pairs) # pairs input
|
|
|
|
self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)],
|
|
|
|
c=3, e=5).items()), pairs) # mixed input
|
|
|
|
|
|
|
|
# make sure no positional args conflict with possible kwdargs
|
|
|
|
self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args,
|
|
|
|
['self'])
|
|
|
|
|
|
|
|
# Make sure that direct calls to __init__ do not clear previous contents
|
|
|
|
d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
|
|
|
|
d.__init__([('e', 5), ('f', 6)], g=7, d=4)
|
|
|
|
self.assertEqual(list(d.items()),
|
|
|
|
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
|
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
OrderedDict().update([('a', 1), ('b', 2)], None) # too many args
|
|
|
|
pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
|
|
|
|
od = OrderedDict()
|
|
|
|
od.update(dict(pairs))
|
|
|
|
self.assertEqual(sorted(od.items()), pairs) # dict input
|
|
|
|
od = OrderedDict()
|
|
|
|
od.update(**dict(pairs))
|
|
|
|
self.assertEqual(sorted(od.items()), pairs) # kwds input
|
|
|
|
od = OrderedDict()
|
|
|
|
od.update(pairs)
|
|
|
|
self.assertEqual(list(od.items()), pairs) # pairs input
|
|
|
|
od = OrderedDict()
|
|
|
|
od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
|
|
|
|
self.assertEqual(list(od.items()), pairs) # mixed input
|
|
|
|
|
|
|
|
# Make sure that direct calls to update do not clear previous contents
|
|
|
|
# add that updates items are not moved to the end
|
|
|
|
d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
|
|
|
|
d.update([('e', 5), ('f', 6)], g=7, d=4)
|
|
|
|
self.assertEqual(list(d.items()),
|
|
|
|
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])
|
|
|
|
|
|
|
|
def test_clear(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
shuffle(pairs)
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
self.assertEqual(len(od), len(pairs))
|
|
|
|
od.clear()
|
|
|
|
self.assertEqual(len(od), 0)
|
|
|
|
|
|
|
|
def test_delitem(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
del od['a']
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue('a' not in od)
|
2009-03-03 00:45:34 -04:00
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
del od['a']
|
|
|
|
self.assertEqual(list(od.items()), pairs[:2] + pairs[3:])
|
|
|
|
|
|
|
|
def test_setitem(self):
|
|
|
|
od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)])
|
|
|
|
od['c'] = 10 # existing element
|
|
|
|
od['f'] = 20 # new element
|
|
|
|
self.assertEqual(list(od.items()),
|
|
|
|
[('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)])
|
|
|
|
|
|
|
|
def test_iterators(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
shuffle(pairs)
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
self.assertEqual(list(od), [t[0] for t in pairs])
|
2009-03-03 03:12:09 -04:00
|
|
|
self.assertEqual(od.keys()[:], [t[0] for t in pairs])
|
|
|
|
self.assertEqual(od.values()[:], [t[1] for t in pairs])
|
|
|
|
self.assertEqual(od.items()[:], pairs)
|
|
|
|
self.assertEqual(list(od.iterkeys()), [t[0] for t in pairs])
|
|
|
|
self.assertEqual(list(od.itervalues()), [t[1] for t in pairs])
|
|
|
|
self.assertEqual(list(od.iteritems()), pairs)
|
2009-03-03 00:45:34 -04:00
|
|
|
self.assertEqual(list(reversed(od)),
|
|
|
|
[t[0] for t in reversed(pairs)])
|
|
|
|
|
|
|
|
def test_popitem(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
shuffle(pairs)
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
while pairs:
|
|
|
|
self.assertEqual(od.popitem(), pairs.pop())
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
od.popitem()
|
|
|
|
self.assertEqual(len(od), 0)
|
|
|
|
|
|
|
|
def test_pop(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
shuffle(pairs)
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
shuffle(pairs)
|
|
|
|
while pairs:
|
|
|
|
k, v = pairs.pop()
|
|
|
|
self.assertEqual(od.pop(k), v)
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
od.pop('xyz')
|
|
|
|
self.assertEqual(len(od), 0)
|
|
|
|
self.assertEqual(od.pop(k, 12345), 12345)
|
|
|
|
|
|
|
|
def test_equality(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
shuffle(pairs)
|
|
|
|
od1 = OrderedDict(pairs)
|
|
|
|
od2 = OrderedDict(pairs)
|
|
|
|
self.assertEqual(od1, od2) # same order implies equality
|
|
|
|
pairs = pairs[2:] + pairs[:2]
|
|
|
|
od2 = OrderedDict(pairs)
|
|
|
|
self.assertNotEqual(od1, od2) # different order implies inequality
|
|
|
|
# comparison to regular dict is not order sensitive
|
|
|
|
self.assertEqual(od1, dict(od2))
|
|
|
|
self.assertEqual(dict(od2), od1)
|
|
|
|
# different length implied inequality
|
|
|
|
self.assertNotEqual(od1, OrderedDict(pairs[:-1]))
|
|
|
|
|
|
|
|
def test_copying(self):
|
|
|
|
# Check that ordered dicts are copyable, deepcopyable, picklable,
|
|
|
|
# and have a repr/eval round-trip
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
update_test = OrderedDict()
|
|
|
|
update_test.update(od)
|
|
|
|
for i, dup in enumerate([
|
|
|
|
od.copy(),
|
|
|
|
copy.copy(od),
|
|
|
|
copy.deepcopy(od),
|
|
|
|
pickle.loads(pickle.dumps(od, 0)),
|
|
|
|
pickle.loads(pickle.dumps(od, 1)),
|
|
|
|
pickle.loads(pickle.dumps(od, 2)),
|
|
|
|
pickle.loads(pickle.dumps(od, -1)),
|
|
|
|
eval(repr(od)),
|
|
|
|
update_test,
|
|
|
|
OrderedDict(od),
|
|
|
|
]):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(dup is not od)
|
2009-03-03 00:45:34 -04:00
|
|
|
self.assertEquals(dup, od)
|
|
|
|
self.assertEquals(list(dup.items()), list(od.items()))
|
|
|
|
self.assertEquals(len(dup), len(od))
|
|
|
|
self.assertEquals(type(dup), type(od))
|
|
|
|
|
2009-03-03 18:59:25 -04:00
|
|
|
def test_yaml_linkage(self):
|
|
|
|
# Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
|
|
|
|
# In yaml, lists are native but tuples are not.
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
# yaml.dump(od) -->
|
|
|
|
# '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1]))
|
2009-03-03 18:59:25 -04:00
|
|
|
|
|
|
|
def test_reduce_not_too_fat(self):
|
|
|
|
# do not save instance dictionary if not needed
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
od = OrderedDict(pairs)
|
2009-06-12 20:03:35 -03:00
|
|
|
self.assertEqual(len(od.__reduce__()), 2)
|
2009-03-03 18:59:25 -04:00
|
|
|
od.x = 10
|
2009-06-12 20:03:35 -03:00
|
|
|
self.assertEqual(len(od.__reduce__()), 3)
|
2009-03-03 18:59:25 -04:00
|
|
|
|
2009-03-03 00:45:34 -04:00
|
|
|
def test_repr(self):
|
|
|
|
od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
|
|
|
|
self.assertEqual(repr(od),
|
|
|
|
"OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])")
|
|
|
|
self.assertEqual(eval(repr(od)), od)
|
|
|
|
self.assertEqual(repr(OrderedDict()), "OrderedDict()")
|
|
|
|
|
|
|
|
def test_setdefault(self):
|
|
|
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
|
|
|
shuffle(pairs)
|
|
|
|
od = OrderedDict(pairs)
|
|
|
|
pair_order = list(od.items())
|
|
|
|
self.assertEqual(od.setdefault('a', 10), 3)
|
|
|
|
# make sure order didn't change
|
|
|
|
self.assertEqual(list(od.items()), pair_order)
|
|
|
|
self.assertEqual(od.setdefault('x', 10), 10)
|
|
|
|
# make sure 'x' is added to the end
|
|
|
|
self.assertEqual(list(od.items())[-1], ('x', 10))
|
|
|
|
|
|
|
|
def test_reinsert(self):
|
|
|
|
# Given insert a, insert b, delete a, re-insert a,
|
|
|
|
# verify that a is now later than b.
|
|
|
|
od = OrderedDict()
|
|
|
|
od['a'] = 1
|
|
|
|
od['b'] = 2
|
|
|
|
del od['a']
|
|
|
|
od['a'] = 1
|
|
|
|
self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
|
|
|
|
type2test = OrderedDict
|
|
|
|
|
2009-03-19 16:59:58 -03:00
|
|
|
def test_popitem(self):
|
|
|
|
d = self._empty_mapping()
|
|
|
|
self.assertRaises(KeyError, d.popitem)
|
|
|
|
|
2009-03-03 00:45:34 -04:00
|
|
|
class MyOrderedDict(OrderedDict):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol):
|
|
|
|
type2test = MyOrderedDict
|
|
|
|
|
2009-03-19 16:59:58 -03:00
|
|
|
def test_popitem(self):
|
|
|
|
d = self._empty_mapping()
|
|
|
|
self.assertRaises(KeyError, d.popitem)
|
2009-03-03 00:45:34 -04:00
|
|
|
|
2008-01-10 20:23:13 -04:00
|
|
|
import doctest, collections
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2007-03-01 02:16:43 -04:00
|
|
|
def test_main(verbose=None):
|
2008-04-01 21:55:04 -03:00
|
|
|
NamedTupleDocs = doctest.DocTestSuite(module=collections)
|
2009-01-12 18:58:41 -04:00
|
|
|
test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs,
|
2009-03-03 00:45:34 -04:00
|
|
|
TestCollectionABCs, TestCounter,
|
|
|
|
TestOrderedDict, GeneralMappingTests, SubclassMappingTests]
|
2007-03-01 02:16:43 -04:00
|
|
|
test_support.run_unittest(*test_classes)
|
2008-04-01 21:55:04 -03:00
|
|
|
test_support.run_doctest(collections, verbose)
|
2007-03-01 02:16:43 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|