1import copy 2import pickle 3import sys 4import unittest 5import collections 6from test import test_support 7 8class DictSetTest(unittest.TestCase): 9 10 def test_constructors_not_callable(self): 11 kt = type({}.viewkeys()) 12 self.assertRaises(TypeError, kt, {}) 13 self.assertRaises(TypeError, kt) 14 it = type({}.viewitems()) 15 self.assertRaises(TypeError, it, {}) 16 self.assertRaises(TypeError, it) 17 vt = type({}.viewvalues()) 18 self.assertRaises(TypeError, vt, {}) 19 self.assertRaises(TypeError, vt) 20 21 def test_dict_keys(self): 22 d = {1: 10, "a": "ABC"} 23 keys = d.viewkeys() 24 self.assertEqual(len(keys), 2) 25 self.assertEqual(set(keys), set([1, "a"])) 26 self.assertEqual(keys, set([1, "a"])) 27 self.assertNotEqual(keys, set([1, "a", "b"])) 28 self.assertNotEqual(keys, set([1, "b"])) 29 self.assertNotEqual(keys, set([1])) 30 self.assertNotEqual(keys, 42) 31 self.assertIn(1, keys) 32 self.assertIn("a", keys) 33 self.assertNotIn(10, keys) 34 self.assertNotIn("Z", keys) 35 self.assertEqual(d.viewkeys(), d.viewkeys()) 36 e = {1: 11, "a": "def"} 37 self.assertEqual(d.viewkeys(), e.viewkeys()) 38 del e["a"] 39 self.assertNotEqual(d.viewkeys(), e.viewkeys()) 40 41 def test_dict_items(self): 42 d = {1: 10, "a": "ABC"} 43 items = d.viewitems() 44 self.assertEqual(len(items), 2) 45 self.assertEqual(set(items), set([(1, 10), ("a", "ABC")])) 46 self.assertEqual(items, set([(1, 10), ("a", "ABC")])) 47 self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"])) 48 self.assertNotEqual(items, set([(1, 10), ("a", "def")])) 49 self.assertNotEqual(items, set([(1, 10)])) 50 self.assertNotEqual(items, 42) 51 self.assertIn((1, 10), items) 52 self.assertIn(("a", "ABC"), items) 53 self.assertNotIn((1, 11), items) 54 self.assertNotIn(1, items) 55 self.assertNotIn((), items) 56 self.assertNotIn((1,), items) 57 self.assertNotIn((1, 2, 3), items) 58 self.assertEqual(d.viewitems(), d.viewitems()) 59 e = d.copy() 60 self.assertEqual(d.viewitems(), e.viewitems()) 61 e["a"] = "def" 62 self.assertNotEqual(d.viewitems(), e.viewitems()) 63 64 def test_dict_mixed_keys_items(self): 65 d = {(1, 1): 11, (2, 2): 22} 66 e = {1: 1, 2: 2} 67 self.assertEqual(d.viewkeys(), e.viewitems()) 68 self.assertNotEqual(d.viewitems(), e.viewkeys()) 69 70 def test_dict_values(self): 71 d = {1: 10, "a": "ABC"} 72 values = d.viewvalues() 73 self.assertEqual(set(values), set([10, "ABC"])) 74 self.assertEqual(len(values), 2) 75 76 def test_dict_repr(self): 77 d = {1: 10, "a": "ABC"} 78 self.assertIsInstance(repr(d), str) 79 r = repr(d.viewitems()) 80 self.assertIsInstance(r, str) 81 self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or 82 r == "dict_items([(1, 10), ('a', 'ABC')])") 83 r = repr(d.viewkeys()) 84 self.assertIsInstance(r, str) 85 self.assertTrue(r == "dict_keys(['a', 1])" or 86 r == "dict_keys([1, 'a'])") 87 r = repr(d.viewvalues()) 88 self.assertIsInstance(r, str) 89 self.assertTrue(r == "dict_values(['ABC', 10])" or 90 r == "dict_values([10, 'ABC'])") 91 92 def test_keys_set_operations(self): 93 d1 = {'a': 1, 'b': 2} 94 d2 = {'b': 3, 'c': 2} 95 d3 = {'d': 4, 'e': 5} 96 self.assertEqual(d1.viewkeys() & d1.viewkeys(), {'a', 'b'}) 97 self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'}) 98 self.assertEqual(d1.viewkeys() & d3.viewkeys(), set()) 99 self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'}) 100 self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'}) 101 self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set()) 102 self.assertEqual(d1.viewkeys() & tuple(d1.viewkeys()), {'a', 'b'}) 103 104 self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'}) 105 self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'}) 106 self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'}) 107 self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'}) 108 self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'}) 109 self.assertEqual(d1.viewkeys() | set(d3.viewkeys()), 110 {'a', 'b', 'd', 'e'}) 111 self.assertEqual(d1.viewkeys() | (1, 2), {'a', 'b', 1, 2}) 112 113 self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set()) 114 self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'}) 115 self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'}) 116 self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set()) 117 self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'}) 118 self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()), 119 {'a', 'b', 'd', 'e'}) 120 self.assertEqual(d1.viewkeys() ^ tuple(d2.keys()), {'a', 'c'}) 121 122 self.assertEqual(d1.viewkeys() - d1.viewkeys(), set()) 123 self.assertEqual(d1.viewkeys() - d2.viewkeys(), {'a'}) 124 self.assertEqual(d1.viewkeys() - d3.viewkeys(), {'a', 'b'}) 125 self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set()) 126 self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), {'a'}) 127 self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), {'a', 'b'}) 128 self.assertEqual(d1.viewkeys() - (0, 1), {'a', 'b'}) 129 130 def test_items_set_operations(self): 131 d1 = {'a': 1, 'b': 2} 132 d2 = {'a': 2, 'b': 2} 133 d3 = {'d': 4, 'e': 5} 134 self.assertEqual( 135 d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)}) 136 self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)}) 137 self.assertEqual(d1.viewitems() & d3.viewitems(), set()) 138 self.assertEqual(d1.viewitems() & set(d1.viewitems()), 139 {('a', 1), ('b', 2)}) 140 self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)}) 141 self.assertEqual(d1.viewitems() & set(d3.viewitems()), set()) 142 143 self.assertEqual(d1.viewitems() | d1.viewitems(), 144 {('a', 1), ('b', 2)}) 145 self.assertEqual(d1.viewitems() | d2.viewitems(), 146 {('a', 1), ('a', 2), ('b', 2)}) 147 self.assertEqual(d1.viewitems() | d3.viewitems(), 148 {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) 149 self.assertEqual(d1.viewitems() | set(d1.viewitems()), 150 {('a', 1), ('b', 2)}) 151 self.assertEqual(d1.viewitems() | set(d2.viewitems()), 152 {('a', 1), ('a', 2), ('b', 2)}) 153 self.assertEqual(d1.viewitems() | set(d3.viewitems()), 154 {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) 155 156 self.assertEqual(d1.viewitems() ^ d1.viewitems(), set()) 157 self.assertEqual(d1.viewitems() ^ d2.viewitems(), 158 {('a', 1), ('a', 2)}) 159 self.assertEqual(d1.viewitems() ^ d3.viewitems(), 160 {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) 161 162 self.assertEqual(d1.viewitems() - d1.viewitems(), set()) 163 self.assertEqual(d1.viewitems() - d2.viewitems(), {('a', 1)}) 164 self.assertEqual(d1.viewitems() - d3.viewitems(), {('a', 1), ('b', 2)}) 165 self.assertEqual(d1.viewitems() - set(d1.viewitems()), set()) 166 self.assertEqual(d1.viewitems() - set(d2.viewitems()), {('a', 1)}) 167 self.assertEqual(d1.viewitems() - set(d3.viewitems()), 168 {('a', 1), ('b', 2)}) 169 170 def test_recursive_repr(self): 171 d = {} 172 d[42] = d.viewvalues() 173 r = repr(d) 174 # Cannot perform a stronger test, as the contents of the repr 175 # are implementation-dependent. All we can say is that we 176 # want a str result, not an exception of any sort. 177 self.assertIsInstance(r, str) 178 d[42] = d.viewitems() 179 r = repr(d) 180 # Again. 181 self.assertIsInstance(r, str) 182 183 def test_deeply_nested_repr(self): 184 d = {} 185 for i in range(sys.getrecursionlimit() + 100): 186 d = {42: d.viewvalues()} 187 self.assertRaises(RuntimeError, repr, d) 188 189 def test_abc_registry(self): 190 d = dict(a=1) 191 192 self.assertIsInstance(d.viewkeys(), collections.KeysView) 193 self.assertIsInstance(d.viewkeys(), collections.MappingView) 194 self.assertIsInstance(d.viewkeys(), collections.Set) 195 self.assertIsInstance(d.viewkeys(), collections.Sized) 196 self.assertIsInstance(d.viewkeys(), collections.Iterable) 197 self.assertIsInstance(d.viewkeys(), collections.Container) 198 199 self.assertIsInstance(d.viewvalues(), collections.ValuesView) 200 self.assertIsInstance(d.viewvalues(), collections.MappingView) 201 self.assertIsInstance(d.viewvalues(), collections.Sized) 202 203 self.assertIsInstance(d.viewitems(), collections.ItemsView) 204 self.assertIsInstance(d.viewitems(), collections.MappingView) 205 self.assertIsInstance(d.viewitems(), collections.Set) 206 self.assertIsInstance(d.viewitems(), collections.Sized) 207 self.assertIsInstance(d.viewitems(), collections.Iterable) 208 self.assertIsInstance(d.viewitems(), collections.Container) 209 210 def test_copy(self): 211 d = {1: 10, "a": "ABC"} 212 self.assertRaises(TypeError, copy.copy, d.viewkeys()) 213 self.assertRaises(TypeError, copy.copy, d.viewvalues()) 214 self.assertRaises(TypeError, copy.copy, d.viewitems()) 215 216 def test_pickle(self): 217 d = {1: 10, "a": "ABC"} 218 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 219 self.assertRaises((TypeError, pickle.PicklingError), 220 pickle.dumps, d.viewkeys(), proto) 221 self.assertRaises((TypeError, pickle.PicklingError), 222 pickle.dumps, d.viewvalues(), proto) 223 self.assertRaises((TypeError, pickle.PicklingError), 224 pickle.dumps, d.viewitems(), proto) 225 226 227def test_main(): 228 test_support.run_unittest(DictSetTest) 229 230if __name__ == "__main__": 231 test_main() 232