10
10
from test .support import threading_helper
11
11
12
12
13
- NTHREADS : int = 10
14
- OBJECT_COUNT : int = 5_000
13
+ NTHREADS = 10
14
+ OBJECT_COUNT = 5_000
15
15
16
16
17
- class HeapKind (Enum ):
17
+ class Heap (Enum ):
18
18
MIN = 1
19
19
MAX = 2
20
20
@@ -28,28 +28,28 @@ def test_racing_heapify(self):
28
28
self .run_concurrently (
29
29
worker_func = heapq .heapify , args = (heap ,), nthreads = NTHREADS
30
30
)
31
- self .assertTrue (self .is_min_heap_property_satisfied (heap ))
31
+ self .assertTrue (self .is_heap_property_satisfied (heap , Heap . MIN ))
32
32
33
33
def test_racing_heappush (self ):
34
34
heap = []
35
35
36
- def heappush_func (heap : list [ int ] ):
36
+ def heappush_func (heap ):
37
37
for item in reversed (range (OBJECT_COUNT )):
38
38
heapq .heappush (heap , item )
39
39
40
40
self .run_concurrently (
41
41
worker_func = heappush_func , args = (heap ,), nthreads = NTHREADS
42
42
)
43
- self .assertTrue (self .is_min_heap_property_satisfied (heap ))
43
+ self .assertTrue (self .is_heap_property_satisfied (heap , Heap . MIN ))
44
44
45
45
def test_racing_heappop (self ):
46
- heap = self .create_heap (OBJECT_COUNT , HeapKind .MIN )
46
+ heap = self .create_heap (OBJECT_COUNT , Heap .MIN )
47
47
48
48
# Each thread pops (OBJECT_COUNT / NTHREADS) items
49
49
self .assertEqual (OBJECT_COUNT % NTHREADS , 0 )
50
50
per_thread_pop_count = OBJECT_COUNT // NTHREADS
51
51
52
- def heappop_func (heap : list [ int ] , pop_count : int ):
52
+ def heappop_func (heap , pop_count ):
53
53
local_list = []
54
54
for _ in range (pop_count ):
55
55
item = heapq .heappop (heap )
@@ -66,10 +66,10 @@ def heappop_func(heap: list[int], pop_count: int):
66
66
self .assertEqual (len (heap ), 0 )
67
67
68
68
def test_racing_heappushpop (self ):
69
- heap = self .create_heap (OBJECT_COUNT , HeapKind .MIN )
69
+ heap = self .create_heap (OBJECT_COUNT , Heap .MIN )
70
70
pushpop_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
71
71
72
- def heappushpop_func (heap : list [ int ] , pushpop_items : list [ int ] ):
72
+ def heappushpop_func (heap , pushpop_items ):
73
73
for item in pushpop_items :
74
74
popped_item = heapq .heappushpop (heap , item )
75
75
self .assertTrue (popped_item <= item )
@@ -80,13 +80,13 @@ def heappushpop_func(heap: list[int], pushpop_items: list[int]):
80
80
nthreads = NTHREADS ,
81
81
)
82
82
self .assertEqual (len (heap ), OBJECT_COUNT )
83
- self .assertTrue (self .is_min_heap_property_satisfied (heap ))
83
+ self .assertTrue (self .is_heap_property_satisfied (heap , Heap . MIN ))
84
84
85
85
def test_racing_heapreplace (self ):
86
- heap = self .create_heap (OBJECT_COUNT , HeapKind .MIN )
86
+ heap = self .create_heap (OBJECT_COUNT , Heap .MIN )
87
87
replace_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
88
88
89
- def heapreplace_func (heap : list [ int ] , replace_items : list [ int ] ):
89
+ def heapreplace_func (heap , replace_items ):
90
90
for item in replace_items :
91
91
popped_item = heapq .heapreplace (heap , item )
92
92
@@ -96,7 +96,7 @@ def heapreplace_func(heap: list[int], replace_items: list[int]):
96
96
nthreads = NTHREADS ,
97
97
)
98
98
self .assertEqual (len (heap ), OBJECT_COUNT )
99
- self .assertTrue (self .is_min_heap_property_satisfied (heap ))
99
+ self .assertTrue (self .is_heap_property_satisfied (heap , Heap . MIN ))
100
100
101
101
def test_racing_heapify_max (self ):
102
102
max_heap = list (range (OBJECT_COUNT ))
@@ -105,28 +105,28 @@ def test_racing_heapify_max(self):
105
105
self .run_concurrently (
106
106
worker_func = heapq .heapify_max , args = (max_heap ,), nthreads = NTHREADS
107
107
)
108
- self .assertTrue (self .is_max_heap_property_satisfied (max_heap ))
108
+ self .assertTrue (self .is_heap_property_satisfied (max_heap , Heap . MAX ))
109
109
110
110
def test_racing_heappush_max (self ):
111
111
max_heap = []
112
112
113
- def heappush_max_func (max_heap : list [ int ] ):
113
+ def heappush_max_func (max_heap ):
114
114
for item in range (OBJECT_COUNT ):
115
115
heapq .heappush_max (max_heap , item )
116
116
117
117
self .run_concurrently (
118
118
worker_func = heappush_max_func , args = (max_heap ,), nthreads = NTHREADS
119
119
)
120
- self .assertTrue (self .is_max_heap_property_satisfied (max_heap ))
120
+ self .assertTrue (self .is_heap_property_satisfied (max_heap , Heap . MAX ))
121
121
122
122
def test_racing_heappop_max (self ):
123
- max_heap = self .create_heap (OBJECT_COUNT , HeapKind .MAX )
123
+ max_heap = self .create_heap (OBJECT_COUNT , Heap .MAX )
124
124
125
125
# Each thread pops (OBJECT_COUNT / NTHREADS) items
126
126
self .assertEqual (OBJECT_COUNT % NTHREADS , 0 )
127
127
per_thread_pop_count = OBJECT_COUNT // NTHREADS
128
128
129
- def heappop_max_func (max_heap : list [ int ] , pop_count : int ):
129
+ def heappop_max_func (max_heap , pop_count ):
130
130
local_list = []
131
131
for _ in range (pop_count ):
132
132
item = heapq .heappop_max (max_heap )
@@ -143,12 +143,10 @@ def heappop_max_func(max_heap: list[int], pop_count: int):
143
143
self .assertEqual (len (max_heap ), 0 )
144
144
145
145
def test_racing_heappushpop_max (self ):
146
- max_heap = self .create_heap (OBJECT_COUNT , HeapKind .MAX )
146
+ max_heap = self .create_heap (OBJECT_COUNT , Heap .MAX )
147
147
pushpop_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
148
148
149
- def heappushpop_max_func (
150
- max_heap : list [int ], pushpop_items : list [int ]
151
- ):
149
+ def heappushpop_max_func (max_heap , pushpop_items ):
152
150
for item in pushpop_items :
153
151
popped_item = heapq .heappushpop_max (max_heap , item )
154
152
self .assertTrue (popped_item >= item )
@@ -159,15 +157,13 @@ def heappushpop_max_func(
159
157
nthreads = NTHREADS ,
160
158
)
161
159
self .assertEqual (len (max_heap ), OBJECT_COUNT )
162
- self .assertTrue (self .is_max_heap_property_satisfied (max_heap ))
160
+ self .assertTrue (self .is_heap_property_satisfied (max_heap , Heap . MAX ))
163
161
164
162
def test_racing_heapreplace_max (self ):
165
- max_heap = self .create_heap (OBJECT_COUNT , HeapKind .MAX )
163
+ max_heap = self .create_heap (OBJECT_COUNT , Heap .MAX )
166
164
replace_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
167
165
168
- def heapreplace_max_func (
169
- max_heap : list [int ], replace_items : list [int ]
170
- ):
166
+ def heapreplace_max_func (max_heap , replace_items ):
171
167
for item in replace_items :
172
168
popped_item = heapq .heapreplace_max (max_heap , item )
173
169
@@ -177,30 +173,18 @@ def heapreplace_max_func(
177
173
nthreads = NTHREADS ,
178
174
)
179
175
self .assertEqual (len (max_heap ), OBJECT_COUNT )
180
- self .assertTrue (self .is_max_heap_property_satisfied (max_heap ))
181
-
182
- def is_min_heap_property_satisfied (self , heap : list [object ]) -> bool :
183
- """
184
- The value of a parent node should be less than or equal to the
185
- values of its children.
186
- """
187
- return self .is_heap_property_satisfied (heap , HeapKind .MIN )
188
-
189
- def is_max_heap_property_satisfied (self , heap : list [object ]) -> bool :
190
- """
191
- The value of a parent node should be greater than or equal to the
192
- values of its children.
193
- """
194
- return self .is_heap_property_satisfied (heap , HeapKind .MAX )
176
+ self .assertTrue (self .is_heap_property_satisfied (max_heap , Heap .MAX ))
195
177
196
178
@staticmethod
197
- def is_heap_property_satisfied (
198
- heap : list [object ], heap_kind : HeapKind
199
- ) -> bool :
179
+ def is_heap_property_satisfied (heap , heap_kind ):
200
180
"""
201
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.
202
186
"""
203
- op = operator .le if heap_kind == HeapKind .MIN else operator .ge
187
+ op = operator .le if heap_kind == Heap .MIN else operator .ge
204
188
# position 0 has no parent
205
189
for pos in range (1 , len (heap )):
206
190
parent_pos = (pos - 1 ) >> 1
@@ -210,36 +194,36 @@ def is_heap_property_satisfied(
210
194
return True
211
195
212
196
@staticmethod
213
- def is_sorted_ascending (lst : list [ object ]) -> bool :
197
+ def is_sorted_ascending (lst ) :
214
198
"""
215
199
Check if the list is sorted in ascending order (non-decreasing).
216
200
"""
217
201
return all (lst [i - 1 ] <= lst [i ] for i in range (1 , len (lst )))
218
202
219
203
@staticmethod
220
- def is_sorted_descending (lst : list [ object ]) -> bool :
204
+ def is_sorted_descending (lst ) :
221
205
"""
222
206
Check if the list is sorted in descending order (non-increasing).
223
207
"""
224
208
return all (lst [i - 1 ] >= lst [i ] for i in range (1 , len (lst )))
225
209
226
210
@staticmethod
227
- def create_heap (size : int , heap_kind : HeapKind ) -> list [ int ] :
211
+ def create_heap (size , heap_kind ) :
228
212
"""
229
213
Create a min/max heap where elements are in the range (0, size - 1) and
230
214
shuffled before heapify.
231
215
"""
232
216
heap = list (range (OBJECT_COUNT ))
233
217
shuffle (heap )
234
- if heap_kind == HeapKind .MIN :
218
+ if heap_kind == Heap .MIN :
235
219
heapq .heapify (heap )
236
220
else :
237
221
heapq .heapify_max (heap )
238
222
239
223
return heap
240
224
241
225
@staticmethod
242
- def create_random_list (a : int , b : int , size : int ) -> list [ int ] :
226
+ def create_random_list (a , b , size ) :
243
227
"""
244
228
Create a list of random numbers between a and b (inclusive).
245
229
"""
0 commit comments