Skip to content

Commit 0465a5f

Browse files
authored
test: cover insertion sort stability with equal-priority items (#15480)
* test: cover insertion sort stability with equal-priority items * test: simplify insertion sort stability doctest output * test: verify insertion sort ignores labels when ordering
1 parent 29f9da7 commit 0465a5f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎sorts/insertion_sort.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
5656
True
5757
>>> values
5858
[1, 2, 4, 7]
59+
60+
Equal-priority items retain their original relative order:
61+
>>> from dataclasses import dataclass, field
62+
>>> @dataclass(order=True)
63+
... class Item:
64+
... priority: int
65+
... label: str = field(compare=False)
66+
>>> items = [Item(2, "A"), Item(1, "X"), Item(2, "B"), Item(1, "Y")]
67+
>>> insertion_sort(items) # doctest: +NORMALIZE_WHITESPACE
68+
[Item(priority=1, label='X'), Item(priority=1, label='Y'),
69+
Item(priority=2, label='A'), Item(priority=2, label='B')]
70+
>>> other = [Item(2, "B"), Item(1, "Y"), Item(2, "A"), Item(1, "X")]
71+
>>> insertion_sort(other) # doctest: +NORMALIZE_WHITESPACE
72+
[Item(priority=1, label='Y'), Item(priority=1, label='X'),
73+
Item(priority=2, label='B'), Item(priority=2, label='A')]
74+
5975
>>> import random
6076
>>> collection = random.sample(range(-50, 50), 100)
6177
>>> insertion_sort(collection) == sorted(collection)

0 commit comments

Comments
 (0)