move sections
[python/dscho.git] / Lib / test / test_dictviews.py
blobf90367660c54b83e4d00e0ac510a6740a9dd1e80
1 import unittest
2 from test import test_support
4 class DictSetTest(unittest.TestCase):
6 def test_constructors_not_callable(self):
7 kt = type({}.viewkeys())
8 self.assertRaises(TypeError, kt, {})
9 self.assertRaises(TypeError, kt)
10 it = type({}.viewitems())
11 self.assertRaises(TypeError, it, {})
12 self.assertRaises(TypeError, it)
13 vt = type({}.viewvalues())
14 self.assertRaises(TypeError, vt, {})
15 self.assertRaises(TypeError, vt)
17 def test_dict_keys(self):
18 d = {1: 10, "a": "ABC"}
19 keys = d.viewkeys()
20 self.assertEqual(len(keys), 2)
21 self.assertEqual(set(keys), set([1, "a"]))
22 self.assertEqual(keys, set([1, "a"]))
23 self.assertNotEqual(keys, set([1, "a", "b"]))
24 self.assertNotEqual(keys, set([1, "b"]))
25 self.assertNotEqual(keys, set([1]))
26 self.assertNotEqual(keys, 42)
27 self.assertIn(1, keys)
28 self.assertIn("a", keys)
29 self.assertNotIn(10, keys)
30 self.assertNotIn("Z", keys)
31 self.assertEqual(d.viewkeys(), d.viewkeys())
32 e = {1: 11, "a": "def"}
33 self.assertEqual(d.viewkeys(), e.viewkeys())
34 del e["a"]
35 self.assertNotEqual(d.viewkeys(), e.viewkeys())
37 def test_dict_items(self):
38 d = {1: 10, "a": "ABC"}
39 items = d.viewitems()
40 self.assertEqual(len(items), 2)
41 self.assertEqual(set(items), set([(1, 10), ("a", "ABC")]))
42 self.assertEqual(items, set([(1, 10), ("a", "ABC")]))
43 self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"]))
44 self.assertNotEqual(items, set([(1, 10), ("a", "def")]))
45 self.assertNotEqual(items, set([(1, 10)]))
46 self.assertNotEqual(items, 42)
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)
54 self.assertEqual(d.viewitems(), d.viewitems())
55 e = d.copy()
56 self.assertEqual(d.viewitems(), e.viewitems())
57 e["a"] = "def"
58 self.assertNotEqual(d.viewitems(), e.viewitems())
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.viewkeys(), e.viewitems())
64 self.assertNotEqual(d.viewitems(), e.viewkeys())
66 def test_dict_values(self):
67 d = {1: 10, "a": "ABC"}
68 values = d.viewvalues()
69 self.assertEqual(set(values), set([10, "ABC"]))
70 self.assertEqual(len(values), 2)
72 def test_dict_repr(self):
73 d = {1: 10, "a": "ABC"}
74 self.assertIsInstance(repr(d), str)
75 r = repr(d.viewitems())
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.viewkeys())
80 self.assertIsInstance(r, str)
81 self.assertTrue(r == "dict_keys(['a', 1])" or
82 r == "dict_keys([1, 'a'])")
83 r = repr(d.viewvalues())
84 self.assertIsInstance(r, str)
85 self.assertTrue(r == "dict_values(['ABC', 10])" or
86 r == "dict_values([10, 'ABC'])")
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.viewkeys() & d1.viewkeys(), {'a', 'b'})
93 self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'})
94 self.assertEqual(d1.viewkeys() & d3.viewkeys(), set())
95 self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'})
96 self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'})
97 self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set())
99 self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'})
100 self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'})
101 self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'})
102 self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'})
103 self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'})
104 self.assertEqual(d1.viewkeys() | set(d3.viewkeys()),
105 {'a', 'b', 'd', 'e'})
107 self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set())
108 self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'})
109 self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'})
110 self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set())
111 self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'})
112 self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()),
113 {'a', 'b', 'd', 'e'})
115 def test_items_set_operations(self):
116 d1 = {'a': 1, 'b': 2}
117 d2 = {'a': 2, 'b': 2}
118 d3 = {'d': 4, 'e': 5}
119 self.assertEqual(
120 d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)})
121 self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)})
122 self.assertEqual(d1.viewitems() & d3.viewitems(), set())
123 self.assertEqual(d1.viewitems() & set(d1.viewitems()),
124 {('a', 1), ('b', 2)})
125 self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)})
126 self.assertEqual(d1.viewitems() & set(d3.viewitems()), set())
128 self.assertEqual(d1.viewitems() | d1.viewitems(),
129 {('a', 1), ('b', 2)})
130 self.assertEqual(d1.viewitems() | d2.viewitems(),
131 {('a', 1), ('a', 2), ('b', 2)})
132 self.assertEqual(d1.viewitems() | d3.viewitems(),
133 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
134 self.assertEqual(d1.viewitems() | set(d1.viewitems()),
135 {('a', 1), ('b', 2)})
136 self.assertEqual(d1.viewitems() | set(d2.viewitems()),
137 {('a', 1), ('a', 2), ('b', 2)})
138 self.assertEqual(d1.viewitems() | set(d3.viewitems()),
139 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
141 self.assertEqual(d1.viewitems() ^ d1.viewitems(), set())
142 self.assertEqual(d1.viewitems() ^ d2.viewitems(),
143 {('a', 1), ('a', 2)})
144 self.assertEqual(d1.viewitems() ^ d3.viewitems(),
145 {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
150 def test_main():
151 test_support.run_unittest(DictSetTest)
153 if __name__ == "__main__":
154 test_main()