blob: 245f8c858b69bcfe2c1c47a02a7dc83592b07981 [file] [log] [blame]
Raymond Hettingerc074e9d2015-05-26 01:47:58 -07001import collections
Serhiy Storchakad7a44152015-11-12 11:23:04 +02002import copy
3import pickle
Guido van Rossumb90c8482007-02-10 01:11:45 +00004import unittest
Guido van Rossumb90c8482007-02-10 01:11:45 +00005
6class DictSetTest(unittest.TestCase):
7
Guido van Rossum3ac67412007-02-10 18:55:06 +00008 def test_constructors_not_callable(self):
Guido van Rossumcc2b0162007-02-11 06:12:03 +00009 kt = type({}.keys())
Guido van Rossum3ac67412007-02-10 18:55:06 +000010 self.assertRaises(TypeError, kt, {})
11 self.assertRaises(TypeError, kt)
Guido van Rossumcc2b0162007-02-11 06:12:03 +000012 it = type({}.items())
Guido van Rossum3ac67412007-02-10 18:55:06 +000013 self.assertRaises(TypeError, it, {})
14 self.assertRaises(TypeError, it)
Guido van Rossumcc2b0162007-02-11 06:12:03 +000015 vt = type({}.values())
Guido van Rossum3ac67412007-02-10 18:55:06 +000016 self.assertRaises(TypeError, vt, {})
17 self.assertRaises(TypeError, vt)
18
Guido van Rossumb90c8482007-02-10 01:11:45 +000019 def test_dict_keys(self):
20 d = {1: 10, "a": "ABC"}
Guido van Rossumcc2b0162007-02-11 06:12:03 +000021 keys = d.keys()
Guido van Rossum83825ac2007-02-10 04:54:19 +000022 self.assertEqual(len(keys), 2)
Guido van Rossumd9214d12007-02-12 02:23:40 +000023 self.assertEqual(set(keys), {1, "a"})
24 self.assertEqual(keys, {1, "a"})
25 self.assertNotEqual(keys, {1, "a", "b"})
26 self.assertNotEqual(keys, {1, "b"})
27 self.assertNotEqual(keys, {1})
28 self.assertNotEqual(keys, 42)
Benjamin Peterson577473f2010-01-19 00:09:57 +000029 self.assertIn(1, keys)
30 self.assertIn("a", keys)
31 self.assertNotIn(10, keys)
32 self.assertNotIn("Z", keys)
Guido van Rossumd9214d12007-02-12 02:23:40 +000033 self.assertEqual(d.keys(), d.keys())
34 e = {1: 11, "a": "def"}
35 self.assertEqual(d.keys(), e.keys())
36 del e["a"]
37 self.assertNotEqual(d.keys(), e.keys())
Guido van Rossumb90c8482007-02-10 01:11:45 +000038
39 def test_dict_items(self):
40 d = {1: 10, "a": "ABC"}
Guido van Rossumcc2b0162007-02-11 06:12:03 +000041 items = d.items()
Guido van Rossum83825ac2007-02-10 04:54:19 +000042 self.assertEqual(len(items), 2)
Guido van Rossumd9214d12007-02-12 02:23:40 +000043 self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
44 self.assertEqual(items, {(1, 10), ("a", "ABC")})
45 self.assertNotEqual(items, {(1, 10), ("a", "ABC"), "junk"})
46 self.assertNotEqual(items, {(1, 10), ("a", "def")})
47 self.assertNotEqual(items, {(1, 10)})
48 self.assertNotEqual(items, 42)
Benjamin Peterson577473f2010-01-19 00:09:57 +000049 self.assertIn((1, 10), items)
50 self.assertIn(("a", "ABC"), items)
51 self.assertNotIn((1, 11), items)
52 self.assertNotIn(1, items)
53 self.assertNotIn((), items)
54 self.assertNotIn((1,), items)
55 self.assertNotIn((1, 2, 3), items)
Guido van Rossumd9214d12007-02-12 02:23:40 +000056 self.assertEqual(d.items(), d.items())
57 e = d.copy()
58 self.assertEqual(d.items(), e.items())
59 e["a"] = "def"
60 self.assertNotEqual(d.items(), e.items())
61
62 def test_dict_mixed_keys_items(self):
63 d = {(1, 1): 11, (2, 2): 22}
64 e = {1: 1, 2: 2}
65 self.assertEqual(d.keys(), e.items())
66 self.assertNotEqual(d.items(), e.keys())
Guido van Rossumb90c8482007-02-10 01:11:45 +000067
68 def test_dict_values(self):
69 d = {1: 10, "a": "ABC"}
Guido van Rossumcc2b0162007-02-11 06:12:03 +000070 values = d.values()
Guido van Rossumb90c8482007-02-10 01:11:45 +000071 self.assertEqual(set(values), {10, "ABC"})
Guido van Rossum83825ac2007-02-10 04:54:19 +000072 self.assertEqual(len(values), 2)
Guido van Rossumb90c8482007-02-10 01:11:45 +000073
Alexandre Vassalottia7142572010-05-04 03:41:49 +000074 def test_dict_repr(self):
75 d = {1: 10, "a": "ABC"}
76 self.assertIsInstance(repr(d), str)
77 r = repr(d.items())
78 self.assertIsInstance(r, str)
79 self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or
80 r == "dict_items([(1, 10), ('a', 'ABC')])")
81 r = repr(d.keys())
82 self.assertIsInstance(r, str)
83 self.assertTrue(r == "dict_keys(['a', 1])" or
84 r == "dict_keys([1, 'a'])")
85 r = repr(d.values())
86 self.assertIsInstance(r, str)
87 self.assertTrue(r == "dict_values(['ABC', 10])" or
88 r == "dict_values([10, 'ABC'])")
89
90 def test_keys_set_operations(self):
91 d1 = {'a': 1, 'b': 2}
92 d2 = {'b': 3, 'c': 2}
93 d3 = {'d': 4, 'e': 5}
94 self.assertEqual(d1.keys() & d1.keys(), {'a', 'b'})
95 self.assertEqual(d1.keys() & d2.keys(), {'b'})
96 self.assertEqual(d1.keys() & d3.keys(), set())
97 self.assertEqual(d1.keys() & set(d1.keys()), {'a', 'b'})
98 self.assertEqual(d1.keys() & set(d2.keys()), {'b'})
99 self.assertEqual(d1.keys() & set(d3.keys()), set())
100
101 self.assertEqual(d1.keys() | d1.keys(), {'a', 'b'})
102 self.assertEqual(d1.keys() | d2.keys(), {'a', 'b', 'c'})
103 self.assertEqual(d1.keys() | d3.keys(), {'a', 'b', 'd', 'e'})
104 self.assertEqual(d1.keys() | set(d1.keys()), {'a', 'b'})
105 self.assertEqual(d1.keys() | set(d2.keys()), {'a', 'b', 'c'})
106 self.assertEqual(d1.keys() | set(d3.keys()),
107 {'a', 'b', 'd', 'e'})
108
109 self.assertEqual(d1.keys() ^ d1.keys(), set())
110 self.assertEqual(d1.keys() ^ d2.keys(), {'a', 'c'})
111 self.assertEqual(d1.keys() ^ d3.keys(), {'a', 'b', 'd', 'e'})
112 self.assertEqual(d1.keys() ^ set(d1.keys()), set())
113 self.assertEqual(d1.keys() ^ set(d2.keys()), {'a', 'c'})
114 self.assertEqual(d1.keys() ^ set(d3.keys()),
115 {'a', 'b', 'd', 'e'})
116
Ezio Melotti25326352013-08-08 20:12:28 +0300117 self.assertEqual(d1.keys() - d1.keys(), set())
118 self.assertEqual(d1.keys() - d2.keys(), {'a'})
119 self.assertEqual(d1.keys() - d3.keys(), {'a', 'b'})
120 self.assertEqual(d1.keys() - set(d1.keys()), set())
121 self.assertEqual(d1.keys() - set(d2.keys()), {'a'})
122 self.assertEqual(d1.keys() - set(d3.keys()), {'a', 'b'})
123
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +0000124 self.assertFalse(d1.keys().isdisjoint(d1.keys()))
125 self.assertFalse(d1.keys().isdisjoint(d2.keys()))
126 self.assertFalse(d1.keys().isdisjoint(list(d2.keys())))
127 self.assertFalse(d1.keys().isdisjoint(set(d2.keys())))
128 self.assertTrue(d1.keys().isdisjoint({'x', 'y', 'z'}))
129 self.assertTrue(d1.keys().isdisjoint(['x', 'y', 'z']))
130 self.assertTrue(d1.keys().isdisjoint(set(['x', 'y', 'z'])))
131 self.assertTrue(d1.keys().isdisjoint(set(['x', 'y'])))
132 self.assertTrue(d1.keys().isdisjoint(['x', 'y']))
133 self.assertTrue(d1.keys().isdisjoint({}))
134 self.assertTrue(d1.keys().isdisjoint(d3.keys()))
135
136 de = {}
137 self.assertTrue(de.keys().isdisjoint(set()))
138 self.assertTrue(de.keys().isdisjoint([]))
139 self.assertTrue(de.keys().isdisjoint(de.keys()))
140 self.assertTrue(de.keys().isdisjoint([1]))
141
Alexandre Vassalottia7142572010-05-04 03:41:49 +0000142 def test_items_set_operations(self):
143 d1 = {'a': 1, 'b': 2}
144 d2 = {'a': 2, 'b': 2}
145 d3 = {'d': 4, 'e': 5}
146 self.assertEqual(
147 d1.items() & d1.items(), {('a', 1), ('b', 2)})
148 self.assertEqual(d1.items() & d2.items(), {('b', 2)})
149 self.assertEqual(d1.items() & d3.items(), set())
150 self.assertEqual(d1.items() & set(d1.items()),
151 {('a', 1), ('b', 2)})
152 self.assertEqual(d1.items() & set(d2.items()), {('b', 2)})
153 self.assertEqual(d1.items() & set(d3.items()), set())
154
155 self.assertEqual(d1.items() | d1.items(),
156 {('a', 1), ('b', 2)})
157 self.assertEqual(d1.items() | d2.items(),
158 {('a', 1), ('a', 2), ('b', 2)})
159 self.assertEqual(d1.items() | d3.items(),
160 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
161 self.assertEqual(d1.items() | set(d1.items()),
162 {('a', 1), ('b', 2)})
163 self.assertEqual(d1.items() | set(d2.items()),
164 {('a', 1), ('a', 2), ('b', 2)})
165 self.assertEqual(d1.items() | set(d3.items()),
166 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
167
168 self.assertEqual(d1.items() ^ d1.items(), set())
169 self.assertEqual(d1.items() ^ d2.items(),
170 {('a', 1), ('a', 2)})
171 self.assertEqual(d1.items() ^ d3.items(),
172 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
173
Ezio Melotti25326352013-08-08 20:12:28 +0300174 self.assertEqual(d1.items() - d1.items(), set())
175 self.assertEqual(d1.items() - d2.items(), {('a', 1)})
176 self.assertEqual(d1.items() - d3.items(), {('a', 1), ('b', 2)})
177 self.assertEqual(d1.items() - set(d1.items()), set())
178 self.assertEqual(d1.items() - set(d2.items()), {('a', 1)})
179 self.assertEqual(d1.items() - set(d3.items()), {('a', 1), ('b', 2)})
180
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +0000181 self.assertFalse(d1.items().isdisjoint(d1.items()))
182 self.assertFalse(d1.items().isdisjoint(d2.items()))
183 self.assertFalse(d1.items().isdisjoint(list(d2.items())))
184 self.assertFalse(d1.items().isdisjoint(set(d2.items())))
185 self.assertTrue(d1.items().isdisjoint({'x', 'y', 'z'}))
186 self.assertTrue(d1.items().isdisjoint(['x', 'y', 'z']))
187 self.assertTrue(d1.items().isdisjoint(set(['x', 'y', 'z'])))
188 self.assertTrue(d1.items().isdisjoint(set(['x', 'y'])))
189 self.assertTrue(d1.items().isdisjoint({}))
190 self.assertTrue(d1.items().isdisjoint(d3.items()))
191
192 de = {}
193 self.assertTrue(de.items().isdisjoint(set()))
194 self.assertTrue(de.items().isdisjoint([]))
195 self.assertTrue(de.items().isdisjoint(de.items()))
196 self.assertTrue(de.items().isdisjoint([1]))
197
Benjamin Peterson611d9012013-05-19 19:39:38 -0700198 def test_recursive_repr(self):
199 d = {}
200 d[42] = d.values()
Yury Selivanovf488fb42015-07-03 01:04:23 -0400201 self.assertRaises(RecursionError, repr, d)
Benjamin Peterson611d9012013-05-19 19:39:38 -0700202
Serhiy Storchakad7a44152015-11-12 11:23:04 +0200203 def test_copy(self):
204 d = {1: 10, "a": "ABC"}
205 self.assertRaises(TypeError, copy.copy, d.keys())
206 self.assertRaises(TypeError, copy.copy, d.values())
207 self.assertRaises(TypeError, copy.copy, d.items())
208
209 def test_pickle(self):
210 d = {1: 10, "a": "ABC"}
211 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
212 self.assertRaises((TypeError, pickle.PicklingError),
213 pickle.dumps, d.keys(), proto)
214 self.assertRaises((TypeError, pickle.PicklingError),
215 pickle.dumps, d.values(), proto)
216 self.assertRaises((TypeError, pickle.PicklingError),
217 pickle.dumps, d.items(), proto)
218
Raymond Hettingerc074e9d2015-05-26 01:47:58 -0700219 def test_abc_registry(self):
220 d = dict(a=1)
221
222 self.assertIsInstance(d.keys(), collections.KeysView)
223 self.assertIsInstance(d.keys(), collections.MappingView)
224 self.assertIsInstance(d.keys(), collections.Set)
225 self.assertIsInstance(d.keys(), collections.Sized)
226 self.assertIsInstance(d.keys(), collections.Iterable)
227 self.assertIsInstance(d.keys(), collections.Container)
228
229 self.assertIsInstance(d.values(), collections.ValuesView)
230 self.assertIsInstance(d.values(), collections.MappingView)
231 self.assertIsInstance(d.values(), collections.Sized)
232
233 self.assertIsInstance(d.items(), collections.ItemsView)
234 self.assertIsInstance(d.items(), collections.MappingView)
235 self.assertIsInstance(d.items(), collections.Set)
236 self.assertIsInstance(d.items(), collections.Sized)
237 self.assertIsInstance(d.items(), collections.Iterable)
238 self.assertIsInstance(d.items(), collections.Container)
239
Alexandre Vassalottia7142572010-05-04 03:41:49 +0000240
Guido van Rossumb90c8482007-02-10 01:11:45 +0000241if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500242 unittest.main()