Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_weakset.TestWeakSet Class Reference

List of all members.

Public Member Functions

def setUp
def test_methods
def test_new_or_init
def test_len
def test_contains
def test_union
def test_or
def test_intersection
def test_isdisjoint
def test_and
def test_difference
def test_sub
def test_symmetric_difference
def test_xor
def test_sub_and_super
def test_gc
def test_subclass_with_custom_hash
def test_init
def test_constructor_identity
def test_hash
def test_clear
def test_copy
def test_add
def test_remove
def test_discard
def test_pop
def test_update
def test_update_set
def test_ior
def test_intersection_update
def test_iand
def test_difference_update
def test_isub
def test_symmetric_difference_update
def test_ixor
def test_inplace_on_self
def test_eq
def test_weak_destroy_while_iterating
def test_weak_destroy_and_mutate_while_iterating

Public Attributes

 items
 items2
 letters
 s
 d
 obj
 fs

Detailed Description

Definition at line 21 of file test_weakset.py.


Member Function Documentation

Definition at line 23 of file test_weakset.py.

00023 
00024     def setUp(self):
00025         # need to keep references to them
00026         self.items = [ustr(c) for c in ('a', 'b', 'c')]
00027         self.items2 = [ustr(c) for c in ('x', 'y', 'z')]
00028         self.letters = [ustr(c) for c in string.ascii_letters]
00029         self.s = WeakSet(self.items)
00030         self.d = dict.fromkeys(self.items)
00031         self.obj = ustr('F')
00032         self.fs = WeakSet([self.obj])

Here is the caller graph for this function:

Definition at line 186 of file test_weakset.py.

00186 
00187     def test_add(self):
00188         x = ustr('Q')
00189         self.s.add(x)
00190         self.assertIn(x, self.s)
00191         dup = self.s.copy()
00192         self.s.add(x)
00193         self.assertEqual(self.s, dup)
00194         self.assertRaises(TypeError, self.s.add, [])
00195         self.fs.add(Foo())
00196         self.assertTrue(len(self.fs) == 1)
00197         self.fs.add(self.obj)
00198         self.assertTrue(len(self.fs) == 1)

Here is the call graph for this function:

Definition at line 90 of file test_weakset.py.

00090 
00091     def test_and(self):
00092         i = self.s.intersection(self.items2)
00093         self.assertEqual(self.s & set(self.items2), i)
00094         self.assertEqual(self.s & frozenset(self.items2), i)

Here is the call graph for this function:

Definition at line 176 of file test_weakset.py.

00176 
00177     def test_clear(self):
00178         self.s.clear()
00179         self.assertEqual(self.s, WeakSet([]))
00180         self.assertEqual(len(self.s), 0)

Here is the call graph for this function:

Definition at line 168 of file test_weakset.py.

00168 
00169     def test_constructor_identity(self):
00170         s = WeakSet(self.items)
00171         t = WeakSet(s)
00172         self.assertNotEqual(id(s), id(t))

Here is the call graph for this function:

Definition at line 50 of file test_weakset.py.

00050 
00051     def test_contains(self):
00052         for c in self.letters:
00053             self.assertEqual(c in self.s, c in self.d)
00054         # 1 is not weakref'able, but that TypeError is caught by __contains__
00055         self.assertNotIn(1, self.s)
00056         self.assertIn(self.obj, self.fs)
00057         del self.obj
00058         self.assertNotIn(ustr('F'), self.fs)

Here is the call graph for this function:

Definition at line 181 of file test_weakset.py.

00181 
00182     def test_copy(self):
00183         dup = self.s.copy()
00184         self.assertEqual(self.s, dup)
00185         self.assertNotEqual(id(self.s), id(dup))

Here is the call graph for this function:

Definition at line 95 of file test_weakset.py.

00095 
00096     def test_difference(self):
00097         i = self.s.difference(self.items2)
00098         for c in self.letters:
00099             self.assertEqual(c in i, c in self.d and c not in self.items2)
00100         self.assertEqual(self.s, WeakSet(self.items))
00101         self.assertEqual(type(i), WeakSet)
00102         self.assertRaises(TypeError, self.s.difference, [[]])

Here is the call graph for this function:

Definition at line 254 of file test_weakset.py.

00254 
00255     def test_difference_update(self):
00256         retval = self.s.difference_update(self.items2)
00257         self.assertEqual(retval, None)
00258         for c in (self.items + self.items2):
00259             if c in self.items and c not in self.items2:
00260                 self.assertIn(c, self.s)
00261             else:
00262                 self.assertNotIn(c, self.s)
00263         self.assertRaises(TypeError, self.s.difference_update, [[]])
00264         self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])

