2017-04-24 03:05:00 -03:00
|
|
|
import collections.abc
|
2015-11-12 05:23:04 -04:00
|
|
|
import copy
|
|
|
|
import pickle
|
2018-01-26 11:46:01 -04:00
|
|
|
import sys
|
2007-02-09 21:11:45 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
class DictSetTest(unittest.TestCase):
|
|
|
|
|
2007-02-10 14:55:06 -04:00
|
|
|
def test_constructors_not_callable(self):
|
2007-02-11 02:12:03 -04:00
|
|
|
kt = type({}.keys())
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assertRaises(TypeError, kt, {})
|
|
|
|
self.assertRaises(TypeError, kt)
|
2007-02-11 02:12:03 -04:00
|
|
|
it = type({}.items())
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assertRaises(TypeError, it, {})
|
|
|
|
self.assertRaises(TypeError, it)
|
2007-02-11 02:12:03 -04:00
|
|
|
vt = type({}.values())
|
2007-02-10 14:55:06 -04:00
|
|
|
self.assertRaises(TypeError, vt, {})
|
|
|
|
self.assertRaises(TypeError, vt)
|
|
|
|
|
2007-02-09 21:11:45 -04:00
|
|
|
def test_dict_keys(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
2007-02-11 02:12:03 -04:00
|
|
|
keys = d.keys()
|
2007-02-10 00:54:19 -04:00
|
|
|
self.assertEqual(len(keys), 2)
|
2007-02-11 22:23:40 -04:00
|
|
|
self.assertEqual(set(keys), {1, "a"})
|
|
|
|
self.assertEqual(keys, {1, "a"})
|
|
|
|
self.assertNotEqual(keys, {1, "a", "b"})
|
|
|
|
self.assertNotEqual(keys, {1, "b"})
|
|
|
|
self.assertNotEqual(keys, {1})
|
|
|
|
self.assertNotEqual(keys, 42)
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertIn(1, keys)
|
|
|
|
self.assertIn("a", keys)
|
|
|
|
self.assertNotIn(10, keys)
|
|
|
|
self.assertNotIn("Z", keys)
|
2007-02-11 22:23:40 -04:00
|
|
|
self.assertEqual(d.keys(), d.keys())
|
|
|
|
e = {1: 11, "a": "def"}
|
|
|
|
self.assertEqual(d.keys(), e.keys())
|
|
|
|
del e["a"]
|
|
|
|
self.assertNotEqual(d.keys(), e.keys())
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
def test_dict_items(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
2007-02-11 02:12:03 -04:00
|
|
|
items = d.items()
|
2007-02-10 00:54:19 -04:00
|
|
|
self.assertEqual(len(items), 2)
|
2007-02-11 22:23:40 -04:00
|
|
|
self.assertEqual(set(items), {(1, 10), ("a", "ABC")})
|
|
|
|
self.assertEqual(items, {(1, 10), ("a", "ABC")})
|
|
|
|
self.assertNotEqual(items, {(1, 10), ("a", "ABC"), "junk"})
|
|
|
|
self.assertNotEqual(items, {(1, 10), ("a", "def")})
|
|
|
|
self.assertNotEqual(items, {(1, 10)})
|
|
|
|
self.assertNotEqual(items, 42)
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertIn((1, 10), items)
|
|
|
|
self.assertIn(("a", "ABC"), items)
|
|
|
|
self.assertNotIn((1, 11), items)
|
|
|
|
self.assertNotIn(1, items)
|
|
|
|
self.assertNotIn((), items)
|
|
|
|
self.assertNotIn((1,), items)
|
|
|
|
self.assertNotIn((1, 2, 3), items)
|
2007-02-11 22:23:40 -04:00
|
|
|
self.assertEqual(d.items(), d.items())
|
|
|
|
e = d.copy()
|
|
|
|
self.assertEqual(d.items(), e.items())
|
|
|
|
e["a"] = "def"
|
|
|
|
self.assertNotEqual(d.items(), e.items())
|
|
|
|
|
|
|
|
def test_dict_mixed_keys_items(self):
|
|
|
|
d = {(1, 1): 11, (2, 2): 22}
|
|
|
|
e = {1: 1, 2: 2}
|
|
|
|
self.assertEqual(d.keys(), e.items())
|
|
|
|
self.assertNotEqual(d.items(), e.keys())
|
2007-02-09 21:11:45 -04:00
|
|
|
|
|
|
|
def test_dict_values(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
2007-02-11 02:12:03 -04:00
|
|
|
values = d.values()
|
2007-02-09 21:11:45 -04:00
|
|
|
self.assertEqual(set(values), {10, "ABC"})
|
2007-02-10 00:54:19 -04:00
|
|
|
self.assertEqual(len(values), 2)
|
2007-02-09 21:11:45 -04:00
|
|
|
|
2010-05-04 00:41:49 -03:00
|
|
|
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}
|
2019-08-26 04:17:43 -03:00
|
|
|
d4 = {'d': 4}
|
|
|
|
|
|
|
|
class CustomSet(set):
|
|
|
|
def intersection(self, other):
|
|
|
|
return CustomSet(super().intersection(other))
|
|
|
|
|
2010-05-04 00:41:49 -03:00
|
|
|
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())
|
2016-03-04 02:05:36 -04:00
|
|
|
self.assertEqual(d1.keys() & tuple(d1.keys()), {'a', 'b'})
|
2019-08-26 04:17:43 -03:00
|
|
|
self.assertEqual(d3.keys() & d4.keys(), {'d'})
|
|
|
|
self.assertEqual(d4.keys() & d3.keys(), {'d'})
|
|
|
|
self.assertEqual(d4.keys() & set(d3.keys()), {'d'})
|
|
|
|
self.assertIsInstance(d4.keys() & frozenset(d3.keys()), set)
|
|
|
|
self.assertIsInstance(frozenset(d3.keys()) & d4.keys(), set)
|
|
|
|
self.assertIs(type(d4.keys() & CustomSet(d3.keys())), set)
|
|
|
|
self.assertIs(type(d1.keys() & []), set)
|
|
|
|
self.assertIs(type([] & d1.keys()), set)
|
2010-05-04 00:41:49 -03:00
|
|
|
|
|
|
|
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'})
|
2016-03-04 02:05:36 -04:00
|
|
|
self.assertEqual(d1.keys() | (1, 2), {'a', 'b', 1, 2})
|
2010-05-04 00:41:49 -03:00
|
|
|
|
|
|
|
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'})
|
2016-03-04 02:05:36 -04:00
|
|
|
self.assertEqual(d1.keys() ^ tuple(d2.keys()), {'a', 'c'})
|
2010-05-04 00:41:49 -03:00
|
|
|
|
2013-08-08 14:12:28 -03:00
|
|
|
self.assertEqual(d1.keys() - d1.keys(), set())
|
|
|
|
self.assertEqual(d1.keys() - d2.keys(), {'a'})
|
|
|
|
self.assertEqual(d1.keys() - d3.keys(), {'a', 'b'})
|
|
|
|
self.assertEqual(d1.keys() - set(d1.keys()), set())
|
|
|
|
self.assertEqual(d1.keys() - set(d2.keys()), {'a'})
|
|
|
|
self.assertEqual(d1.keys() - set(d3.keys()), {'a', 'b'})
|
2016-03-04 02:05:36 -04:00
|
|
|
self.assertEqual(d1.keys() - (0, 1), {'a', 'b'})
|
2013-08-08 14:12:28 -03:00
|
|
|
|
2010-09-02 12:06:06 -03:00
|
|
|
self.assertFalse(d1.keys().isdisjoint(d1.keys()))
|
|
|
|
self.assertFalse(d1.keys().isdisjoint(d2.keys()))
|
|
|
|
self.assertFalse(d1.keys().isdisjoint(list(d2.keys())))
|
|
|
|
self.assertFalse(d1.keys().isdisjoint(set(d2.keys())))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint({'x', 'y', 'z'}))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint(['x', 'y', 'z']))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint(set(['x', 'y', 'z'])))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint(set(['x', 'y'])))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint(['x', 'y']))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint({}))
|
|
|
|
self.assertTrue(d1.keys().isdisjoint(d3.keys()))
|
|
|
|
|
|
|
|
de = {}
|
|
|
|
self.assertTrue(de.keys().isdisjoint(set()))
|
|
|
|
self.assertTrue(de.keys().isdisjoint([]))
|
|
|
|
self.assertTrue(de.keys().isdisjoint(de.keys()))
|
|
|
|
self.assertTrue(de.keys().isdisjoint([1]))
|
|
|
|
|
2010-05-04 00:41:49 -03:00
|
|
|
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)})
|
|
|
|
|
2013-08-08 14:12:28 -03:00
|
|
|
self.assertEqual(d1.items() - d1.items(), set())
|
|
|
|
self.assertEqual(d1.items() - d2.items(), {('a', 1)})
|
|
|
|
self.assertEqual(d1.items() - d3.items(), {('a', 1), ('b', 2)})
|
|
|
|
self.assertEqual(d1.items() - set(d1.items()), set())
|
|
|
|
self.assertEqual(d1.items() - set(d2.items()), {('a', 1)})
|
|
|
|
self.assertEqual(d1.items() - set(d3.items()), {('a', 1), ('b', 2)})
|
|
|
|
|
2010-09-02 12:06:06 -03:00
|
|
|
self.assertFalse(d1.items().isdisjoint(d1.items()))
|
|
|
|
self.assertFalse(d1.items().isdisjoint(d2.items()))
|
|
|
|
self.assertFalse(d1.items().isdisjoint(list(d2.items())))
|
|
|
|
self.assertFalse(d1.items().isdisjoint(set(d2.items())))
|
|
|
|
self.assertTrue(d1.items().isdisjoint({'x', 'y', 'z'}))
|
|
|
|
self.assertTrue(d1.items().isdisjoint(['x', 'y', 'z']))
|
|
|
|
self.assertTrue(d1.items().isdisjoint(set(['x', 'y', 'z'])))
|
|
|
|
self.assertTrue(d1.items().isdisjoint(set(['x', 'y'])))
|
|
|
|
self.assertTrue(d1.items().isdisjoint({}))
|
|
|
|
self.assertTrue(d1.items().isdisjoint(d3.items()))
|
|
|
|
|
|
|
|
de = {}
|
|
|
|
self.assertTrue(de.items().isdisjoint(set()))
|
|
|
|
self.assertTrue(de.items().isdisjoint([]))
|
|
|
|
self.assertTrue(de.items().isdisjoint(de.items()))
|
|
|
|
self.assertTrue(de.items().isdisjoint([1]))
|
|
|
|
|
2019-10-06 08:28:33 -03:00
|
|
|
def test_set_operations_with_iterator(self):
|
|
|
|
origin = {1: 2, 3: 4}
|
|
|
|
self.assertEqual(origin.keys() & iter([1, 2]), {1})
|
|
|
|
self.assertEqual(origin.keys() | iter([1, 2]), {1, 2, 3})
|
|
|
|
self.assertEqual(origin.keys() ^ iter([1, 2]), {2, 3})
|
2019-10-08 12:59:10 -03:00
|
|
|
self.assertEqual(origin.keys() - iter([1, 2]), {3})
|
2019-10-06 08:28:33 -03:00
|
|
|
|
|
|
|
items = origin.items()
|
|
|
|
self.assertEqual(items & iter([(1, 2)]), {(1, 2)})
|
|
|
|
self.assertEqual(items ^ iter([(1, 2)]), {(3, 4)})
|
|
|
|
self.assertEqual(items | iter([(1, 2)]), {(1, 2), (3, 4)})
|
2019-10-08 12:59:10 -03:00
|
|
|
self.assertEqual(items - iter([(1, 2)]), {(3, 4)})
|
2019-10-06 08:28:33 -03:00
|
|
|
|
2019-10-13 08:49:05 -03:00
|
|
|
def test_set_operations_with_noniterable(self):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.keys() & 1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.keys() | 1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.keys() ^ 1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.keys() - 1
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.items() & 1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.items() | 1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.items() ^ 1
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
{}.items() - 1
|
|
|
|
|
2013-05-19 23:39:38 -03:00
|
|
|
def test_recursive_repr(self):
|
|
|
|
d = {}
|
|
|
|
d[42] = d.values()
|
2018-01-26 11:46:01 -04:00
|
|
|
r = repr(d)
|
|
|
|
# Cannot perform a stronger test, as the contents of the repr
|
|
|
|
# are implementation-dependent. All we can say is that we
|
|
|
|
# want a str result, not an exception of any sort.
|
|
|
|
self.assertIsInstance(r, str)
|
|
|
|
d[42] = d.items()
|
|
|
|
r = repr(d)
|
|
|
|
# Again.
|
|
|
|
self.assertIsInstance(r, str)
|
|
|
|
|
|
|
|
def test_deeply_nested_repr(self):
|
|
|
|
d = {}
|
|
|
|
for i in range(sys.getrecursionlimit() + 100):
|
|
|
|
d = {42: d.values()}
|
2015-07-03 02:04:23 -03:00
|
|
|
self.assertRaises(RecursionError, repr, d)
|
2013-05-19 23:39:38 -03:00
|
|
|
|
2015-11-12 05:23:04 -04:00
|
|
|
def test_copy(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
|
|
|
self.assertRaises(TypeError, copy.copy, d.keys())
|
|
|
|
self.assertRaises(TypeError, copy.copy, d.values())
|
|
|
|
self.assertRaises(TypeError, copy.copy, d.items())
|
|
|
|
|
2016-09-19 01:46:08 -03:00
|
|
|
def test_compare_error(self):
|
|
|
|
class Exc(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BadEq:
|
|
|
|
def __hash__(self):
|
|
|
|
return 7
|
|
|
|
def __eq__(self, other):
|
|
|
|
raise Exc
|
|
|
|
|
|
|
|
k1, k2 = BadEq(), BadEq()
|
|
|
|
v1, v2 = BadEq(), BadEq()
|
|
|
|
d = {k1: v1}
|
|
|
|
|
|
|
|
self.assertIn(k1, d)
|
|
|
|
self.assertIn(k1, d.keys())
|
|
|
|
self.assertIn(v1, d.values())
|
|
|
|
self.assertIn((k1, v1), d.items())
|
|
|
|
|
|
|
|
self.assertRaises(Exc, d.__contains__, k2)
|
|
|
|
self.assertRaises(Exc, d.keys().__contains__, k2)
|
|
|
|
self.assertRaises(Exc, d.items().__contains__, (k2, v1))
|
|
|
|
self.assertRaises(Exc, d.items().__contains__, (k1, v2))
|
|
|
|
with self.assertRaises(Exc):
|
|
|
|
v2 in d.values()
|
|
|
|
|
2015-11-12 05:23:04 -04:00
|
|
|
def test_pickle(self):
|
|
|
|
d = {1: 10, "a": "ABC"}
|
|
|
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
|
|
|
self.assertRaises((TypeError, pickle.PicklingError),
|
|
|
|
pickle.dumps, d.keys(), proto)
|
|
|
|
self.assertRaises((TypeError, pickle.PicklingError),
|
|
|
|
pickle.dumps, d.values(), proto)
|
|
|
|
self.assertRaises((TypeError, pickle.PicklingError),
|
|
|
|
pickle.dumps, d.items(), proto)
|
|
|
|
|
2015-05-26 05:47:58 -03:00
|
|
|
def test_abc_registry(self):
|
|
|
|
d = dict(a=1)
|
|
|
|
|
2017-04-24 03:05:00 -03:00
|
|
|
self.assertIsInstance(d.keys(), collections.abc.KeysView)
|
|
|
|
self.assertIsInstance(d.keys(), collections.abc.MappingView)
|
|
|
|
self.assertIsInstance(d.keys(), collections.abc.Set)
|
|
|
|
self.assertIsInstance(d.keys(), collections.abc.Sized)
|
|
|
|
self.assertIsInstance(d.keys(), collections.abc.Iterable)
|
|
|
|
self.assertIsInstance(d.keys(), collections.abc.Container)
|
|
|
|
|
|
|
|
self.assertIsInstance(d.values(), collections.abc.ValuesView)
|
|
|
|
self.assertIsInstance(d.values(), collections.abc.MappingView)
|
|
|
|
self.assertIsInstance(d.values(), collections.abc.Sized)
|
|
|
|
|
|
|
|
self.assertIsInstance(d.items(), collections.abc.ItemsView)
|
|
|
|
self.assertIsInstance(d.items(), collections.abc.MappingView)
|
|
|
|
self.assertIsInstance(d.items(), collections.abc.Set)
|
|
|
|
self.assertIsInstance(d.items(), collections.abc.Sized)
|
|
|
|
self.assertIsInstance(d.items(), collections.abc.Iterable)
|
|
|
|
self.assertIsInstance(d.items(), collections.abc.Container)
|
2015-05-26 05:47:58 -03:00
|
|
|
|
2010-05-04 00:41:49 -03:00
|
|
|
|
2007-02-09 21:11:45 -04:00
|
|
|
if __name__ == "__main__":
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|