1010from test .support import threading_helper
1111
1212
13- NTHREADS : int = 10
14- OBJECT_COUNT : int = 5_000
13+ NTHREADS = 10
14+ OBJECT_COUNT = 5_000
1515
1616
17- class HeapKind (Enum ):
17+ class Heap (Enum ):
1818 MIN = 1
1919 MAX = 2
2020
@@ -28,28 +28,28 @@ def test_racing_heapify(self):
2828 self .run_concurrently (
2929 worker_func = heapq .heapify , args = (heap ,), nthreads = NTHREADS
3030 )
31- self .assertTrue (self .is_min_heap_property_satisfied (heap ))
31+ self .assertTrue (self .is_heap_property_satisfied (heap , Heap . MIN ))
3232
3333 def test_racing_heappush (self ):
3434 heap = []
3535
36- def heappush_func (heap : list [ int ] ):
36+ def heappush_func (heap ):
3737 for item in reversed (range (OBJECT_COUNT )):
3838 heapq .heappush (heap , item )
3939
4040 self .run_concurrently (
4141 worker_func = heappush_func , args = (heap ,), nthreads = NTHREADS
4242 )
43- self .assertTrue (self .is_min_heap_property_satisfied (heap ))
43+ self .assertTrue (self .is_heap_property_satisfied (heap , Heap . MIN ))
4444
4545 def test_racing_heappop (self ):
46- heap = self .create_heap (OBJECT_COUNT , HeapKind .MIN )
46+ heap = self .create_heap (OBJECT_COUNT , Heap .MIN )
4747
4848 # Each thread pops (OBJECT_COUNT / NTHREADS) items
4949 self .assertEqual (OBJECT_COUNT % NTHREADS , 0 )
5050 per_thread_pop_count = OBJECT_COUNT // NTHREADS
5151
52- def heappop_func (heap : list [ int ] , pop_count : int ):
52+ def heappop_func (heap , pop_count ):
5353 local_list = []
5454 for _ in range (pop_count ):
5555 item = heapq .heappop (heap )
@@ -66,10 +66,10 @@ def heappop_func(heap: list[int], pop_count: int):
6666 self .assertEqual (len (heap ), 0 )
6767
6868 def test_racing_heappushpop (self ):
69- heap = self .create_heap (OBJECT_COUNT , HeapKind .MIN )
69+ heap = self .create_heap (OBJECT_COUNT , Heap .MIN )
7070 pushpop_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
7171
72- def heappushpop_func (heap : list [ int ] , pushpop_items : list [ int ] ):
72+ def heappushpop_func (heap , pushpop_items ):
7373 for item in pushpop_items :
7474 popped_item = heapq .heappushpop (heap , item )
7575 self .assertTrue (popped_item <= item )
@@ -80,13 +80,13 @@ def heappushpop_func(heap: list[int], pushpop_items: list[int]):
8080 nthreads = NTHREADS ,
8181 )
8282 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 ))
8484
8585 def test_racing_heapreplace (self ):
86- heap = self .create_heap (OBJECT_COUNT , HeapKind .MIN )
86+ heap = self .create_heap (OBJECT_COUNT , Heap .MIN )
8787 replace_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
8888
89- def heapreplace_func (heap : list [ int ] , replace_items : list [ int ] ):
89+ def heapreplace_func (heap , replace_items ):
9090 for item in replace_items :
9191 popped_item = heapq .heapreplace (heap , item )
9292
@@ -96,7 +96,7 @@ def heapreplace_func(heap: list[int], replace_items: list[int]):
9696 nthreads = NTHREADS ,
9797 )
9898 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 ))
100100
101101 def test_racing_heapify_max (self ):
102102 max_heap = list (range (OBJECT_COUNT ))
@@ -105,28 +105,28 @@ def test_racing_heapify_max(self):
105105 self .run_concurrently (
106106 worker_func = heapq .heapify_max , args = (max_heap ,), nthreads = NTHREADS
107107 )
108- self .assertTrue (self .is_max_heap_property_satisfied (max_heap ))
108+ self .assertTrue (self .is_heap_property_satisfied (max_heap , Heap . MAX ))
109109
110110 def test_racing_heappush_max (self ):
111111 max_heap = []
112112
113- def heappush_max_func (max_heap : list [ int ] ):
113+ def heappush_max_func (max_heap ):
114114 for item in range (OBJECT_COUNT ):
115115 heapq .heappush_max (max_heap , item )
116116
117117 self .run_concurrently (
118118 worker_func = heappush_max_func , args = (max_heap ,), nthreads = NTHREADS
119119 )
120- self .assertTrue (self .is_max_heap_property_satisfied (max_heap ))
120+ self .assertTrue (self .is_heap_property_satisfied (max_heap , Heap . MAX ))
121121
122122 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 )
124124
125125 # Each thread pops (OBJECT_COUNT / NTHREADS) items
126126 self .assertEqual (OBJECT_COUNT % NTHREADS , 0 )
127127 per_thread_pop_count = OBJECT_COUNT // NTHREADS
128128
129- def heappop_max_func (max_heap : list [ int ] , pop_count : int ):
129+ def heappop_max_func (max_heap , pop_count ):
130130 local_list = []
131131 for _ in range (pop_count ):
132132 item = heapq .heappop_max (max_heap )
@@ -143,12 +143,10 @@ def heappop_max_func(max_heap: list[int], pop_count: int):
143143 self .assertEqual (len (max_heap ), 0 )
144144
145145 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 )
147147 pushpop_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
148148
149- def heappushpop_max_func (
150- max_heap : list [int ], pushpop_items : list [int ]
151- ):
149+ def heappushpop_max_func (max_heap , pushpop_items ):
152150 for item in pushpop_items :
153151 popped_item = heapq .heappushpop_max (max_heap , item )
154152 self .assertTrue (popped_item >= item )
@@ -159,15 +157,13 @@ def heappushpop_max_func(
159157 nthreads = NTHREADS ,
160158 )
161159 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 ))
163161
164162 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 )
166164 replace_items = self .create_random_list (- 5_000 , 10_000 , OBJECT_COUNT )
167165
168- def heapreplace_max_func (
169- max_heap : list [int ], replace_items : list [int ]
170- ):
166+ def heapreplace_max_func (max_heap , replace_items ):
171167 for item in replace_items :
172168 popped_item = heapq .heapreplace_max (max_heap , item )
173169
@@ -177,30 +173,18 @@ def heapreplace_max_func(
177173 nthreads = NTHREADS ,
178174 )
179175 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 ))
195177
196178 @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 ):
200180 """
201181 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.
202186 """
203- op = operator .le if heap_kind == HeapKind .MIN else operator .ge
187+ op = operator .le if heap_kind == Heap .MIN else operator .ge
204188 # position 0 has no parent
205189 for pos in range (1 , len (heap )):
206190 parent_pos = (pos - 1 ) >> 1
@@ -210,36 +194,36 @@ def is_heap_property_satisfied(
210194 return True
211195
212196 @staticmethod
213- def is_sorted_ascending (lst : list [ object ]) -> bool :
197+ def is_sorted_ascending (lst ) :
214198 """
215199 Check if the list is sorted in ascending order (non-decreasing).
216200 """
217201 return all (lst [i - 1 ] <= lst [i ] for i in range (1 , len (lst )))
218202
219203 @staticmethod
220- def is_sorted_descending (lst : list [ object ]) -> bool :
204+ def is_sorted_descending (lst ) :
221205 """
222206 Check if the list is sorted in descending order (non-increasing).
223207 """
224208 return all (lst [i - 1 ] >= lst [i ] for i in range (1 , len (lst )))
225209
226210 @staticmethod
227- def create_heap (size : int , heap_kind : HeapKind ) -> list [ int ] :
211+ def create_heap (size , heap_kind ) :
228212 """
229213 Create a min/max heap where elements are in the range (0, size - 1) and
230214 shuffled before heapify.
231215 """
232216 heap = list (range (OBJECT_COUNT ))
233217 shuffle (heap )
234- if heap_kind == HeapKind .MIN :
218+ if heap_kind == Heap .MIN :
235219 heapq .heapify (heap )
236220 else :
237221 heapq .heapify_max (heap )
238222
239223 return heap
240224
241225 @staticmethod
242- def create_random_list (a : int , b : int , size : int ) -> list [ int ] :
226+ def create_random_list (a , b , size ) :
243227 """
244228 Create a list of random numbers between a and b (inclusive).
245229 """
0 commit comments