Here is the call graph for this function:

Definition at line 206 of file test_weakset.py.

00206 
00207     def test_discard(self):
00208         a, q = ustr('a'), ustr('Q')
00209         self.s.discard(a)
00210         self.assertNotIn(a, self.s)
00211         self.s.discard(q)
00212         self.assertRaises(TypeError, self.s.discard, [])

Here is the call graph for this function:

Definition at line 303 of file test_weakset.py.

00303 
00304     def test_eq(self):
00305         # issue 5964
00306         self.assertTrue(self.s == self.s)
00307         self.assertTrue(self.s == WeakSet(self.items))
00308         self.assertFalse(self.s == set(self.items))
00309         self.assertFalse(self.s == list(self.items))
00310         self.assertFalse(self.s == tuple(self.items))
00311         self.assertFalse(self.s == WeakSet([Foo]))
00312         self.assertFalse(self.s == 1)

Here is the call graph for this function:

Definition at line 138 of file test_weakset.py.

00138 
00139     def test_gc(self):
00140         # Create a nest of cycles to exercise overall ref count check
00141         s = WeakSet(Foo() for i in range(1000))
00142         for elem in s:
00143             elem.cycle = s
00144             elem.sub = elem
00145             elem.set = WeakSet([elem])

Definition at line 173 of file test_weakset.py.

00173 
00174     def test_hash(self):
00175         self.assertRaises(TypeError, hash, self.s)

Here is the call graph for this function:

Definition at line 246 of file test_weakset.py.

00246 
00247     def test_iand(self):
00248         self.s &= set(self.items2)
00249         for c in (self.items + self.items2):
00250             if c in self.items2 and c in self.items:
00251                 self.assertIn(c, self.s)
00252             else:
00253                 self.assertNotIn(c, self.s)

Here is the call graph for this function:

Definition at line 159 of file test_weakset.py.

00159 
00160     def test_init(self):
00161         s = WeakSet()
00162         s.__init__(self.items)
00163         self.assertEqual(s, self.s)
00164         s.__init__(self.items2)
00165         self.assertEqual(s, WeakSet(self.items2))
00166         self.assertRaises(TypeError, s.__init__, s, 2);
00167         self.assertRaises(TypeError, s.__init__, 1);

Here is the call graph for this function:

Definition at line 291 of file test_weakset.py.

00291 
00292     def test_inplace_on_self(self):
00293         t = self.s.copy()
00294         t |= t
00295         self.assertEqual(t, self.s)
00296         t &= t
00297         self.assertEqual(t, self.s)
00298         t -= t
00299         self.assertEqual(t, WeakSet())
00300         t = self.s.copy()
00301         t ^= t
00302         self.assertEqual(t, WeakSet())

Here is the call graph for this function:

Definition at line 76 of file test_weakset.py.

00076 
00077     def test_intersection(self):
00078         i = self.s.intersection(self.items2)
00079         for c in self.letters:
00080             self.assertEqual(c in i, c in self.d and c in self.items2)
00081         self.assertEqual(self.s, WeakSet(self.items))
00082         self.assertEqual(type(i), WeakSet)
00083         for C in set, frozenset, dict.fromkeys, list, tuple:
00084             x = WeakSet([])
00085             self.assertEqual(self.s.intersection(C(self.items2)), x)

Here is the call graph for this function:

Definition at line 236 of file test_weakset.py.

00236 
00237     def test_intersection_update(self):
00238         retval = self.s.intersection_update(self.items2)
00239         self.assertEqual(retval, None)
00240         for c in (self.items + self.items2):
00241             if c in self.items2 and c in self.items:
00242                 self.assertIn(c, self.s)
00243             else:
00244                 self.assertNotIn(c, self.s)
00245         self.assertRaises(TypeError, self.s.intersection_update, [[]])

Here is the call graph for this function:

Definition at line 231 of file test_weakset.py.

00231 
00232     def test_ior(self):
00233         self.s |= set(self.items2)
00234         for c in (self.items + self.items2):
00235             self.assertIn(c, self.s)

Here is the call graph for this function:

Definition at line 86 of file test_weakset.py.

00086 
00087     def test_isdisjoint(self):
00088         self.assertTrue(self.s.isdisjoint(WeakSet(self.items2)))
00089         self.assertTrue(not self.s.isdisjoint(WeakSet(self.letters)))

Here is the call graph for this function:

Definition at line 265 of file test_weakset.py.

