blob: 4c040796f5e2e192d96db4b603cc44feabea91bc [file] [log] [blame]
Guido van Rossumb90c8482007-02-10 01:11:45 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Guido van Rossumb90c8482007-02-10 01:11:45 +00003
4class DictSetTest(unittest.TestCase):
5
Guido van Rossum3ac67412007-02-10 18:55:06 +00006 def test_constructors_not_callable(self):
Guido van Rossumcc2b0162007-02-11 06:12:03 +00007 kt = type({}.keys())
Guido van Rossum3ac67412007-02-10 18:55:06 +00008 self.assertRaises(TypeError, kt, {})
9 self.assertRaises(TypeError, kt)
Guido van Rossumcc2b0162007-02-11 06:12:03 +000010 it = type({}.items())
Guido van Rossum3ac67412007-02-10 18:55:06 +000011 self.assertRaises(TypeError, it, {})
12 self.assertRaises(TypeError, it)
Guido van Rossumcc2b0162007-02-11 06:12:03 +000013 vt = type({}.values())
Guido van Rossum3ac67412007-02-10 18:55:06 +000014 self.assertRaises(TypeError, vt, {})
15 self.assertRaises(TypeError, vt)
16
Guido van Rossumb90c8482007-02-10 01:11:45 +000017 def test_dict_keys(self):
18 d = {1: 10, "a": "ABC"}
Guido van Rossumcc2b0162007-02-11 06:12:03 +000019 keys = d.keys()
Guido van Rossum83825ac2007-02-10 04:54:19 +000020 self.assertEqual(len(keys), 2)
Guido van Rossumd9214d12007-02-12 02:23:40 +000021 self.assertEqual(set(keys), {1, "a"})
22 self.assertEqual(keys, {1, "a"})
23 self.assertNotEqual(keys, {1, "a", "b"})
24 self.assertNotEqual(keys, {1, "b"})
25 self.assertNotEqual(keys, {1})
26 self.assertNotEqual(keys, 42)
Benjamin Peterson577473f2010-01-19 00:09:57 +000027 self.assertIn(1, keys)
28 self.assertIn("a", keys)
29 self.assertNotIn(10, keys)
30 self.assertNotIn("Z", keys)
Guido van Rossumd9214d12007-02-12 02:23:40 +000031 self.assertEqual(d.keys(), d.keys())
32 e = {1: 11, "a": "def"}
33 self.assertEqual(d.keys(), e.keys())
34 del e["a"]
35 self.assertNotEqual(d.keys(), e.keys())
Guido van Rossumb90c8482007-02-10 01:11:45 +000036
37 def test_dict_items(self):
38 d = {1: 10, "a": "ABC"}
Guido van Rossumcc2b0162007-02-11 06:12:03 +000039 items = d.items()
Guido van Rossum83825ac2007-02-10 04:54:19 +000040 self.assertEqual(len(items), 2)
Guido van Rossumd9214d12007-02-12 02:23:40 +000041 self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
42 self.assertEqual(items, {(1, 10), ("a", "ABC")})
43 self.assertNotEqual(items, {(1, 10), ("a", "ABC"), "junk"})
44 self.assertNotEqual(items, {(1, 10), ("a", "def")})
45 self.assertNotEqual(items, {(1, 10)})
46 self.assertNotEqual(items, 42)
Benjamin Peterson577473f2010-01-19 00:09:57 +000047 self.assertIn((1, 10), items)
48 self.assertIn(("a", "ABC"), items)
49 self.assertNotIn((1, 11), items)
50 self.assertNotIn(1, items)
51 self.assertNotIn((), items)
52 self.assertNotIn((1,), items)
53 self.assertNotIn((1, 2, 3), items)
Guido van Rossumd9214d12007-02-12 02:23:40 +000054 self.assertEqual(d.items(), d.items())
55 e = d.copy()
56 self.assertEqual(d.items(), e.items())
57 e["a"] = "def"
58 self.assertNotEqual(d.items(), e.items())
59
60 def test_dict_mixed_keys_items(self):
61 d = {(1, 1): 11, (2, 2): 22}
62 e = {1: 1, 2: 2}
63 self.assertEqual(d.keys(), e.items())
64 self.assertNotEqual(d.items(), e.keys())
Guido van Rossumb90c8482007-02-10 01:11:45 +000065
66 def test_dict_values(self):
67 d = {1: 10, "a": "ABC"}
Guido van Rossumcc2b0162007-02-11 06:12:03 +000068 values = d.values()
Guido van Rossumb90c8482007-02-10 01:11:45 +000069 self.assertEqual(set(values), {10, "ABC"})
Guido van Rossum83825ac2007-02-10 04:54:19 +000070 self.assertEqual(len(values), 2)
Guido van Rossumb90c8482007-02-10 01:11:45 +000071
Alexandre Vassalottia7142572010-05-04 03:41:49 +000072 def test_dict_repr(self):
73 d = {1: 10, "a": "ABC"}
74 self.assertIsInstance(repr(d), str)
75 r = repr(d.items())
76 self.assertIsInstance(r, str)
77 self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or
78 r == "dict_items([(1, 10), ('a', 'ABC')])")
79 r = repr(d.keys())
80 self.assertIsInstance(r, str)
81 self.assertTrue(r == "dict_keys(['a', 1])" or
82 r == "dict_keys([1, 'a'])")
83 r = repr(d.values())
84 self.assertIsInstance(r, str)
85 self.assertTrue(r == "dict_values(['ABC', 10])" or
86 r == "dict_values([10, 'ABC'])")
87
88 def test_keys_set_operations(self):
89 d1 = {'a': 1, 'b': 2}
90 d2 = {'b': 3, 'c': 2}
91 d3 = {'d': 4, 'e': 5}
92 self.assertEqual(d1.keys() & d1.keys(), {'a', 'b'})
93 self.assertEqual(d1.keys() & d2.keys(), {'b'})
94 self.assertEqual(d1.keys() & d3.keys(), set())
95 self.assertEqual(d1.keys() & set(d1.keys()), {'a', 'b'})
96 self.assertEqual(d1.keys() & set(d2.keys()), {'b'})
97 self.assertEqual(d1.keys() & set(d3.keys()), set())
98
99 self.assertEqual(d1.keys() | d1.keys(), {'a', 'b'})
100 self.assertEqual(d1.keys() | d2.keys(), {'a', 'b', 'c'})
101 self.assertEqual(d1.keys() | d3.keys(), {'a', 'b', 'd', 'e'})
102 self.assertEqual(d1.keys() | set(d1.keys()), {'a', 'b'})
103 self.assertEqual(d1.keys() | set(d2.keys()), {'a', 'b', 'c'})
104 self.assertEqual(d1.keys() | set(d3.keys()),
105 {'a', 'b', 'd', 'e'})
106
107 self.assertEqual(d1.keys() ^ d1.keys(), set())
108 self.assertEqual(d1.keys() ^ d2.keys(), {'a', 'c'})
109 self.assertEqual(d1.keys() ^ d3.keys(), {'a', 'b', 'd', 'e'})
110 self.assertEqual(d1.keys() ^ set(d1.keys()), set())
111 self.assertEqual(d1.keys() ^ set(d2.keys()), {'a', 'c'})
112 self.assertEqual(d1.keys() ^ set(d3.keys()),
113 {'a', 'b', 'd', 'e'})
114
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +0000115 self.assertFalse(d1.keys().isdisjoint(d1.keys()))
116 self.assertFalse(d1.keys().isdisjoint(d2.keys()))
117 self.assertFalse(d1.keys().isdisjoint(list(d2.keys())))
118 self.assertFalse(d1.keys().isdisjoint(set(d2.keys())))
119 self.assertTrue(d1.keys().isdisjoint({'x', 'y', 'z'}))
120 self.assertTrue(d1.keys().isdisjoint(['x', 'y', 'z']))
121 self.assertTrue(d1.keys().isdisjoint(set(['x', 'y', 'z'])))
122 self.assertTrue(d1.keys().isdisjoint(set(['x', 'y'])))
123 self.assertTrue(d1.keys().isdisjoint(['x', 'y']))
124 self.assertTrue(d1.keys().isdisjoint({}))
125 self.assertTrue(d1.keys().isdisjoint(d3.keys()))
126
127 de = {}
128 self.assertTrue(de.keys().isdisjoint(set()))
129 self.assertTrue(de.keys().isdisjoint([]))
130 self.assertTrue(de.keys().isdisjoint(de.keys()))
131 self.assertTrue(de.keys().isdisjoint([1]))
132
Alexandre Vassalottia7142572010-05-04 03:41:49 +0000133 def test_items_set_operations(self):
134 d1 = {'a': 1, 'b': 2}
135 d2 = {'a': 2, 'b': 2}
136 d3 = {'d': 4, 'e': 5}
137 self.assertEqual(
138 d1.items() & d1.items(), {('a', 1), ('b', 2)})
139 self.assertEqual(d1.items() & d2.items(), {('b', 2)})
140 self.assertEqual(d1.items() & d3.items(), set())
141 self.assertEqual(d1.items() & set(d1.items()),
142 {('a', 1), ('b', 2)})
143 self.assertEqual(d1.items() & set(d2.items()), {('b', 2)})
144 self.assertEqual(d1.items() & set(d3.items()), set())
145
146 self.assertEqual(d1.items() | d1.items(),
147 {('a', 1), ('b', 2)})
148 self.assertEqual(d1.items() | d2.items(),
149 {('a', 1), ('a', 2), ('b', 2)})
150 self.assertEqual(d1.items() | d3.items(),
151 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
152 self.assertEqual(d1.items() | set(d1.items()),
153 {('a', 1), ('b', 2)})
154 self.assertEqual(d1.items() | set(d2.items()),
155 {('a', 1), ('a', 2), ('b', 2)})
156 self.assertEqual(d1.items() | set(d3.items()),
157 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
158
159 self.assertEqual(d1.items() ^ d1.items(), set())
160 self.assertEqual(d1.items() ^ d2.items(),
161 {('a', 1), ('a', 2)})
162 self.assertEqual(d1.items() ^ d3.items(),
163 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
164
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +0000165 self.assertFalse(d1.items().isdisjoint(d1.items()))
166 self.assertFalse(d1.items().isdisjoint(d2.items()))
167 self.assertFalse(d1.items().isdisjoint(list(d2.items())))
168 self.assertFalse(d1.items().isdisjoint(set(d2.items())))
169 self.assertTrue(d1.items().isdisjoint({'x', 'y', 'z'}))
170 self.assertTrue(d1.items().isdisjoint(['x', 'y', 'z']))
171 self.assertTrue(d1.items().isdisjoint(set(['x', 'y', 'z'])))
172 self.assertTrue(d1.items().isdisjoint(set(['x', 'y'])))
173 self.assertTrue(d1.items().isdisjoint({}))
174 self.assertTrue(d1.items().isdisjoint(d3.items()))
175
176 de = {}
177 self.assertTrue(de.items().isdisjoint(set()))
178 self.assertTrue(de.items().isdisjoint([]))
179 self.assertTrue(de.items().isdisjoint(de.items()))
180 self.assertTrue(de.items().isdisjoint([1]))
181
Alexandre Vassalottia7142572010-05-04 03:41:49 +0000182
Guido van Rossumb90c8482007-02-10 01:11:45 +0000183def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000184 support.run_unittest(DictSetTest)
Guido van Rossumb90c8482007-02-10 01:11:45 +0000185
186if __name__ == "__main__":
187 test_main()