Skip to content

Commit 7ffdb6d

Browse files
committed
gh-116738: Use invariant checks from heapq test
1 parent 3f09689 commit 7ffdb6d

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

Lib/test/test_free_threading/test_heapq.py

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import unittest
22

33
import heapq
4-
import operator
54

65
from enum import Enum
76
from threading import Thread, Barrier
87
from random import shuffle, randint
98

109
from test.support import threading_helper
10+
from test import test_heapq
1111

1212

1313
NTHREADS = 10
@@ -21,14 +21,17 @@ class Heap(Enum):
2121

2222
@threading_helper.requires_working_threading()
2323
class TestHeapq(unittest.TestCase):
24+
def setUp(self):
25+
self.test_heapq = test_heapq.TestHeapPython()
26+
2427
def test_racing_heapify(self):
2528
heap = list(range(OBJECT_COUNT))
2629
shuffle(heap)
2730

2831
self.run_concurrently(
2932
worker_func=heapq.heapify, args=(heap,), nthreads=NTHREADS
3033
)
31-
self.assertTrue(self.is_heap_property_satisfied(heap, Heap.MIN))
34+
self.test_heapq.check_invariant(heap)
3235

3336
def test_racing_heappush(self):
3437
heap = []
@@ -40,7 +43,7 @@ def heappush_func(heap):
4043
self.run_concurrently(
4144
worker_func=heappush_func, args=(heap,), nthreads=NTHREADS
4245
)
43-
self.assertTrue(self.is_heap_property_satisfied(heap, Heap.MIN))
46+
self.test_heapq.check_invariant(heap)
4447

4548
def test_racing_heappop(self):
4649
heap = self.create_heap(OBJECT_COUNT, Heap.MIN)
@@ -80,7 +83,7 @@ def heappushpop_func(heap, pushpop_items):
8083
nthreads=NTHREADS,
8184
)
8285
self.assertEqual(len(heap), OBJECT_COUNT)
83-
self.assertTrue(self.is_heap_property_satisfied(heap, Heap.MIN))
86+
self.test_heapq.check_invariant(heap)
8487

8588
def test_racing_heapreplace(self):
8689
heap = self.create_heap(OBJECT_COUNT, Heap.MIN)
@@ -96,7 +99,7 @@ def heapreplace_func(heap, replace_items):
9699
nthreads=NTHREADS,
97100
)
98101
self.assertEqual(len(heap), OBJECT_COUNT)
99-
self.assertTrue(self.is_heap_property_satisfied(heap, Heap.MIN))
102+
self.test_heapq.check_invariant(heap)
100103

101104
def test_racing_heapify_max(self):
102105
max_heap = list(range(OBJECT_COUNT))
@@ -105,7 +108,7 @@ def test_racing_heapify_max(self):
105108
self.run_concurrently(
106109
worker_func=heapq.heapify_max, args=(max_heap,), nthreads=NTHREADS
107110
)
108-
self.assertTrue(self.is_heap_property_satisfied(max_heap, Heap.MAX))
111+
self.test_heapq.check_max_invariant(max_heap)
109112

110113
def test_racing_heappush_max(self):
111114
max_heap = []
@@ -117,7 +120,7 @@ def heappush_max_func(max_heap):
117120
self.run_concurrently(
118121
worker_func=heappush_max_func, args=(max_heap,), nthreads=NTHREADS
119122
)
120-
self.assertTrue(self.is_heap_property_satisfied(max_heap, Heap.MAX))
123+
self.test_heapq.check_max_invariant(max_heap)
121124

122125
def test_racing_heappop_max(self):
123126
max_heap = self.create_heap(OBJECT_COUNT, Heap.MAX)
@@ -157,7 +160,7 @@ def heappushpop_max_func(max_heap, pushpop_items):
157160
nthreads=NTHREADS,
158161
)
159162
self.assertEqual(len(max_heap), OBJECT_COUNT)
160-
self.assertTrue(self.is_heap_property_satisfied(max_heap, Heap.MAX))
163+
self.test_heapq.check_max_invariant(max_heap)
161164

162165
def test_racing_heapreplace_max(self):
163166
max_heap = self.create_heap(OBJECT_COUNT, Heap.MAX)
@@ -173,25 +176,7 @@ def heapreplace_max_func(max_heap, replace_items):
173176
nthreads=NTHREADS,
174177
)
175178
self.assertEqual(len(max_heap), OBJECT_COUNT)
176-
self.assertTrue(self.is_heap_property_satisfied(max_heap, Heap.MAX))
177-
178-
@staticmethod
179-
def is_heap_property_satisfied(heap, heap_kind):
180-
"""
181-
Check if the heap property is satisfied.
182-
MIN-Heap: The value of a parent node should be less than or equal to the
183-
values of its children.
184-
MAX-Heap: The value of a parent node should be greater than or equal to the
185-
values of its children.
186-
"""
187-
op = operator.le if heap_kind == Heap.MIN else operator.ge
188-
# position 0 has no parent
189-
for pos in range(1, len(heap)):
190-
parent_pos = (pos - 1) >> 1
191-
if not op(heap[parent_pos], heap[pos]):
192-
return False
193-
194-
return True
179+
self.test_heapq.check_max_invariant(max_heap)
195180

196181
@staticmethod
197182
def is_sorted_ascending(lst):

0 commit comments

Comments
 (0)