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 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 115 | 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 Vassalotti | a714257 | 2010-05-04 03:41:49 +0000 | [diff] [blame] | 133 | 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 Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 165 | 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 Vassalotti | a714257 | 2010-05-04 03:41:49 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 183 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 184 | support.run_unittest(DictSetTest) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 185 | |
| 186 | if __name__ == "__main__": |
| 187 | test_main() |