@@ -3,14 +3,16 @@ import { MinPriorityQueue } from '../MinPriorityQueue'
33describe ( 'MinPriorityQueue' , ( ) => {
44 const values = [ 5 , 2 , 4 , 1 , 7 , 6 , 3 , 8 ]
55 const capacity = values . length
6+ let queue
67
7- const Queue = new MinPriorityQueue ( capacity )
8-
9- values . forEach ( v => Queue . insert ( v ) )
8+ beforeEach ( ( ) => {
9+ queue = new MinPriorityQueue ( capacity )
10+ values . forEach ( v => queue . insert ( v ) )
11+ } )
1012
1113 it ( 'Check heap ordering' , ( ) => {
1214 const mockFn = jest . fn ( )
13- Queue . print ( mockFn )
15+ queue . print ( mockFn )
1416
1517 expect ( mockFn . mock . calls . length ) . toBe ( 1 ) // Expect one call
1618 expect ( mockFn . mock . calls [ 0 ] . length ) . toBe ( 1 ) // Expect one argument
@@ -21,10 +23,9 @@ describe('MinPriorityQueue', () => {
2123 } )
2224
2325 it ( 'heapSort() expected to reverse the heap ordering' , ( ) => {
24- Queue . heapSort ( )
25-
26+ queue . heapReverse ( )
2627 const mockFn = jest . fn ( )
27- Queue . print ( mockFn )
28+ queue . print ( mockFn )
2829
2930 expect ( mockFn . mock . calls . length ) . toBe ( 1 )
3031 expect ( mockFn . mock . calls [ 0 ] . length ) . toBe ( 1 )
@@ -33,4 +34,22 @@ describe('MinPriorityQueue', () => {
3334 expect ( heap . length ) . toBe ( capacity )
3435 expect ( heap ) . toStrictEqual ( [ 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ] )
3536 } )
37+
38+ describe ( 'delete() function work properly' , ( ) => {
39+ it ( 'return undefined if heap is empty' , ( ) => {
40+ const minqueue = new MinPriorityQueue ( capacity )
41+ const min = minqueue . delete ( )
42+ expect ( min ) . toBe ( undefined )
43+ } )
44+ it ( 'return min value and remove it' , ( ) => {
45+ const sortedValues = values . sort ( )
46+ let initialSize = queue . size
47+ sortedValues . forEach ( ( minValue , index ) => {
48+ const min = queue . delete ( )
49+ expect ( min ) . toBe ( minValue )
50+ expect ( queue . size ) . toBe ( -- initialSize )
51+ } )
52+ expect ( queue . size ) . toBe ( 0 )
53+ } )
54+ } )
3655} )
0 commit comments