@@ -96,7 +96,7 @@ describe("PriorityQueue", () => {
9696 } ) ;
9797 } ) ;
9898
99- describe ( "remove ()" , async ( ) => {
99+ describe ( "removeTop ()" , async ( ) => {
100100 describe ( "uint" , ( ) => {
101101 it ( "should add and remove the top elements" , async ( ) => {
102102 await mock . addUint ( 1 , 1 ) ;
@@ -190,4 +190,153 @@ describe("PriorityQueue", () => {
190190 } ) ;
191191 } ) ;
192192 } ) ;
193+
194+ describe ( "remove()" , ( ) => {
195+ describe ( "uint" , ( ) => {
196+ it ( "should remove a specific element from the middle" , async ( ) => {
197+ await mock . addUint ( 1 , 1 ) ;
198+ await mock . addUint ( 2 , 2 ) ;
199+ await mock . addUint ( 3 , 3 ) ;
200+ await mock . addUint ( 4 , 4 ) ;
201+ await mock . addUint ( 5 , 5 ) ;
202+
203+ expect ( await mock . lengthUint ( ) ) . to . equal ( 5n ) ;
204+ expect ( await mock . topValueUint ( ) ) . to . equal ( 5n ) ;
205+
206+ // Remove element with value 3
207+ await mock . removeUint ( 3 ) ;
208+
209+ expect ( await mock . lengthUint ( ) ) . to . equal ( 4n ) ;
210+ expect ( await mock . topValueUint ( ) ) . to . equal ( 5n ) ;
211+ expect ( await mock . valuesUint ( ) ) . to . deep . equal ( [ 5n , 4n , 2n , 1n ] ) ;
212+ } ) ;
213+
214+ it ( "should remove the top element" , async ( ) => {
215+ await mock . addUint ( 1 , 1 ) ;
216+ await mock . addUint ( 2 , 2 ) ;
217+ await mock . addUint ( 3 , 3 ) ;
218+
219+ // Remove the top element (value 3, priority 3)
220+ await mock . removeUint ( 3 ) ;
221+
222+ expect ( await mock . lengthUint ( ) ) . to . equal ( 2n ) ;
223+ expect ( await mock . topValueUint ( ) ) . to . equal ( 2n ) ;
224+ } ) ;
225+
226+ it ( "should remove the last element" , async ( ) => {
227+ await mock . addUint ( 1 , 1 ) ;
228+ await mock . addUint ( 2 , 2 ) ;
229+ await mock . addUint ( 3 , 3 ) ;
230+
231+ // Remove element with value 1 (should be at the end)
232+ await mock . removeUint ( 1 ) ;
233+
234+ expect ( await mock . lengthUint ( ) ) . to . equal ( 2n ) ;
235+ expect ( await mock . topValueUint ( ) ) . to . equal ( 3n ) ;
236+ } ) ;
237+
238+ it ( "should remove the element with the same priority" , async ( ) => {
239+ await mock . addUint ( 1 , ethers . MaxUint256 ) ;
240+ await mock . addUint ( 2 , ethers . MaxUint256 ) ;
241+ await mock . addUint ( 3 , ethers . MaxUint256 ) ;
242+
243+ await mock . removeUint ( 2 ) ;
244+
245+ expect ( await mock . lengthUint ( ) ) . to . equal ( 2n ) ;
246+ expect ( await mock . topValueUint ( ) ) . to . equal ( 1n ) ;
247+
248+ await mock . removeUint ( 3 ) ;
249+
250+ expect ( await mock . lengthUint ( ) ) . to . equal ( 1n ) ;
251+ expect ( await mock . topValueUint ( ) ) . to . equal ( 1n ) ;
252+ } ) ;
253+
254+ it ( "should return false when removing non-existent element" , async ( ) => {
255+ await mock . addUint ( 1 , 1 ) ;
256+ await mock . addUint ( 2 , 2 ) ;
257+
258+ // Check the return value using staticCall (view function call)
259+ expect ( await mock . removeUint . staticCall ( 99 ) ) . to . equal ( false ) ;
260+ expect ( await mock . lengthUint ( ) ) . to . equal ( 2n ) ;
261+ } ) ;
262+
263+ it ( "should return false when removing from empty queue" , async ( ) => {
264+ expect ( await mock . removeUint . staticCall ( 1 ) ) . to . equal ( false ) ;
265+ expect ( await mock . lengthUint ( ) ) . to . equal ( 0n ) ;
266+ } ) ;
267+
268+ it ( "should handle removing elements with same priority" , async ( ) => {
269+ await mock . addUint ( 1 , 5 ) ;
270+ await mock . addUint ( 2 , 5 ) ;
271+ await mock . addUint ( 3 , 5 ) ;
272+
273+ await mock . removeUint ( 2 ) ;
274+ expect ( await mock . lengthUint ( ) ) . to . equal ( 2n ) ;
275+ } ) ;
276+
277+ it ( "should handle removing all elements one by one" , async ( ) => {
278+ await mock . addUint ( 1 , 1 ) ;
279+ await mock . addUint ( 2 , 2 ) ;
280+ await mock . addUint ( 3 , 3 ) ;
281+
282+ await mock . removeUint ( 2 ) ;
283+ expect ( await mock . lengthUint ( ) ) . to . equal ( 2n ) ;
284+
285+ await mock . removeUint ( 1 ) ;
286+ expect ( await mock . lengthUint ( ) ) . to . equal ( 1n ) ;
287+
288+ await mock . removeUint ( 3 ) ;
289+ expect ( await mock . lengthUint ( ) ) . to . equal ( 0n ) ;
290+ } ) ;
291+ } ) ;
292+
293+ describe ( "bytes32" , ( ) => {
294+ it ( "should remove a specific bytes32 element" , async ( ) => {
295+ const val1 = ethers . keccak256 ( "0x01" ) ;
296+ const val2 = ethers . keccak256 ( "0x02" ) ;
297+ const val3 = ethers . keccak256 ( "0x03" ) ;
298+
299+ await mock . addBytes32 ( val1 , 1 ) ;
300+ await mock . addBytes32 ( val2 , 2 ) ;
301+ await mock . addBytes32 ( val3 , 3 ) ;
302+
303+ await mock . removeBytes32 ( val2 ) ;
304+ expect ( await mock . lengthBytes32 ( ) ) . to . equal ( 2n ) ;
305+ expect ( await mock . topValueBytes32 ( ) ) . to . equal ( val3 ) ;
306+ } ) ;
307+
308+ it ( "should return false when removing non-existent bytes32 element" , async ( ) => {
309+ const val1 = ethers . keccak256 ( "0x01" ) ;
310+ const val2 = ethers . keccak256 ( "0x02" ) ;
311+
312+ await mock . addBytes32 ( val1 , 1 ) ;
313+
314+ expect ( await mock . removeBytes32 . staticCall ( val2 ) ) . to . equal ( false ) ;
315+ expect ( await mock . lengthBytes32 ( ) ) . to . equal ( 1n ) ;
316+ } ) ;
317+ } ) ;
318+
319+ describe ( "address" , ( ) => {
320+ it ( "should remove a specific address element" , async ( ) => {
321+ const [ FIRST , SECOND , THIRD ] = await ethers . getSigners ( ) ;
322+
323+ await mock . addAddress ( FIRST . address , 1 ) ;
324+ await mock . addAddress ( SECOND . address , 2 ) ;
325+ await mock . addAddress ( THIRD . address , 3 ) ;
326+
327+ await mock . removeAddress ( SECOND . address ) ;
328+ expect ( await mock . lengthAddress ( ) ) . to . equal ( 2n ) ;
329+ expect ( await mock . topValueAddress ( ) ) . to . equal ( THIRD . address ) ;
330+ } ) ;
331+
332+ it ( "should return false when removing non-existent address element" , async ( ) => {
333+ const [ FIRST , SECOND ] = await ethers . getSigners ( ) ;
334+
335+ await mock . addAddress ( FIRST . address , 1 ) ;
336+
337+ expect ( await mock . removeAddress . staticCall ( SECOND . address ) ) . to . equal ( false ) ;
338+ expect ( await mock . lengthAddress ( ) ) . to . equal ( 1n ) ;
339+ } ) ;
340+ } ) ;
341+ } ) ;
193342} ) ;
0 commit comments