Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_defaultdict.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | a8deeb549e1a |
| children |
| rev | line source |
|---|---|
| 36656 | 1 """Unit tests for collections.defaultdict.""" |
| 2 | |
| 3 import os | |
| 4 import copy | |
| 5 import tempfile | |
| 6 import unittest | |
|
39206
025c27b812f6
Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
7 from test import test_support |
| 36656 | 8 |
| 9 from collections import defaultdict | |
| 10 | |
| 11 def foobar(): | |
| 12 return list | |
| 13 | |
| 14 class TestDefaultDict(unittest.TestCase): | |
| 15 | |
| 16 def test_basic(self): | |
| 17 d1 = defaultdict() | |
| 18 self.assertEqual(d1.default_factory, None) | |
| 19 d1.default_factory = list | |
| 20 d1[12].append(42) | |
| 21 self.assertEqual(d1, {12: [42]}) | |
| 22 d1[12].append(24) | |
| 23 self.assertEqual(d1, {12: [42, 24]}) | |
| 24 d1[13] | |
| 25 d1[14] | |
| 26 self.assertEqual(d1, {12: [42, 24], 13: [], 14: []}) | |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
45231
diff
changeset
|
27 self.assertTrue(d1[12] is not d1[13] is not d1[14]) |
| 36656 | 28 d2 = defaultdict(list, foo=1, bar=2) |
| 29 self.assertEqual(d2.default_factory, list) | |
| 30 self.assertEqual(d2, {"foo": 1, "bar": 2}) | |
| 31 self.assertEqual(d2["foo"], 1) | |
| 32 self.assertEqual(d2["bar"], 2) | |
| 33 self.assertEqual(d2[42], []) | |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
34 self.assertIn("foo", d2) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
35 self.assertIn("foo", d2.keys()) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
36 self.assertIn("bar", d2) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
37 self.assertIn("bar", d2.keys()) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
38 self.assertIn(42, d2) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
39 self.assertIn(42, d2.keys()) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
40 self.assertNotIn(12, d2) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55535
diff
changeset
|
41 self.assertNotIn(12, d2.keys()) |
| 36656 | 42 d2.default_factory = None |
| 43 self.assertEqual(d2.default_factory, None) | |
| 44 try: | |
| 45 d2[15] | |
| 46 except KeyError, err: | |
| 47 self.assertEqual(err.args, (15,)) | |
| 48 else: | |
| 49 self.fail("d2[15] didn't raise KeyError") | |
|
40760
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
39206
diff
changeset
|
50 self.assertRaises(TypeError, defaultdict, 1) |
| 36656 | 51 |
| 52 def test_missing(self): | |
| 53 d1 = defaultdict() | |
| 54 self.assertRaises(KeyError, d1.__missing__, 42) | |
| 55 d1.default_factory = list | |
| 56 self.assertEqual(d1.__missing__(42), []) | |
| 57 | |
| 58 def test_repr(self): | |
| 59 d1 = defaultdict() | |
| 60 self.assertEqual(d1.default_factory, None) | |
| 61 self.assertEqual(repr(d1), "defaultdict(None, {})") | |
|
55534
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
62 self.assertEqual(eval(repr(d1)), d1) |
| 36656 | 63 d1[11] = 41 |
| 64 self.assertEqual(repr(d1), "defaultdict(None, {11: 41})") | |
|
40760
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
39206
diff
changeset
|
65 d2 = defaultdict(int) |
|
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
39206
diff
changeset
|
66 self.assertEqual(d2.default_factory, int) |
| 36656 | 67 d2[12] = 42 |
|
40760
2900c7564055
Check for a common user error with defaultdict().
Raymond Hettinger <python@rcn.com>
parents:
39206
diff
changeset
|
68 self.assertEqual(repr(d2), "defaultdict(<type 'int'>, {12: 42})") |
| 36656 | 69 def foo(): return 43 |
| 70 d3 = defaultdict(foo) | |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
45231
diff
changeset
|
71 self.assertTrue(d3.default_factory is foo) |
| 36656 | 72 d3[13] |
| 73 self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo)) | |
| 74 | |
| 75 def test_print(self): | |
| 76 d1 = defaultdict() | |
| 77 def foo(): return 42 | |
| 78 d2 = defaultdict(foo, {1: 2}) | |
| 79 # NOTE: We can't use tempfile.[Named]TemporaryFile since this | |
| 80 # code must exercise the tp_print C code, which only gets | |
| 81 # invoked for *real* files. | |
| 82 tfn = tempfile.mktemp() | |
| 83 try: | |
| 84 f = open(tfn, "w+") | |
| 85 try: | |
| 86 print >>f, d1 | |
| 87 print >>f, d2 | |
| 88 f.seek(0) | |
| 89 self.assertEqual(f.readline(), repr(d1) + "\n") | |
| 90 self.assertEqual(f.readline(), repr(d2) + "\n") | |
| 91 finally: | |
| 92 f.close() | |
| 93 finally: | |
| 94 os.remove(tfn) | |
| 95 | |
| 96 def test_copy(self): | |
| 97 d1 = defaultdict() | |
| 98 d2 = d1.copy() | |
| 99 self.assertEqual(type(d2), defaultdict) | |
| 100 self.assertEqual(d2.default_factory, None) | |
| 101 self.assertEqual(d2, {}) | |
| 102 d1.default_factory = list | |
| 103 d3 = d1.copy() | |
| 104 self.assertEqual(type(d3), defaultdict) | |
| 105 self.assertEqual(d3.default_factory, list) | |
| 106 self.assertEqual(d3, {}) | |
| 107 d1[42] | |
| 108 d4 = d1.copy() | |
| 109 self.assertEqual(type(d4), defaultdict) | |
| 110 self.assertEqual(d4.default_factory, list) | |
| 111 self.assertEqual(d4, {42: []}) | |
| 112 d4[12] | |
| 113 self.assertEqual(d4, {42: [], 12: []}) | |
| 114 | |
|
55534
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
115 # Issue 6637: Copy fails for empty default dict |
|
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
116 d = defaultdict() |
|
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
117 d['a'] = 42 |
|
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
118 e = d.copy() |
|
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
119 self.assertEqual(e['a'], 42) |
|
dd4da75ebc97
Issue 6637: defaultdict.copy() failed with an empty factory.
Raymond Hettinger <python@rcn.com>
parents:
45231
diff
changeset
|
120 |
| 36656 | 121 def test_shallow_copy(self): |
| 122 d1 = defaultdict(foobar, {1: 1}) | |
| 123 d2 = copy.copy(d1) | |
| 124 self.assertEqual(d2.default_factory, foobar) | |
| 125 self.assertEqual(d2, d1) | |
| 126 d1.default_factory = list | |
| 127 d2 = copy.copy(d1) | |
| 128 self.assertEqual(d2.default_factory, list) | |
| 129 self.assertEqual(d2, d1) | |
| 130 | |
| 131 def test_deep_copy(self): | |
| 132 d1 = defaultdict(foobar, {1: [1]}) | |
| 133 d2 = copy.deepcopy(d1) | |
| 134 self.assertEqual(d2.default_factory, foobar) | |
| 135 self.assertEqual(d2, d1) | |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
45231
diff
changeset
|
136 self.assertTrue(d1[1] is not d2[1]) |
| 36656 | 137 d1.default_factory = list |
| 138 d2 = copy.deepcopy(d1) | |
| 139 self.assertEqual(d2.default_factory, list) | |
| 140 self.assertEqual(d2, d1) | |
| 141 | |
|
41021
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
142 def test_keyerror_without_factory(self): |
|
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
143 d1 = defaultdict() |
|
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
144 try: |
|
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
145 d1[(1,)] |
|
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
146 except KeyError, err: |
|
41617
615795d52b9f
Deprecate BaseException.message as per PEP 352.
Brett Cannon <bcannon@gmail.com>
parents:
41021
diff
changeset
|
147 self.assertEqual(err.args[0], (1,)) |
|
41021
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
148 else: |
|
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
149 self.fail("expected KeyError") |
|
09d08fcb8f7b
A test case for the defaultdict KeyError bug.
Georg Brandl <georg@python.org>
parents:
40760
diff
changeset
|
150 |
|
45232
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
151 def test_recursive_repr(self): |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
152 # Issue2045: stack overflow when default_factory is a bound method |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
153 class sub(defaultdict): |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
154 def __init__(self): |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
155 self.default_factory = self._factory |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
156 def _factory(self): |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
157 return [] |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
158 d = sub() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
45231
diff
changeset
|
159 self.assertTrue(repr(d).startswith( |
|
45232
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
160 "defaultdict(<bound method sub._factory of defaultdict(...")) |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
161 |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
162 # NOTE: printing a subclass of a builtin type does not call its |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
163 # tp_print slot. So this part is essentially the same test as above. |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
164 tfn = tempfile.mktemp() |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
165 try: |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
166 f = open(tfn, "w+") |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
167 try: |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
168 print >>f, d |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
169 finally: |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
170 f.close() |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
171 finally: |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
172 os.remove(tfn) |
|
4166945a0cc4
issue 2045: Infinite recursion when printing a subclass of defaultdict,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41021
diff
changeset
|
173 |
|
73881
a8deeb549e1a
#13531: add a test for defaultdict with a non-callable arg. Patch by Mike Cheng.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68256
diff
changeset
|
174 def test_callable_arg(self): |
|
a8deeb549e1a
#13531: add a test for defaultdict with a non-callable arg. Patch by Mike Cheng.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68256
diff
changeset
|
175 self.assertRaises(TypeError, defaultdict, {}) |
| 36656 | 176 |
|
39206
025c27b812f6
Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
177 def test_main(): |
|
025c27b812f6
Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
178 test_support.run_unittest(TestDefaultDict) |
|
025c27b812f6
Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
179 |
| 36656 | 180 if __name__ == "__main__": |
|
39206
025c27b812f6
Add test_main() methods. These three tests were never run
Georg Brandl <georg@python.org>
parents:
36656
diff
changeset
|
181 test_main() |
