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