00265 
00266     def test_isub(self):
00267         self.s -= set(self.items2)
00268         for c in (self.items + self.items2):
00269             if c in self.items and c not in self.items2:
00270                 self.assertIn(c, self.s)
00271             else:
00272                 self.assertNotIn(c, self.s)

Here is the call graph for this function:

Definition at line 283 of file test_weakset.py.

00283 
00284     def test_ixor(self):
00285         self.s ^= set(self.items2)
00286         for c in (self.items + self.items2):
00287             if (c in self.items) ^ (c in self.items2):
00288                 self.assertIn(c, self.s)
00289             else:
00290                 self.assertNotIn(c, self.s)

Here is the call graph for this function:

Definition at line 44 of file test_weakset.py.

00044 
00045     def test_len(self):
00046         self.assertEqual(len(self.s), len(self.d))
00047         self.assertEqual(len(self.fs), 1)
00048         del self.obj
00049         self.assertEqual(len(self.fs), 0)

Here is the call graph for this function:

Definition at line 33 of file test_weakset.py.

00033 
00034     def test_methods(self):
00035         weaksetmethods = dir(WeakSet)
00036         for method in dir(set):
00037             if method == 'test_c_api' or method.startswith('_'):
00038                 continue
00039             self.assertIn(method, weaksetmethods,
00040                          "WeakSet missing method " + method)

Here is the call graph for this function:

Definition at line 41 of file test_weakset.py.

00041 
00042     def test_new_or_init(self):
00043         self.assertRaises(TypeError, WeakSet, [], 2)

Here is the call graph for this function:

Definition at line 71 of file test_weakset.py.

00071 
00072     def test_or(self):
00073         i = self.s.union(self.items2)
00074         self.assertEqual(self.s | set(self.items2), i)
00075         self.assertEqual(self.s | frozenset(self.items2), i)

Here is the call graph for this function:

Definition at line 213 of file test_weakset.py.

00213 
00214     def test_pop(self):
00215         for i in range(len(self.s)):
00216             elem = self.s.pop()
00217             self.assertNotIn(elem, self.s)
00218         self.assertRaises(KeyError, self.s.pop)

Here is the call graph for this function:

Definition at line 199 of file test_weakset.py.

00199 
00200     def test_remove(self):
00201         x = ustr('a')
00202         self.s.remove(x)
00203         self.assertNotIn(x, self.s)
00204         self.assertRaises(KeyError, self.s.remove, x)
00205         self.assertRaises(TypeError, self.s.remove, [])

Here is the call graph for this function:

Definition at line 103 of file test_weakset.py.

00103 
00104     def test_sub(self):
00105         i = self.s.difference(self.items2)
00106         self.assertEqual(self.s - set(self.items2), i)
00107         self.assertEqual(self.s - frozenset(self.items2), i)

Here is the call graph for this function:

Definition at line 121 of file test_weakset.py.

00121 
00122     def test_sub_and_super(self):
00123         pl, ql, rl = map(lambda s: [ustr(c) for c in s], ['ab', 'abcde', 'def'])
00124         p, q, r = map(WeakSet, (pl, ql, rl))
00125         self.assertTrue(p < q)
00126         self.assertTrue(p <= q)
00127         self.assertTrue(q <= q)
00128         self.assertTrue(q > p)
00129         self.assertTrue(q >= p)
00130         self.assertFalse(q < r)
00131         self.assertFalse(q <= r)
00132         self.assertFalse(q > r)
00133         self.assertFalse(q >= r)
00134         self.assertTrue(set('a').issubset('abc'))
00135         self.assertTrue(set('abc').issuperset('a'))
00136         self.assertFalse(set('a').issubset('cbs'))
00137         self.assertFalse(set('cbs').issuperset('a'))

Here is the call graph for this function:

Definition at line 146 of file test_weakset.py.

00146 
00147     def test_subclass_with_custom_hash(self):
00148         # Bug #1257731
00149         class H(WeakSet):
00150             def __hash__(self):
00151                 return int(id(self) & 0x7fffffff)
00152         s=H()
00153         f=set()
00154         f.add(s)
00155         self.assertIn(s, f)
00156         f.remove(s)
00157         f.add(s)
00158         f.discard(s)

Here is the call graph for this function:

Definition at line 108 of file test_weakset.py.

00108 
00109     def test_symmetric_difference(self):
00110         i = self.s.symmetric_difference(self.items2)
00111         for c in self.letters:
00112             self.assertEqual(c in i, (c in self.d) ^ (c in self.items2))
00113         self.assertEqual(self.s, WeakSet(self.items))
00114         self.assertEqual(type(i), WeakSet)
00115         self.assertRaises(TypeError, self.s.symmetric_difference, [[]])

