summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-05-26 07:14:14 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-05-26 07:14:14 (GMT)
commitaa92d342f1249d3f8b5b7ca28a6efe43dd7e1eb5 (patch)
treeec984605d8b11d15899415fbff67389a26be1409 /Lib/test/test_collections.py
parentdfe098d2150b4dc2842a15c6c30438effb40c0b7 (diff)
parentdd5e53a086fcabc84ee1ac96b98057437863973a (diff)
downloadcpython-aa92d342f1249d3f8b5b7ca28a6efe43dd7e1eb5.zip
cpython-aa92d342f1249d3f8b5b7ca28a6efe43dd7e1eb5.tar.gz
cpython-aa92d342f1249d3f8b5b7ca28a6efe43dd7e1eb5.tar.bz2
merge
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r--Lib/test/test_collections.py160
1 files changed, 156 insertions, 4 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index a53862a..e948106 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -773,14 +773,166 @@ class TestCollectionABCs(ABCTestCase):
cs = MyComparableSet()
ncs = MyNonComparableSet()
+ self.assertFalse(ncs < cs)
+ self.assertTrue(ncs <= cs)
+ self.assertFalse(ncs > cs)
+ self.assertTrue(ncs >= cs)
+
+ def assertSameSet(self, s1, s2):
+ # coerce both to a real set then check equality
+ self.assertSetEqual(set(s1), set(s2))
+
+ def test_Set_interoperability_with_real_sets(self):
+ # Issue: 8743
+ class ListSet(Set):
+ def __init__(self, elements=()):
+ self.data = []
+ for elem in elements:
+ if elem not in self.data:
+ self.data.append(elem)
+ def __contains__(self, elem):
+ return elem in self.data
+ def __iter__(self):
+ return iter(self.data)
+ def __len__(self):
+ return len(self.data)
+ def __repr__(self):
+ return 'Set({!r})'.format(self.data)
+
+ r1 = set('abc')
+ r2 = set('bcd')
+ r3 = set('abcde')
+ f1 = ListSet('abc')
+ f2 = ListSet('bcd')
+ f3 = ListSet('abcde')
+ l1 = list('abccba')
+ l2 = list('bcddcb')
+ l3 = list('abcdeedcba')
+
+ target = r1 & r2
+ self.assertSameSet(f1 & f2, target)
+ self.assertSameSet(f1 & r2, target)
+ self.assertSameSet(r2 & f1, target)
+ self.assertSameSet(f1 & l2, target)
+
+ target = r1 | r2
+ self.assertSameSet(f1 | f2, target)
+ self.assertSameSet(f1 | r2, target)
+ self.assertSameSet(r2 | f1, target)
+ self.assertSameSet(f1 | l2, target)
+
+ fwd_target = r1 - r2
+ rev_target = r2 - r1
+ self.assertSameSet(f1 - f2, fwd_target)
+ self.assertSameSet(f2 - f1, rev_target)
+ self.assertSameSet(f1 - r2, fwd_target)
+ self.assertSameSet(f2 - r1, rev_target)
+ self.assertSameSet(r1 - f2, fwd_target)
+ self.assertSameSet(r2 - f1, rev_target)
+ self.assertSameSet(f1 - l2, fwd_target)
+ self.assertSameSet(f2 - l1, rev_target)
+
+ target = r1 ^ r2
+ self.assertSameSet(f1 ^ f2, target)
+ self.assertSameSet(f1 ^ r2, target)
+ self.assertSameSet(r2 ^ f1, target)
+ self.assertSameSet(f1 ^ l2, target)
+
+ # Don't change the following to use assertLess or other
+ # "more specific" unittest assertions. The current
+ # assertTrue/assertFalse style makes the pattern of test
+ # case combinations clear and allows us to know for sure
+ # the exact operator being invoked.
+
+ # proper subset
+ self.assertTrue(f1 < f3)
+ self.assertFalse(f1 < f1)
+ self.assertFalse(f1 < f2)
+ self.assertTrue(r1 < f3)
+ self.assertFalse(r1 < f1)
+ self.assertFalse(r1 < f2)
+ self.assertTrue(r1 < r3)
+ self.assertFalse(r1 < r1)
+ self.assertFalse(r1 < r2)
+ with self.assertRaises(TypeError):
+ f1 < l3
+ with self.assertRaises(TypeError):
+ f1 < l1
+ with self.assertRaises(TypeError):
+ f1 < l2
+
+ # any subset
+ self.assertTrue(f1 <= f3)
+ self.assertTrue(f1 <= f1)
+ self.assertFalse(f1 <= f2)
+ self.assertTrue(r1 <= f3)
+ self.assertTrue(r1 <= f1)
+ self.assertFalse(r1 <= f2)
+ self.assertTrue(r1 <= r3)
+ self.assertTrue(r1 <= r1)
+ self.assertFalse(r1 <= r2)
+ with self.assertRaises(TypeError):
+ f1 <= l3
+ with self.assertRaises(TypeError):
+ f1 <= l1
+ with self.assertRaises(TypeError):
+ f1 <= l2
+
+ # proper superset
+ self.assertTrue(f3 > f1)
+ self.assertFalse(f1 > f1)
+ self.assertFalse(f2 > f1)
+ self.assertTrue(r3 > r1)
+ self.assertFalse(f1 > r1)
+ self.assertFalse(f2 > r1)
+ self.assertTrue(r3 > r1)
+ self.assertFalse(r1 > r1)
+ self.assertFalse(r2 > r1)
+ with self.assertRaises(TypeError):
+ f1 > l3
+ with self.assertRaises(TypeError):
+ f1 > l1
with self.assertRaises(TypeError):
- ncs < cs
+ f1 > l2
+
+ # any superset
+ self.assertTrue(f3 >= f1)
+ self.assertTrue(f1 >= f1)
+ self.assertFalse(f2 >= f1)
+ self.assertTrue(r3 >= r1)
+ self.assertTrue(f1 >= r1)
+ self.assertFalse(f2 >= r1)
+ self.assertTrue(r3 >= r1)
+ self.assertTrue(r1 >= r1)
+ self.assertFalse(r2 >= r1)
with self.assertRaises(TypeError):
- ncs <= cs
+ f1 >= l3
with self.assertRaises(TypeError):
- cs > ncs
+ f1 >=l1
with self.assertRaises(TypeError):
- cs >= ncs
+ f1 >= l2
+
+ # equality
+ self.assertTrue(f1 == f1)
+ self.assertTrue(r1 == f1)
+ self.assertTrue(f1 == r1)
+ self.assertFalse(f1 == f3)
+ self.assertFalse(r1 == f3)
+ self.assertFalse(f1 == r3)
+ self.assertFalse(f1 == l3)
+ self.assertFalse(f1 == l1)
+ self.assertFalse(f1 == l2)
+
+ # inequality
+ self.assertFalse(f1 != f1)
+ self.assertFalse(r1 != f1)
+ self.assertFalse(f1 != r1)
+ self.assertTrue(f1 != f3)
+ self.assertTrue(r1 != f3)
+ self.assertTrue(f1 != r3)
+ self.assertTrue(f1 != l3)
+ self.assertTrue(f1 != l1)
+ self.assertTrue(f1 != l2)
def test_Mapping(self):
for sample in [dict]: