Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3 | |
| 4 | class DictSetTest(unittest.TestCase): |
| 5 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 6 | def test_constructors_not_callable(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 7 | kt = type({}.keys()) |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 8 | self.assertRaises(TypeError, kt, {}) |
| 9 | self.assertRaises(TypeError, kt) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 10 | it = type({}.items()) |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 11 | self.assertRaises(TypeError, it, {}) |
| 12 | self.assertRaises(TypeError, it) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 13 | vt = type({}.values()) |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 14 | self.assertRaises(TypeError, vt, {}) |
| 15 | self.assertRaises(TypeError, vt) |
| 16 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 17 | def test_dict_keys(self): |
| 18 | d = {1: 10, "a": "ABC"} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 19 | keys = d.keys() |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 20 | self.assertEqual(len(keys), 2) |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 21 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 27 | self.assertIn(1, keys) |
| 28 | self.assertIn("a", keys) |
| 29 | self.assertNotIn(10, keys) |
| 30 | self.assertNotIn("Z", keys) |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 31 | 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 Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 36 | |
| 37 | def test_dict_items(self): |
| 38 | d = {1: 10, "a": "ABC"} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 39 | items = d.items() |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 40 | self.assertEqual(len(items), 2) |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 41 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 47 | 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 Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 54 | 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 Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 65 | |
| 66 | def test_dict_values(self): |
| 67 | d = {1: 10, "a": "ABC"} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 68 | values = d.values() |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 69 | self.assertEqual(set(values), {10, "ABC"}) |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 70 | self.assertEqual(len(values), 2) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 71 | |
Alexandre Vassalotti | a714257 | 2010-05-04 03:41:49 +0000 | [diff] [blame] | 72 | 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 | |
Ezio Melotti | 2532635 | 2013-08-08 20:12:28 +0300 | [diff] [blame] | 115 | self.assertEqual(d1.keys() - d1.keys(), set()) |
| 116 | self.assertEqual(d1.keys() - d2.keys(), {'a'}) |
| 117 | self.assertEqual(d1.keys() - d3.keys(), {'a', 'b'}) |
| 118 | self.assertEqual(d1.keys() - set(d1.keys()), set()) |
| 119 | self.assertEqual(d1.keys() - set(d2.keys()), {'a'}) |
| 120 | self.assertEqual(d1.keys() - set(d3.keys()), {'a', 'b'}) |
| 121 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 122 | self.assertFalse(d1.keys().isdisjoint(d1.keys())) |
| 123 | self.assertFalse(d1.keys().isdisjoint(d2.keys())) |
| 124 | self.assertFalse(d1.keys().isdisjoint(list(d2.keys()))) |
| 125 | self.assertFalse(d1.keys().isdisjoint(set(d2.keys()))) |
| 126 | self.assertTrue(d1.keys().isdisjoint({'x', 'y', 'z'})) |
| 127 | self.assertTrue(d1.keys().isdisjoint(['x', 'y', 'z'])) |
| 128 | self.assertTrue(d1.keys().isdisjoint(set(['x', 'y', 'z']))) |
| 129 | self.assertTrue(d1.keys().isdisjoint(set(['x', 'y']))) |
| 130 | self.assertTrue(d1.keys().isdisjoint(['x', 'y'])) |
| 131 | self.assertTrue(d1.keys().isdisjoint({})) |
| 132 | self.assertTrue(d1.keys().isdisjoint(d3.keys())) |
| 133 | |
| 134 | de = {} |
| 135 | self.assertTrue(de.keys().isdisjoint(set())) |
| 136 | self.assertTrue(de.keys().isdisjoint([])) |
| 137 | self.assertTrue(de.keys().isdisjoint(de.keys())) |
| 138 | self.assertTrue(de.keys().isdisjoint([1])) |
| 139 | |
Alexandre Vassalotti | a714257 | 2010-05-04 03:41:49 +0000 | [diff] [blame] | 140 | def test_items_set_operations(self): |
| 141 | d1 = {'a': 1, 'b': 2} |
| 142 | d2 = {'a': 2, 'b': 2} |
| 143 | d3 = {'d': 4, 'e': 5} |
| 144 | self.assertEqual( |
| 145 | d1.items() & d1.items(), {('a', 1), ('b', 2)}) |
| 146 | self.assertEqual(d1.items() & d2.items(), {('b', 2)}) |
| 147 | self.assertEqual(d1.items() & d3.items(), set()) |
| 148 | self.assertEqual(d1.items() & set(d1.items()), |
| 149 | {('a', 1), ('b', 2)}) |
| 150 | self.assertEqual(d1.items() & set(d2.items()), {('b', 2)}) |
| 151 | self.assertEqual(d1.items() & set(d3.items()), set()) |
| 152 | |
| 153 | self.assertEqual(d1.items() | d1.items(), |
| 154 | {('a', 1), ('b', 2)}) |
| 155 | self.assertEqual(d1.items() | d2.items(), |
| 156 | {('a', 1), ('a', 2), ('b', 2)}) |
| 157 | self.assertEqual(d1.items() | d3.items(), |
| 158 | {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) |
| 159 | self.assertEqual(d1.items() | set(d1.items()), |
| 160 | {('a', 1), ('b', 2)}) |
| 161 | self.assertEqual(d1.items() | set(d2.items()), |
| 162 | {('a', 1), ('a', 2), ('b', 2)}) |
| 163 | self.assertEqual(d1.items() | set(d3.items()), |
| 164 | {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) |
| 165 | |
| 166 | self.assertEqual(d1.items() ^ d1.items(), set()) |
| 167 | self.assertEqual(d1.items() ^ d2.items(), |
| 168 | {('a', 1), ('a', 2)}) |
| 169 | self.assertEqual(d1.items() ^ d3.items(), |
| 170 | {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) |
| 171 | |
Ezio Melotti | 2532635 | 2013-08-08 20:12:28 +0300 | [diff] [blame] | 172 | self.assertEqual(d1.items() - d1.items(), set()) |
| 173 | self.assertEqual(d1.items() - d2.items(), {('a', 1)}) |
| 174 | self.assertEqual(d1.items() - d3.items(), {('a', 1), ('b', 2)}) |
| 175 | self.assertEqual(d1.items() - set(d1.items()), set()) |
| 176 | self.assertEqual(d1.items() - set(d2.items()), {('a', 1)}) |
| 177 | self.assertEqual(d1.items() - set(d3.items()), {('a', 1), ('b', 2)}) |
| 178 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 179 | self.assertFalse(d1.items().isdisjoint(d1.items())) |
| 180 | self.assertFalse(d1.items().isdisjoint(d2.items())) |
| 181 | self.assertFalse(d1.items().isdisjoint(list(d2.items()))) |
| 182 | self.assertFalse(d1.items().isdisjoint(set(d2.items()))) |
| 183 | self.assertTrue(d1.items().isdisjoint({'x', 'y', 'z'})) |
| 184 | self.assertTrue(d1.items().isdisjoint(['x', 'y', 'z'])) |
| 185 | self.assertTrue(d1.items().isdisjoint(set(['x', 'y', 'z']))) |
| 186 | self.assertTrue(d1.items().isdisjoint(set(['x', 'y']))) |
| 187 | self.assertTrue(d1.items().isdisjoint({})) |
| 188 | self.assertTrue(d1.items().isdisjoint(d3.items())) |
| 189 | |
| 190 | de = {} |
| 191 | self.assertTrue(de.items().isdisjoint(set())) |
| 192 | self.assertTrue(de.items().isdisjoint([])) |
| 193 | self.assertTrue(de.items().isdisjoint(de.items())) |
| 194 | self.assertTrue(de.items().isdisjoint([1])) |
| 195 | |
Benjamin Peterson | 611d901 | 2013-05-19 19:39:38 -0700 | [diff] [blame] | 196 | def test_recursive_repr(self): |
| 197 | d = {} |
| 198 | d[42] = d.values() |
| 199 | self.assertRaises(RuntimeError, repr, d) |
| 200 | |
Alexandre Vassalotti | a714257 | 2010-05-04 03:41:49 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 202 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 203 | support.run_unittest(DictSetTest) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 204 | |
| 205 | if __name__ == "__main__": |
| 206 | test_main() |