Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_collections.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 20e7aa42d6fe |
| children | 23a373c8ec1b |
| rev | line source |
|---|---|
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
1 |
|
44627
7851e75f5438
Run doctests on the collections module
Raymond Hettinger <python@rcn.com>
parents:
44485
diff
changeset
|
2 import unittest, doctest |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
3 import inspect |
| 40992 | 4 from test import test_support |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
5 from collections import namedtuple, Counter, OrderedDict |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
6 from test import mapping_tests |
|
47526
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
7 import pickle, cPickle, copy |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
8 from random import randrange, shuffle |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
9 import operator |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
10 from collections import Hashable, Iterable, Iterator |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
11 from collections import Sized, Container, Callable |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
12 from collections import Set, MutableSet |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
13 from collections import Mapping, MutableMapping |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
14 from collections import Sequence, MutableSequence |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
15 |
|
47526
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
16 TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests |
| 40992 | 17 |
| 18 class TestNamedTuple(unittest.TestCase): | |
| 19 | |
| 20 def test_factory(self): | |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
21 Point = namedtuple('Point', 'x y') |
| 40992 | 22 self.assertEqual(Point.__name__, 'Point') |
| 23 self.assertEqual(Point.__doc__, 'Point(x, y)') | |
| 24 self.assertEqual(Point.__slots__, ()) | |
| 25 self.assertEqual(Point.__module__, __name__) | |
| 26 self.assertEqual(Point.__getitem__, tuple.__getitem__) | |
|
44464
398b6246759f
Minor fix-ups to named tuples:
Raymond Hettinger <python@rcn.com>
parents:
44345
diff
changeset
|
27 self.assertEqual(Point._fields, ('x', 'y')) |
|
43579
046edaa1566a
More docs, error messages, and tests
Raymond Hettinger <python@rcn.com>
parents:
43500
diff
changeset
|
28 |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
29 self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char |
|
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
30 self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword |
|
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
31 self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit |
|
43579
046edaa1566a
More docs, error messages, and tests
Raymond Hettinger <python@rcn.com>
parents:
43500
diff
changeset
|
32 |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
33 self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char |
|
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
34 self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword |
|
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
35 self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit |
|
44294
294b1a79f201
Cleaner method naming convention
Raymond Hettinger <python@rcn.com>
parents:
44178
diff
changeset
|
36 self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
37 self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field |
|
43579
046edaa1566a
More docs, error messages, and tests
Raymond Hettinger <python@rcn.com>
parents:
43500
diff
changeset
|
38 |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
39 namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names |
|
44294
294b1a79f201
Cleaner method naming convention
Raymond Hettinger <python@rcn.com>
parents:
44178
diff
changeset
|
40 namedtuple('_', 'a b c') # Test leading underscores in a typename |
| 40992 | 41 |
|
49301
f323fa4e48d7
Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly if the field names were input in Unicode.
Raymond Hettinger <python@rcn.com>
parents:
48213
diff
changeset
|
42 nt = namedtuple('nt', u'the quick brown fox') # check unicode input |
|
f323fa4e48d7
Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly if the field names were input in Unicode.
Raymond Hettinger <python@rcn.com>
parents:
48213
diff
changeset
|
43 self.assert_("u'" not in repr(nt._fields)) |
|
f323fa4e48d7
Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly if the field names were input in Unicode.
Raymond Hettinger <python@rcn.com>
parents:
48213
diff
changeset
|
44 nt = namedtuple('nt', (u'the', u'quick')) # check unicode input |
|
f323fa4e48d7
Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly if the field names were input in Unicode.
Raymond Hettinger <python@rcn.com>
parents:
48213
diff
changeset
|
45 self.assert_("u'" not in repr(nt._fields)) |
|
f323fa4e48d7
Fix namedtuple bug reported by Glenn Linderman. Template did not form correctly if the field names were input in Unicode.
Raymond Hettinger <python@rcn.com>
parents:
48213
diff
changeset
|
46 |
|
44484
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
47 self.assertRaises(TypeError, Point._make, [11]) # catch too few args |
|
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
48 self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args |
|
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
49 |
|
51499
beef8818a66c
Issue 1818: collections.namedtuple() to support automatic renaming of invalid fieldnames.
Raymond Hettinger <python@rcn.com>
parents:
51147
diff
changeset
|
50 def test_name_fixer(self): |
|
beef8818a66c
Issue 1818: collections.namedtuple() to support automatic renaming of invalid fieldnames.
Raymond Hettinger <python@rcn.com>
parents:
51147
diff
changeset
|
51 for spec, renamed in [ |
|
52751
20e7aa42d6fe
Have namedtuple's field renamer assign names that
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
52 [('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char |
|
20e7aa42d6fe
Have namedtuple's field renamer assign names that
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
53 [('abc', 'class'), ('abc', '_1')], # field has keyword |
|
20e7aa42d6fe
Have namedtuple's field renamer assign names that
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
54 [('8efg', '9ghi'), ('_0', '_1')], # field starts with digit |
|
20e7aa42d6fe
Have namedtuple's field renamer assign names that
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
55 [('abc', '_efg'), ('abc', '_1')], # field with leading underscore |
|
20e7aa42d6fe
Have namedtuple's field renamer assign names that
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
56 [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')], # duplicate field |
|
20e7aa42d6fe
Have namedtuple's field renamer assign names that
Raymond Hettinger <python@rcn.com>
parents:
52661
diff
changeset
|
57 [('abc', '', 'x'), ('abc', '_1', 'x')], # fieldname is a space |
|
51499
beef8818a66c
Issue 1818: collections.namedtuple() to support automatic renaming of invalid fieldnames.
Raymond Hettinger <python@rcn.com>
parents:
51147
diff
changeset
|
58 ]: |
|
beef8818a66c
Issue 1818: collections.namedtuple() to support automatic renaming of invalid fieldnames.
Raymond Hettinger <python@rcn.com>
parents:
51147
diff
changeset
|
59 self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed) |
|
beef8818a66c
Issue 1818: collections.namedtuple() to support automatic renaming of invalid fieldnames.
Raymond Hettinger <python@rcn.com>
parents:
51147
diff
changeset
|
60 |
| 40992 | 61 def test_instance(self): |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
62 Point = namedtuple('Point', 'x y') |
| 40992 | 63 p = Point(11, 22) |
| 64 self.assertEqual(p, Point(x=11, y=22)) | |
| 65 self.assertEqual(p, Point(11, y=22)) | |
| 66 self.assertEqual(p, Point(y=22, x=11)) | |
| 67 self.assertEqual(p, Point(*(11, 22))) | |
| 68 self.assertEqual(p, Point(**dict(x=11, y=22))) | |
| 69 self.assertRaises(TypeError, Point, 1) # too few args | |
| 70 self.assertRaises(TypeError, Point, 1, 2, 3) # too many args | |
| 71 self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument | |
| 72 self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument | |
| 73 self.assertEqual(repr(p), 'Point(x=11, y=22)') | |
| 74 self.assert_('__dict__' not in dir(p)) # verify instance has no dict | |
| 75 self.assert_('__weakref__' not in dir(p)) | |
|
44484
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
76 self.assertEqual(p, Point._make([11, 22])) # test _make classmethod |
|
44294
294b1a79f201
Cleaner method naming convention
Raymond Hettinger <python@rcn.com>
parents:
44178
diff
changeset
|
77 self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute |
|
294b1a79f201
Cleaner method naming convention
Raymond Hettinger <python@rcn.com>
parents:
44178
diff
changeset
|
78 self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method |
|
294b1a79f201
Cleaner method naming convention
Raymond Hettinger <python@rcn.com>
parents:
44178
diff
changeset
|
79 self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method |
|
43377
2e7dd201ea1d
Sync-up named tuples with the latest version of the ASPN recipe.
Raymond Hettinger <python@rcn.com>
parents:
41801
diff
changeset
|
80 |
|
44485
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
81 try: |
|
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
82 p._replace(x=1, error=2) |
|
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
83 except ValueError: |
|
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
84 pass |
|
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
85 else: |
|
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
86 self._fail('Did not detect an incorrect fieldname') |
|
7bb91b3ba0ec
Add error-checking to namedtuple's _replace() method.
Raymond Hettinger <python@rcn.com>
parents:
44484
diff
changeset
|
87 |
|
43377
2e7dd201ea1d
Sync-up named tuples with the latest version of the ASPN recipe.
Raymond Hettinger <python@rcn.com>
parents:
41801
diff
changeset
|
88 # verify that field string can have commas |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
89 Point = namedtuple('Point', 'x, y') |
|
43377
2e7dd201ea1d
Sync-up named tuples with the latest version of the ASPN recipe.
Raymond Hettinger <python@rcn.com>
parents:
41801
diff
changeset
|
90 p = Point(x=11, y=22) |
|
2e7dd201ea1d
Sync-up named tuples with the latest version of the ASPN recipe.
Raymond Hettinger <python@rcn.com>
parents:
41801
diff
changeset
|
91 self.assertEqual(repr(p), 'Point(x=11, y=22)') |
| 40992 | 92 |
|
43494
4db5dab7f235
Add comments to NamedTuple code.
Raymond Hettinger <python@rcn.com>
parents:
43466
diff
changeset
|
93 # verify that fieldspec can be a non-string sequence |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
94 Point = namedtuple('Point', ('x', 'y')) |
|
43494
4db5dab7f235
Add comments to NamedTuple code.
Raymond Hettinger <python@rcn.com>
parents:
43466
diff
changeset
|
95 p = Point(x=11, y=22) |
|
4db5dab7f235
Add comments to NamedTuple code.
Raymond Hettinger <python@rcn.com>
parents:
43466
diff
changeset
|
96 self.assertEqual(repr(p), 'Point(x=11, y=22)') |
|
4db5dab7f235
Add comments to NamedTuple code.
Raymond Hettinger <python@rcn.com>
parents:
43466
diff
changeset
|
97 |
| 40992 | 98 def test_tupleness(self): |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
99 Point = namedtuple('Point', 'x y') |
| 40992 | 100 p = Point(11, 22) |
| 101 | |
| 102 self.assert_(isinstance(p, tuple)) | |
| 103 self.assertEqual(p, (11, 22)) # matches a real tuple | |
| 104 self.assertEqual(tuple(p), (11, 22)) # coercable to a real tuple | |
| 105 self.assertEqual(list(p), [11, 22]) # coercable to a list | |
| 106 self.assertEqual(max(p), 22) # iterable | |
| 107 self.assertEqual(max(*p), 22) # star-able | |
| 108 x, y = p | |
| 109 self.assertEqual(p, (x, y)) # unpacks like a tuple | |
| 110 self.assertEqual((p[0], p[1]), (11, 22)) # indexable like a tuple | |
| 111 self.assertRaises(IndexError, p.__getitem__, 3) | |
| 112 | |
| 113 self.assertEqual(p.x, x) | |
| 114 self.assertEqual(p.y, y) | |
| 115 self.assertRaises(AttributeError, eval, 'p.z', locals()) | |
| 116 | |
|
43388
2fdff0b5beb6
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
Raymond Hettinger <python@rcn.com>
parents:
43377
diff
changeset
|
117 def test_odd_sizes(self): |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
118 Zero = namedtuple('Zero', '') |
|
43388
2fdff0b5beb6
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
Raymond Hettinger <python@rcn.com>
parents:
43377
diff
changeset
|
119 self.assertEqual(Zero(), ()) |
|
44484
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
120 self.assertEqual(Zero._make([]), ()) |
|
44330
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
121 self.assertEqual(repr(Zero()), 'Zero()') |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
122 self.assertEqual(Zero()._asdict(), {}) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
123 self.assertEqual(Zero()._fields, ()) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
124 |
|
43654
a0983f55c957
Shorter name for namedtuple()
Raymond Hettinger <python@rcn.com>
parents:
43579
diff
changeset
|
125 Dot = namedtuple('Dot', 'd') |
|
43388
2fdff0b5beb6
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
Raymond Hettinger <python@rcn.com>
parents:
43377
diff
changeset
|
126 self.assertEqual(Dot(1), (1,)) |
|
44484
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
127 self.assertEqual(Dot._make([1]), (1,)) |
|
44330
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
128 self.assertEqual(Dot(1).d, 1) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
129 self.assertEqual(repr(Dot(1)), 'Dot(d=1)') |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
130 self.assertEqual(Dot(1)._asdict(), {'d':1}) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
131 self.assertEqual(Dot(1)._replace(d=999), (999,)) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
132 self.assertEqual(Dot(1)._fields, ('d',)) |
|
43388
2fdff0b5beb6
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
Raymond Hettinger <python@rcn.com>
parents:
43377
diff
changeset
|
133 |
|
47526
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
134 n = 5000 |
|
44330
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
135 import string, random |
|
47154
20b1c5c4e304
Take namedtuple item names only from ascii_letters (this blew up on OSX),
Georg Brandl <georg@python.org>
parents:
46249
diff
changeset
|
136 names = list(set(''.join([random.choice(string.ascii_letters) |
|
20b1c5c4e304
Take namedtuple item names only from ascii_letters (this blew up on OSX),
Georg Brandl <georg@python.org>
parents:
46249
diff
changeset
|
137 for j in range(10)]) for i in range(n))) |
|
20b1c5c4e304
Take namedtuple item names only from ascii_letters (this blew up on OSX),
Georg Brandl <georg@python.org>
parents:
46249
diff
changeset
|
138 n = len(names) |
|
44330
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
139 Big = namedtuple('Big', names) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
140 b = Big(*range(n)) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
141 self.assertEqual(b, tuple(range(n))) |
|
44484
dcc7c865c77b
Improve namedtuple's _cast() method with a docstring, new name, and error-checking.
Raymond Hettinger <python@rcn.com>
parents:
44464
diff
changeset
|
142 self.assertEqual(Big._make(range(n)), tuple(range(n))) |
|
44330
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
143 for pos, name in enumerate(names): |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
144 self.assertEqual(getattr(b, name), pos) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
145 repr(b) # make sure repr() doesn't blow-up |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
146 d = b._asdict() |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
147 d_expected = dict(zip(names, range(n))) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
148 self.assertEqual(d, d_expected) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
149 b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)])) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
150 b2_expected = range(n) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
151 b2_expected[1] = 999 |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
152 b2_expected[-5] = 42 |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
153 self.assertEqual(b2, tuple(b2_expected)) |
|
4a6c06a77deb
Add more namedtuple() test cases. Neaten the code and comments.
Raymond Hettinger <python@rcn.com>
parents:
44294
diff
changeset
|
154 self.assertEqual(b._fields, tuple(names)) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
155 |
|
47526
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
156 def test_pickle(self): |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
157 p = TestNT(x=10, y=20, z=30) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
158 for module in pickle, cPickle: |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
159 loads = getattr(module, 'loads') |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
160 dumps = getattr(module, 'dumps') |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
161 for protocol in -1, 0, 1, 2: |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
162 q = loads(dumps(p, protocol)) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
163 self.assertEqual(p, q) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
164 self.assertEqual(p._fields, q._fields) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
165 |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
166 def test_copy(self): |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
167 p = TestNT(x=10, y=20, z=30) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
168 for copier in copy.copy, copy.deepcopy: |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
169 q = copier(p) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
170 self.assertEqual(p, q) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
171 self.assertEqual(p._fields, q._fields) |
|
246a802637ad
Issue3065: Fixed pickling of named tuples. Added tests.
Raymond Hettinger <python@rcn.com>
parents:
47154
diff
changeset
|
172 |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
173 class ABCTestCase(unittest.TestCase): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
174 |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
175 def validate_abstract_methods(self, abc, *names): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
176 methodstubs = dict.fromkeys(names, lambda s, *args: 0) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
177 |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
178 # everything should work will all required methods are present |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
179 C = type('C', (abc,), methodstubs) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
180 C() |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
181 |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
182 # instantiation should fail if a required method is missing |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
183 for name in names: |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
184 stubs = methodstubs.copy() |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
185 del stubs[name] |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
186 C = type('C', (abc,), stubs) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
187 self.assertRaises(TypeError, C, name) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
188 |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
189 |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
190 class TestOneTrickPonyABCs(ABCTestCase): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
191 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
192 def test_Hashable(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
193 # Check some non-hashables |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
194 non_samples = [list(), set(), dict()] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
195 for x in non_samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
196 self.failIf(isinstance(x, Hashable), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
197 self.failIf(issubclass(type(x), Hashable), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
198 # Check some hashables |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
199 samples = [None, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
200 int(), float(), complex(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
201 str(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
202 tuple(), frozenset(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
203 int, list, object, type, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
204 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
205 for x in samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
206 self.failUnless(isinstance(x, Hashable), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
207 self.failUnless(issubclass(type(x), Hashable), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
208 self.assertRaises(TypeError, Hashable) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
209 # Check direct subclassing |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
210 class H(Hashable): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
211 def __hash__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
212 return super(H, self).__hash__() |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47812
diff
changeset
|
213 __eq__ = Hashable.__eq__ # Silence Py3k warning |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
214 self.assertEqual(hash(H()), 0) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
215 self.failIf(issubclass(int, H)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
216 self.validate_abstract_methods(Hashable, '__hash__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
217 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
218 def test_Iterable(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
219 # Check some non-iterables |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
220 non_samples = [None, 42, 3.14, 1j] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
221 for x in non_samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
222 self.failIf(isinstance(x, Iterable), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
223 self.failIf(issubclass(type(x), Iterable), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
224 # Check some iterables |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
225 samples = [str(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
226 tuple(), list(), set(), frozenset(), dict(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
227 dict().keys(), dict().items(), dict().values(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
228 (lambda: (yield))(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
229 (x for x in []), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
230 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
231 for x in samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
232 self.failUnless(isinstance(x, Iterable), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
233 self.failUnless(issubclass(type(x), Iterable), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
234 # Check direct subclassing |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
235 class I(Iterable): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
236 def __iter__(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
237 return super(I, self).__iter__() |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
238 self.assertEqual(list(I()), []) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
239 self.failIf(issubclass(str, I)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
240 self.validate_abstract_methods(Iterable, '__iter__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
241 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
242 def test_Iterator(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
243 non_samples = [None, 42, 3.14, 1j, "".encode('ascii'), "", (), [], |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
244 {}, set()] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
245 for x in non_samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
246 self.failIf(isinstance(x, Iterator), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
247 self.failIf(issubclass(type(x), Iterator), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
248 samples = [iter(str()), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
249 iter(tuple()), iter(list()), iter(dict()), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
250 iter(set()), iter(frozenset()), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
251 iter(dict().keys()), iter(dict().items()), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
252 iter(dict().values()), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
253 (lambda: (yield))(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
254 (x for x in []), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
255 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
256 for x in samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
257 self.failUnless(isinstance(x, Iterator), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
258 self.failUnless(issubclass(type(x), Iterator), repr(type(x))) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
259 self.validate_abstract_methods(Iterator, 'next') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
260 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
261 def test_Sized(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
262 non_samples = [None, 42, 3.14, 1j, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
263 (lambda: (yield))(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
264 (x for x in []), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
265 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
266 for x in non_samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
267 self.failIf(isinstance(x, Sized), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
268 self.failIf(issubclass(type(x), Sized), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
269 samples = [str(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
270 tuple(), list(), set(), frozenset(), dict(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
271 dict().keys(), dict().items(), dict().values(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
272 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
273 for x in samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
274 self.failUnless(isinstance(x, Sized), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
275 self.failUnless(issubclass(type(x), Sized), repr(type(x))) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
276 self.validate_abstract_methods(Sized, '__len__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
277 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
278 def test_Container(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
279 non_samples = [None, 42, 3.14, 1j, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
280 (lambda: (yield))(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
281 (x for x in []), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
282 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
283 for x in non_samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
284 self.failIf(isinstance(x, Container), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
285 self.failIf(issubclass(type(x), Container), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
286 samples = [str(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
287 tuple(), list(), set(), frozenset(), dict(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
288 dict().keys(), dict().items(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
289 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
290 for x in samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
291 self.failUnless(isinstance(x, Container), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
292 self.failUnless(issubclass(type(x), Container), repr(type(x))) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
293 self.validate_abstract_methods(Container, '__contains__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
294 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
295 def test_Callable(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
296 non_samples = [None, 42, 3.14, 1j, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
297 "", "".encode('ascii'), (), [], {}, set(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
298 (lambda: (yield))(), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
299 (x for x in []), |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
300 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
301 for x in non_samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
302 self.failIf(isinstance(x, Callable), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
303 self.failIf(issubclass(type(x), Callable), repr(type(x))) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
304 samples = [lambda: None, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
305 type, int, object, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
306 len, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
307 list.append, [].append, |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
308 ] |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
309 for x in samples: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
310 self.failUnless(isinstance(x, Callable), repr(x)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
311 self.failUnless(issubclass(type(x), Callable), repr(type(x))) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
312 self.validate_abstract_methods(Callable, '__call__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
313 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
314 def test_direct_subclassing(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
315 for B in Hashable, Iterable, Iterator, Sized, Container, Callable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
316 class C(B): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
317 pass |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
318 self.failUnless(issubclass(C, B)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
319 self.failIf(issubclass(int, C)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
320 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
321 def test_registration(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
322 for B in Hashable, Iterable, Iterator, Sized, Container, Callable: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
323 class C: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
324 __metaclass__ = type |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
325 __hash__ = None # Make sure it isn't hashable by default |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
326 self.failIf(issubclass(C, B), B.__name__) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
327 B.register(C) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
328 self.failUnless(issubclass(C, B)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
329 |
|
52661
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
330 class WithSet(MutableSet): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
331 |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
332 def __init__(self, it=()): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
333 self.data = set(it) |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
334 |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
335 def __len__(self): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
336 return len(self.data) |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
337 |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
338 def __iter__(self): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
339 return iter(self.data) |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
340 |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
341 def __contains__(self, item): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
342 return item in self.data |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
343 |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
344 def add(self, item): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
345 self.data.add(item) |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
346 |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
347 def discard(self, item): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
348 self.data.discard(item) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
349 |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
350 class TestCollectionABCs(ABCTestCase): |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
351 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
352 # XXX For now, we only test some virtual inheritance properties. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
353 # We should also test the proper behavior of the collection ABCs |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
354 # as real base classes or mix-in classes. |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
355 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
356 def test_Set(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
357 for sample in [set, frozenset]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
358 self.failUnless(isinstance(sample(), Set)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
359 self.failUnless(issubclass(sample, Set)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
360 self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
361 |
|
47812
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
362 def test_hash_Set(self): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
363 class OneTwoThreeSet(Set): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
364 def __init__(self): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
365 self.contents = [1, 2, 3] |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
366 def __contains__(self, x): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
367 return x in self.contents |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
368 def __len__(self): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
369 return len(self.contents) |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
370 def __iter__(self): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
371 return iter(self.contents) |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
372 def __hash__(self): |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
373 return self._hash() |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
374 a, b = OneTwoThreeSet(), OneTwoThreeSet() |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
375 self.failUnless(hash(a) == hash(b)) |
|
f0027bfdf9bc
Issue 3161: Missing import and test.
Raymond Hettinger <python@rcn.com>
parents:
47526
diff
changeset
|
376 |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
377 def test_MutableSet(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
378 self.failUnless(isinstance(set(), MutableSet)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
379 self.failUnless(issubclass(set, MutableSet)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
380 self.failIf(isinstance(frozenset(), MutableSet)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
381 self.failIf(issubclass(frozenset, MutableSet)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
382 self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__', |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
383 'add', 'discard') |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
384 |
|
52661
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
385 def test_issue_5647(self): |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
386 # MutableSet.__iand__ mutated the set during iteration |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
387 s = WithSet('abcd') |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
388 s &= WithSet('cdef') # This used to fail |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
389 self.assertEqual(set(s), set('cd')) |
|
2e4dbb395417
Issue #5647: MutableSet.__iand__() no longer mutates self during iteration.
Raymond Hettinger <python@rcn.com>
parents:
52227
diff
changeset
|
390 |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
391 def test_issue_4920(self): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
392 # MutableSet.pop() method did not work |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
393 class MySet(collections.MutableSet): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
394 __slots__=['__s'] |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
395 def __init__(self,items=None): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
396 if items is None: |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
397 items=[] |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
398 self.__s=set(items) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
399 def __contains__(self,v): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
400 return v in self.__s |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
401 def __iter__(self): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
402 return iter(self.__s) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
403 def __len__(self): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
404 return len(self.__s) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
405 def add(self,v): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
406 result=v not in self.__s |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
407 self.__s.add(v) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
408 return result |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
409 def discard(self,v): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
410 result=v in self.__s |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
411 self.__s.discard(v) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
412 return result |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
413 def __repr__(self): |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
414 return "MySet(%s)" % repr(list(self)) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
415 s = MySet([5,43,2,1]) |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
416 self.assertEqual(s.pop(), 1) |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
417 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
418 def test_Mapping(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
419 for sample in [dict]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
420 self.failUnless(isinstance(sample(), Mapping)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
421 self.failUnless(issubclass(sample, Mapping)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
422 self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__', |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
423 '__getitem__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
424 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
425 def test_MutableMapping(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
426 for sample in [dict]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
427 self.failUnless(isinstance(sample(), MutableMapping)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
428 self.failUnless(issubclass(sample, MutableMapping)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
429 self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__', '__len__', |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
430 '__getitem__', '__setitem__', '__delitem__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
431 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
432 def test_Sequence(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
433 for sample in [tuple, list, str]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
434 self.failUnless(isinstance(sample(), Sequence)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
435 self.failUnless(issubclass(sample, Sequence)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
436 self.failUnless(issubclass(basestring, Sequence)) |
|
51836
9c25a0790022
Backport 69934: Register xrange() as a Sequence.
Raymond Hettinger <python@rcn.com>
parents:
51499
diff
changeset
|
437 self.failUnless(isinstance(range(10), Sequence)) |
|
9c25a0790022
Backport 69934: Register xrange() as a Sequence.
Raymond Hettinger <python@rcn.com>
parents:
51499
diff
changeset
|
438 self.failUnless(issubclass(xrange, Sequence)) |
|
9c25a0790022
Backport 69934: Register xrange() as a Sequence.
Raymond Hettinger <python@rcn.com>
parents:
51499
diff
changeset
|
439 self.failUnless(issubclass(str, Sequence)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
440 self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__', |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
441 '__getitem__') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
442 |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
443 def test_MutableSequence(self): |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
444 for sample in [tuple, str]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
445 self.failIf(isinstance(sample(), MutableSequence)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
446 self.failIf(issubclass(sample, MutableSequence)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
447 for sample in [list]: |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
448 self.failUnless(isinstance(sample(), MutableSequence)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
449 self.failUnless(issubclass(sample, MutableSequence)) |
|
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
450 self.failIf(issubclass(basestring, MutableSequence)) |
|
51147
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
451 self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__', |
|
944046808359
Issue 4920: Fixed next() vs __next__() issues in the ABCs
Raymond Hettinger <python@rcn.com>
parents:
50966
diff
changeset
|
452 '__len__', '__getitem__', '__setitem__', '__delitem__', 'insert') |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
453 |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
454 class TestCounter(unittest.TestCase): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
455 |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
456 def test_basics(self): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
457 c = Counter('abcaba') |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
458 self.assertEqual(c, Counter({'a':3 , 'b': 2, 'c': 1})) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
459 self.assertEqual(c, Counter(a=3, b=2, c=1)) |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
460 self.assert_(isinstance(c, dict)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
461 self.assert_(isinstance(c, Mapping)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
462 self.assert_(issubclass(Counter, dict)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
463 self.assert_(issubclass(Counter, Mapping)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
464 self.assertEqual(len(c), 3) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
465 self.assertEqual(sum(c.values()), 6) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
466 self.assertEqual(sorted(c.values()), [1, 2, 3]) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
467 self.assertEqual(sorted(c.keys()), ['a', 'b', 'c']) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
468 self.assertEqual(sorted(c), ['a', 'b', 'c']) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
469 self.assertEqual(sorted(c.items()), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
470 [('a', 3), ('b', 2), ('c', 1)]) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
471 self.assertEqual(c['b'], 2) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
472 self.assertEqual(c['z'], 0) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
473 self.assertEqual(c.has_key('c'), True) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
474 self.assertEqual(c.has_key('z'), False) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
475 self.assertEqual(c.__contains__('c'), True) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
476 self.assertEqual(c.__contains__('z'), False) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
477 self.assertEqual(c.get('b', 10), 2) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
478 self.assertEqual(c.get('z', 10), 10) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
479 self.assertEqual(c, dict(a=3, b=2, c=1)) |
|
50756
f45997d9ac55
Simplify Counter() API. Replace items keyword argument
Raymond Hettinger <python@rcn.com>
parents:
50753
diff
changeset
|
480 self.assertEqual(repr(c), "Counter({'a': 3, 'b': 2, 'c': 1})") |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
481 self.assertEqual(c.most_common(), [('a', 3), ('b', 2), ('c', 1)]) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
482 for i in range(5): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
483 self.assertEqual(c.most_common(i), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
484 [('a', 3), ('b', 2), ('c', 1)][:i]) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
485 self.assertEqual(''.join(sorted(c.elements())), 'aaabbc') |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
486 c['a'] += 1 # increment an existing value |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
487 c['b'] -= 2 # sub existing value to zero |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
488 del c['c'] # remove an entry |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
489 del c['c'] # make sure that del doesn't raise KeyError |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
490 c['d'] -= 2 # sub from a missing value |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
491 c['e'] = -5 # directly assign a missing value |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
492 c['f'] += 4 # add to a missing value |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
493 self.assertEqual(c, dict(a=4, b=0, d=-2, e=-5, f=4)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
494 self.assertEqual(''.join(sorted(c.elements())), 'aaaaffff') |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
495 self.assertEqual(c.pop('f'), 4) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
496 self.assertEqual('f' in c, False) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
497 for i in range(3): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
498 elem, cnt = c.popitem() |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
499 self.assertEqual(elem in c, False) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
500 c.clear() |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
501 self.assertEqual(c, {}) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
502 self.assertEqual(repr(c), 'Counter()') |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
503 self.assertRaises(NotImplementedError, Counter.fromkeys, 'abc') |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
504 self.assertRaises(TypeError, hash, c) |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
505 c.update(dict(a=5, b=3)) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
506 c.update(c=1) |
|
50756
f45997d9ac55
Simplify Counter() API. Replace items keyword argument
Raymond Hettinger <python@rcn.com>
parents:
50753
diff
changeset
|
507 c.update(Counter('a' * 50 + 'b' * 30)) |
|
50791
d2cc26bda6a9
Add tests for __init__() and update() with no args.
Raymond Hettinger <python@rcn.com>
parents:
50756
diff
changeset
|
508 c.update() # test case with no args |
|
50756
f45997d9ac55
Simplify Counter() API. Replace items keyword argument
Raymond Hettinger <python@rcn.com>
parents:
50753
diff
changeset
|
509 c.__init__('a' * 500 + 'b' * 300) |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
510 c.__init__('cdc') |
|
50791
d2cc26bda6a9
Add tests for __init__() and update() with no args.
Raymond Hettinger <python@rcn.com>
parents:
50756
diff
changeset
|
511 c.__init__() |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
512 self.assertEqual(c, dict(a=555, b=333, c=3, d=1)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
513 self.assertEqual(c.setdefault('d', 5), 1) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
514 self.assertEqual(c['d'], 1) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
515 self.assertEqual(c.setdefault('e', 5), 5) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
516 self.assertEqual(c['e'], 5) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
517 |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
518 def test_copying(self): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
519 # Check that counters are copyable, deepcopyable, picklable, and |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
520 #have a repr/eval round-trip |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
521 words = Counter('which witch had which witches wrist watch'.split()) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
522 update_test = Counter() |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
523 update_test.update(words) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
524 for i, dup in enumerate([ |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
525 words.copy(), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
526 copy.copy(words), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
527 copy.deepcopy(words), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
528 pickle.loads(pickle.dumps(words, 0)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
529 pickle.loads(pickle.dumps(words, 1)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
530 pickle.loads(pickle.dumps(words, 2)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
531 pickle.loads(pickle.dumps(words, -1)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
532 cPickle.loads(cPickle.dumps(words, 0)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
533 cPickle.loads(cPickle.dumps(words, 1)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
534 cPickle.loads(cPickle.dumps(words, 2)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
535 cPickle.loads(cPickle.dumps(words, -1)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
536 eval(repr(words)), |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
537 update_test, |
|
50756
f45997d9ac55
Simplify Counter() API. Replace items keyword argument
Raymond Hettinger <python@rcn.com>
parents:
50753
diff
changeset
|
538 Counter(words), |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
539 ]): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
540 msg = (i, dup, words) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
541 self.assert_(dup is not words) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
542 self.assertEquals(dup, words) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
543 self.assertEquals(len(dup), len(words)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
544 self.assertEquals(type(dup), type(words)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
545 |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
546 def test_conversions(self): |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
547 # Convert to: set, list, dict |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
548 s = 'she sells sea shells by the sea shore' |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
549 self.assertEqual(sorted(Counter(s).elements()), sorted(s)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
550 self.assertEqual(sorted(Counter(s)), sorted(set(s))) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
551 self.assertEqual(dict(Counter(s)), dict(Counter(s).items())) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
552 self.assertEqual(set(Counter(s)), set(s)) |
|
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
553 |
|
50966
cbe54651e3b9
Tighten-up the docs for Counter().
Raymond Hettinger <python@rcn.com>
parents:
50964
diff
changeset
|
554 def test_invariant_for_the_in_operator(self): |
|
cbe54651e3b9
Tighten-up the docs for Counter().
Raymond Hettinger <python@rcn.com>
parents:
50964
diff
changeset
|
555 c = Counter(a=10, b=-2, c=0) |
|
cbe54651e3b9
Tighten-up the docs for Counter().
Raymond Hettinger <python@rcn.com>
parents:
50964
diff
changeset
|
556 for elem in c: |
|
cbe54651e3b9
Tighten-up the docs for Counter().
Raymond Hettinger <python@rcn.com>
parents:
50964
diff
changeset
|
557 self.assert_(elem in c) |
|
cbe54651e3b9
Tighten-up the docs for Counter().
Raymond Hettinger <python@rcn.com>
parents:
50964
diff
changeset
|
558 |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
559 def test_multiset_operations(self): |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
560 # Verify that adding a zero counter will strip zeros and negatives |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
561 c = Counter(a=10, b=-2, c=0) + Counter() |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
562 self.assertEqual(dict(c), dict(a=10)) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
563 |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
564 elements = 'abcd' |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
565 for i in range(1000): |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
566 # test random pairs of multisets |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
567 p = Counter(dict((elem, randrange(-2,4)) for elem in elements)) |
|
50964
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
568 p.update(e=1, f=-1, g=0) |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
569 q = Counter(dict((elem, randrange(-2,4)) for elem in elements)) |
|
50964
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
570 q.update(h=1, i=-1, j=0) |
|
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
571 for counterop, numberop in [ |
|
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
572 (Counter.__add__, lambda x, y: max(0, x+y)), |
|
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
573 (Counter.__sub__, lambda x, y: max(0, x-y)), |
|
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
574 (Counter.__or__, lambda x, y: max(0,x,y)), |
|
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
575 (Counter.__and__, lambda x, y: max(0, min(x,y))), |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
576 ]: |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
577 result = counterop(p, q) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
578 for x in elements: |
|
50964
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
579 self.assertEqual(numberop(p[x], q[x]), result[x], |
|
8bf204af9691
Simplify explanation of multiset operations by removing restrictions on negative inputs.
Raymond Hettinger <python@rcn.com>
parents:
50920
diff
changeset
|
580 (counterop, x, p, q)) |
|
50920
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
581 # verify that results exclude non-positive counts |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
582 self.assert_(x>0 for x in result.values()) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
583 |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
584 elements = 'abcdef' |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
585 for i in range(100): |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
586 # verify that random multisets with no repeats are exactly like sets |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
587 p = Counter(dict((elem, randrange(0, 2)) for elem in elements)) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
588 q = Counter(dict((elem, randrange(0, 2)) for elem in elements)) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
589 for counterop, setop in [ |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
590 (Counter.__sub__, set.__sub__), |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
591 (Counter.__or__, set.__or__), |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
592 (Counter.__and__, set.__and__), |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
593 ]: |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
594 counter_result = counterop(p, q) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
595 set_result = setop(set(p.elements()), set(q.elements())) |
|
191b7b3ddf4f
Build-outs for Counter() class:
Raymond Hettinger <python@rcn.com>
parents:
50791
diff
changeset
|
596 self.assertEqual(counter_result, dict.fromkeys(set_result, 1)) |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
597 |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
598 class TestOrderedDict(unittest.TestCase): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
599 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
600 def test_init(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
601 with self.assertRaises(TypeError): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
602 OrderedDict([('a', 1), ('b', 2)], None) # too many args |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
603 pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
604 self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs) # dict input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
605 self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs) # kwds input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
606 self.assertEqual(list(OrderedDict(pairs).items()), pairs) # pairs input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
607 self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)], |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
608 c=3, e=5).items()), pairs) # mixed input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
609 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
610 # make sure no positional args conflict with possible kwdargs |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
611 self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args, |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
612 ['self']) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
613 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
614 # Make sure that direct calls to __init__ do not clear previous contents |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
615 d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
616 d.__init__([('e', 5), ('f', 6)], g=7, d=4) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
617 self.assertEqual(list(d.items()), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
618 [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
619 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
620 def test_update(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
621 with self.assertRaises(TypeError): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
622 OrderedDict().update([('a', 1), ('b', 2)], None) # too many args |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
623 pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
624 od = OrderedDict() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
625 od.update(dict(pairs)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
626 self.assertEqual(sorted(od.items()), pairs) # dict input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
627 od = OrderedDict() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
628 od.update(**dict(pairs)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
629 self.assertEqual(sorted(od.items()), pairs) # kwds input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
630 od = OrderedDict() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
631 od.update(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
632 self.assertEqual(list(od.items()), pairs) # pairs input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
633 od = OrderedDict() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
634 od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
635 self.assertEqual(list(od.items()), pairs) # mixed input |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
636 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
637 # Make sure that direct calls to update do not clear previous contents |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
638 # add that updates items are not moved to the end |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
639 d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
640 d.update([('e', 5), ('f', 6)], g=7, d=4) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
641 self.assertEqual(list(d.items()), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
642 [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
643 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
644 def test_clear(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
645 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
646 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
647 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
648 self.assertEqual(len(od), len(pairs)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
649 od.clear() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
650 self.assertEqual(len(od), 0) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
651 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
652 def test_delitem(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
653 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
654 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
655 del od['a'] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
656 self.assert_('a' not in od) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
657 with self.assertRaises(KeyError): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
658 del od['a'] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
659 self.assertEqual(list(od.items()), pairs[:2] + pairs[3:]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
660 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
661 def test_setitem(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
662 od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
663 od['c'] = 10 # existing element |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
664 od['f'] = 20 # new element |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
665 self.assertEqual(list(od.items()), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
666 [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
667 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
668 def test_iterators(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
669 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
670 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
671 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
672 self.assertEqual(list(od), [t[0] for t in pairs]) |
| 51968 | 673 self.assertEqual(od.keys()[:], [t[0] for t in pairs]) |
| 674 self.assertEqual(od.values()[:], [t[1] for t in pairs]) | |
| 675 self.assertEqual(od.items()[:], pairs) | |
| 676 self.assertEqual(list(od.iterkeys()), [t[0] for t in pairs]) | |
| 677 self.assertEqual(list(od.itervalues()), [t[1] for t in pairs]) | |
| 678 self.assertEqual(list(od.iteritems()), pairs) | |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
679 self.assertEqual(list(reversed(od)), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
680 [t[0] for t in reversed(pairs)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
681 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
682 def test_popitem(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
683 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
684 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
685 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
686 while pairs: |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
687 self.assertEqual(od.popitem(), pairs.pop()) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
688 with self.assertRaises(KeyError): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
689 od.popitem() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
690 self.assertEqual(len(od), 0) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
691 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
692 def test_pop(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
693 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
694 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
695 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
696 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
697 while pairs: |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
698 k, v = pairs.pop() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
699 self.assertEqual(od.pop(k), v) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
700 with self.assertRaises(KeyError): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
701 od.pop('xyz') |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
702 self.assertEqual(len(od), 0) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
703 self.assertEqual(od.pop(k, 12345), 12345) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
704 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
705 def test_equality(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
706 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
707 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
708 od1 = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
709 od2 = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
710 self.assertEqual(od1, od2) # same order implies equality |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
711 pairs = pairs[2:] + pairs[:2] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
712 od2 = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
713 self.assertNotEqual(od1, od2) # different order implies inequality |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
714 # comparison to regular dict is not order sensitive |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
715 self.assertEqual(od1, dict(od2)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
716 self.assertEqual(dict(od2), od1) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
717 # different length implied inequality |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
718 self.assertNotEqual(od1, OrderedDict(pairs[:-1])) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
719 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
720 def test_copying(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
721 # Check that ordered dicts are copyable, deepcopyable, picklable, |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
722 # and have a repr/eval round-trip |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
723 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
724 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
725 update_test = OrderedDict() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
726 update_test.update(od) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
727 for i, dup in enumerate([ |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
728 od.copy(), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
729 copy.copy(od), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
730 copy.deepcopy(od), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
731 pickle.loads(pickle.dumps(od, 0)), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
732 pickle.loads(pickle.dumps(od, 1)), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
733 pickle.loads(pickle.dumps(od, 2)), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
734 pickle.loads(pickle.dumps(od, -1)), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
735 eval(repr(od)), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
736 update_test, |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
737 OrderedDict(od), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
738 ]): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
739 self.assert_(dup is not od) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
740 self.assertEquals(dup, od) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
741 self.assertEquals(list(dup.items()), list(od.items())) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
742 self.assertEquals(len(dup), len(od)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
743 self.assertEquals(type(dup), type(od)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
744 |
|
51988
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
745 def test_yaml_linkage(self): |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
746 # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature. |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
747 # In yaml, lists are native but tuples are not. |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
748 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
749 od = OrderedDict(pairs) |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
750 # yaml.dump(od) --> |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
751 # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n' |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
752 self.assert_(all(type(pair)==list for pair in od.__reduce__()[1])) |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
753 |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
754 def test_reduce_not_too_fat(self): |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
755 # do not save instance dictionary if not needed |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
756 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
757 od = OrderedDict(pairs) |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
758 self.assertEqual(len(od.__reduce__()), 2) |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
759 od.x = 10 |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
760 self.assertEqual(len(od.__reduce__()), 3) |
|
778599ce81a1
Backport 70140, 70141, 70143, and 70144.
Raymond Hettinger <python@rcn.com>
parents:
51968
diff
changeset
|
761 |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
762 def test_repr(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
763 od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
764 self.assertEqual(repr(od), |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
765 "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])") |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
766 self.assertEqual(eval(repr(od)), od) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
767 self.assertEqual(repr(OrderedDict()), "OrderedDict()") |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
768 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
769 def test_setdefault(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
770 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
771 shuffle(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
772 od = OrderedDict(pairs) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
773 pair_order = list(od.items()) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
774 self.assertEqual(od.setdefault('a', 10), 3) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
775 # make sure order didn't change |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
776 self.assertEqual(list(od.items()), pair_order) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
777 self.assertEqual(od.setdefault('x', 10), 10) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
778 # make sure 'x' is added to the end |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
779 self.assertEqual(list(od.items())[-1], ('x', 10)) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
780 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
781 def test_reinsert(self): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
782 # Given insert a, insert b, delete a, re-insert a, |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
783 # verify that a is now later than b. |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
784 od = OrderedDict() |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
785 od['a'] = 1 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
786 od['b'] = 2 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
787 del od['a'] |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
788 od['a'] = 1 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
789 self.assertEqual(list(od.items()), [('b', 2), ('a', 1)]) |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
790 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
791 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
792 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
793 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
794 type2test = OrderedDict |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
795 |
|
52227
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
796 def test_popitem(self): |
|
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
797 d = self._empty_mapping() |
|
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
798 self.assertRaises(KeyError, d.popitem) |
|
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
799 |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
800 class MyOrderedDict(OrderedDict): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
801 pass |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
802 |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
803 class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
804 type2test = MyOrderedDict |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
805 |
|
52227
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
806 def test_popitem(self): |
|
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
807 d = self._empty_mapping() |
|
8fd1721c32c4
* Add clearer comment to initialization code.
Raymond Hettinger <python@rcn.com>
parents:
51988
diff
changeset
|
808 self.assertRaises(KeyError, d.popitem) |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
809 |
|
44627
7851e75f5438
Run doctests on the collections module
Raymond Hettinger <python@rcn.com>
parents:
44485
diff
changeset
|
810 import doctest, collections |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43856
diff
changeset
|
811 |
| 40992 | 812 def test_main(verbose=None): |
|
46249
d6e1333e6075
A DocTestSuite cannot run multiple times: it clears its globals dictionary after the first run.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44627
diff
changeset
|
813 NamedTupleDocs = doctest.DocTestSuite(module=collections) |
|
50753
5934612d5070
Issue 1696199: Add collections.Counter().
Raymond Hettinger <python@rcn.com>
parents:
49301
diff
changeset
|
814 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, |
|
51962
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
815 TestCollectionABCs, TestCounter, |
|
4a4da968de2a
Backport PEP 372: OrderedDict()
Raymond Hettinger <python@rcn.com>
parents:
51836
diff
changeset
|
816 TestOrderedDict, GeneralMappingTests, SubclassMappingTests] |
| 40992 | 817 test_support.run_unittest(*test_classes) |
|
46249
d6e1333e6075
A DocTestSuite cannot run multiple times: it clears its globals dictionary after the first run.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44627
diff
changeset
|
818 test_support.run_doctest(collections, verbose) |
| 40992 | 819 |
| 820 if __name__ == "__main__": | |
| 821 test_main(verbose=True) |