Here is the call graph for this function:

Definition at line 273 of file test_weakset.py.

00273 
00274     def test_symmetric_difference_update(self):
00275         retval = self.s.symmetric_difference_update(self.items2)
00276         self.assertEqual(retval, None)
00277         for c in (self.items + self.items2):
00278             if (c in self.items) ^ (c in self.items2):
00279                 self.assertIn(c, self.s)
00280             else:
00281                 self.assertNotIn(c, self.s)
00282         self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])

Here is the call graph for this function:

Definition at line 59 of file test_weakset.py.

00059 
00060     def test_union(self):
00061         u = self.s.union(self.items2)
00062         for c in self.letters:
00063             self.assertEqual(c in u, c in self.d or c in self.items2)
00064         self.assertEqual(self.s, WeakSet(self.items))
00065         self.assertEqual(type(u), WeakSet)
00066         self.assertRaises(TypeError, self.s.union, [[]])
00067         for C in set, frozenset, dict.fromkeys, list, tuple:
00068             x = WeakSet(self.items + self.items2)
00069             c = C(self.items2)
00070             self.assertEqual(self.s.union(c), x)

Here is the call graph for this function:

Definition at line 219 of file test_weakset.py.

00219 
00220     def test_update(self):
00221         retval = self.s.update(self.items2)
00222         self.assertEqual(retval, None)
00223         for c in (self.items + self.items2):
00224             self.assertIn(c, self.s)
00225         self.assertRaises(TypeError, self.s.update, [[]])

Here is the call graph for this function:

Definition at line 226 of file test_weakset.py.

00226 
00227     def test_update_set(self):
00228         self.s.update(set(self.items2))
00229         for c in (self.items + self.items2):
00230             self.assertIn(c, self.s)

Here is the call graph for this function:

Definition at line 329 of file test_weakset.py.

00329 
00330     def test_weak_destroy_and_mutate_while_iterating(self):
00331         # Issue #7105: iterators shouldn't crash when a key is implicitly removed
00332         items = [ustr(c) for c in string.ascii_letters]
00333         s = WeakSet(items)
00334         @contextlib.contextmanager
00335         def testcontext():
00336             try:
00337                 it = iter(s)
00338                 next(it)
00339                 # Schedule an item for removal and recreate it
00340                 u = ustr(str(items.pop()))
00341                 gc.collect()      # just in case
00342                 yield u
00343             finally:
00344                 it = None           # should commit all removals
00345 
00346         with testcontext() as u:
00347             self.assertNotIn(u, s)
00348         with testcontext() as u:
00349             self.assertRaises(KeyError, s.remove, u)
00350         self.assertNotIn(u, s)
00351         with testcontext() as u:
00352             s.add(u)
00353         self.assertIn(u, s)
00354         t = s.copy()
00355         with testcontext() as u:
00356             s.update(t)
00357         self.assertEqual(len(s), len(t))
00358         with testcontext() as u:
00359             s.clear()
00360         self.assertEqual(len(s), 0)
00361 

Here is the call graph for this function:

Definition at line 313 of file test_weakset.py.

00313 
00314     def test_weak_destroy_while_iterating(self):
00315         # Issue #7105: iterators shouldn't crash when a key is implicitly removed
00316         # Create new items to be sure no-one else holds a reference
00317         items = [ustr(c) for c in ('a', 'b', 'c')]
00318         s = WeakSet(items)
00319         it = iter(s)
00320         next(it)             # Trigger internal iteration
00321         # Destroy an item
00322         del items[-1]
00323         gc.collect()    # just in case
00324         # We have removed either the first consumed items, or another one
00325         self.assertIn(len(list(it)), [len(items), len(items) - 1])
00326         del it
00327         # The removal has been committed
00328         self.assertEqual(len(s), len(items))

Here is the call graph for this function:

Definition at line 116 of file test_weakset.py.

00116 
00117     def test_xor(self):
00118         i = self.s.symmetric_difference(self.items2)
00119         self.assertEqual(self.s ^ set(self.items2), i)
00120         self.assertEqual(self.s ^ frozenset(self.items2), i)

Here is the call graph for this function:


Member Data Documentation

Definition at line 29 of file test_weakset.py.

Definition at line 31 of file test_weakset.py.

Definition at line 25 of file test_weakset.py.

Definition at line 26 of file test_weakset.py.

Definition at line 27 of file test_weakset.py.

Definition at line 30 of file test_weakset.py.

Definition at line 28 of file test_weakset.py.


The documentation for this class was generated from the following file: