10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport gc 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport weakref 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport operator 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport copy 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport pickle 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom random import randrange, shuffle 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport collections 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass PassThru(Exception): 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef check_pass_thru(): 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise PassThru 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield 1 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BadCmp: 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 1 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ReprWrapper: 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Used to test self-referential repr() calls' 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __repr__(self): 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return repr(self.value) 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass HashCountingInt(int): 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'int-like object that counts the number of times __hash__ is called' 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, *args): 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.hash_count = 0 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.hash_count += 1 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int.__hash__(self) 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestJointOps(unittest.TestCase): 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Tests common to both set and frozenset 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.word = word = 'simsalabim' 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherword = 'madagascar' 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s = self.thetype(word) 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.d = dict.fromkeys(word) 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_new_or_init(self): 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.thetype, [], 2) 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, set().__init__, a=1) 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_uniquification(self): 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao actual = sorted(self.s) 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = sorted(self.d) 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(actual, expected) 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.thetype, check_pass_thru()) 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.thetype, [[]]) 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_len(self): 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(self.s), len(self.d)) 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_contains(self): 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in self.letters: 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(c in self.s, c in self.d) 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.__contains__, [[]]) 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([frozenset(self.letters)]) 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(self.thetype(self.letters), s) 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union(self): 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u = self.s.union(self.otherword) 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in self.letters: 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(c in u, c in self.d or c in self.otherword) 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, self.thetype(self.word)) 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(type(u), self.thetype) 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.union, check_pass_thru()) 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.union, [[]]) 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd')) 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg')) 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc')) 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef')) 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').union(C('ef'), C('fg')), set('abcefg')) 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Issue #6573 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = self.thetype() 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x.union(set([1]), x, set([2])), self.thetype([1, 2])) 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_or(self): 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.union(self.otherword) 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s | set(self.otherword), i) 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s | frozenset(self.otherword), i) 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s | self.otherword 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("s|t did not screen-out general iterables") 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection(self): 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.intersection(self.otherword) 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in self.letters: 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(c in i, c in self.d and c in self.otherword) 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, self.thetype(self.word)) 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(type(i), self.thetype) 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.intersection, check_pass_thru()) 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc')) 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set('')) 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').intersection(C('ef')), set('')) 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b')) 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcba') 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao z = s.intersection() 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.thetype == frozenset(): 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(id(s), id(z)) 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(s), id(z)) 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isdisjoint(self): 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def f(s1, s2): 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Pure python equivalent of isdisjoint()' 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return not set(s1).intersection(s2) 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for larg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef': 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s1 = self.thetype(larg) 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for rarg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef': 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s2 = C(rarg) 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao actual = s1.isdisjoint(s2) 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = f(s1, s2) 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(actual, expected) 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(actual is True or actual is False) 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_and(self): 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.intersection(self.otherword) 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s & set(self.otherword), i) 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s & frozenset(self.otherword), i) 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s & self.otherword 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("s&t did not screen-out general iterables") 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference(self): 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.difference(self.otherword) 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in self.letters: 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(c in i, c in self.d and c not in self.otherword) 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, self.thetype(self.word)) 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(type(i), self.thetype) 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.difference, check_pass_thru()) 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.difference, [[]]) 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab')) 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc')) 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').difference(), set('abc')) 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').difference(C('a'), C('b')), set('c')) 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sub(self): 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.difference(self.otherword) 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s - set(self.otherword), i) 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s - frozenset(self.otherword), i) 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s - self.otherword 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("s-t did not screen-out general iterables") 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_symmetric_difference(self): 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.symmetric_difference(self.otherword) 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in self.letters: 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword)) 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, self.thetype(self.word)) 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(type(i), self.thetype) 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru()) 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.symmetric_difference, [[]]) 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd')) 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg')) 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a')) 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.thetype('abcba').symmetric_difference(C('ef')), set('abcef')) 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_xor(self): 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao i = self.s.symmetric_difference(self.otherword) 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s ^ set(self.otherword), i) 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s ^ frozenset(self.otherword), i) 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s ^ self.otherword 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("s^t did not screen-out general iterables") 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_equality(self): 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, set(self.word)) 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, frozenset(self.word)) 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s == self.word, False) 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(self.s, set(self.otherword)) 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(self.s, frozenset(self.otherword)) 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s != self.word, True) 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_setOfFrozensets(self): 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba']) 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(t) 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(s), 3) 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_compare(self): 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.__cmp__, self.s) 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sub_and_super(self): 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p, q, r = map(self.thetype, ['ab', 'abcde', 'def']) 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(p < q) 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(p <= q) 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(q <= q) 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(q > p) 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(q >= p) 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(q < r) 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(q <= r) 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(q > r) 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(q >= r) 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(set('a').issubset('abc')) 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(set('abc').issuperset('a')) 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(set('a').issubset('cbs')) 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(set('cbs').issuperset('a')) 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_pickling(self): 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in range(pickle.HIGHEST_PROTOCOL + 1): 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = pickle.dumps(self.s, i) 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = pickle.loads(p) 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup)) 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if type(self.s) not in (set, frozenset): 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.x = 10 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = pickle.dumps(self.s) 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = pickle.loads(p) 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s.x, dup.x) 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy(self): 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Tracer: 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, value): 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.value = value 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.value 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __deepcopy__(self, memo=None): 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return Tracer(self.value + 1) 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = Tracer(10) 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([t]) 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = copy.deepcopy(s) 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(s), id(dup)) 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for elem in dup: 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao newt = elem 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(t), id(newt)) 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(t.value + 1, newt.value) 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_gc(self): 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Create a nest of cycles to exercise overall ref count check 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class A: 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = set(A() for i in xrange(1000)) 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for elem in s: 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elem.cycle = s 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elem.sub = elem 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elem.set = set([elem]) 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_subclass_with_custom_hash(self): 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Bug #1257731 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class H(self.thetype): 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return int(id(self) & 0x7fffffff) 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s=H() 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f=set() 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.add(s) 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(s, f) 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.remove(s) 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.add(s) 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.discard(s) 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_badcmp(self): 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([BadCmp()]) 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Detect comparison errors during insertion and lookup 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, self.thetype, [BadCmp(), BadCmp()]) 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, s.__contains__, BadCmp()) 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Detect errors during mutating operations 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(s, 'add'): 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, s.add, BadCmp()) 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, s.discard, BadCmp()) 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, s.remove, BadCmp()) 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_cyclical_repr(self): 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w = ReprWrapper() 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([w]) 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w.value = s 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name = repr(s).partition('(')[0] # strip class name from repr string 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(repr(s), '%s([%s(...)])' % (name, name)) 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_cyclical_print(self): 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w = ReprWrapper() 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([w]) 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao w.value = s 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo = open(test_support.TESTFN, "wb") 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print >> fo, s, 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo.close() 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo = open(test_support.TESTFN, "rb") 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(fo.read(), repr(s)) 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo.close() 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.unlink(test_support.TESTFN) 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_do_not_rehash_dict_keys(self): 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n = 10 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = dict.fromkeys(map(HashCountingInt, xrange(n))) 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(d) 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.difference(d) 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(s, 'symmetric_difference_update'): 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.symmetric_difference_update(d) 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d2 = dict.fromkeys(set(d)) 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d3 = dict.fromkeys(frozenset(d)) 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d3 = dict.fromkeys(frozenset(d), 123) 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sum(elem.hash_count for elem in d), n) 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(d3, dict.fromkeys(d, 123)) 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_container_iterator(self): 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Bug #3680: tp_traverse was not implemented for set iterator object 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = C() 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ref = weakref.ref(obj) 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao container = set([obj, 1]) 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj.x = iter(container) 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del obj, container 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao gc.collect() 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(ref() is None, "Cycle was not collected") 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSet(TestJointOps): 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thetype = set 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_init(self): 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype() 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.__init__(self.word) 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(self.word)) 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.__init__(self.otherword) 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(self.otherword)) 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, s.__init__, s, 2); 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, s.__init__, 1); 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_constructor_identity(self): 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(range(3)) 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = self.thetype(s) 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(s), id(t)) 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hash(self): 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, hash, self.s) 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_clear(self): 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.clear() 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, set()) 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(self.s), 0) 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy(self): 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = self.s.copy() 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, dup) 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(self.s), id(dup)) 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_add(self): 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.add('Q') 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn('Q', self.s) 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = self.s.copy() 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.add('Q') 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.s, dup) 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.add, []) 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_remove(self): 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.remove('a') 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn('a', self.s) 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(KeyError, self.s.remove, 'Q') 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.remove, []) 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([frozenset(self.word)]) 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(self.thetype(self.word), s) 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.remove(self.thetype(self.word)) 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(self.thetype(self.word), s) 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(KeyError, self.s.remove, self.thetype(self.word)) 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_remove_keyerror_unpacking(self): 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # bug: www.python.org/sf/1576657 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v1 in ['Q', (1,)]: 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.remove(v1) 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError, e: 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v2 = e.args[0] 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(v1, v2) 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail() 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_remove_keyerror_set(self): 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao key = self.thetype([3, 4]) 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.remove(key) 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except KeyError as e: 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(e.args[0] is key, 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "KeyError should be {0}, not {1}".format(key, 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e.args[0])) 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail() 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_discard(self): 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.discard('a') 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn('a', self.s) 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s.discard('Q') 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.discard, []) 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype([frozenset(self.word)]) 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(self.thetype(self.word), s) 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.discard(self.thetype(self.word)) 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(self.thetype(self.word), s) 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.discard(self.thetype(self.word)) 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_pop(self): 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(len(self.s)): 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elem = self.s.pop() 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(elem, self.s) 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(KeyError, self.s.pop) 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_update(self): 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao retval = self.s.update(self.otherword) 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(retval, None) 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.update, check_pass_thru()) 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.update, [[]]) 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')): 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcba') 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.update(C(p)), None) 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(q)) 4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'): 4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao q = 'ahi' 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcba') 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.update(C(p), C(q)), None) 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(s) | set(p) | set(q)) 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_ior(self): 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s |= set(self.otherword) 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_update(self): 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao retval = self.s.intersection_update(self.otherword) 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(retval, None) 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c in self.otherword and c in self.word: 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(c, self.s) 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru()) 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.intersection_update, [[]]) 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')): 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcba') 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.intersection_update(C(p)), None) 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(q)) 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ss = 'abcba' 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(ss) 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = 'cbc' 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.intersection_update(C(p), C(t)), None) 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set('abcba')&set(p)&set(t)) 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_iand(self): 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s &= set(self.otherword) 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c in self.otherword and c in self.word: 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(c, self.s) 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_update(self): 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao retval = self.s.difference_update(self.otherword) 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(retval, None) 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c in self.word and c not in self.otherword: 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(c, self.s) 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.difference_update, check_pass_thru()) 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.difference_update, [[]]) 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')): 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcba') 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.difference_update(C(p)), None) 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(q)) 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcdefghih') 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.difference_update() 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, self.thetype('abcdefghih')) 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcdefghih') 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.difference_update(C('aba')) 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, self.thetype('cdefghih')) 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcdefghih') 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.difference_update(C('cdc'), C('aba')) 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, self.thetype('efghih')) 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isub(self): 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s -= set(self.otherword) 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if c in self.word and c not in self.otherword: 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(c, self.s) 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_symmetric_difference_update(self): 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao retval = self.s.symmetric_difference_update(self.otherword) 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(retval, None) 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if (c in self.word) ^ (c in self.otherword): 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(c, self.s) 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru()) 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')): 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple: 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('abcba') 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s.symmetric_difference_update(C(p)), None) 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(q)) 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_ixor(self): 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.s ^= set(self.otherword) 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for c in (self.word + self.otherword): 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if (c in self.word) ^ (c in self.otherword): 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(c, self.s) 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(c, self.s) 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_inplace_on_self(self): 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = self.s.copy() 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t |= t 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(t, self.s) 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t &= t 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(t, self.s) 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t -= t 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(t, self.thetype()) 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = self.s.copy() 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t ^= t 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(t, self.thetype()) 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_weakref(self): 5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype('gallahad') 5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = weakref.proxy(s) 5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(str(p), str(s)) 5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = None 5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ReferenceError, str, p) 5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # C API test only available in a debug build 5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if hasattr(set, "test_c_api"): 5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_c_api(self): 5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(set().test_c_api(), True) 5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SetSubclass(set): 5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSetSubclass(TestSet): 5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thetype = SetSubclass 5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SetSubclassWithKeywordArgs(set): 5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, iterable=[], newarg=None): 5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set.__init__(self, iterable) 5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSetSubclassWithKeywordArgs(TestSet): 5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_keywords_in_subclass(self): 5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'SF bug #1486663 -- this used to erroneously raise a TypeError' 5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao SetSubclassWithKeywordArgs(newarg=1) 5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestFrozenSet(TestJointOps): 5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thetype = frozenset 5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_init(self): 5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(self.word) 5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.__init__(self.otherword) 5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, set(self.word)) 5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_singleton_empty_frozenset(self): 5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = frozenset() 5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''), 5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao frozenset(), frozenset([]), frozenset(()), frozenset(''), 5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao frozenset(xrange(0)), frozenset(frozenset()), 5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao frozenset(f), f] 5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # All of the empty frozensets should have just one id() 6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(set(map(id, efs))), 1) 6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_constructor_identity(self): 6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(range(3)) 6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = self.thetype(s) 6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(id(s), id(t)) 6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hash(self): 6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(hash(self.thetype('abcdeb')), 6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hash(self.thetype('ebecda'))) 6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make sure that all permutations give the same hash value 6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n = 100 6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao seq = [randrange(n) for i in xrange(n)] 6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao results = set() 6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(200): 6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao shuffle(seq) 6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao results.add(hash(self.thetype(seq))) 6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(results), 1) 6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy(self): 6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = self.s.copy() 6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(id(self.s), id(dup)) 6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_frozen_as_dictkey(self): 6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao seq = range(10) + list('abcdefg') + ['apple'] 6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao key1 = self.thetype(seq) 6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao key2 = self.thetype(reversed(seq)) 6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(key1, key2) 6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(key1), id(key2)) 6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = {} 6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d[key1] = 42 6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(d[key2], 42) 6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hash_caching(self): 6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = self.thetype('abcdcda') 6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(hash(f), hash(f)) 6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_hash_effectiveness(self): 6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao n = 13 6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao hashvalues = set() 6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao addhashvalue = hashvalues.add 6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao elemmasks = [(i+1, 1<<i) for i in range(n)] 6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(2**n): 6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i]))) 6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(hashvalues), 2**n) 6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass FrozenSetSubclass(frozenset): 6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestFrozenSetSubclass(TestFrozenSet): 6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thetype = FrozenSetSubclass 6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_constructor_identity(self): 6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype(range(3)) 6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = self.thetype(s) 6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(s), id(t)) 6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy(self): 6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = self.s.copy() 6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(id(self.s), id(dup)) 6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_nested_empty_constructor(self): 6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = self.thetype() 6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = self.thetype(s) 6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s, t) 6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_singleton_empty_frozenset(self): 6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Frozenset = self.thetype 6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = frozenset() 6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao F = Frozenset() 6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao efs = [Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''), 6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''), 6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Frozenset(xrange(0)), Frozenset(Frozenset()), 6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Frozenset(frozenset()), f, F, Frozenset(f), Frozenset(F)] 6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # All empty frozenset subclass instances should have different ids 6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(set(map(id, efs))), len(efs)) 6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Tests taken from test_sets.py ============================================= 6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoempty_set = set() 6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOps(unittest.TestCase): 6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_repr(self): 6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.repr is not None: 6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(repr(self.set), self.repr) 6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def check_repr_against_values(self): 6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao text = repr(self.set) 6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(text.startswith('{')) 6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(text.endswith('}')) 6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = text[1:-1].split(', ') 6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result.sort() 6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sorted_repr_values = [repr(value) for value in self.values] 6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sorted_repr_values.sort() 6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, sorted_repr_values) 7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_print(self): 7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo = open(test_support.TESTFN, "wb") 7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print >> fo, self.set, 7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo.close() 7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo = open(test_support.TESTFN, "rb") 7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(fo.read(), repr(self.set)) 7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao finally: 7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao fo.close() 7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.unlink(test_support.TESTFN) 7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_length(self): 7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(self.set), self.length) 7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_self_equality(self): 7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, self.set) 7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_equivalent_equality(self): 7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, self.dup) 7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy(self): 7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set.copy(), self.dup) 7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_self_union(self): 7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set | self.set 7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, self.dup) 7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_union(self): 7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set | empty_set 7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, self.dup) 7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_empty(self): 7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = empty_set | self.set 7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, self.dup) 7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_self_intersection(self): 7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set & self.set 7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, self.dup) 7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_intersection(self): 7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set & empty_set 7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, empty_set) 7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_empty(self): 7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = empty_set & self.set 7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, empty_set) 7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_self_isdisjoint(self): 7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set.isdisjoint(self.set) 7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, not self.set) 7510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_isdisjoint(self): 7530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set.isdisjoint(empty_set) 7540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, True) 7550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isdisjoint_empty(self): 7570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = empty_set.isdisjoint(self.set) 7580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, True) 7590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_self_symmetric_difference(self): 7610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set ^ self.set 7620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, empty_set) 7630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_symmetric_difference(self): 7650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set ^ empty_set 7660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, self.set) 7670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_self_difference(self): 7690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set - self.set 7700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, empty_set) 7710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_difference(self): 7730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set - empty_set 7740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, self.dup) 7750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_empty_difference_rev(self): 7770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = empty_set - self.set 7780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, empty_set) 7790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_iteration(self): 7810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v in self.set: 7820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(v, self.values) 7830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao setiter = iter(self.set) 7840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # note: __length_hint__ is an internal undocumented API, 7850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # don't rely on it in your own programs 7860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(setiter.__length_hint__(), len(self.set)) 7870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_pickling(self): 7890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao p = pickle.dumps(self.set) 7900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao copy = pickle.loads(p) 7910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, copy, 7920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "%s != %s" % (self.set, copy)) 7930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 7950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsEmpty(TestBasicOps): 7970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 7980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "empty set" 7990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = [] 8000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 0 8030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.repr = "set([])" 8040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 8060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsSingleton(TestBasicOps): 8080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 8090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "unit set (number)" 8100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = [3] 8110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 1 8140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.repr = "set([3])" 8150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_in(self): 8170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(3, self.set) 8180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_not_in(self): 8200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(2, self.set) 8210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 8230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsTuple(TestBasicOps): 8250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 8260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "unit set (tuple)" 8270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = [(0, "zero")] 8280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 1 8310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.repr = "set([(0, 'zero')])" 8320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_in(self): 8340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn((0, "zero"), self.set) 8350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_not_in(self): 8370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(9, self.set) 8380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 8400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8410a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsTriple(TestBasicOps): 8420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 8430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "triple set" 8440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = [0, "zero", operator.add] 8450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 3 8480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.repr = None 8490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 8510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsString(TestBasicOps): 8530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 8540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "string set" 8550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = ["a", "b", "c"] 8560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 3 8590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_repr(self): 8610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_repr_against_values() 8620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 8640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8650a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsUnicode(TestBasicOps): 8660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 8670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "unicode set" 8680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = [u"a", u"b", u"c"] 8690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 3 8720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_repr(self): 8740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_repr_against_values() 8750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 8770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBasicOpsMixedStringUnicode(TestBasicOps): 8790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 8800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.case = "string and bytes set" 8810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = ["a", "b", u"a", u"b"] 8820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 8830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.dup = set(self.values) 8840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.length = 4 8850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_repr(self): 8870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with test_support.check_warnings(): 8880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.check_repr_against_values() 8890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 8910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef baditer(): 8930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise TypeError 8940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield True 8950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef gooditer(): 8970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield True 8980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8990a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestExceptionPropagation(unittest.TestCase): 9000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """SF 628246: Set constructor should not trap iterator TypeErrors""" 9010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_instanceWithException(self): 9030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, set, baditer()) 9040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_instancesWithoutException(self): 9060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # All of these iterables should load without exception. 9070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set([1,2,3]) 9080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set((1,2,3)) 9090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set({'one':1, 'two':2, 'three':3}) 9100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set(xrange(3)) 9110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set('abc') 9120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set(gooditer()) 9130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_changingSizeWhileIterating(self): 9150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = set([1,2,3]) 9160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 9170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in s: 9180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s.update([4]) 9190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except RuntimeError: 9200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 9210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 9220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("no exception when changing size during iteration") 9230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 9250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9260a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSetOfSets(unittest.TestCase): 9270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_constructor(self): 9280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao inner = frozenset([1]) 9290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao outer = set([inner]) 9300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao element = outer.pop() 9310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(type(element), frozenset) 9320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao outer.add(inner) # Rebuild set of sets with .add method 9330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao outer.remove(inner) 9340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(outer, set()) # Verify that remove worked 9350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao outer.discard(inner) # Absence of KeyError indicates working fine 9360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 9380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestBinaryOps(unittest.TestCase): 9400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 9410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((2, 4, 6)) 9420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_eq(self): # SF bug 643115 9440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set({2:1,4:3,6:5})) 9450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_subset(self): 9470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set | set([2]) 9480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set((2, 4, 6))) 9490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_superset(self): 9510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set | set([2, 4, 6, 8]) 9520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([2, 4, 6, 8])) 9530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_overlap(self): 9550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set | set([3, 4, 5]) 9560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([2, 3, 4, 5, 6])) 9570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_non_overlap(self): 9590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set | set([8]) 9600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([2, 4, 6, 8])) 9610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_subset(self): 9630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set & set((2, 4)) 9640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set((2, 4))) 9650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_superset(self): 9670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set & set([2, 4, 6, 8]) 9680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([2, 4, 6])) 9690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_overlap(self): 9710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set & set([3, 4, 5]) 9720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([4])) 9730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_non_overlap(self): 9750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set & set([8]) 9760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, empty_set) 9770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isdisjoint_subset(self): 9790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set.isdisjoint(set((2, 4))) 9800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, False) 9810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isdisjoint_superset(self): 9830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set.isdisjoint(set([2, 4, 6, 8])) 9840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, False) 9850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isdisjoint_overlap(self): 9870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set.isdisjoint(set([3, 4, 5])) 9880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, False) 9890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_isdisjoint_non_overlap(self): 9910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set.isdisjoint(set([8])) 9920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, True) 9930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_subset(self): 9950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set ^ set((2, 4)) 9960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([6])) 9970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_superset(self): 9990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set ^ set((2, 4, 6, 8)) 10000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([8])) 10010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_overlap(self): 10030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set ^ set((3, 4, 5)) 10040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([2, 3, 5, 6])) 10050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_non_overlap(self): 10070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = self.set ^ set([8]) 10080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, set([2, 4, 6, 8])) 10090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_cmp(self): 10110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b = set('a'), set('b') 10120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, cmp, a, b) 10130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # You can view this as a buglet: cmp(a, a) does not raise TypeError, 10150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True, 10160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # which Python thinks is good enough to synthesize a cmp() result 10170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # without calling __cmp__. 10180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(cmp(a, a), 0) 10190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, cmp, a, 12) 10210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, cmp, "abc", a) 10220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 10240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestUpdateOps(unittest.TestCase): 10260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 10270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((2, 4, 6)) 10280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_subset(self): 10300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set |= set([2]) 10310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set((2, 4, 6))) 10320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_superset(self): 10340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set |= set([2, 4, 6, 8]) 10350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 4, 6, 8])) 10360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_overlap(self): 10380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set |= set([3, 4, 5]) 10390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 3, 4, 5, 6])) 10400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_non_overlap(self): 10420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set |= set([8]) 10430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 4, 6, 8])) 10440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union_method_call(self): 10460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.update(set([3, 4, 5])) 10470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 3, 4, 5, 6])) 10480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_subset(self): 10500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set &= set((2, 4)) 10510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set((2, 4))) 10520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_superset(self): 10540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set &= set([2, 4, 6, 8]) 10550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 4, 6])) 10560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_overlap(self): 10580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set &= set([3, 4, 5]) 10590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([4])) 10600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_non_overlap(self): 10620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set &= set([8]) 10630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, empty_set) 10640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_method_call(self): 10660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.intersection_update(set([3, 4, 5])) 10670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([4])) 10680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_subset(self): 10700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set ^= set((2, 4)) 10710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([6])) 10720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_superset(self): 10740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set ^= set((2, 4, 6, 8)) 10750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([8])) 10760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_overlap(self): 10780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set ^= set((3, 4, 5)) 10790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 3, 5, 6])) 10800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_non_overlap(self): 10820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set ^= set([8]) 10830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 4, 6, 8])) 10840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_method_call(self): 10860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.symmetric_difference_update(set([3, 4, 5])) 10870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 3, 5, 6])) 10880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_subset(self): 10900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set -= set((2, 4)) 10910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([6])) 10920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_superset(self): 10940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set -= set((2, 4, 6, 8)) 10950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([])) 10960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_overlap(self): 10980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set -= set((3, 4, 5)) 10990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 6])) 11000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_non_overlap(self): 11020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set -= set([8]) 11030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 4, 6])) 11040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_method_call(self): 11060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.difference_update(set([3, 4, 5])) 11070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set([2, 6])) 11080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 11100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestMutate(unittest.TestCase): 11120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 11130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.values = ["a", "b", "c"] 11140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(self.values) 11150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_add_present(self): 11170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.add("c") 11180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set("abc")) 11190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_add_absent(self): 11210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.add("d") 11220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set("abcd")) 11230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_add_until_full(self): 11250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp = set() 11260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected_len = 0 11270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v in self.values: 11280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tmp.add(v) 11290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected_len += 1 11300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(tmp), expected_len) 11310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(tmp, self.set) 11320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_remove_present(self): 11340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.remove("b") 11350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set("ac")) 11360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_remove_absent(self): 11380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 11390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.remove("d") 11400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("Removing missing element should have raised LookupError") 11410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except LookupError: 11420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 11430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_remove_until_empty(self): 11450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected_len = len(self.set) 11460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v in self.values: 11470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.remove(v) 11480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected_len -= 1 11490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(self.set), expected_len) 11500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_discard_present(self): 11520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.discard("c") 11530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set("ab")) 11540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_discard_absent(self): 11560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.discard("d") 11570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set("abc")) 11580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_clear(self): 11600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.clear() 11610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(self.set), 0) 11620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_pop(self): 11640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao popped = {} 11650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao while self.set: 11660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao popped[self.set.pop()] = None 11670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(popped), len(self.values)) 11680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v in self.values: 11690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(v, popped) 11700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_update_empty_tuple(self): 11720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.update(()) 11730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set(self.values)) 11740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_update_unit_tuple_overlap(self): 11760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.update(("a",)) 11770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set(self.values)) 11780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_update_unit_tuple_non_overlap(self): 11800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.update(("a", "z")) 11810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set, set(self.values + ["z"])) 11820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 11840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSubsets(unittest.TestCase): 11860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao case2method = {"<=": "issubset", 11880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ">=": "issuperset", 11890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao } 11900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao reverse = {"==": "==", 11920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "!=": "!=", 11930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "<": ">", 11940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ">": "<", 11950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "<=": ">=", 11960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ">=": "<=", 11970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao } 11980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_issubset(self): 12000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = self.left 12010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = self.right 12020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for case in "!=", "==", "<", "<=", ">", ">=": 12030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = case in self.cases 12040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test the binary infix spelling. 12050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = eval("x" + case + "y", locals()) 12060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, expected) 12070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test the "friendly" method-name spelling, if one exists. 12080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if case in TestSubsets.case2method: 12090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao method = getattr(x, TestSubsets.case2method[case]) 12100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = method(y) 12110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, expected) 12120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Now do the same for the operands reversed. 12140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao rcase = TestSubsets.reverse[case] 12150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = eval("y" + rcase + "x", locals()) 12160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, expected) 12170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if rcase in TestSubsets.case2method: 12180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao method = getattr(y, TestSubsets.case2method[rcase]) 12190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = method(x) 12200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result, expected) 12210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 12220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSubsetEqualEmpty(TestSubsets): 12240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left = set() 12250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao right = set() 12260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name = "both empty" 12270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cases = "==", "<=", ">=" 12280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 12300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSubsetEqualNonEmpty(TestSubsets): 12320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left = set([1, 2]) 12330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao right = set([1, 2]) 12340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name = "equal pair" 12350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cases = "==", "<=", ">=" 12360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 12380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSubsetEmptyNonEmpty(TestSubsets): 12400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left = set() 12410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao right = set([1, 2]) 12420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name = "one empty, one non-empty" 12430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cases = "!=", "<", "<=" 12440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 12460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSubsetPartial(TestSubsets): 12480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left = set([1]) 12490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao right = set([1, 2]) 12500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name = "one a non-empty proper subset of other" 12510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cases = "!=", "<", "<=" 12520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 12540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestSubsetNonOverlap(TestSubsets): 12560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao left = set([1]) 12570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao right = set([2]) 12580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao name = "neither empty, neither contains" 12590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cases = "!=" 12600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 12620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12630a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsInBinaryOps(unittest.TestCase): 12640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_eq_ne(self): 12660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Unlike the others, this is testing that == and != *are* allowed. 12670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.other == self.set, False) 12680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set == self.other, False) 12690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.other != self.set, True) 12700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.set != self.other, True) 12710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_ge_gt_le_lt(self): 12730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set < self.other) 12740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set <= self.other) 12750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set > self.other) 12760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set >= self.other) 12770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other < self.set) 12790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other <= self.set) 12800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other > self.set) 12810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other >= self.set) 12820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_update_operator(self): 12840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 12850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set |= self.other 12860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 12870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 12880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 12890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("expected TypeError") 12900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_update(self): 12920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 12930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.update(self.other) 12940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 12950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.set.update, self.other) 12960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 12970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_union(self): 12980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set | self.other) 12990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other | self.set) 13000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.union(self.other) 13020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.set.union, self.other) 13040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_update_operator(self): 13060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 13070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set &= self.other 13080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 13090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 13100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("expected TypeError") 13120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection_update(self): 13140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.intersection_update(self.other) 13160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, 13180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.intersection_update, 13190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other) 13200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_intersection(self): 13220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set & self.other) 13230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other & self.set) 13240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.intersection(self.other) 13260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.set.intersection, self.other) 13280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_update_operator(self): 13300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 13310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set ^= self.other 13320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 13330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 13340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("expected TypeError") 13360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference_update(self): 13380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.symmetric_difference_update(self.other) 13400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, 13420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.symmetric_difference_update, 13430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other) 13440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_sym_difference(self): 13460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set ^ self.other) 13470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other ^ self.set) 13480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.symmetric_difference(self.other) 13500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.set.symmetric_difference, self.other) 13520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_update_operator(self): 13540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 13550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set -= self.other 13560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except TypeError: 13570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 13580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("expected TypeError") 13600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference_update(self): 13620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.difference_update(self.other) 13640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, 13660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.difference_update, 13670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other) 13680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_difference(self): 13700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.set - self.other) 13710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, lambda: self.other - self.set) 13720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.otherIsIterable: 13730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set.difference(self.other) 13740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 13750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, self.set.difference, self.other) 13760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 13780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsNumeric(TestOnlySetsInBinaryOps): 13800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 13810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((1, 2, 3)) 13820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other = 19 13830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherIsIterable = False 13840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 13860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13870a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsDict(TestOnlySetsInBinaryOps): 13880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 13890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((1, 2, 3)) 13900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other = {1:2, 3:4} 13910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherIsIterable = True 13920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 13940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 13950a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsOperator(TestOnlySetsInBinaryOps): 13960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 13970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((1, 2, 3)) 13980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other = operator.add 13990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherIsIterable = False 14000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_ge_gt_le_lt(self): 14020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with test_support.check_py3k_warnings(): 14030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(TestOnlySetsOperator, self).test_ge_gt_le_lt() 14040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsTuple(TestOnlySetsInBinaryOps): 14080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((1, 2, 3)) 14100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other = (2, 4, 6) 14110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherIsIterable = True 14120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsString(TestOnlySetsInBinaryOps): 14160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((1, 2, 3)) 14180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other = 'abc' 14190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherIsIterable = True 14200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestOnlySetsGenerator(TestOnlySetsInBinaryOps): 14240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def gen(): 14260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(0, 10, 2): 14270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield i 14280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set((1, 2, 3)) 14290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.other = gen() 14300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.otherIsIterable = True 14310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 14330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopying(unittest.TestCase): 14350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy(self): 14370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = list(self.set.copy()) 14380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(dup), len(self.set)) 14390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for el in self.set: 14400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(el, dup) 14410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pos = dup.index(el) 14420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIs(el, dup.pop(pos)) 14430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(dup) 14440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deep_copy(self): 14460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dup = copy.deepcopy(self.set) 14470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(dup, self.set) 14480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopyingEmpty(TestCopying): 14520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set() 14540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopyingSingleton(TestCopying): 14580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(["hello"]) 14600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14630a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopyingTriple(TestCopying): 14640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set(["zero", 0, None]) 14660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopyingTuple(TestCopying): 14700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set([(1, 2)]) 14720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#------------------------------------------------------------------------------ 14740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14750a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopyingNested(TestCopying): 14760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.set = set([((1, 2), (3, 4))]) 14780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 14800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestIdentities(unittest.TestCase): 14820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 14830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.a = set('abracadabra') 14840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.b = set('alacazam') 14850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_binopsVsSubsets(self): 14870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b = self.a, self.b 14880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(a - b < a) 14890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(b - a < b) 14900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(a & b < a) 14910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(a & b < b) 14920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(a | b > a) 14930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(a | b > b) 14940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(a ^ b < a | b) 14950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 14960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_commutativity(self): 14970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b = self.a, self.b 14980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a&b, b&a) 14990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a|b, b|a) 15000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a^b, b^a) 15010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if a != b: 15020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(a-b, b-a) 15030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_summations(self): 15050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # check that sums of parts equal the whole 15060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b = self.a, self.b 15070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a-b)|(a&b)|(b-a), a|b) 15080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a&b)|(a^b), a|b) 15090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a|(b-a), a|b) 15100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a-b)|b, a|b) 15110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a-b)|(a&b), a) 15120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((b-a)|(a&b), b) 15130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a-b)|(b-a), a^b) 15140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_exclusion(self): 15160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # check that inverse operations show non-overlap 15170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b, zero = self.a, self.b, set() 15180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a-b)&b, zero) 15190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((b-a)&a, zero) 15200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual((a&b)&(a^b), zero) 15210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Tests derived from test_itertools.py ======================================= 15230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef R(seqn): 15250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Regular generator' 15260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in seqn: 15270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield i 15280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass G: 15300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Sequence using __getitem__' 15310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.seqn = seqn 15330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getitem__(self, i): 15340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.seqn[i] 15350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass I: 15370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Sequence using iterator protocol' 15380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.seqn = seqn 15400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = 0 15410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __iter__(self): 15420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 15430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next(self): 15440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.i >= len(self.seqn): raise StopIteration 15450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v = self.seqn[self.i] 15460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i += 1 15470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return v 15480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Ig: 15500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Sequence using iterator protocol defined with a generator' 15510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.seqn = seqn 15530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = 0 15540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __iter__(self): 15550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for val in self.seqn: 15560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield val 15570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass X: 15590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Missing __getitem__ and __iter__' 15600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.seqn = seqn 15620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = 0 15630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next(self): 15640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if self.i >= len(self.seqn): raise StopIteration 15650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v = self.seqn[self.i] 15660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i += 1 15670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return v 15680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass N: 15700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Iterator missing next()' 15710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.seqn = seqn 15730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = 0 15740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __iter__(self): 15750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 15760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15770a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass E: 15780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Test propagation of exceptions' 15790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.seqn = seqn 15810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = 0 15820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __iter__(self): 15830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 15840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next(self): 15850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3 // 0 15860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15870a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass S: 15880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Test immediate stop' 15890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, seqn): 15900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 15910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __iter__(self): 15920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self 15930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def next(self): 15940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise StopIteration 15950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 15960a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom itertools import chain, imap 15970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef L(seqn): 15980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Test multiple tiers of iterators' 15990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return chain(imap(lambda x:x, R(Ig(G(seqn))))) 16000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16010a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestVariousIteratorArgs(unittest.TestCase): 16020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_constructor(self): 16040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for cons in (set, frozenset): 16050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): 16060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for g in (G, I, Ig, S, L, R): 16070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(cons(g(s)), set(g(s))) 16080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, cons , X(s)) 16090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, cons , N(s)) 16100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ZeroDivisionError, cons , E(s)) 16110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_inline_methods(self): 16130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = set('november') 16140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for data in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'): 16150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for meth in (s.union, s.intersection, s.difference, s.symmetric_difference, s.isdisjoint): 16160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for g in (G, I, Ig, L, R): 16170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = meth(data) 16180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao actual = meth(G(data)) 16190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if isinstance(expected, bool): 16200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(actual, expected) 16210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 16220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(actual, expected) 16230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, meth, X(s)) 16240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, meth, N(s)) 16250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ZeroDivisionError, meth, E(s)) 16260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_inplace_methods(self): 16280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for data in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'): 16290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for methname in ('update', 'intersection_update', 16300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'difference_update', 'symmetric_difference_update'): 16310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for g in (G, I, Ig, S, L, R): 16320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = set('january') 16330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao t = s.copy() 16340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao getattr(s, methname)(list(g(data))) 16350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao getattr(t, methname)(g(data)) 16360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(s, t) 16370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, getattr(set('january'), methname), X(data)) 16390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, getattr(set('january'), methname), N(data)) 16400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data)) 16410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16420a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass bad_eq: 16430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __eq__(self, other): 16440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if be_bad: 16450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2.clear() 16460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ZeroDivisionError 16470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self is other 16480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 16490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 16500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass bad_dict_clear: 16520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __eq__(self, other): 16530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if be_bad: 16540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dict2.clear() 16550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self is other 16560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __hash__(self): 16570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 0 16580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16590a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestWeirdBugs(unittest.TestCase): 16600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_8420_set_merge(self): 16610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This used to segfault 16620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao global be_bad, set2, dict2 16630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be_bad = False 16640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = {bad_eq()} 16650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = {bad_eq() for i in range(75)} 16660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be_bad = True 16670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ZeroDivisionError, set1.update, set2) 16680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be_bad = False 16700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = {bad_dict_clear()} 16710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dict2 = {bad_dict_clear(): None} 16720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao be_bad = True 16730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1.symmetric_difference_update(dict2) 16740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Application tests (based on David Eppstein's graph recipes ==================================== 16760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16770a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef powerset(U): 16780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Generates all subsets of a set or sequence U.""" 16790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao U = iter(U) 16800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 16810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = frozenset([U.next()]) 16820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for S in powerset(U): 16830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield S 16840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield S | x 16850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except StopIteration: 16860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao yield frozenset() 16870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16880a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef cube(n): 16890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Graph of n-dimensional hypercube.""" 16900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao singletons = [frozenset([x]) for x in range(n)] 16910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return dict([(x, frozenset([x^s for s in singletons])) 16920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for x in powerset(range(n))]) 16930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 16940a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef linegraph(G): 16950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Graph, the vertices of which are edges of G, 16960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with two vertices being adjacent iff the corresponding 16970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao edges share a vertex.""" 16980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao L = {} 16990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for x in G: 17000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for y in G[x]: 17010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao nx = [frozenset([x,z]) for z in G[x] if z != y] 17020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ny = [frozenset([y,z]) for z in G[y] if z != x] 17030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao L[frozenset([x,y])] = frozenset(nx+ny) 17040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return L 17050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17060a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef faces(G): 17070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Return a set of faces in G. Where a face is a set of vertices on that face' 17080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # currently limited to triangles,squares, and pentagons 17090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = set() 17100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v1, edges in G.items(): 17110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v2 in edges: 17120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v3 in G[v2]: 17130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if v1 == v3: 17140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 17150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if v1 in G[v3]: 17160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.add(frozenset([v1, v2, v3])) 17170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v4 in G[v3]: 17190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if v4 == v2: 17200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 17210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if v1 in G[v4]: 17220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.add(frozenset([v1, v2, v3, v4])) 17230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 17240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for v5 in G[v4]: 17250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if v5 == v3 or v5 == v2: 17260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao continue 17270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if v1 in G[v5]: 17280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.add(frozenset([v1, v2, v3, v4, v5])) 17290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return f 17300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestGraphs(unittest.TestCase): 17330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_cube(self): 17350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao g = cube(3) # vert --> {v1, v2, v3} 17370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao vertices1 = set(g) 17380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(vertices1), 8) # eight vertices 17390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for edge in g.values(): 17400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(edge), 3) # each vertex connects to three edges 17410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao vertices2 = set(v for edges in g.values() for v in edges) 17420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(vertices1, vertices2) # edge vertices in original set 17430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cubefaces = faces(g) 17450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(cubefaces), 6) # six faces 17460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for face in cubefaces: 17470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(face), 4) # each face is a square 17480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_cuboctahedron(self): 17500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # http://en.wikipedia.org/wiki/Cuboctahedron 17520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 8 triangular faces and 6 square faces 17530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 12 indentical vertices each connecting a triangle and square 17540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao g = cube(3) 17560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cuboctahedron = linegraph(g) # V( --> {V1, V2, V3, V4} 17570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(cuboctahedron), 12)# twelve vertices 17580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao vertices = set(cuboctahedron) 17600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for edges in cuboctahedron.values(): 17610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(edges), 4) # each vertex connects to four other vertices 17620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao othervertices = set(edge for edges in cuboctahedron.values() for edge in edges) 17630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(vertices, othervertices) # edge vertices in original set 17640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cubofaces = faces(cuboctahedron) 17660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao facesizes = collections.defaultdict(int) 17670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for face in cubofaces: 17680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao facesizes[len(face)] += 1 17690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(facesizes[3], 8) # eight triangular faces 17700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(facesizes[4], 6) # six square faces 17710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for vertex in cuboctahedron: 17730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao edge = vertex # Cuboctahedron vertices are edges in Cube 17740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(edge), 2) # Two cube vertices define an edge 17750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for cubevert in edge: 17760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(cubevert, g) 17770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#============================================================================== 17800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 17810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(verbose=None): 17820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_classes = ( 17830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSet, 17840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSetSubclass, 17850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSetSubclassWithKeywordArgs, 17860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestFrozenSet, 17870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestFrozenSetSubclass, 17880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSetOfSets, 17890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestExceptionPropagation, 17900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestBasicOpsEmpty, 17910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestBasicOpsSingleton, 17920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestBasicOpsTuple, 17930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestBasicOpsTriple, 17940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestBinaryOps, 17950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestUpdateOps, 17960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestMutate, 17970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSubsetEqualEmpty, 17980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSubsetEqualNonEmpty, 17990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSubsetEmptyNonEmpty, 18000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSubsetPartial, 18010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestSubsetNonOverlap, 18020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestOnlySetsNumeric, 18030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestOnlySetsDict, 18040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestOnlySetsOperator, 18050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestOnlySetsTuple, 18060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestOnlySetsString, 18070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestOnlySetsGenerator, 18080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestCopyingEmpty, 18090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestCopyingSingleton, 18100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestCopyingTriple, 18110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestCopyingTuple, 18120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestCopyingNested, 18130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestIdentities, 18140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestVariousIteratorArgs, 18150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestGraphs, 18160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestWeirdBugs, 18170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ) 18180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.run_unittest(*test_classes) 18200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # verify reference counting 18220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if verbose and hasattr(sys, "gettotalrefcount"): 18230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao import gc 18240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao counts = [None] * 5 18250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for i in xrange(len(counts)): 18260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.run_unittest(*test_classes) 18270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao gc.collect() 18280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao counts[i] = sys.gettotalrefcount() 18290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao print counts 18300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 18310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__": 18320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_main(verbose=True) 1833