Lines Matching refs:self
21 def test_py_functions(self):
23 self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq')
26 def test_c_functions(self):
28 self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
40 def find(self, *args, **kwargs):
50 def test_push_pop(self):
54 self.check_invariant(heap)
58 self.module.heappush(heap, item)
59 self.check_invariant(heap)
62 item = self.module.heappop(heap)
63 self.check_invariant(heap)
67 self.assertEqual(data_sorted, results)
69 self.check_invariant(results)
71 self.assertRaises(TypeError, self.module.heappush, [])
73 self.assertRaises(TypeError, self.module.heappush, None, None)
74 self.assertRaises(TypeError, self.module.heappop, None)
78 def check_invariant(self, heap):
83 self.assertTrue(heap[parentpos] <= item)
85 def test_heapify(self):
88 self.module.heapify(heap)
89 self.check_invariant(heap)
91 self.assertRaises(TypeError, self.module.heapify, None)
93 def test_naive_nbest(self):
97 self.module.heappush(heap, item)
99 self.module.heappop(heap)
101 self.assertEqual(heap, sorted(data)[-10:])
103 def heapiter(self, heap):
107 yield self.module.heappop(heap)
111 def test_nbest(self):
119 self.module.heapify(heap)
122 self.module.heapreplace(heap, item)
123 self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
125 self.assertRaises(TypeError, self.module.heapreplace, None)
126 self.assertRaises(TypeError, self.module.heapreplace, None, None)
127 self.assertRaises(IndexError, self.module.heapreplace, [], None)
129 def test_nbest_with_pushpop(self):
132 self.module.heapify(heap)
134 self.module.heappushpop(heap, item)
135 self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
136 self.assertEqual(self.module.heappushpop([], 'x'), 'x')
138 def test_heappushpop(self):
140 x = self.module.heappushpop(h, 10)
141 self.assertEqual((h, x), ([], 10))
144 x = self.module.heappushpop(h, 10.0)
145 self.assertEqual((h, x), ([10], 10.0))
146 self.assertEqual(type(h[0]), int)
147 self.assertEqual(type(x), float)
150 x = self.module.heappushpop(h, 9)
151 self.assertEqual((h, x), ([10], 9))
154 x = self.module.heappushpop(h, 11)
155 self.assertEqual((h, x), ([11], 10))
157 def test_heappop_max(self):
161 self.assertEqual(self.module._heappop_max(h), 3)
162 self.assertEqual(self.module._heappop_max(h), 2)
164 def test_heapsort(self):
171 self.module.heapify(heap)
175 self.module.heappush(heap, item)
176 heap_sorted = [self.module.heappop(heap) for i in range(size)]
177 self.assertEqual(heap_sorted, sorted(data))
179 def test_merge(self):
193 self.assertEqual(sorted(chain(*inputs), key=key, reverse=reverse),
194 list(self.module.merge(*seqs, key=key, reverse=reverse)))
195 self.assertEqual(list(self.module.merge()), [])
197 def test_empty_merges(self):
200 self.assertEqual(list(self.module.merge([], [])), [])
201 self.assertEqual(list(self.module.merge([], [], key=lambda: 6)), [])
203 def test_merge_does_not_suppress_index_error(self):
209 with self.assertRaises(IndexError):
210 list(self.module.merge(iterable(), iterable()))
212 def test_merge_stability(self):
224 result = [i.pair for i in self.module.merge(*inputs)]
225 self.assertEqual(result, sorted(result))
227 def test_nsmallest(self):
231 self.assertEqual(list(self.module.nsmallest(n, data)),
233 self.assertEqual(list(self.module.nsmallest(n, data, key=f)),
236 def test_nlargest(self):
240 self.assertEqual(list(self.module.nlargest(n, data)),
242 self.assertEqual(list(self.module.nlargest(n, data, key=f)),
245 def test_comparison_operator(self):
250 self.module.heapify(data)
251 return [self.module.heappop(data).x for i in range(len(data))]
253 def __init__(self, x):
254 self.x = x
255 def __lt__(self, other):
256 return self.x > other.x
258 def __init__(self, x):
259 self.x = x
260 def __le__(self, other):
261 return self.x >= other.x
264 self.assertEqual(hsort(data, LT), target)
265 self.assertRaises(TypeError, data, LE)
281 def __len__(self):
286 def __eq__(self, other):
297 def __init__(self, seqn):
298 self.seqn = seqn
299 def __getitem__(self, i):
300 return self.seqn[i]
304 def __init__(self, seqn):
305 self.seqn = seqn
306 self.i = 0
307 def __iter__(self):
308 return self
309 def __next__(self):
310 if self.i >= len(self.seqn): raise StopIteration
311 v = self.seqn[self.i]
312 self.i += 1
317 def __init__(self, seqn):
318 self.seqn = seqn
319 self.i = 0
320 def __iter__(self):
321 for val in self.seqn:
326 def __init__(self, seqn):
327 self.seqn = seqn
328 self.i = 0
329 def __next__(self):
330 if self.i >= len(self.seqn): raise StopIteration
331 v = self.seqn[self.i]
332 self.i += 1
337 def __init__(self, seqn):
338 self.seqn = seqn
339 self.i = 0
340 def __iter__(self):
341 return self
345 def __init__(self, seqn):
346 self.seqn = seqn
347 self.i = 0
348 def __iter__(self):
349 return self
350 def __next__(self):
355 def __init__(self, seqn):
357 def __iter__(self):
358 return self
359 def __next__(self):
369 def __init__(self, value, heap):
370 self.value = value
371 self.heap = heap
373 def __lt__(self, other):
374 self.heap[:] = []
375 return self.value < other.value
380 def test_non_sequence(self):
381 for f in (self.module.heapify, self.module.heappop):
382 self.assertRaises((TypeError, AttributeError), f, 10)
383 for f in (self.module.heappush, self.module.heapreplace,
384 self.module.nlargest, self.module.nsmallest):
385 self.assertRaises((TypeError, AttributeError), f, 10, 10)
387 def test_len_only(self):
388 for f in (self.module.heapify, self.module.heappop):
389 self.assertRaises((TypeError, AttributeError), f, LenOnly())
390 for f in (self.module.heappush, self.module.heapreplace):
391 self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10)
392 for f in (self.module.nlargest, self.module.nsmallest):
393 self.assertRaises(TypeError, f, 2, LenOnly())
395 def test_cmp_err(self):
397 for f in (self.module.heapify, self.module.heappop):
398 self.assertRaises(ZeroDivisionError, f, seq)
399 for f in (self.module.heappush, self.module.heapreplace):
400 self.assertRaises(ZeroDivisionError, f, seq, 10)
401 for f in (self.module.nlargest, self.module.nsmallest):
402 self.assertRaises(ZeroDivisionError, f, 2, seq)
404 def test_arg_parsing(self):
405 for f in (self.module.heapify, self.module.heappop,
406 self.module.heappush, self.module.heapreplace,
407 self.module.nlargest, self.module.nsmallest):
408 self.assertRaises((TypeError, AttributeError), f, 10)
410 def test_iterable_args(self):
411 for f in (self.module.nlargest, self.module.nsmallest):
414 self.assertEqual(list(f(2, g(s))), list(f(2,s)))
415 self.assertEqual(list(f(2, S(s))), [])
416 self.assertRaises(TypeError, f, 2, X(s))
417 self.assertRaises(TypeError, f, 2, N(s))
418 self.assertRaises(ZeroDivisionError, f, 2, E(s))
422 def test_heappush_mutating_heap(self):
426 with self.assertRaises((IndexError, RuntimeError)):
427 self.module.heappush(heap, SideEffectLT(5, heap))
429 def test_heappop_mutating_heap(self):
433 with self.assertRaises((IndexError, RuntimeError)):
434 self.module.heappop(heap)
436 def test_comparison_operator_modifiying_heap(self):
440 def __lt__(self, o):
445 self.module.heappush(heap, EvilClass(0))
446 self.assertRaises(IndexError, self.module.heappushpop, heap, 1)
448 def test_comparison_operator_modifiying_heap_two_heaps(self):
451 def __lt__(self, o):
456 def __lt__(self, o):
462 self.module.heappush(list1, h(0))
463 self.module.heappush(list2, g(0))
465 self.assertRaises((IndexError, RuntimeError), self.module.heappush, list1, g(1))
466 self.assertRaises((IndexError, RuntimeError), self.module.heappush, list2, h(1))