@@ -641,3 +641,86 @@ describe('Heap instances', function () {
641641 } ) ;
642642 } ) ;
643643} ) ;
644+
645+ describe ( 'Heap with custom IsEqual function' , function ( ) {
646+ describe ( 'when using object heap with custom IsEqual' , function ( ) {
647+ let heap : Heap < { value : number } > ;
648+
649+ beforeEach ( function ( ) {
650+ heap = new Heap < { value : number } > ( ( a , b ) => b . value - a . value ) ;
651+ heap . add ( { value : 1 } ) ;
652+ heap . add ( { value : 2 } ) ;
653+ heap . add ( { value : 3 } ) ;
654+ } ) ;
655+
656+ describe ( '#contains(element, fn)' , function ( ) {
657+ it ( 'should find element using custom IsEqual that ignores the needle parameter' , function ( ) {
658+ // Using a custom IsEqual that only checks the element, ignoring the needle
659+ expect ( heap . contains ( 0 as any , ( a ) => a . value === 2 ) ) . toBe ( true ) ;
660+ expect ( heap . contains ( 0 as any , ( a ) => a . value === 1 ) ) . toBe ( true ) ;
661+ expect ( heap . contains ( 0 as any , ( a ) => a . value === 3 ) ) . toBe ( true ) ;
662+ expect ( heap . contains ( 0 as any , ( a ) => a . value === 99 ) ) . toBe ( false ) ;
663+ } ) ;
664+ } ) ;
665+
666+ describe ( '#indexOf(element, fn)' , function ( ) {
667+ it ( 'should find index using custom IsEqual that ignores the needle parameter' , function ( ) {
668+ // Should find the element regardless of what needle is passed
669+ expect ( heap . indexOf ( 0 as any , ( a ) => a . value === 2 ) ) . not . toBe ( - 1 ) ;
670+ expect ( heap . indexOf ( 0 as any , ( a ) => a . value === 1 ) ) . not . toBe ( - 1 ) ;
671+ expect ( heap . indexOf ( 0 as any , ( a ) => a . value === 3 ) ) . not . toBe ( - 1 ) ;
672+ expect ( heap . indexOf ( 0 as any , ( a ) => a . value === 99 ) ) . toBe ( - 1 ) ;
673+ } ) ;
674+
675+ it ( 'should find correct element at returned index' , function ( ) {
676+ const idx = heap . indexOf ( 0 as any , ( a ) => a . value === 2 ) ;
677+ expect ( heap . heapArray [ idx ] . value ) . toBe ( 2 ) ;
678+ } ) ;
679+ } ) ;
680+
681+ describe ( '#indexOfEvery(element, fn)' , function ( ) {
682+ it ( 'should find all indexes using custom IsEqual' , function ( ) {
683+ heap . add ( { value : 2 } ) ; // Add duplicate
684+ const indexes = heap . indexOfEvery ( 0 as any , ( a ) => a . value === 2 ) ;
685+ expect ( indexes . length ) . toBe ( 2 ) ;
686+ for ( const idx of indexes ) {
687+ expect ( heap . heapArray [ idx ] . value ) . toBe ( 2 ) ;
688+ }
689+ } ) ;
690+ } ) ;
691+
692+ describe ( '#remove(element, fn)' , function ( ) {
693+ it ( 'should remove element using custom IsEqual that ignores the needle parameter' , function ( ) {
694+ // This was the failing case from the issue
695+ expect ( heap . remove ( 0 as any , ( a ) => a . value === 2 ) ) . toBe ( true ) ;
696+ expect ( heap . length ) . toBe ( 2 ) ;
697+ expect ( heap . contains ( 0 as any , ( a ) => a . value === 2 ) ) . toBe ( false ) ;
698+ expect ( heap . check ( ) ) . not . toBeDefined ( ) ;
699+ } ) ;
700+
701+ it ( 'should remove any element regardless of heap position' , function ( ) {
702+ // Test removing elements at different positions
703+ const heap2 = new Heap < { value : number } > ( ( a , b ) => b . value - a . value ) ;
704+ heap2 . add ( { value : 1 } ) ;
705+ heap2 . add ( { value : 2 } ) ;
706+ heap2 . add ( { value : 3 } ) ;
707+
708+ expect ( heap2 . remove ( 0 as any , ( a ) => a . value === 1 ) ) . toBe ( true ) ;
709+ expect ( heap2 . check ( ) ) . not . toBeDefined ( ) ;
710+
711+ const heap3 = new Heap < { value : number } > ( ( a , b ) => b . value - a . value ) ;
712+ heap3 . add ( { value : 1 } ) ;
713+ heap3 . add ( { value : 2 } ) ;
714+ heap3 . add ( { value : 3 } ) ;
715+
716+ expect ( heap3 . remove ( 0 as any , ( a ) => a . value === 3 ) ) . toBe ( true ) ;
717+ expect ( heap3 . check ( ) ) . not . toBeDefined ( ) ;
718+ } ) ;
719+
720+ it ( 'should return false when element is not found' , function ( ) {
721+ expect ( heap . remove ( 0 as any , ( a ) => a . value === 99 ) ) . toBe ( false ) ;
722+ expect ( heap . length ) . toBe ( 3 ) ;
723+ } ) ;
724+ } ) ;
725+ } ) ;
726+ } ) ;
0 commit comments