Skip to content

Commit 31e1e3d

Browse files
Test cases and Python methos for nlargest/nsmallest
1 parent 8aaddc8 commit 31e1e3d

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lib/heapq.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,22 @@ def replace(self, item):
644644
"""
645645
return heapreplace(self._queue, item)
646646

647+
def nsmallest(self, n, key=None):
648+
"""Return a list of the n smallest items, smallest first.
649+
650+
The heap is left unchanged. If *key* is given, it is applied to
651+
each item to determine the ordering.
652+
"""
653+
return nsmallest(n, self._queue, key=key)
654+
655+
def nlargest(self, n, key=None):
656+
"""Return a list of the n largest items, largest first.
657+
658+
The heap is left unchanged. If *key* is given, it is applied to
659+
each item to determine the ordering.
660+
"""
661+
return nlargest(n, self._queue, key=key)
662+
647663
def __len__(self):
648664
return len(self._queue)
649665

@@ -704,6 +720,22 @@ def replace(self, item):
704720
"""
705721
return heapreplace_max(self._queue, item)
706722

723+
def nsmallest(self, n, key=None):
724+
"""Return a list of the n smallest items, smallest first.
725+
726+
The heap is left unchanged. If *key* is given, it is applied to
727+
each item to determine the ordering.
728+
"""
729+
return nsmallest(n, self._queue, key=key)
730+
731+
def nlargest(self, n, key=None):
732+
"""Return a list of the n largest items, largest first.
733+
734+
The heap is left unchanged. If *key* is given, it is applied to
735+
each item to determine the ordering.
736+
"""
737+
return nlargest(n, self._queue, key=key)
738+
707739
def __len__(self):
708740
return len(self._queue)
709741

Lib/test/test_heapq.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,26 @@ def test_replace(self):
715715
self.assertEqual(h.replace(new_item), expected)
716716
self.assertEqual(len(h), len(self.ordered))
717717

718+
def test_nsmallest(self):
719+
data = [random.randrange(1000) for _ in range(100)]
720+
h = self.cls(data)
721+
self.assertEqual(h.nsmallest(10), sorted(data)[:10])
722+
# the heap is not modified
723+
self.assertEqual(len(h), len(data))
724+
725+
def test_nlargest(self):
726+
data = [random.randrange(1000) for _ in range(100)]
727+
h = self.cls(data)
728+
self.assertEqual(h.nlargest(10), sorted(data, reverse=True)[:10])
729+
# the heap is not modified
730+
self.assertEqual(len(h), len(data))
731+
732+
def test_nsmallest_nlargest_with_key(self):
733+
data = ['a', 'ee', 'ddd', 'bbbb', 'ccccc']
734+
h = self.cls(data)
735+
self.assertEqual(h.nsmallest(2, key=len), ['a', 'ee'])
736+
self.assertEqual(h.nlargest(2, key=len), ['ccccc', 'bbbb'])
737+
718738
def test_repr(self):
719739
h = self.cls(self.ordered)
720740
self.assertEqual(repr(h), f'{self.cls.__name__}({self.ordered!r})')
@@ -791,6 +811,18 @@ def test_replace_delegates(self):
791811
h.replace(42)
792812
m.assert_called_once_with(h._queue, 42)
793813

814+
def test_nsmallest_delegates(self):
815+
h = self.cls([1, 2, 3])
816+
with mock.patch.object(py_heapq, 'nsmallest') as m:
817+
h.nsmallest(2, key=str)
818+
m.assert_called_once_with(2, h._queue, key=str)
819+
820+
def test_nlargest_delegates(self):
821+
h = self.cls([1, 2, 3])
822+
with mock.patch.object(py_heapq, 'nlargest') as m:
823+
h.nlargest(2, key=str)
824+
m.assert_called_once_with(2, h._queue, key=str)
825+
794826

795827
class TestMinHeapDelegation(HeapClassDelegationTest, TestCase):
796828
cls = py_heapq.MinHeap

0 commit comments

Comments
 (0)