Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1 | """Unit tests for collections.py.""" |
| 2 | |
Benjamin Peterson | 4ad6bd5 | 2010-05-21 20:55:22 +0000 | [diff] [blame] | 3 | import unittest, doctest, operator |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 4 | import inspect |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 6 | from collections import namedtuple, Counter, OrderedDict, _count_elements |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 7 | from test import mapping_tests |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 8 | import pickle, copy |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 9 | from random import randrange, shuffle |
Raymond Hettinger | 499b2ee | 2009-05-27 01:53:46 +0000 | [diff] [blame] | 10 | import keyword |
| 11 | import re |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 12 | import sys |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 13 | from collections import _ChainMap |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 14 | from collections import Hashable, Iterable, Iterator |
| 15 | from collections import Sized, Container, Callable |
| 16 | from collections import Set, MutableSet |
Raymond Hettinger | 9117c75 | 2010-08-22 07:44:24 +0000 | [diff] [blame] | 17 | from collections import Mapping, MutableMapping, KeysView, ItemsView, UserDict |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 18 | from collections import Sequence, MutableSequence |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 19 | from collections import ByteString |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 20 | |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 21 | |
| 22 | ################################################################################ |
| 23 | ### _ChainMap (helper class for configparser) |
| 24 | ################################################################################ |
| 25 | |
| 26 | class TestChainMap(unittest.TestCase): |
| 27 | |
| 28 | def test_basics(self): |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 29 | c = _ChainMap() |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 30 | c['a'] = 1 |
| 31 | c['b'] = 2 |
| 32 | d = c.new_child() |
| 33 | d['b'] = 20 |
| 34 | d['c'] = 30 |
| 35 | self.assertEqual(d.maps, [{'b':20, 'c':30}, {'a':1, 'b':2}]) # check internal state |
| 36 | self.assertEqual(d.items(), dict(a=1, b=20, c=30).items()) # check items/iter/getitem |
| 37 | self.assertEqual(len(d), 3) # check len |
| 38 | for key in 'abc': # check contains |
| 39 | self.assertIn(key, d) |
| 40 | for k, v in dict(a=1, b=20, c=30, z=100).items(): # check get |
| 41 | self.assertEqual(d.get(k, 100), v) |
| 42 | |
| 43 | del d['b'] # unmask a value |
| 44 | self.assertEqual(d.maps, [{'c':30}, {'a':1, 'b':2}]) # check internal state |
| 45 | self.assertEqual(d.items(), dict(a=1, b=2, c=30).items()) # check items/iter/getitem |
| 46 | self.assertEqual(len(d), 3) # check len |
| 47 | for key in 'abc': # check contains |
| 48 | self.assertIn(key, d) |
| 49 | for k, v in dict(a=1, b=2, c=30, z=100).items(): # check get |
| 50 | self.assertEqual(d.get(k, 100), v) |
| 51 | self.assertIn(repr(d), [ # check repr |
| 52 | type(d).__name__ + "({'c': 30}, {'a': 1, 'b': 2})", |
| 53 | type(d).__name__ + "({'c': 30}, {'b': 2, 'a': 1})" |
| 54 | ]) |
| 55 | |
| 56 | for e in d.copy(), copy.copy(d): # check shallow copies |
| 57 | self.assertEqual(d, e) |
| 58 | self.assertEqual(d.maps, e.maps) |
| 59 | self.assertIsNot(d, e) |
| 60 | self.assertIsNot(d.maps[0], e.maps[0]) |
| 61 | for m1, m2 in zip(d.maps[1:], e.maps[1:]): |
| 62 | self.assertIs(m1, m2) |
| 63 | |
| 64 | for e in [pickle.loads(pickle.dumps(d)), |
| 65 | copy.deepcopy(d), |
| 66 | eval(repr(d)) |
| 67 | ]: # check deep copies |
| 68 | self.assertEqual(d, e) |
| 69 | self.assertEqual(d.maps, e.maps) |
| 70 | self.assertIsNot(d, e) |
| 71 | for m1, m2 in zip(d.maps, e.maps): |
| 72 | self.assertIsNot(m1, m2, e) |
| 73 | |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 74 | d = d.new_child() |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 75 | d['b'] = 5 |
| 76 | self.assertEqual(d.maps, [{'b': 5}, {'c':30}, {'a':1, 'b':2}]) |
| 77 | self.assertEqual(d.parents.maps, [{'c':30}, {'a':1, 'b':2}]) # check parents |
| 78 | self.assertEqual(d['b'], 5) # find first in chain |
| 79 | self.assertEqual(d.parents['b'], 2) # look beyond maps[0] |
| 80 | |
| 81 | def test_contructor(self): |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 82 | self.assertEqual(_ChainMap().maps, [{}]) # no-args --> one new dict |
| 83 | self.assertEqual(_ChainMap({1:2}).maps, [{1:2}]) # 1 arg --> list |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 84 | |
| 85 | def test_missing(self): |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 86 | class DefaultChainMap(_ChainMap): |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 87 | def __missing__(self, key): |
| 88 | return 999 |
| 89 | d = DefaultChainMap(dict(a=1, b=2), dict(b=20, c=30)) |
| 90 | for k, v in dict(a=1, b=2, c=30, d=999).items(): |
| 91 | self.assertEqual(d[k], v) # check __getitem__ w/missing |
| 92 | for k, v in dict(a=1, b=2, c=30, d=77).items(): |
| 93 | self.assertEqual(d.get(k, 77), v) # check get() w/ missing |
| 94 | for k, v in dict(a=True, b=True, c=True, d=False).items(): |
| 95 | self.assertEqual(k in d, v) # check __contains__ w/missing |
| 96 | self.assertEqual(d.pop('a', 1001), 1, d) |
| 97 | self.assertEqual(d.pop('a', 1002), 1002) # check pop() w/missing |
| 98 | self.assertEqual(d.popitem(), ('b', 2)) # check popitem() w/missing |
| 99 | with self.assertRaises(KeyError): |
| 100 | d.popitem() |
| 101 | |
| 102 | def test_dict_coercion(self): |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 103 | d = _ChainMap(dict(a=1, b=2), dict(b=20, c=30)) |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 104 | self.assertEqual(dict(d), dict(a=1, b=2, c=30)) |
| 105 | self.assertEqual(dict(d.items()), dict(a=1, b=2, c=30)) |
| 106 | |
| 107 | |
| 108 | ################################################################################ |
| 109 | ### Named Tuples |
| 110 | ################################################################################ |
| 111 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 112 | TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 113 | |
| 114 | class TestNamedTuple(unittest.TestCase): |
| 115 | |
| 116 | def test_factory(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 117 | Point = namedtuple('Point', 'x y') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 118 | self.assertEqual(Point.__name__, 'Point') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 119 | self.assertEqual(Point.__slots__, ()) |
| 120 | self.assertEqual(Point.__module__, __name__) |
| 121 | self.assertEqual(Point.__getitem__, tuple.__getitem__) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 122 | self.assertEqual(Point._fields, ('x', 'y')) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 123 | |
| 124 | self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char |
| 125 | self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword |
| 126 | self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit |
| 127 | |
| 128 | self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char |
| 129 | self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword |
| 130 | self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 131 | self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 132 | self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field |
| 133 | |
| 134 | namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 135 | namedtuple('_', 'a b c') # Test leading underscores in a typename |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 137 | nt = namedtuple('nt', 'the quick brown fox') # check unicode input |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 138 | self.assertNotIn("u'", repr(nt._fields)) |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 139 | nt = namedtuple('nt', ('the', 'quick')) # check unicode input |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 140 | self.assertNotIn("u'", repr(nt._fields)) |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 141 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 142 | self.assertRaises(TypeError, Point._make, [11]) # catch too few args |
| 143 | self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args |
| 144 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 145 | @unittest.skipIf(sys.flags.optimize >= 2, |
| 146 | "Docstrings are omitted with -O2 and above") |
| 147 | def test_factory_doc_attr(self): |
| 148 | Point = namedtuple('Point', 'x y') |
| 149 | self.assertEqual(Point.__doc__, 'Point(x, y)') |
| 150 | |
Benjamin Peterson | a86f2c0 | 2009-02-10 02:41:10 +0000 | [diff] [blame] | 151 | def test_name_fixer(self): |
| 152 | for spec, renamed in [ |
Raymond Hettinger | 5614524 | 2009-04-02 22:31:59 +0000 | [diff] [blame] | 153 | [('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char |
| 154 | [('abc', 'class'), ('abc', '_1')], # field has keyword |
| 155 | [('8efg', '9ghi'), ('_0', '_1')], # field starts with digit |
| 156 | [('abc', '_efg'), ('abc', '_1')], # field with leading underscore |
| 157 | [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')], # duplicate field |
| 158 | [('abc', '', 'x'), ('abc', '_1', 'x')], # fieldname is a space |
Benjamin Peterson | a86f2c0 | 2009-02-10 02:41:10 +0000 | [diff] [blame] | 159 | ]: |
| 160 | self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed) |
| 161 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 162 | def test_instance(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 163 | Point = namedtuple('Point', 'x y') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 164 | p = Point(11, 22) |
| 165 | self.assertEqual(p, Point(x=11, y=22)) |
| 166 | self.assertEqual(p, Point(11, y=22)) |
| 167 | self.assertEqual(p, Point(y=22, x=11)) |
| 168 | self.assertEqual(p, Point(*(11, 22))) |
| 169 | self.assertEqual(p, Point(**dict(x=11, y=22))) |
| 170 | self.assertRaises(TypeError, Point, 1) # too few args |
| 171 | self.assertRaises(TypeError, Point, 1, 2, 3) # too many args |
| 172 | self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument |
| 173 | self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument |
| 174 | self.assertEqual(repr(p), 'Point(x=11, y=22)') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 175 | self.assertNotIn('__dict__', dir(p)) # verify instance has no dict |
| 176 | self.assertNotIn('__weakref__', dir(p)) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 177 | self.assertEqual(p, Point._make([11, 22])) # test _make classmethod |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 178 | self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute |
| 179 | self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method |
| 180 | self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 182 | try: |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 183 | p._replace(x=1, error=2) |
| 184 | except ValueError: |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 185 | pass |
| 186 | else: |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 187 | self._fail('Did not detect an incorrect fieldname') |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 188 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 189 | # verify that field string can have commas |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 190 | Point = namedtuple('Point', 'x, y') |
| 191 | p = Point(x=11, y=22) |
| 192 | self.assertEqual(repr(p), 'Point(x=11, y=22)') |
| 193 | |
| 194 | # verify that fieldspec can be a non-string sequence |
| 195 | Point = namedtuple('Point', ('x', 'y')) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 196 | p = Point(x=11, y=22) |
| 197 | self.assertEqual(repr(p), 'Point(x=11, y=22)') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 198 | |
| 199 | def test_tupleness(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 200 | Point = namedtuple('Point', 'x y') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 201 | p = Point(11, 22) |
| 202 | |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 203 | self.assertIsInstance(p, tuple) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 204 | self.assertEqual(p, (11, 22)) # matches a real tuple |
| 205 | self.assertEqual(tuple(p), (11, 22)) # coercable to a real tuple |
| 206 | self.assertEqual(list(p), [11, 22]) # coercable to a list |
| 207 | self.assertEqual(max(p), 22) # iterable |
| 208 | self.assertEqual(max(*p), 22) # star-able |
| 209 | x, y = p |
| 210 | self.assertEqual(p, (x, y)) # unpacks like a tuple |
| 211 | self.assertEqual((p[0], p[1]), (11, 22)) # indexable like a tuple |
| 212 | self.assertRaises(IndexError, p.__getitem__, 3) |
| 213 | |
| 214 | self.assertEqual(p.x, x) |
| 215 | self.assertEqual(p.y, y) |
| 216 | self.assertRaises(AttributeError, eval, 'p.z', locals()) |
| 217 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 218 | def test_odd_sizes(self): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 219 | Zero = namedtuple('Zero', '') |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 220 | self.assertEqual(Zero(), ()) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 221 | self.assertEqual(Zero._make([]), ()) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 222 | self.assertEqual(repr(Zero()), 'Zero()') |
| 223 | self.assertEqual(Zero()._asdict(), {}) |
| 224 | self.assertEqual(Zero()._fields, ()) |
| 225 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 226 | Dot = namedtuple('Dot', 'd') |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 227 | self.assertEqual(Dot(1), (1,)) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 228 | self.assertEqual(Dot._make([1]), (1,)) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 229 | self.assertEqual(Dot(1).d, 1) |
| 230 | self.assertEqual(repr(Dot(1)), 'Dot(d=1)') |
| 231 | self.assertEqual(Dot(1)._asdict(), {'d':1}) |
| 232 | self.assertEqual(Dot(1)._replace(d=999), (999,)) |
| 233 | self.assertEqual(Dot(1)._fields, ('d',)) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 234 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 235 | # n = 5000 |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 236 | n = 254 # SyntaxError: more than 255 arguments: |
| 237 | import string, random |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 238 | names = list(set(''.join([random.choice(string.ascii_letters) |
| 239 | for j in range(10)]) for i in range(n))) |
| 240 | n = len(names) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 241 | Big = namedtuple('Big', names) |
| 242 | b = Big(*range(n)) |
| 243 | self.assertEqual(b, tuple(range(n))) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 244 | self.assertEqual(Big._make(range(n)), tuple(range(n))) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 245 | for pos, name in enumerate(names): |
| 246 | self.assertEqual(getattr(b, name), pos) |
| 247 | repr(b) # make sure repr() doesn't blow-up |
| 248 | d = b._asdict() |
| 249 | d_expected = dict(zip(names, range(n))) |
| 250 | self.assertEqual(d, d_expected) |
| 251 | b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)])) |
| 252 | b2_expected = list(range(n)) |
| 253 | b2_expected[1] = 999 |
| 254 | b2_expected[-5] = 42 |
| 255 | self.assertEqual(b2, tuple(b2_expected)) |
| 256 | self.assertEqual(b._fields, tuple(names)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 257 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 258 | def test_pickle(self): |
| 259 | p = TestNT(x=10, y=20, z=30) |
| 260 | for module in (pickle,): |
| 261 | loads = getattr(module, 'loads') |
| 262 | dumps = getattr(module, 'dumps') |
| 263 | for protocol in -1, 0, 1, 2: |
| 264 | q = loads(dumps(p, protocol)) |
| 265 | self.assertEqual(p, q) |
| 266 | self.assertEqual(p._fields, q._fields) |
| 267 | |
| 268 | def test_copy(self): |
| 269 | p = TestNT(x=10, y=20, z=30) |
| 270 | for copier in copy.copy, copy.deepcopy: |
| 271 | q = copier(p) |
| 272 | self.assertEqual(p, q) |
| 273 | self.assertEqual(p._fields, q._fields) |
| 274 | |
Raymond Hettinger | 089ba7f | 2009-05-27 00:38:24 +0000 | [diff] [blame] | 275 | def test_name_conflicts(self): |
| 276 | # Some names like "self", "cls", "tuple", "itemgetter", and "property" |
| 277 | # failed when used as field names. Test to make sure these now work. |
| 278 | T = namedtuple('T', 'itemgetter property self cls tuple') |
| 279 | t = T(1, 2, 3, 4, 5) |
| 280 | self.assertEqual(t, (1,2,3,4,5)) |
| 281 | newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50) |
| 282 | self.assertEqual(newt, (10,20,30,40,50)) |
| 283 | |
Raymond Hettinger | 499b2ee | 2009-05-27 01:53:46 +0000 | [diff] [blame] | 284 | # Broader test of all interesting names in a template |
| 285 | with support.captured_stdout() as template: |
| 286 | T = namedtuple('T', 'x', verbose=True) |
| 287 | words = set(re.findall('[A-Za-z]+', template.getvalue())) |
| 288 | words -= set(keyword.kwlist) |
| 289 | T = namedtuple('T', words) |
| 290 | # test __new__ |
| 291 | values = tuple(range(len(words))) |
| 292 | t = T(*values) |
| 293 | self.assertEqual(t, values) |
| 294 | t = T(**dict(zip(T._fields, values))) |
| 295 | self.assertEqual(t, values) |
| 296 | # test _make |
| 297 | t = T._make(values) |
| 298 | self.assertEqual(t, values) |
| 299 | # exercise __repr__ |
| 300 | repr(t) |
| 301 | # test _asdict |
| 302 | self.assertEqual(t._asdict(), dict(zip(T._fields, values))) |
| 303 | # test _replace |
| 304 | t = T._make(values) |
| 305 | newvalues = tuple(v*10 for v in values) |
| 306 | newt = t._replace(**dict(zip(T._fields, newvalues))) |
| 307 | self.assertEqual(newt, newvalues) |
| 308 | # test _fields |
| 309 | self.assertEqual(T._fields, tuple(words)) |
| 310 | # test __getnewargs__ |
| 311 | self.assertEqual(t.__getnewargs__(), values) |
| 312 | |
Raymond Hettinger | d331ce9 | 2010-08-08 01:13:42 +0000 | [diff] [blame] | 313 | def test_repr(self): |
| 314 | with support.captured_stdout() as template: |
| 315 | A = namedtuple('A', 'x', verbose=True) |
| 316 | self.assertEqual(repr(A(1)), 'A(x=1)') |
| 317 | # repr should show the name of the subclass |
| 318 | class B(A): |
| 319 | pass |
| 320 | self.assertEqual(repr(B(1)), 'B(x=1)') |
| 321 | |
| 322 | |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 323 | ################################################################################ |
| 324 | ### Abstract Base Classes |
| 325 | ################################################################################ |
| 326 | |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 327 | class ABCTestCase(unittest.TestCase): |
| 328 | |
| 329 | def validate_abstract_methods(self, abc, *names): |
| 330 | methodstubs = dict.fromkeys(names, lambda s, *args: 0) |
| 331 | |
| 332 | # everything should work will all required methods are present |
| 333 | C = type('C', (abc,), methodstubs) |
| 334 | C() |
| 335 | |
| 336 | # instantiation should fail if a required method is missing |
| 337 | for name in names: |
| 338 | stubs = methodstubs.copy() |
| 339 | del stubs[name] |
| 340 | C = type('C', (abc,), stubs) |
| 341 | self.assertRaises(TypeError, C, name) |
| 342 | |
Florent Xicluna | ce153f6 | 2010-03-08 15:34:35 +0000 | [diff] [blame] | 343 | def validate_isinstance(self, abc, name): |
| 344 | stub = lambda s, *args: 0 |
| 345 | |
| 346 | C = type('C', (object,), {'__hash__': None}) |
| 347 | setattr(C, name, stub) |
| 348 | self.assertIsInstance(C(), abc) |
| 349 | self.assertTrue(issubclass(C, abc)) |
| 350 | |
| 351 | C = type('C', (object,), {'__hash__': None}) |
| 352 | self.assertNotIsInstance(C(), abc) |
| 353 | self.assertFalse(issubclass(C, abc)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 354 | |
Benjamin Peterson | 4ad6bd5 | 2010-05-21 20:55:22 +0000 | [diff] [blame] | 355 | def validate_comparison(self, instance): |
| 356 | ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub'] |
| 357 | operators = {} |
| 358 | for op in ops: |
| 359 | name = '__' + op + '__' |
| 360 | operators[name] = getattr(operator, name) |
| 361 | |
| 362 | class Other: |
| 363 | def __init__(self): |
| 364 | self.right_side = False |
| 365 | def __eq__(self, other): |
| 366 | self.right_side = True |
| 367 | return True |
| 368 | __lt__ = __eq__ |
| 369 | __gt__ = __eq__ |
| 370 | __le__ = __eq__ |
| 371 | __ge__ = __eq__ |
| 372 | __ne__ = __eq__ |
| 373 | __ror__ = __eq__ |
| 374 | __rand__ = __eq__ |
| 375 | __rxor__ = __eq__ |
| 376 | __rsub__ = __eq__ |
| 377 | |
| 378 | for name, op in operators.items(): |
| 379 | if not hasattr(instance, name): |
| 380 | continue |
| 381 | other = Other() |
| 382 | op(instance, other) |
| 383 | self.assertTrue(other.right_side,'Right side not called for %s.%s' |
| 384 | % (type(instance), name)) |
| 385 | |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 386 | class TestOneTrickPonyABCs(ABCTestCase): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 387 | |
| 388 | def test_Hashable(self): |
| 389 | # Check some non-hashables |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 390 | non_samples = [bytearray(), list(), set(), dict()] |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 391 | for x in non_samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 392 | self.assertNotIsInstance(x, Hashable) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 393 | self.assertFalse(issubclass(type(x), Hashable), repr(type(x))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 394 | # Check some hashables |
| 395 | samples = [None, |
| 396 | int(), float(), complex(), |
Guido van Rossum | 07d4e78 | 2007-07-03 16:59:47 +0000 | [diff] [blame] | 397 | str(), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 398 | tuple(), frozenset(), |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 399 | int, list, object, type, bytes() |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 400 | ] |
| 401 | for x in samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 402 | self.assertIsInstance(x, Hashable) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 403 | self.assertTrue(issubclass(type(x), Hashable), repr(type(x))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 404 | self.assertRaises(TypeError, Hashable) |
| 405 | # Check direct subclassing |
| 406 | class H(Hashable): |
| 407 | def __hash__(self): |
| 408 | return super().__hash__() |
| 409 | self.assertEqual(hash(H()), 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 410 | self.assertFalse(issubclass(int, H)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 411 | self.validate_abstract_methods(Hashable, '__hash__') |
Florent Xicluna | ce153f6 | 2010-03-08 15:34:35 +0000 | [diff] [blame] | 412 | self.validate_isinstance(Hashable, '__hash__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 413 | |
| 414 | def test_Iterable(self): |
| 415 | # Check some non-iterables |
| 416 | non_samples = [None, 42, 3.14, 1j] |
| 417 | for x in non_samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 418 | self.assertNotIsInstance(x, Iterable) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 419 | self.assertFalse(issubclass(type(x), Iterable), repr(type(x))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 420 | # Check some iterables |
Guido van Rossum | 07d4e78 | 2007-07-03 16:59:47 +0000 | [diff] [blame] | 421 | samples = [bytes(), str(), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 422 | tuple(), list(), set(), frozenset(), dict(), |
| 423 | dict().keys(), dict().items(), dict().values(), |
| 424 | (lambda: (yield))(), |
| 425 | (x for x in []), |
| 426 | ] |
| 427 | for x in samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 428 | self.assertIsInstance(x, Iterable) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 429 | self.assertTrue(issubclass(type(x), Iterable), repr(type(x))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 430 | # Check direct subclassing |
| 431 | class I(Iterable): |
| 432 | def __iter__(self): |
| 433 | return super().__iter__() |
| 434 | self.assertEqual(list(I()), []) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 435 | self.assertFalse(issubclass(str, I)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 436 | self.validate_abstract_methods(Iterable, '__iter__') |
Florent Xicluna | ce153f6 | 2010-03-08 15:34:35 +0000 | [diff] [blame] | 437 | self.validate_isinstance(Iterable, '__iter__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 438 | |
| 439 | def test_Iterator(self): |
Guido van Rossum | 07d4e78 | 2007-07-03 16:59:47 +0000 | [diff] [blame] | 440 | non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()] |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 441 | for x in non_samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 442 | self.assertNotIsInstance(x, Iterator) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 443 | self.assertFalse(issubclass(type(x), Iterator), repr(type(x))) |
Guido van Rossum | 07d4e78 | 2007-07-03 16:59:47 +0000 | [diff] [blame] | 444 | samples = [iter(bytes()), iter(str()), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 445 | iter(tuple()), iter(list()), iter(dict()), |
| 446 | iter(set()), iter(frozenset()), |
| 447 | iter(dict().keys()), iter(dict().items()), |
| 448 | iter(dict().values()), |
| 449 | (lambda: (yield))(), |
| 450 | (x for x in []), |
| 451 | ] |
| 452 | for x in samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 453 | self.assertIsInstance(x, Iterator) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 454 | self.assertTrue(issubclass(type(x), Iterator), repr(type(x))) |
Raymond Hettinger | ead2222 | 2010-11-29 03:56:12 +0000 | [diff] [blame] | 455 | self.validate_abstract_methods(Iterator, '__next__', '__iter__') |
| 456 | |
| 457 | # Issue 10565 |
| 458 | class NextOnly: |
| 459 | def __next__(self): |
| 460 | yield 1 |
| 461 | raise StopIteration |
| 462 | self.assertNotIsInstance(NextOnly(), Iterator) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 463 | |
| 464 | def test_Sized(self): |
| 465 | non_samples = [None, 42, 3.14, 1j, |
| 466 | (lambda: (yield))(), |
| 467 | (x for x in []), |
| 468 | ] |
| 469 | for x in non_samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 470 | self.assertNotIsInstance(x, Sized) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 471 | self.assertFalse(issubclass(type(x), Sized), repr(type(x))) |
Guido van Rossum | 07d4e78 | 2007-07-03 16:59:47 +0000 | [diff] [blame] | 472 | samples = [bytes(), str(), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 473 | tuple(), list(), set(), frozenset(), dict(), |
| 474 | dict().keys(), dict().items(), dict().values(), |
| 475 | ] |
| 476 | for x in samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 477 | self.assertIsInstance(x, Sized) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 478 | self.assertTrue(issubclass(type(x), Sized), repr(type(x))) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 479 | self.validate_abstract_methods(Sized, '__len__') |
Florent Xicluna | ce153f6 | 2010-03-08 15:34:35 +0000 | [diff] [blame] | 480 | self.validate_isinstance(Sized, '__len__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 481 | |
| 482 | def test_Container(self): |
| 483 | non_samples = [None, 42, 3.14, 1j, |
| 484 | (lambda: (yield))(), |
| 485 | (x for x in []), |
| 486 | ] |
| 487 | for x in non_samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 488 | self.assertNotIsInstance(x, Container) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 489 | self.assertFalse(issubclass(type(x), Container), repr(type(x))) |
Guido van Rossum | 07d4e78 | 2007-07-03 16:59:47 +0000 | [diff] [blame] | 490 | samples = [bytes(), str(), |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 491 | tuple(), list(), set(), frozenset(), dict(), |
| 492 | dict().keys(), dict().items(), |
| 493 | ] |
| 494 | for x in samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 495 | self.assertIsInstance(x, Container) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 496 | self.assertTrue(issubclass(type(x), Container), repr(type(x))) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 497 | self.validate_abstract_methods(Container, '__contains__') |
Florent Xicluna | ce153f6 | 2010-03-08 15:34:35 +0000 | [diff] [blame] | 498 | self.validate_isinstance(Container, '__contains__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 499 | |
| 500 | def test_Callable(self): |
| 501 | non_samples = [None, 42, 3.14, 1j, |
| 502 | "", b"", (), [], {}, set(), |
| 503 | (lambda: (yield))(), |
| 504 | (x for x in []), |
| 505 | ] |
| 506 | for x in non_samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 507 | self.assertNotIsInstance(x, Callable) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 508 | self.assertFalse(issubclass(type(x), Callable), repr(type(x))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 509 | samples = [lambda: None, |
| 510 | type, int, object, |
| 511 | len, |
| 512 | list.append, [].append, |
| 513 | ] |
| 514 | for x in samples: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 515 | self.assertIsInstance(x, Callable) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 516 | self.assertTrue(issubclass(type(x), Callable), repr(type(x))) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 517 | self.validate_abstract_methods(Callable, '__call__') |
Florent Xicluna | ce153f6 | 2010-03-08 15:34:35 +0000 | [diff] [blame] | 518 | self.validate_isinstance(Callable, '__call__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 519 | |
| 520 | def test_direct_subclassing(self): |
| 521 | for B in Hashable, Iterable, Iterator, Sized, Container, Callable: |
| 522 | class C(B): |
| 523 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 524 | self.assertTrue(issubclass(C, B)) |
| 525 | self.assertFalse(issubclass(int, C)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 526 | |
| 527 | def test_registration(self): |
| 528 | for B in Hashable, Iterable, Iterator, Sized, Container, Callable: |
| 529 | class C: |
| 530 | __hash__ = None # Make sure it isn't hashable by default |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 531 | self.assertFalse(issubclass(C, B), B.__name__) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 532 | B.register(C) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 533 | self.assertTrue(issubclass(C, B)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 534 | |
Raymond Hettinger | 3f10a95 | 2009-04-01 19:05:50 +0000 | [diff] [blame] | 535 | class WithSet(MutableSet): |
| 536 | |
| 537 | def __init__(self, it=()): |
| 538 | self.data = set(it) |
| 539 | |
| 540 | def __len__(self): |
| 541 | return len(self.data) |
| 542 | |
| 543 | def __iter__(self): |
| 544 | return iter(self.data) |
| 545 | |
| 546 | def __contains__(self, item): |
| 547 | return item in self.data |
| 548 | |
| 549 | def add(self, item): |
| 550 | self.data.add(item) |
| 551 | |
| 552 | def discard(self, item): |
| 553 | self.data.discard(item) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 554 | |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 555 | class TestCollectionABCs(ABCTestCase): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 556 | |
| 557 | # XXX For now, we only test some virtual inheritance properties. |
| 558 | # We should also test the proper behavior of the collection ABCs |
| 559 | # as real base classes or mix-in classes. |
| 560 | |
| 561 | def test_Set(self): |
| 562 | for sample in [set, frozenset]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 563 | self.assertIsInstance(sample(), Set) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 564 | self.assertTrue(issubclass(sample, Set)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 565 | self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__') |
Benjamin Peterson | 4ad6bd5 | 2010-05-21 20:55:22 +0000 | [diff] [blame] | 566 | class MySet(Set): |
| 567 | def __contains__(self, x): |
| 568 | return False |
| 569 | def __len__(self): |
| 570 | return 0 |
| 571 | def __iter__(self): |
| 572 | return iter([]) |
| 573 | self.validate_comparison(MySet()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 574 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 575 | def test_hash_Set(self): |
| 576 | class OneTwoThreeSet(Set): |
| 577 | def __init__(self): |
| 578 | self.contents = [1, 2, 3] |
| 579 | def __contains__(self, x): |
| 580 | return x in self.contents |
| 581 | def __len__(self): |
| 582 | return len(self.contents) |
| 583 | def __iter__(self): |
| 584 | return iter(self.contents) |
| 585 | def __hash__(self): |
| 586 | return self._hash() |
| 587 | a, b = OneTwoThreeSet(), OneTwoThreeSet() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 588 | self.assertTrue(hash(a) == hash(b)) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 589 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 590 | def test_MutableSet(self): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 591 | self.assertIsInstance(set(), MutableSet) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 592 | self.assertTrue(issubclass(set, MutableSet)) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 593 | self.assertNotIsInstance(frozenset(), MutableSet) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 594 | self.assertFalse(issubclass(frozenset, MutableSet)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 595 | self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__', |
| 596 | 'add', 'discard') |
| 597 | |
Raymond Hettinger | 3f10a95 | 2009-04-01 19:05:50 +0000 | [diff] [blame] | 598 | def test_issue_5647(self): |
| 599 | # MutableSet.__iand__ mutated the set during iteration |
| 600 | s = WithSet('abcd') |
| 601 | s &= WithSet('cdef') # This used to fail |
| 602 | self.assertEqual(set(s), set('cd')) |
| 603 | |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 604 | def test_issue_4920(self): |
| 605 | # MutableSet.pop() method did not work |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 606 | class MySet(MutableSet): |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 607 | __slots__=['__s'] |
| 608 | def __init__(self,items=None): |
| 609 | if items is None: |
| 610 | items=[] |
| 611 | self.__s=set(items) |
| 612 | def __contains__(self,v): |
| 613 | return v in self.__s |
| 614 | def __iter__(self): |
| 615 | return iter(self.__s) |
| 616 | def __len__(self): |
| 617 | return len(self.__s) |
| 618 | def add(self,v): |
| 619 | result=v not in self.__s |
| 620 | self.__s.add(v) |
| 621 | return result |
| 622 | def discard(self,v): |
| 623 | result=v in self.__s |
| 624 | self.__s.discard(v) |
| 625 | return result |
| 626 | def __repr__(self): |
| 627 | return "MySet(%s)" % repr(list(self)) |
| 628 | s = MySet([5,43,2,1]) |
| 629 | self.assertEqual(s.pop(), 1) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 630 | |
Daniel Stutzbach | 31da5b2 | 2010-08-24 20:49:57 +0000 | [diff] [blame] | 631 | def test_issue8750(self): |
| 632 | empty = WithSet() |
| 633 | full = WithSet(range(10)) |
| 634 | s = WithSet(full) |
| 635 | s -= s |
| 636 | self.assertEqual(s, empty) |
| 637 | s = WithSet(full) |
| 638 | s ^= s |
| 639 | self.assertEqual(s, empty) |
| 640 | s = WithSet(full) |
| 641 | s &= s |
| 642 | self.assertEqual(s, full) |
| 643 | s |= s |
| 644 | self.assertEqual(s, full) |
| 645 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 646 | def test_Mapping(self): |
| 647 | for sample in [dict]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 648 | self.assertIsInstance(sample(), Mapping) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 649 | self.assertTrue(issubclass(sample, Mapping)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 650 | self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__', |
| 651 | '__getitem__') |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 652 | class MyMapping(Mapping): |
Benjamin Peterson | 4ad6bd5 | 2010-05-21 20:55:22 +0000 | [diff] [blame] | 653 | def __len__(self): |
| 654 | return 0 |
| 655 | def __getitem__(self, i): |
| 656 | raise IndexError |
| 657 | def __iter__(self): |
| 658 | return iter(()) |
| 659 | self.validate_comparison(MyMapping()) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 660 | |
| 661 | def test_MutableMapping(self): |
| 662 | for sample in [dict]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 663 | self.assertIsInstance(sample(), MutableMapping) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 664 | self.assertTrue(issubclass(sample, MutableMapping)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 665 | self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__', |
| 666 | '__getitem__', '__setitem__', '__delitem__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 667 | |
Raymond Hettinger | 9117c75 | 2010-08-22 07:44:24 +0000 | [diff] [blame] | 668 | def test_MutableMapping_subclass(self): |
| 669 | # Test issue 9214 |
| 670 | mymap = UserDict() |
| 671 | mymap['red'] = 5 |
| 672 | self.assertIsInstance(mymap.keys(), Set) |
| 673 | self.assertIsInstance(mymap.keys(), KeysView) |
| 674 | self.assertIsInstance(mymap.items(), Set) |
| 675 | self.assertIsInstance(mymap.items(), ItemsView) |
| 676 | |
| 677 | mymap = UserDict() |
| 678 | mymap['red'] = 5 |
| 679 | z = mymap.keys() | {'orange'} |
| 680 | self.assertIsInstance(z, set) |
| 681 | list(z) |
| 682 | mymap['blue'] = 7 # Shouldn't affect 'z' |
| 683 | self.assertEqual(sorted(z), ['orange', 'red']) |
| 684 | |
| 685 | mymap = UserDict() |
| 686 | mymap['red'] = 5 |
| 687 | z = mymap.items() | {('orange', 3)} |
| 688 | self.assertIsInstance(z, set) |
| 689 | list(z) |
| 690 | mymap['blue'] = 7 # Shouldn't affect 'z' |
| 691 | self.assertEqual(sorted(z), [('orange', 3), ('red', 5)]) |
| 692 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 693 | def test_Sequence(self): |
| 694 | for sample in [tuple, list, bytes, str]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 695 | self.assertIsInstance(sample(), Sequence) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 696 | self.assertTrue(issubclass(sample, Sequence)) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 697 | self.assertIsInstance(range(10), Sequence) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 698 | self.assertTrue(issubclass(range, Sequence)) |
| 699 | self.assertTrue(issubclass(str, Sequence)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 700 | self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__', |
| 701 | '__getitem__') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 702 | |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 703 | def test_ByteString(self): |
| 704 | for sample in [bytes, bytearray]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 705 | self.assertIsInstance(sample(), ByteString) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 706 | self.assertTrue(issubclass(sample, ByteString)) |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 707 | for sample in [str, list, tuple]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 708 | self.assertNotIsInstance(sample(), ByteString) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 709 | self.assertFalse(issubclass(sample, ByteString)) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 710 | self.assertNotIsInstance(memoryview(b""), ByteString) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 711 | self.assertFalse(issubclass(memoryview, ByteString)) |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 713 | def test_MutableSequence(self): |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 714 | for sample in [tuple, str, bytes]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 715 | self.assertNotIsInstance(sample(), MutableSequence) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 716 | self.assertFalse(issubclass(sample, MutableSequence)) |
Guido van Rossum | d05eb00 | 2007-11-21 22:26:24 +0000 | [diff] [blame] | 717 | for sample in [list, bytearray]: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 718 | self.assertIsInstance(sample(), MutableSequence) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 719 | self.assertTrue(issubclass(sample, MutableSequence)) |
| 720 | self.assertFalse(issubclass(str, MutableSequence)) |
Raymond Hettinger | ae65018 | 2009-01-28 23:33:59 +0000 | [diff] [blame] | 721 | self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__', |
| 722 | '__len__', '__getitem__', '__setitem__', '__delitem__', 'insert') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 723 | |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 724 | |
| 725 | ################################################################################ |
| 726 | ### Counter |
| 727 | ################################################################################ |
| 728 | |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 729 | class TestCounter(unittest.TestCase): |
| 730 | |
| 731 | def test_basics(self): |
| 732 | c = Counter('abcaba') |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 733 | self.assertEqual(c, Counter({'a':3 , 'b': 2, 'c': 1})) |
| 734 | self.assertEqual(c, Counter(a=3, b=2, c=1)) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 735 | self.assertIsInstance(c, dict) |
| 736 | self.assertIsInstance(c, Mapping) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 737 | self.assertTrue(issubclass(Counter, dict)) |
| 738 | self.assertTrue(issubclass(Counter, Mapping)) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 739 | self.assertEqual(len(c), 3) |
| 740 | self.assertEqual(sum(c.values()), 6) |
| 741 | self.assertEqual(sorted(c.values()), [1, 2, 3]) |
| 742 | self.assertEqual(sorted(c.keys()), ['a', 'b', 'c']) |
| 743 | self.assertEqual(sorted(c), ['a', 'b', 'c']) |
| 744 | self.assertEqual(sorted(c.items()), |
| 745 | [('a', 3), ('b', 2), ('c', 1)]) |
| 746 | self.assertEqual(c['b'], 2) |
| 747 | self.assertEqual(c['z'], 0) |
| 748 | self.assertEqual(c.__contains__('c'), True) |
| 749 | self.assertEqual(c.__contains__('z'), False) |
| 750 | self.assertEqual(c.get('b', 10), 2) |
| 751 | self.assertEqual(c.get('z', 10), 10) |
| 752 | self.assertEqual(c, dict(a=3, b=2, c=1)) |
| 753 | self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})") |
| 754 | self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)]) |
| 755 | for i in range(5): |
| 756 | self.assertEqual(c.most_common(i), |
| 757 | [('a', 3), ('b', 2), ('c', 1)][:i]) |
| 758 | self.assertEqual(''.join(sorted(c.elements())), 'aaabbc') |
| 759 | c['a'] += 1 # increment an existing value |
| 760 | c['b'] -= 2 # sub existing value to zero |
| 761 | del c['c'] # remove an entry |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 762 | del c['c'] # make sure that del doesn't raise KeyError |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 763 | c['d'] -= 2 # sub from a missing value |
| 764 | c['e'] = -5 # directly assign a missing value |
| 765 | c['f'] += 4 # add to a missing value |
| 766 | self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4)) |
| 767 | self.assertEqual(''.join(sorted(c.elements())), 'aaaaffff') |
| 768 | self.assertEqual(c.pop('f'), 4) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 769 | self.assertNotIn('f', c) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 770 | for i in range(3): |
| 771 | elem, cnt = c.popitem() |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 772 | self.assertNotIn(elem, c) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 773 | c.clear() |
| 774 | self.assertEqual(c, {}) |
| 775 | self.assertEqual(repr(c), 'Counter()') |
| 776 | self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc') |
| 777 | self.assertRaises(TypeError, hash, c) |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 778 | c.update(dict(a=5, b=3)) |
| 779 | c.update(c=1) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 780 | c.update(Counter('a' * 50 + 'b' * 30)) |
| 781 | c.update() # test case with no args |
| 782 | c.__init__('a' * 500 + 'b' * 300) |
| 783 | c.__init__('cdc') |
| 784 | c.__init__() |
| 785 | self.assertEqual(c, dict(a=555, b=333, c=3, d=1)) |
| 786 | self.assertEqual(c.setdefault('d', 5), 1) |
| 787 | self.assertEqual(c['d'], 1) |
| 788 | self.assertEqual(c.setdefault('e', 5), 5) |
| 789 | self.assertEqual(c['e'], 5) |
| 790 | |
| 791 | def test_copying(self): |
| 792 | # Check that counters are copyable, deepcopyable, picklable, and |
| 793 | #have a repr/eval round-trip |
| 794 | words = Counter('which witch had which witches wrist watch'.split()) |
| 795 | update_test = Counter() |
| 796 | update_test.update(words) |
| 797 | for i, dup in enumerate([ |
| 798 | words.copy(), |
| 799 | copy.copy(words), |
| 800 | copy.deepcopy(words), |
| 801 | pickle.loads(pickle.dumps(words, 0)), |
| 802 | pickle.loads(pickle.dumps(words, 1)), |
| 803 | pickle.loads(pickle.dumps(words, 2)), |
| 804 | pickle.loads(pickle.dumps(words, -1)), |
| 805 | eval(repr(words)), |
| 806 | update_test, |
| 807 | Counter(words), |
| 808 | ]): |
| 809 | msg = (i, dup, words) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 810 | self.assertTrue(dup is not words) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 811 | self.assertEqual(dup, words) |
| 812 | self.assertEqual(len(dup), len(words)) |
| 813 | self.assertEqual(type(dup), type(words)) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 814 | |
Raymond Hettinger | 1c746c2 | 2011-04-15 13:16:46 -0700 | [diff] [blame] | 815 | def test_copy_subclass(self): |
| 816 | class MyCounter(Counter): |
| 817 | pass |
| 818 | c = MyCounter('slartibartfast') |
| 819 | d = c.copy() |
| 820 | self.assertEqual(d, c) |
| 821 | self.assertEqual(len(d), len(c)) |
| 822 | self.assertEqual(type(d), type(c)) |
| 823 | |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 824 | def test_conversions(self): |
| 825 | # Convert to: set, list, dict |
| 826 | s = 'she sells sea shells by the sea shore' |
| 827 | self.assertEqual(sorted(Counter(s).elements()), sorted(s)) |
| 828 | self.assertEqual(sorted(Counter(s)), sorted(set(s))) |
| 829 | self.assertEqual(dict(Counter(s)), dict(Counter(s).items())) |
| 830 | self.assertEqual(set(Counter(s)), set(s)) |
| 831 | |
Raymond Hettinger | 670eaec | 2009-01-21 23:14:07 +0000 | [diff] [blame] | 832 | def test_invariant_for_the_in_operator(self): |
| 833 | c = Counter(a=10, b=-2, c=0) |
| 834 | for elem in c: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 835 | self.assertTrue(elem in c) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 836 | self.assertIn(elem, c) |
Raymond Hettinger | 670eaec | 2009-01-21 23:14:07 +0000 | [diff] [blame] | 837 | |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 838 | def test_multiset_operations(self): |
| 839 | # Verify that adding a zero counter will strip zeros and negatives |
| 840 | c = Counter(a=10, b=-2, c=0) + Counter() |
| 841 | self.assertEqual(dict(c), dict(a=10)) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 842 | |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 843 | elements = 'abcd' |
| 844 | for i in range(1000): |
| 845 | # test random pairs of multisets |
| 846 | p = Counter(dict((elem, randrange(-2,4)) for elem in elements)) |
Raymond Hettinger | e0d1b9f | 2009-01-21 20:36:27 +0000 | [diff] [blame] | 847 | p.update(e=1, f=-1, g=0) |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 848 | q = Counter(dict((elem, randrange(-2,4)) for elem in elements)) |
Raymond Hettinger | e0d1b9f | 2009-01-21 20:36:27 +0000 | [diff] [blame] | 849 | q.update(h=1, i=-1, j=0) |
| 850 | for counterop, numberop in [ |
| 851 | (Counter.__add__, lambda x, y: max(0, x+y)), |
| 852 | (Counter.__sub__, lambda x, y: max(0, x-y)), |
| 853 | (Counter.__or__, lambda x, y: max(0,x,y)), |
| 854 | (Counter.__and__, lambda x, y: max(0, min(x,y))), |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 855 | ]: |
| 856 | result = counterop(p, q) |
| 857 | for x in elements: |
Raymond Hettinger | e0d1b9f | 2009-01-21 20:36:27 +0000 | [diff] [blame] | 858 | self.assertEqual(numberop(p[x], q[x]), result[x], |
| 859 | (counterop, x, p, q)) |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 860 | # verify that results exclude non-positive counts |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 861 | self.assertTrue(x>0 for x in result.values()) |
Raymond Hettinger | 4d2073a | 2009-01-20 03:41:22 +0000 | [diff] [blame] | 862 | |
| 863 | elements = 'abcdef' |
| 864 | for i in range(100): |
| 865 | # verify that random multisets with no repeats are exactly like sets |
| 866 | p = Counter(dict((elem, randrange(0, 2)) for elem in elements)) |
| 867 | q = Counter(dict((elem, randrange(0, 2)) for elem in elements)) |
| 868 | for counterop, setop in [ |
| 869 | (Counter.__sub__, set.__sub__), |
| 870 | (Counter.__or__, set.__or__), |
| 871 | (Counter.__and__, set.__and__), |
| 872 | ]: |
| 873 | counter_result = counterop(p, q) |
| 874 | set_result = setop(set(p.elements()), set(q.elements())) |
| 875 | self.assertEqual(counter_result, dict.fromkeys(set_result, 1)) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 876 | |
Raymond Hettinger | 9c01e44 | 2010-04-03 10:32:58 +0000 | [diff] [blame] | 877 | def test_subtract(self): |
| 878 | c = Counter(a=-5, b=0, c=5, d=10, e=15,g=40) |
| 879 | c.subtract(a=1, b=2, c=-3, d=10, e=20, f=30, h=-50) |
| 880 | self.assertEqual(c, Counter(a=-6, b=-2, c=8, d=0, e=-5, f=-30, g=40, h=50)) |
| 881 | c = Counter(a=-5, b=0, c=5, d=10, e=15,g=40) |
| 882 | c.subtract(Counter(a=1, b=2, c=-3, d=10, e=20, f=30, h=-50)) |
| 883 | self.assertEqual(c, Counter(a=-6, b=-2, c=8, d=0, e=-5, f=-30, g=40, h=50)) |
| 884 | c = Counter('aaabbcd') |
| 885 | c.subtract('aaaabbcce') |
| 886 | self.assertEqual(c, Counter(a=-1, b=0, c=-1, d=1, e=-1)) |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 887 | |
Raymond Hettinger | 426e052 | 2011-01-03 02:12:02 +0000 | [diff] [blame] | 888 | def test_helper_function(self): |
| 889 | # two paths, one for real dicts and one for other mappings |
| 890 | elems = list('abracadabra') |
| 891 | |
| 892 | d = dict() |
| 893 | _count_elements(d, elems) |
| 894 | self.assertEqual(d, {'a': 5, 'r': 2, 'b': 2, 'c': 1, 'd': 1}) |
| 895 | |
| 896 | m = OrderedDict() |
| 897 | _count_elements(m, elems) |
| 898 | self.assertEqual(m, |
| 899 | OrderedDict([('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)])) |
| 900 | |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 901 | |
| 902 | ################################################################################ |
| 903 | ### OrderedDict |
| 904 | ################################################################################ |
| 905 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 906 | class TestOrderedDict(unittest.TestCase): |
| 907 | |
| 908 | def test_init(self): |
| 909 | with self.assertRaises(TypeError): |
| 910 | OrderedDict([('a', 1), ('b', 2)], None) # too many args |
| 911 | pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] |
| 912 | self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs) # dict input |
| 913 | self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs) # kwds input |
| 914 | self.assertEqual(list(OrderedDict(pairs).items()), pairs) # pairs input |
| 915 | self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)], |
| 916 | c=3, e=5).items()), pairs) # mixed input |
| 917 | |
| 918 | # make sure no positional args conflict with possible kwdargs |
| 919 | self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args, |
| 920 | ['self']) |
| 921 | |
| 922 | # Make sure that direct calls to __init__ do not clear previous contents |
| 923 | d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) |
| 924 | d.__init__([('e', 5), ('f', 6)], g=7, d=4) |
| 925 | self.assertEqual(list(d.items()), |
| 926 | [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) |
| 927 | |
| 928 | def test_update(self): |
| 929 | with self.assertRaises(TypeError): |
| 930 | OrderedDict().update([('a', 1), ('b', 2)], None) # too many args |
| 931 | pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] |
| 932 | od = OrderedDict() |
| 933 | od.update(dict(pairs)) |
| 934 | self.assertEqual(sorted(od.items()), pairs) # dict input |
| 935 | od = OrderedDict() |
| 936 | od.update(**dict(pairs)) |
| 937 | self.assertEqual(sorted(od.items()), pairs) # kwds input |
| 938 | od = OrderedDict() |
| 939 | od.update(pairs) |
| 940 | self.assertEqual(list(od.items()), pairs) # pairs input |
| 941 | od = OrderedDict() |
| 942 | od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5) |
| 943 | self.assertEqual(list(od.items()), pairs) # mixed input |
| 944 | |
Mark Dickinson | b214e90 | 2010-07-11 18:53:06 +0000 | [diff] [blame] | 945 | # Issue 9137: Named argument called 'other' or 'self' |
| 946 | # shouldn't be treated specially. |
| 947 | od = OrderedDict() |
| 948 | od.update(self=23) |
| 949 | self.assertEqual(list(od.items()), [('self', 23)]) |
| 950 | od = OrderedDict() |
| 951 | od.update(other={}) |
| 952 | self.assertEqual(list(od.items()), [('other', {})]) |
| 953 | od = OrderedDict() |
| 954 | od.update(red=5, blue=6, other=7, self=8) |
| 955 | self.assertEqual(sorted(list(od.items())), |
| 956 | [('blue', 6), ('other', 7), ('red', 5), ('self', 8)]) |
| 957 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 958 | # Make sure that direct calls to update do not clear previous contents |
| 959 | # add that updates items are not moved to the end |
| 960 | d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) |
| 961 | d.update([('e', 5), ('f', 6)], g=7, d=4) |
| 962 | self.assertEqual(list(d.items()), |
| 963 | [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) |
| 964 | |
Raymond Hettinger | 345c49b | 2011-01-01 23:51:55 +0000 | [diff] [blame] | 965 | def test_abc(self): |
| 966 | self.assertIsInstance(OrderedDict(), MutableMapping) |
| 967 | self.assertTrue(issubclass(OrderedDict, MutableMapping)) |
| 968 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 969 | def test_clear(self): |
| 970 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 971 | shuffle(pairs) |
| 972 | od = OrderedDict(pairs) |
| 973 | self.assertEqual(len(od), len(pairs)) |
| 974 | od.clear() |
| 975 | self.assertEqual(len(od), 0) |
| 976 | |
| 977 | def test_delitem(self): |
| 978 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 979 | od = OrderedDict(pairs) |
| 980 | del od['a'] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 981 | self.assertNotIn('a', od) |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 982 | with self.assertRaises(KeyError): |
| 983 | del od['a'] |
| 984 | self.assertEqual(list(od.items()), pairs[:2] + pairs[3:]) |
| 985 | |
| 986 | def test_setitem(self): |
| 987 | od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)]) |
| 988 | od['c'] = 10 # existing element |
| 989 | od['f'] = 20 # new element |
| 990 | self.assertEqual(list(od.items()), |
| 991 | [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)]) |
| 992 | |
| 993 | def test_iterators(self): |
| 994 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 995 | shuffle(pairs) |
| 996 | od = OrderedDict(pairs) |
| 997 | self.assertEqual(list(od), [t[0] for t in pairs]) |
| 998 | self.assertEqual(list(od.keys()), [t[0] for t in pairs]) |
| 999 | self.assertEqual(list(od.values()), [t[1] for t in pairs]) |
| 1000 | self.assertEqual(list(od.items()), pairs) |
| 1001 | self.assertEqual(list(reversed(od)), |
| 1002 | [t[0] for t in reversed(pairs)]) |
| 1003 | |
| 1004 | def test_popitem(self): |
| 1005 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1006 | shuffle(pairs) |
| 1007 | od = OrderedDict(pairs) |
| 1008 | while pairs: |
| 1009 | self.assertEqual(od.popitem(), pairs.pop()) |
| 1010 | with self.assertRaises(KeyError): |
| 1011 | od.popitem() |
| 1012 | self.assertEqual(len(od), 0) |
| 1013 | |
| 1014 | def test_pop(self): |
| 1015 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1016 | shuffle(pairs) |
| 1017 | od = OrderedDict(pairs) |
| 1018 | shuffle(pairs) |
| 1019 | while pairs: |
| 1020 | k, v = pairs.pop() |
| 1021 | self.assertEqual(od.pop(k), v) |
| 1022 | with self.assertRaises(KeyError): |
| 1023 | od.pop('xyz') |
| 1024 | self.assertEqual(len(od), 0) |
| 1025 | self.assertEqual(od.pop(k, 12345), 12345) |
| 1026 | |
Raymond Hettinger | 345c49b | 2011-01-01 23:51:55 +0000 | [diff] [blame] | 1027 | # make sure pop still works when __missing__ is defined |
| 1028 | class Missing(OrderedDict): |
| 1029 | def __missing__(self, key): |
| 1030 | return 0 |
| 1031 | m = Missing(a=1) |
| 1032 | self.assertEqual(m.pop('b', 5), 5) |
| 1033 | self.assertEqual(m.pop('a', 6), 1) |
| 1034 | self.assertEqual(m.pop('a', 6), 6) |
| 1035 | with self.assertRaises(KeyError): |
| 1036 | m.pop('a') |
| 1037 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1038 | def test_equality(self): |
| 1039 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1040 | shuffle(pairs) |
| 1041 | od1 = OrderedDict(pairs) |
| 1042 | od2 = OrderedDict(pairs) |
| 1043 | self.assertEqual(od1, od2) # same order implies equality |
| 1044 | pairs = pairs[2:] + pairs[:2] |
| 1045 | od2 = OrderedDict(pairs) |
| 1046 | self.assertNotEqual(od1, od2) # different order implies inequality |
| 1047 | # comparison to regular dict is not order sensitive |
| 1048 | self.assertEqual(od1, dict(od2)) |
| 1049 | self.assertEqual(dict(od2), od1) |
| 1050 | # different length implied inequality |
| 1051 | self.assertNotEqual(od1, OrderedDict(pairs[:-1])) |
| 1052 | |
| 1053 | def test_copying(self): |
| 1054 | # Check that ordered dicts are copyable, deepcopyable, picklable, |
| 1055 | # and have a repr/eval round-trip |
| 1056 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1057 | od = OrderedDict(pairs) |
| 1058 | update_test = OrderedDict() |
| 1059 | update_test.update(od) |
| 1060 | for i, dup in enumerate([ |
| 1061 | od.copy(), |
| 1062 | copy.copy(od), |
| 1063 | copy.deepcopy(od), |
| 1064 | pickle.loads(pickle.dumps(od, 0)), |
| 1065 | pickle.loads(pickle.dumps(od, 1)), |
| 1066 | pickle.loads(pickle.dumps(od, 2)), |
| 1067 | pickle.loads(pickle.dumps(od, 3)), |
| 1068 | pickle.loads(pickle.dumps(od, -1)), |
| 1069 | eval(repr(od)), |
| 1070 | update_test, |
| 1071 | OrderedDict(od), |
| 1072 | ]): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1073 | self.assertTrue(dup is not od) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1074 | self.assertEqual(dup, od) |
| 1075 | self.assertEqual(list(dup.items()), list(od.items())) |
| 1076 | self.assertEqual(len(dup), len(od)) |
| 1077 | self.assertEqual(type(dup), type(od)) |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1078 | |
Raymond Hettinger | 5b26fb5 | 2009-03-03 22:38:22 +0000 | [diff] [blame] | 1079 | def test_yaml_linkage(self): |
| 1080 | # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature. |
| 1081 | # In yaml, lists are native but tuples are not. |
| 1082 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1083 | od = OrderedDict(pairs) |
| 1084 | # yaml.dump(od) --> |
| 1085 | # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n' |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1086 | self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1])) |
Raymond Hettinger | 5b26fb5 | 2009-03-03 22:38:22 +0000 | [diff] [blame] | 1087 | |
Raymond Hettinger | b212157 | 2009-03-03 22:50:04 +0000 | [diff] [blame] | 1088 | def test_reduce_not_too_fat(self): |
| 1089 | # do not save instance dictionary if not needed |
| 1090 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1091 | od = OrderedDict(pairs) |
| 1092 | self.assertEqual(len(od.__reduce__()), 2) |
| 1093 | od.x = 10 |
| 1094 | self.assertEqual(len(od.__reduce__()), 3) |
| 1095 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1096 | def test_repr(self): |
| 1097 | od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]) |
| 1098 | self.assertEqual(repr(od), |
| 1099 | "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])") |
| 1100 | self.assertEqual(eval(repr(od)), od) |
| 1101 | self.assertEqual(repr(OrderedDict()), "OrderedDict()") |
| 1102 | |
Raymond Hettinger | dc08a14 | 2010-09-12 05:15:22 +0000 | [diff] [blame] | 1103 | def test_repr_recursive(self): |
| 1104 | # See issue #9826 |
| 1105 | od = OrderedDict.fromkeys('abc') |
| 1106 | od['x'] = od |
| 1107 | self.assertEqual(repr(od), |
| 1108 | "OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])") |
| 1109 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1110 | def test_setdefault(self): |
| 1111 | pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
| 1112 | shuffle(pairs) |
| 1113 | od = OrderedDict(pairs) |
| 1114 | pair_order = list(od.items()) |
| 1115 | self.assertEqual(od.setdefault('a', 10), 3) |
| 1116 | # make sure order didn't change |
| 1117 | self.assertEqual(list(od.items()), pair_order) |
| 1118 | self.assertEqual(od.setdefault('x', 10), 10) |
| 1119 | # make sure 'x' is added to the end |
| 1120 | self.assertEqual(list(od.items())[-1], ('x', 10)) |
| 1121 | |
Raymond Hettinger | a673b1f | 2010-12-31 23:16:17 +0000 | [diff] [blame] | 1122 | # make sure setdefault still works when __missing__ is defined |
| 1123 | class Missing(OrderedDict): |
| 1124 | def __missing__(self, key): |
| 1125 | return 0 |
| 1126 | self.assertEqual(Missing().setdefault(5, 9), 9) |
| 1127 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1128 | def test_reinsert(self): |
| 1129 | # Given insert a, insert b, delete a, re-insert a, |
| 1130 | # verify that a is now later than b. |
| 1131 | od = OrderedDict() |
| 1132 | od['a'] = 1 |
| 1133 | od['b'] = 2 |
| 1134 | del od['a'] |
| 1135 | od['a'] = 1 |
| 1136 | self.assertEqual(list(od.items()), [('b', 2), ('a', 1)]) |
| 1137 | |
Raymond Hettinger | f45abc9 | 2010-09-06 21:26:09 +0000 | [diff] [blame] | 1138 | def test_move_to_end(self): |
| 1139 | od = OrderedDict.fromkeys('abcde') |
| 1140 | self.assertEqual(list(od), list('abcde')) |
| 1141 | od.move_to_end('c') |
| 1142 | self.assertEqual(list(od), list('abdec')) |
| 1143 | od.move_to_end('c', 0) |
| 1144 | self.assertEqual(list(od), list('cabde')) |
| 1145 | od.move_to_end('c', 0) |
| 1146 | self.assertEqual(list(od), list('cabde')) |
| 1147 | od.move_to_end('e') |
| 1148 | self.assertEqual(list(od), list('cabde')) |
| 1149 | with self.assertRaises(KeyError): |
| 1150 | od.move_to_end('x') |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1151 | |
Raymond Hettinger | 35c87f2 | 2010-09-16 19:10:17 +0000 | [diff] [blame] | 1152 | def test_sizeof(self): |
| 1153 | # Wimpy test: Just verify the reported size is larger than a regular dict |
| 1154 | d = dict(a=1) |
| 1155 | od = OrderedDict(**d) |
| 1156 | self.assertGreater(sys.getsizeof(od), sys.getsizeof(d)) |
| 1157 | |
Raymond Hettinger | 32062e9 | 2011-01-01 22:38:00 +0000 | [diff] [blame] | 1158 | def test_override_update(self): |
| 1159 | # Verify that subclasses can override update() without breaking __init__() |
| 1160 | class MyOD(OrderedDict): |
| 1161 | def update(self, *args, **kwds): |
| 1162 | raise Exception() |
| 1163 | items = [('a', 1), ('c', 3), ('b', 2)] |
| 1164 | self.assertEqual(list(MyOD(items).items()), items) |
| 1165 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1166 | class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol): |
| 1167 | type2test = OrderedDict |
| 1168 | |
Raymond Hettinger | dc879f0 | 2009-03-19 20:30:56 +0000 | [diff] [blame] | 1169 | def test_popitem(self): |
| 1170 | d = self._empty_mapping() |
| 1171 | self.assertRaises(KeyError, d.popitem) |
| 1172 | |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1173 | class MyOrderedDict(OrderedDict): |
| 1174 | pass |
| 1175 | |
| 1176 | class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): |
| 1177 | type2test = MyOrderedDict |
| 1178 | |
Raymond Hettinger | dc879f0 | 2009-03-19 20:30:56 +0000 | [diff] [blame] | 1179 | def test_popitem(self): |
| 1180 | d = self._empty_mapping() |
| 1181 | self.assertRaises(KeyError, d.popitem) |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1182 | |
| 1183 | |
Raymond Hettinger | dcb29c9 | 2011-02-23 08:28:06 +0000 | [diff] [blame] | 1184 | ################################################################################ |
| 1185 | ### Run tests |
| 1186 | ################################################################################ |
| 1187 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1188 | import doctest, collections |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1189 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1190 | def test_main(verbose=None): |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 1191 | NamedTupleDocs = doctest.DocTestSuite(module=collections) |
Raymond Hettinger | b8baf63 | 2009-01-14 02:20:07 +0000 | [diff] [blame] | 1192 | test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, |
Łukasz Langa | 75da860 | 2011-04-29 11:35:03 +0200 | [diff] [blame] | 1193 | TestCollectionABCs, TestCounter, TestChainMap, |
Raymond Hettinger | 2d32f63 | 2009-03-02 21:24:57 +0000 | [diff] [blame] | 1194 | TestOrderedDict, GeneralMappingTests, SubclassMappingTests] |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1195 | support.run_unittest(*test_classes) |
| 1196 | support.run_doctest(collections, verbose) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1198 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1199 | if __name__ == "__main__": |
| 1200 | test_main(verbose=True) |