ยปCore Development>Code coverage>Lib/test/test_defaultdict.py

Python code coverage for Lib/test/test_defaultdict.py

#countcontent
1n/a"""Unit tests for collections.defaultdict."""
2n/a
3n/aimport os
4n/aimport copy
5n/aimport pickle
6n/aimport tempfile
7n/aimport unittest
8n/a
9n/afrom collections import defaultdict
10n/a
11n/adef foobar():
12n/a return list
13n/a
14n/aclass TestDefaultDict(unittest.TestCase):
15n/a
16n/a def test_basic(self):
17n/a d1 = defaultdict()
18n/a self.assertEqual(d1.default_factory, None)
19n/a d1.default_factory = list
20n/a d1[12].append(42)
21n/a self.assertEqual(d1, {12: [42]})
22n/a d1[12].append(24)
23n/a self.assertEqual(d1, {12: [42, 24]})
24n/a d1[13]
25n/a d1[14]
26n/a self.assertEqual(d1, {12: [42, 24], 13: [], 14: []})
27n/a self.assertTrue(d1[12] is not d1[13] is not d1[14])
28n/a d2 = defaultdict(list, foo=1, bar=2)
29n/a self.assertEqual(d2.default_factory, list)
30n/a self.assertEqual(d2, {"foo": 1, "bar": 2})
31n/a self.assertEqual(d2["foo"], 1)
32n/a self.assertEqual(d2["bar"], 2)
33n/a self.assertEqual(d2[42], [])
34n/a self.assertIn("foo", d2)
35n/a self.assertIn("foo", d2.keys())
36n/a self.assertIn("bar", d2)
37n/a self.assertIn("bar", d2.keys())
38n/a self.assertIn(42, d2)
39n/a self.assertIn(42, d2.keys())
40n/a self.assertNotIn(12, d2)
41n/a self.assertNotIn(12, d2.keys())
42n/a d2.default_factory = None
43n/a self.assertEqual(d2.default_factory, None)
44n/a try:
45n/a d2[15]
46n/a except KeyError as err:
47n/a self.assertEqual(err.args, (15,))
48n/a else:
49n/a self.fail("d2[15] didn't raise KeyError")
50n/a self.assertRaises(TypeError, defaultdict, 1)
51n/a
52n/a def test_missing(self):
53n/a d1 = defaultdict()
54n/a self.assertRaises(KeyError, d1.__missing__, 42)
55n/a d1.default_factory = list
56n/a self.assertEqual(d1.__missing__(42), [])
57n/a
58n/a def test_repr(self):
59n/a d1 = defaultdict()
60n/a self.assertEqual(d1.default_factory, None)
61n/a self.assertEqual(repr(d1), "defaultdict(None, {})")
62n/a self.assertEqual(eval(repr(d1)), d1)
63n/a d1[11] = 41
64n/a self.assertEqual(repr(d1), "defaultdict(None, {11: 41})")
65n/a d2 = defaultdict(int)
66n/a self.assertEqual(d2.default_factory, int)
67n/a d2[12] = 42
68n/a self.assertEqual(repr(d2), "defaultdict(<class 'int'>, {12: 42})")
69n/a def foo(): return 43
70n/a d3 = defaultdict(foo)
71n/a self.assertTrue(d3.default_factory is foo)
72n/a d3[13]
73n/a self.assertEqual(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo))
74n/a
75n/a def test_print(self):
76n/a d1 = defaultdict()
77n/a def foo(): return 42
78n/a d2 = defaultdict(foo, {1: 2})
79n/a # NOTE: We can't use tempfile.[Named]TemporaryFile since this
80n/a # code must exercise the tp_print C code, which only gets
81n/a # invoked for *real* files.
82n/a tfn = tempfile.mktemp()
83n/a try:
84n/a f = open(tfn, "w+")
85n/a try:
86n/a print(d1, file=f)
87n/a print(d2, file=f)
88n/a f.seek(0)
89n/a self.assertEqual(f.readline(), repr(d1) + "\n")
90n/a self.assertEqual(f.readline(), repr(d2) + "\n")
91n/a finally:
92n/a f.close()
93n/a finally:
94n/a os.remove(tfn)
95n/a
96n/a def test_copy(self):
97n/a d1 = defaultdict()
98n/a d2 = d1.copy()
99n/a self.assertEqual(type(d2), defaultdict)
100n/a self.assertEqual(d2.default_factory, None)
101n/a self.assertEqual(d2, {})
102n/a d1.default_factory = list
103n/a d3 = d1.copy()
104n/a self.assertEqual(type(d3), defaultdict)
105n/a self.assertEqual(d3.default_factory, list)
106n/a self.assertEqual(d3, {})
107n/a d1[42]
108n/a d4 = d1.copy()
109n/a self.assertEqual(type(d4), defaultdict)
110n/a self.assertEqual(d4.default_factory, list)
111n/a self.assertEqual(d4, {42: []})
112n/a d4[12]
113n/a self.assertEqual(d4, {42: [], 12: []})
114n/a
115n/a # Issue 6637: Copy fails for empty default dict
116n/a d = defaultdict()
117n/a d['a'] = 42
118n/a e = d.copy()
119n/a self.assertEqual(e['a'], 42)
120n/a
121n/a def test_shallow_copy(self):
122n/a d1 = defaultdict(foobar, {1: 1})
123n/a d2 = copy.copy(d1)
124n/a self.assertEqual(d2.default_factory, foobar)
125n/a self.assertEqual(d2, d1)
126n/a d1.default_factory = list
127n/a d2 = copy.copy(d1)
128n/a self.assertEqual(d2.default_factory, list)
129n/a self.assertEqual(d2, d1)
130n/a
131n/a def test_deep_copy(self):
132n/a d1 = defaultdict(foobar, {1: [1]})
133n/a d2 = copy.deepcopy(d1)
134n/a self.assertEqual(d2.default_factory, foobar)
135n/a self.assertEqual(d2, d1)
136n/a self.assertTrue(d1[1] is not d2[1])
137n/a d1.default_factory = list
138n/a d2 = copy.deepcopy(d1)
139n/a self.assertEqual(d2.default_factory, list)
140n/a self.assertEqual(d2, d1)
141n/a
142n/a def test_keyerror_without_factory(self):
143n/a d1 = defaultdict()
144n/a try:
145n/a d1[(1,)]
146n/a except KeyError as err:
147n/a self.assertEqual(err.args[0], (1,))
148n/a else:
149n/a self.fail("expected KeyError")
150n/a
151n/a def test_recursive_repr(self):
152n/a # Issue2045: stack overflow when default_factory is a bound method
153n/a class sub(defaultdict):
154n/a def __init__(self):
155n/a self.default_factory = self._factory
156n/a def _factory(self):
157n/a return []
158n/a d = sub()
159n/a self.assertRegex(repr(d),
160n/a r"defaultdict\(<bound method .*sub\._factory "
161n/a r"of defaultdict\(\.\.\., \{\}\)>, \{\}\)")
162n/a
163n/a # NOTE: printing a subclass of a builtin type does not call its
164n/a # tp_print slot. So this part is essentially the same test as above.
165n/a tfn = tempfile.mktemp()
166n/a try:
167n/a f = open(tfn, "w+")
168n/a try:
169n/a print(d, file=f)
170n/a finally:
171n/a f.close()
172n/a finally:
173n/a os.remove(tfn)
174n/a
175n/a def test_callable_arg(self):
176n/a self.assertRaises(TypeError, defaultdict, {})
177n/a
178n/a def test_pickling(self):
179n/a d = defaultdict(int)
180n/a d[1]
181n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
182n/a s = pickle.dumps(d, proto)
183n/a o = pickle.loads(s)
184n/a self.assertEqual(d, o)
185n/a
186n/aif __name__ == "__main__":
187n/a unittest.main()