summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dictviews.py
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2010-05-04 03:41:49 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2010-05-04 03:41:49 (GMT)
commita714257a662b6557b8cd0a351fed2470af5ed355 (patch)
treee69d62165e2c36bb8d10131bd55ced173125b9c3 /Lib/test/test_dictviews.py
parentefb706734c8a06b251d08906b6d9ab2820dc1b4e (diff)
downloadcpython-a714257a662b6557b8cd0a351fed2470af5ed355.zip
cpython-a714257a662b6557b8cd0a351fed2470af5ed355.tar.gz
cpython-a714257a662b6557b8cd0a351fed2470af5ed355.tar.bz2
Forward port unit tests for set operation and repr.
Patch by Alexander Belopolsky.
Diffstat (limited to 'Lib/test/test_dictviews.py')
-rw-r--r--Lib/test/test_dictviews.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index 4d12647..277e05b 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -69,6 +69,82 @@ class DictSetTest(unittest.TestCase):
self.assertEqual(set(values), {10, "ABC"})
self.assertEqual(len(values), 2)
+ def test_dict_repr(self):
+ d = {1: 10, "a": "ABC"}
+ self.assertIsInstance(repr(d), str)
+ r = repr(d.items())
+ self.assertIsInstance(r, str)
+ self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or
+ r == "dict_items([(1, 10), ('a', 'ABC')])")
+ r = repr(d.keys())
+ self.assertIsInstance(r, str)
+ self.assertTrue(r == "dict_keys(['a', 1])" or
+ r == "dict_keys([1, 'a'])")
+ r = repr(d.values())
+ self.assertIsInstance(r, str)
+ self.assertTrue(r == "dict_values(['ABC', 10])" or
+ r == "dict_values([10, 'ABC'])")
+
+ def test_keys_set_operations(self):
+ d1 = {'a': 1, 'b': 2}
+ d2 = {'b': 3, 'c': 2}
+ d3 = {'d': 4, 'e': 5}
+ self.assertEqual(d1.keys() & d1.keys(), {'a', 'b'})
+ self.assertEqual(d1.keys() & d2.keys(), {'b'})
+ self.assertEqual(d1.keys() & d3.keys(), set())
+ self.assertEqual(d1.keys() & set(d1.keys()), {'a', 'b'})
+ self.assertEqual(d1.keys() & set(d2.keys()), {'b'})
+ self.assertEqual(d1.keys() & set(d3.keys()), set())
+
+ self.assertEqual(d1.keys() | d1.keys(), {'a', 'b'})
+ self.assertEqual(d1.keys() | d2.keys(), {'a', 'b', 'c'})
+ self.assertEqual(d1.keys() | d3.keys(), {'a', 'b', 'd', 'e'})
+ self.assertEqual(d1.keys() | set(d1.keys()), {'a', 'b'})
+ self.assertEqual(d1.keys() | set(d2.keys()), {'a', 'b', 'c'})
+ self.assertEqual(d1.keys() | set(d3.keys()),
+ {'a', 'b', 'd', 'e'})
+
+ self.assertEqual(d1.keys() ^ d1.keys(), set())
+ self.assertEqual(d1.keys() ^ d2.keys(), {'a', 'c'})
+ self.assertEqual(d1.keys() ^ d3.keys(), {'a', 'b', 'd', 'e'})
+ self.assertEqual(d1.keys() ^ set(d1.keys()), set())
+ self.assertEqual(d1.keys() ^ set(d2.keys()), {'a', 'c'})
+ self.assertEqual(d1.keys() ^ set(d3.keys()),
+ {'a', 'b', 'd', 'e'})
+
+ def test_items_set_operations(self):
+ d1 = {'a': 1, 'b': 2}
+ d2 = {'a': 2, 'b': 2}
+ d3 = {'d': 4, 'e': 5}
+ self.assertEqual(
+ d1.items() & d1.items(), {('a', 1), ('b', 2)})
+ self.assertEqual(d1.items() & d2.items(), {('b', 2)})
+ self.assertEqual(d1.items() & d3.items(), set())
+ self.assertEqual(d1.items() & set(d1.items()),
+ {('a', 1), ('b', 2)})
+ self.assertEqual(d1.items() & set(d2.items()), {('b', 2)})
+ self.assertEqual(d1.items() & set(d3.items()), set())
+
+ self.assertEqual(d1.items() | d1.items(),
+ {('a', 1), ('b', 2)})
+ self.assertEqual(d1.items() | d2.items(),
+ {('a', 1), ('a', 2), ('b', 2)})
+ self.assertEqual(d1.items() | d3.items(),
+ {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
+ self.assertEqual(d1.items() | set(d1.items()),
+ {('a', 1), ('b', 2)})
+ self.assertEqual(d1.items() | set(d2.items()),
+ {('a', 1), ('a', 2), ('b', 2)})
+ self.assertEqual(d1.items() | set(d3.items()),
+ {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
+
+ self.assertEqual(d1.items() ^ d1.items(), set())
+ self.assertEqual(d1.items() ^ d2.items(),
+ {('a', 1), ('a', 2)})
+ self.assertEqual(d1.items() ^ d3.items(),
+ {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
+
+
def test_main():
support.run_unittest(DictSetTest)