1+ import { PriorityQueue } from '../src' ;
2+ import { IPriorityQueue } from '../typing/priority-queue' ;
3+ import { testCase1 , testCase2 } from './fixtures/test-case' ;
4+
5+ describe ( 'testing priority queue' , ( ) => {
6+ let p : IPriorityQueue < number > ;
7+
8+ describe ( 'max priority queue' , ( ) => {
9+ beforeEach ( ( ) => {
10+ p = new PriorityQueue ( ) ;
11+ } ) ;
12+
13+ afterEach ( ( ) => {
14+ p = null ;
15+ } ) ;
16+
17+ it ( 'should return max heap of test case 1' , ( ) => {
18+ for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
19+ p . push ( testCase1 [ i ] ) ;
20+ }
21+ const actual = p . toArray ( ) ;
22+ const expected = [ 8 , 7 , 4 , 1 , 2 , 1 ] ;
23+ expect ( actual ) . toEqual ( expected ) ;
24+ } ) ;
25+
26+ it ( 'should return max heap of test case 2' , ( ) => {
27+ for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
28+ p . push ( testCase2 [ i ] ) ;
29+ }
30+ const actual = p . toArray ( ) ;
31+ const expected = [ 4 , 3 , 4 , 3 , 2 ] ;
32+ expect ( actual ) . toEqual ( expected ) ;
33+ } ) ;
34+
35+ it ( 'should get the max value without remove' , ( ) => {
36+ for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
37+ p . push ( testCase2 [ i ] ) ;
38+ }
39+ const max = p . top ( ) ;
40+ expect ( max ) . toEqual ( 4 ) ;
41+
42+ const actual = p . toArray ( ) ;
43+ const expected = [ 4 , 3 , 4 , 3 , 2 ] ;
44+ expect ( actual ) . toEqual ( expected ) ;
45+ } ) ;
46+
47+ it ( 'should extract the max value & rebuild max heap of test case 2' , ( ) => {
48+ for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
49+ p . push ( testCase2 [ i ] ) ;
50+ }
51+ expect ( p . size ( ) ) . toEqual ( testCase2 . length ) ;
52+ const max = p . pop ( ) ;
53+ expect ( max ) . toEqual ( 4 ) ;
54+ expect ( p . size ( ) ) . toEqual ( testCase2 . length - 1 ) ;
55+
56+ const actual = p . toArray ( ) ;
57+ const expected = [ 4 , 3 , 2 , 3 ] ;
58+ expect ( actual ) . toEqual ( expected ) ;
59+ } ) ;
60+
61+ it ( 'should call empty & call pop and top with empty queue' , ( ) => {
62+ for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
63+ p . push ( testCase1 [ i ] ) ;
64+ }
65+
66+ const expected = [ 8 , 7 , 4 , 2 , 1 , 1 ] ;
67+ let index = 0 ;
68+
69+ while ( ! p . empty ( ) ) {
70+ const item = p . pop ( ) ;
71+ expect ( item ) . toEqual ( expected [ index ] ) ;
72+ index ++ ;
73+ }
74+
75+ let item = p . top ( ) ;
76+ expect ( item ) . toEqual ( null ) ;
77+ item = p . pop ( ) ;
78+ expect ( item ) . toEqual ( null ) ;
79+ } ) ;
80+
81+ } ) ;
82+
83+ describe ( 'min priority queue' , ( ) => {
84+ beforeEach ( ( ) => {
85+ p = new PriorityQueue ( function ( a , b ) {
86+ return a > b ;
87+ } ) ;
88+ } ) ;
89+
90+ afterEach ( ( ) => {
91+ p = null ;
92+ } ) ;
93+
94+ it ( 'should return min heap of test case 1' , ( ) => {
95+ for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
96+ p . push ( testCase1 [ i ] ) ;
97+ }
98+ const actual = p . toArray ( ) ;
99+ const expected = [ 1 , 2 , 1 , 7 , 8 , 4 ] ;
100+ expect ( actual ) . toEqual ( expected ) ;
101+ } ) ;
102+
103+ it ( 'should return min heap of test case 2' , ( ) => {
104+ for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
105+ p . push ( testCase2 [ i ] ) ;
106+ }
107+ const actual = p . toArray ( ) ;
108+ const expected = [ 2 , 3 , 4 , 4 , 3 ] ;
109+ expect ( actual ) . toEqual ( expected ) ;
110+ } ) ;
111+
112+ it ( 'should extract the min value & rebuild min heap of test case 2' , ( ) => {
113+ for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
114+ p . push ( testCase2 [ i ] ) ;
115+ }
116+ const min = p . pop ( ) ;
117+ expect ( min ) . toEqual ( 2 ) ;
118+
119+ const actual = p . toArray ( ) ;
120+ const expected = [ 3 , 3 , 4 , 4 ] ;
121+ expect ( actual ) . toEqual ( expected ) ;
122+ } ) ;
123+ } ) ;
124+ } ) ;
0 commit comments