ยปCore Development>Code coverage>Lib/test/test_weakset.py

Python code coverage for Lib/test/test_weakset.py

#countcontent
1n/aimport unittest
2n/afrom weakref import WeakSet
3n/aimport string
4n/afrom collections import UserString as ustr
5n/aimport gc
6n/aimport contextlib
7n/a
8n/a
9n/aclass Foo:
10n/a pass
11n/a
12n/aclass RefCycle:
13n/a def __init__(self):
14n/a self.cycle = self
15n/a
16n/a
17n/aclass TestWeakSet(unittest.TestCase):
18n/a
19n/a def setUp(self):
20n/a # need to keep references to them
21n/a self.items = [ustr(c) for c in ('a', 'b', 'c')]
22n/a self.items2 = [ustr(c) for c in ('x', 'y', 'z')]
23n/a self.ab_items = [ustr(c) for c in 'ab']
24n/a self.abcde_items = [ustr(c) for c in 'abcde']
25n/a self.def_items = [ustr(c) for c in 'def']
26n/a self.ab_weakset = WeakSet(self.ab_items)
27n/a self.abcde_weakset = WeakSet(self.abcde_items)
28n/a self.def_weakset = WeakSet(self.def_items)
29n/a self.letters = [ustr(c) for c in string.ascii_letters]
30n/a self.s = WeakSet(self.items)
31n/a self.d = dict.fromkeys(self.items)
32n/a self.obj = ustr('F')
33n/a self.fs = WeakSet([self.obj])
34n/a
35n/a def test_methods(self):
36n/a weaksetmethods = dir(WeakSet)
37n/a for method in dir(set):
38n/a if method == 'test_c_api' or method.startswith('_'):
39n/a continue
40n/a self.assertIn(method, weaksetmethods,
41n/a "WeakSet missing method " + method)
42n/a
43n/a def test_new_or_init(self):
44n/a self.assertRaises(TypeError, WeakSet, [], 2)
45n/a
46n/a def test_len(self):
47n/a self.assertEqual(len(self.s), len(self.d))
48n/a self.assertEqual(len(self.fs), 1)
49n/a del self.obj
50n/a self.assertEqual(len(self.fs), 0)
51n/a
52n/a def test_contains(self):
53n/a for c in self.letters:
54n/a self.assertEqual(c in self.s, c in self.d)
55n/a # 1 is not weakref'able, but that TypeError is caught by __contains__
56n/a self.assertNotIn(1, self.s)
57n/a self.assertIn(self.obj, self.fs)
58n/a del self.obj
59n/a self.assertNotIn(ustr('F'), self.fs)
60n/a
61n/a def test_union(self):
62n/a u = self.s.union(self.items2)
63n/a for c in self.letters:
64n/a self.assertEqual(c in u, c in self.d or c in self.items2)
65n/a self.assertEqual(self.s, WeakSet(self.items))
66n/a self.assertEqual(type(u), WeakSet)
67n/a self.assertRaises(TypeError, self.s.union, [[]])
68n/a for C in set, frozenset, dict.fromkeys, list, tuple:
69n/a x = WeakSet(self.items + self.items2)
70n/a c = C(self.items2)
71n/a self.assertEqual(self.s.union(c), x)
72n/a del c
73n/a self.assertEqual(len(u), len(self.items) + len(self.items2))
74n/a self.items2.pop()
75n/a gc.collect()
76n/a self.assertEqual(len(u), len(self.items) + len(self.items2))
77n/a
78n/a def test_or(self):
79n/a i = self.s.union(self.items2)
80n/a self.assertEqual(self.s | set(self.items2), i)
81n/a self.assertEqual(self.s | frozenset(self.items2), i)
82n/a
83n/a def test_intersection(self):
84n/a s = WeakSet(self.letters)
85n/a i = s.intersection(self.items2)
86n/a for c in self.letters:
87n/a self.assertEqual(c in i, c in self.items2 and c in self.letters)
88n/a self.assertEqual(s, WeakSet(self.letters))
89n/a self.assertEqual(type(i), WeakSet)
90n/a for C in set, frozenset, dict.fromkeys, list, tuple:
91n/a x = WeakSet([])
92n/a self.assertEqual(i.intersection(C(self.items)), x)
93n/a self.assertEqual(len(i), len(self.items2))
94n/a self.items2.pop()
95n/a gc.collect()
96n/a self.assertEqual(len(i), len(self.items2))
97n/a
98n/a def test_isdisjoint(self):
99n/a self.assertTrue(self.s.isdisjoint(WeakSet(self.items2)))
100n/a self.assertTrue(not self.s.isdisjoint(WeakSet(self.letters)))
101n/a
102n/a def test_and(self):
103n/a i = self.s.intersection(self.items2)
104n/a self.assertEqual(self.s & set(self.items2), i)
105n/a self.assertEqual(self.s & frozenset(self.items2), i)
106n/a
107n/a def test_difference(self):
108n/a i = self.s.difference(self.items2)
109n/a for c in self.letters:
110n/a self.assertEqual(c in i, c in self.d and c not in self.items2)
111n/a self.assertEqual(self.s, WeakSet(self.items))
112n/a self.assertEqual(type(i), WeakSet)
113n/a self.assertRaises(TypeError, self.s.difference, [[]])
114n/a
115n/a def test_sub(self):
116n/a i = self.s.difference(self.items2)
117n/a self.assertEqual(self.s - set(self.items2), i)
118n/a self.assertEqual(self.s - frozenset(self.items2), i)
119n/a
120n/a def test_symmetric_difference(self):
121n/a i = self.s.symmetric_difference(self.items2)
122n/a for c in self.letters:
123n/a self.assertEqual(c in i, (c in self.d) ^ (c in self.items2))
124n/a self.assertEqual(self.s, WeakSet(self.items))
125n/a self.assertEqual(type(i), WeakSet)
126n/a self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
127n/a self.assertEqual(len(i), len(self.items) + len(self.items2))
128n/a self.items2.pop()
129n/a gc.collect()
130n/a self.assertEqual(len(i), len(self.items) + len(self.items2))
131n/a
132n/a def test_xor(self):
133n/a i = self.s.symmetric_difference(self.items2)
134n/a self.assertEqual(self.s ^ set(self.items2), i)
135n/a self.assertEqual(self.s ^ frozenset(self.items2), i)
136n/a
137n/a def test_sub_and_super(self):
138n/a self.assertTrue(self.ab_weakset <= self.abcde_weakset)
139n/a self.assertTrue(self.abcde_weakset <= self.abcde_weakset)
140n/a self.assertTrue(self.abcde_weakset >= self.ab_weakset)
141n/a self.assertFalse(self.abcde_weakset <= self.def_weakset)
142n/a self.assertFalse(self.abcde_weakset >= self.def_weakset)
143n/a self.assertTrue(set('a').issubset('abc'))
144n/a self.assertTrue(set('abc').issuperset('a'))
145n/a self.assertFalse(set('a').issubset('cbs'))
146n/a self.assertFalse(set('cbs').issuperset('a'))
147n/a
148n/a def test_lt(self):
149n/a self.assertTrue(self.ab_weakset < self.abcde_weakset)
150n/a self.assertFalse(self.abcde_weakset < self.def_weakset)
151n/a self.assertFalse(self.ab_weakset < self.ab_weakset)
152n/a self.assertFalse(WeakSet() < WeakSet())
153n/a
154n/a def test_gt(self):
155n/a self.assertTrue(self.abcde_weakset > self.ab_weakset)
156n/a self.assertFalse(self.abcde_weakset > self.def_weakset)
157n/a self.assertFalse(self.ab_weakset > self.ab_weakset)
158n/a self.assertFalse(WeakSet() > WeakSet())
159n/a
160n/a def test_gc(self):
161n/a # Create a nest of cycles to exercise overall ref count check
162n/a s = WeakSet(Foo() for i in range(1000))
163n/a for elem in s:
164n/a elem.cycle = s
165n/a elem.sub = elem
166n/a elem.set = WeakSet([elem])
167n/a
168n/a def test_subclass_with_custom_hash(self):
169n/a # Bug #1257731
170n/a class H(WeakSet):
171n/a def __hash__(self):
172n/a return int(id(self) & 0x7fffffff)
173n/a s=H()
174n/a f=set()
175n/a f.add(s)
176n/a self.assertIn(s, f)
177n/a f.remove(s)
178n/a f.add(s)
179n/a f.discard(s)
180n/a
181n/a def test_init(self):
182n/a s = WeakSet()
183n/a s.__init__(self.items)
184n/a self.assertEqual(s, self.s)
185n/a s.__init__(self.items2)
186n/a self.assertEqual(s, WeakSet(self.items2))
187n/a self.assertRaises(TypeError, s.__init__, s, 2);
188n/a self.assertRaises(TypeError, s.__init__, 1);
189n/a
190n/a def test_constructor_identity(self):
191n/a s = WeakSet(self.items)
192n/a t = WeakSet(s)
193n/a self.assertNotEqual(id(s), id(t))
194n/a
195n/a def test_hash(self):
196n/a self.assertRaises(TypeError, hash, self.s)
197n/a
198n/a def test_clear(self):
199n/a self.s.clear()
200n/a self.assertEqual(self.s, WeakSet([]))
201n/a self.assertEqual(len(self.s), 0)
202n/a
203n/a def test_copy(self):
204n/a dup = self.s.copy()
205n/a self.assertEqual(self.s, dup)
206n/a self.assertNotEqual(id(self.s), id(dup))
207n/a
208n/a def test_add(self):
209n/a x = ustr('Q')
210n/a self.s.add(x)
211n/a self.assertIn(x, self.s)
212n/a dup = self.s.copy()
213n/a self.s.add(x)
214n/a self.assertEqual(self.s, dup)
215n/a self.assertRaises(TypeError, self.s.add, [])
216n/a self.fs.add(Foo())
217n/a self.assertTrue(len(self.fs) == 1)
218n/a self.fs.add(self.obj)
219n/a self.assertTrue(len(self.fs) == 1)
220n/a
221n/a def test_remove(self):
222n/a x = ustr('a')
223n/a self.s.remove(x)
224n/a self.assertNotIn(x, self.s)
225n/a self.assertRaises(KeyError, self.s.remove, x)
226n/a self.assertRaises(TypeError, self.s.remove, [])
227n/a
228n/a def test_discard(self):
229n/a a, q = ustr('a'), ustr('Q')
230n/a self.s.discard(a)
231n/a self.assertNotIn(a, self.s)
232n/a self.s.discard(q)
233n/a self.assertRaises(TypeError, self.s.discard, [])
234n/a
235n/a def test_pop(self):
236n/a for i in range(len(self.s)):
237n/a elem = self.s.pop()
238n/a self.assertNotIn(elem, self.s)
239n/a self.assertRaises(KeyError, self.s.pop)
240n/a
241n/a def test_update(self):
242n/a retval = self.s.update(self.items2)
243n/a self.assertEqual(retval, None)
244n/a for c in (self.items + self.items2):
245n/a self.assertIn(c, self.s)
246n/a self.assertRaises(TypeError, self.s.update, [[]])
247n/a
248n/a def test_update_set(self):
249n/a self.s.update(set(self.items2))
250n/a for c in (self.items + self.items2):
251n/a self.assertIn(c, self.s)
252n/a
253n/a def test_ior(self):
254n/a self.s |= set(self.items2)
255n/a for c in (self.items + self.items2):
256n/a self.assertIn(c, self.s)
257n/a
258n/a def test_intersection_update(self):
259n/a retval = self.s.intersection_update(self.items2)
260n/a self.assertEqual(retval, None)
261n/a for c in (self.items + self.items2):
262n/a if c in self.items2 and c in self.items:
263n/a self.assertIn(c, self.s)
264n/a else:
265n/a self.assertNotIn(c, self.s)
266n/a self.assertRaises(TypeError, self.s.intersection_update, [[]])
267n/a
268n/a def test_iand(self):
269n/a self.s &= set(self.items2)
270n/a for c in (self.items + self.items2):
271n/a if c in self.items2 and c in self.items:
272n/a self.assertIn(c, self.s)
273n/a else:
274n/a self.assertNotIn(c, self.s)
275n/a
276n/a def test_difference_update(self):
277n/a retval = self.s.difference_update(self.items2)
278n/a self.assertEqual(retval, None)
279n/a for c in (self.items + self.items2):
280n/a if c in self.items and c not in self.items2:
281n/a self.assertIn(c, self.s)
282n/a else:
283n/a self.assertNotIn(c, self.s)
284n/a self.assertRaises(TypeError, self.s.difference_update, [[]])
285n/a self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
286n/a
287n/a def test_isub(self):
288n/a self.s -= set(self.items2)
289n/a for c in (self.items + self.items2):
290n/a if c in self.items and c not in self.items2:
291n/a self.assertIn(c, self.s)
292n/a else:
293n/a self.assertNotIn(c, self.s)
294n/a
295n/a def test_symmetric_difference_update(self):
296n/a retval = self.s.symmetric_difference_update(self.items2)
297n/a self.assertEqual(retval, None)
298n/a for c in (self.items + self.items2):
299n/a if (c in self.items) ^ (c in self.items2):
300n/a self.assertIn(c, self.s)
301n/a else:
302n/a self.assertNotIn(c, self.s)
303n/a self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
304n/a
305n/a def test_ixor(self):
306n/a self.s ^= set(self.items2)
307n/a for c in (self.items + self.items2):
308n/a if (c in self.items) ^ (c in self.items2):
309n/a self.assertIn(c, self.s)
310n/a else:
311n/a self.assertNotIn(c, self.s)
312n/a
313n/a def test_inplace_on_self(self):
314n/a t = self.s.copy()
315n/a t |= t
316n/a self.assertEqual(t, self.s)
317n/a t &= t
318n/a self.assertEqual(t, self.s)
319n/a t -= t
320n/a self.assertEqual(t, WeakSet())
321n/a t = self.s.copy()
322n/a t ^= t
323n/a self.assertEqual(t, WeakSet())
324n/a
325n/a def test_eq(self):
326n/a # issue 5964
327n/a self.assertTrue(self.s == self.s)
328n/a self.assertTrue(self.s == WeakSet(self.items))
329n/a self.assertFalse(self.s == set(self.items))
330n/a self.assertFalse(self.s == list(self.items))
331n/a self.assertFalse(self.s == tuple(self.items))
332n/a self.assertFalse(self.s == WeakSet([Foo]))
333n/a self.assertFalse(self.s == 1)
334n/a
335n/a def test_ne(self):
336n/a self.assertTrue(self.s != set(self.items))
337n/a s1 = WeakSet()
338n/a s2 = WeakSet()
339n/a self.assertFalse(s1 != s2)
340n/a
341n/a def test_weak_destroy_while_iterating(self):
342n/a # Issue #7105: iterators shouldn't crash when a key is implicitly removed
343n/a # Create new items to be sure no-one else holds a reference
344n/a items = [ustr(c) for c in ('a', 'b', 'c')]
345n/a s = WeakSet(items)
346n/a it = iter(s)
347n/a next(it) # Trigger internal iteration
348n/a # Destroy an item
349n/a del items[-1]
350n/a gc.collect() # just in case
351n/a # We have removed either the first consumed items, or another one
352n/a self.assertIn(len(list(it)), [len(items), len(items) - 1])
353n/a del it
354n/a # The removal has been committed
355n/a self.assertEqual(len(s), len(items))
356n/a
357n/a def test_weak_destroy_and_mutate_while_iterating(self):
358n/a # Issue #7105: iterators shouldn't crash when a key is implicitly removed
359n/a items = [ustr(c) for c in string.ascii_letters]
360n/a s = WeakSet(items)
361n/a @contextlib.contextmanager
362n/a def testcontext():
363n/a try:
364n/a it = iter(s)
365n/a # Start iterator
366n/a yielded = ustr(str(next(it)))
367n/a # Schedule an item for removal and recreate it
368n/a u = ustr(str(items.pop()))
369n/a if yielded == u:
370n/a # The iterator still has a reference to the removed item,
371n/a # advance it (issue #20006).
372n/a next(it)
373n/a gc.collect() # just in case
374n/a yield u
375n/a finally:
376n/a it = None # should commit all removals
377n/a
378n/a with testcontext() as u:
379n/a self.assertNotIn(u, s)
380n/a with testcontext() as u:
381n/a self.assertRaises(KeyError, s.remove, u)
382n/a self.assertNotIn(u, s)
383n/a with testcontext() as u:
384n/a s.add(u)
385n/a self.assertIn(u, s)
386n/a t = s.copy()
387n/a with testcontext() as u:
388n/a s.update(t)
389n/a self.assertEqual(len(s), len(t))
390n/a with testcontext() as u:
391n/a s.clear()
392n/a self.assertEqual(len(s), 0)
393n/a
394n/a def test_len_cycles(self):
395n/a N = 20
396n/a items = [RefCycle() for i in range(N)]
397n/a s = WeakSet(items)
398n/a del items
399n/a it = iter(s)
400n/a try:
401n/a next(it)
402n/a except StopIteration:
403n/a pass
404n/a gc.collect()
405n/a n1 = len(s)
406n/a del it
407n/a gc.collect()
408n/a n2 = len(s)
409n/a # one item may be kept alive inside the iterator
410n/a self.assertIn(n1, (0, 1))
411n/a self.assertEqual(n2, 0)
412n/a
413n/a def test_len_race(self):
414n/a # Extended sanity checks for len() in the face of cyclic collection
415n/a self.addCleanup(gc.set_threshold, *gc.get_threshold())
416n/a for th in range(1, 100):
417n/a N = 20
418n/a gc.collect(0)
419n/a gc.set_threshold(th, th, th)
420n/a items = [RefCycle() for i in range(N)]
421n/a s = WeakSet(items)
422n/a del items
423n/a # All items will be collected at next garbage collection pass
424n/a it = iter(s)
425n/a try:
426n/a next(it)
427n/a except StopIteration:
428n/a pass
429n/a n1 = len(s)
430n/a del it
431n/a n2 = len(s)
432n/a self.assertGreaterEqual(n1, 0)
433n/a self.assertLessEqual(n1, N)
434n/a self.assertGreaterEqual(n2, 0)
435n/a self.assertLessEqual(n2, n1)
436n/a
437n/a
438n/aif __name__ == "__main__":
439n/a unittest.main()