1- import { PriorityQueue } from "../src" ;
2- import { IPriorityQueue } from "../typings/priority-queue" ;
3- import { testCase1 , testCase2 , testCase3 } from "./fixtures/test-case" ;
1+ import { PriorityQueue } from '../src' ;
2+ import { IPriorityQueue } from '../typings/priority-queue' ;
3+ import {
4+ testCase1 ,
5+ testCase2 ,
6+ testCase3 ,
7+ testCase4 ,
8+ } from './fixtures/test-case' ;
49
5- describe ( " testing priority queue" , ( ) => {
10+ describe ( ' testing priority queue' , ( ) => {
611 let p : IPriorityQueue < number > ;
712
8- describe ( " max priority queue" , ( ) => {
13+ describe ( ' max priority queue' , ( ) => {
914 beforeEach ( ( ) => {
1015 p = new PriorityQueue ( ) ;
1116 } ) ;
@@ -14,7 +19,7 @@ describe("testing priority queue", () => {
1419 p = null ;
1520 } ) ;
1621
17- it ( " should return max heap of test case 1" , ( ) => {
22+ it ( ' should return max heap of test case 1' , ( ) => {
1823 for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
1924 p . push ( testCase1 [ i ] ) ;
2025 }
@@ -23,7 +28,7 @@ describe("testing priority queue", () => {
2328 expect ( actual ) . toEqual ( expected ) ;
2429 } ) ;
2530
26- it ( " should return max heap of test case 2" , ( ) => {
31+ it ( ' should return max heap of test case 2' , ( ) => {
2732 for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
2833 p . push ( testCase2 [ i ] ) ;
2934 }
@@ -32,7 +37,7 @@ describe("testing priority queue", () => {
3237 expect ( actual ) . toEqual ( expected ) ;
3338 } ) ;
3439
35- it ( " should get the max value without remove" , ( ) => {
40+ it ( ' should get the max value without remove' , ( ) => {
3641 for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
3742 p . push ( testCase2 [ i ] ) ;
3843 }
@@ -44,7 +49,7 @@ describe("testing priority queue", () => {
4449 expect ( actual ) . toEqual ( expected ) ;
4550 } ) ;
4651
47- it ( " should extract the max value & rebuild max heap of test case 2" , ( ) => {
52+ it ( ' should extract the max value & rebuild max heap of test case 2' , ( ) => {
4853 for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
4954 p . push ( testCase2 [ i ] ) ;
5055 }
@@ -58,7 +63,7 @@ describe("testing priority queue", () => {
5863 expect ( actual ) . toEqual ( expected ) ;
5964 } ) ;
6065
61- it ( " should call empty & call pop and top with empty queue" , ( ) => {
66+ it ( ' should call empty & call pop and top with empty queue' , ( ) => {
6267 for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
6368 p . push ( testCase1 [ i ] ) ;
6469 }
@@ -77,10 +82,19 @@ describe("testing priority queue", () => {
7782 item = p . pop ( ) ;
7883 expect ( item ) . toEqual ( null ) ;
7984 } ) ;
85+
86+ it ( 'should clear the queue' , ( ) => {
87+ for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
88+ p . push ( testCase1 [ i ] ) ;
89+ }
90+ expect ( p . size ( ) ) . toEqual ( 6 ) ;
91+ p . clear ( ) ;
92+ expect ( p . size ( ) ) . toEqual ( 0 ) ;
93+ } ) ;
8094 } ) ;
8195
82- describe ( " testing with array of object" , ( ) => {
83- it ( " should return max heap of test case 3" , ( ) => {
96+ describe ( ' testing with array of object' , ( ) => {
97+ it ( ' should return max heap of test case 3' , ( ) => {
8498 p = new PriorityQueue ( function ( a : any , b : any ) {
8599 return a . value < b . value ;
86100 } ) ;
@@ -90,17 +104,17 @@ describe("testing priority queue", () => {
90104 }
91105 const actual = p . toArray ( ) ;
92106 const expected = [
93- { text : "e" , value : 8 } ,
94- { text : "b" , value : 7 } ,
95- { text : "c" , value : 4 } ,
96- { text : "d" , value : 1 } ,
97- { text : "a" , value : 2 } ,
98- { text : "f" , value : 1 } ,
107+ { text : 'e' , value : 8 } ,
108+ { text : 'b' , value : 7 } ,
109+ { text : 'c' , value : 4 } ,
110+ { text : 'd' , value : 1 } ,
111+ { text : 'a' , value : 2 } ,
112+ { text : 'f' , value : 1 } ,
99113 ] ;
100114 expect ( actual ) . toEqual ( expected ) ;
101115 } ) ;
102116
103- it ( " should return min heap of test case 3" , ( ) => {
117+ it ( ' should return min heap of test case 3' , ( ) => {
104118 p = new PriorityQueue ( function ( a : any , b : any ) {
105119 return a . value > b . value ;
106120 } ) ;
@@ -110,12 +124,12 @@ describe("testing priority queue", () => {
110124 }
111125 const actual = p . toArray ( ) ;
112126 const expected = [
113- { text : "d" , value : 1 } ,
114- { text : "a" , value : 2 } ,
115- { text : "f" , value : 1 } ,
116- { text : "b" , value : 7 } ,
117- { text : "e" , value : 8 } ,
118- { text : "c" , value : 4 } ,
127+ { text : 'd' , value : 1 } ,
128+ { text : 'a' , value : 2 } ,
129+ { text : 'f' , value : 1 } ,
130+ { text : 'b' , value : 7 } ,
131+ { text : 'e' , value : 8 } ,
132+ { text : 'c' , value : 4 } ,
119133 ] ;
120134 expect ( actual ) . toEqual ( expected ) ;
121135 } ) ;
@@ -131,16 +145,16 @@ describe("testing priority queue", () => {
131145
132146 expect ( p . size ( ) ) . toEqual ( testCase3 . length ) ;
133147 const max = p . pop ( ) ;
134- expect ( max ) . toEqual ( { text : "e" , value : 8 } ) ;
148+ expect ( max ) . toEqual ( { text : 'e' , value : 8 } ) ;
135149 expect ( p . size ( ) ) . toEqual ( testCase3 . length - 1 ) ;
136150
137151 const actual = p . toArray ( ) ;
138152 const expected = [
139- { text : "b" , value : 7 } ,
140- { text : "a" , value : 2 } ,
141- { text : "c" , value : 4 } ,
142- { text : "d" , value : 1 } ,
143- { text : "f" , value : 1 } ,
153+ { text : 'b' , value : 7 } ,
154+ { text : 'a' , value : 2 } ,
155+ { text : 'c' , value : 4 } ,
156+ { text : 'd' , value : 1 } ,
157+ { text : 'f' , value : 1 } ,
144158 ] ;
145159 expect ( actual ) . toEqual ( expected ) ;
146160 } ) ;
@@ -156,22 +170,22 @@ describe("testing priority queue", () => {
156170
157171 expect ( p . size ( ) ) . toEqual ( testCase3 . length ) ;
158172 const min = p . pop ( ) ;
159- expect ( min ) . toEqual ( { text : "d" , value : 1 } ) ;
173+ expect ( min ) . toEqual ( { text : 'd' , value : 1 } ) ;
160174 expect ( p . size ( ) ) . toEqual ( testCase3 . length - 1 ) ;
161175
162176 const actual = p . toArray ( ) ;
163177 const expected = [
164- { text : "f" , value : 1 } ,
165- { text : "a" , value : 2 } ,
166- { text : "c" , value : 4 } ,
167- { text : "b" , value : 7 } ,
168- { text : "e" , value : 8 } ,
178+ { text : 'f' , value : 1 } ,
179+ { text : 'a' , value : 2 } ,
180+ { text : 'c' , value : 4 } ,
181+ { text : 'b' , value : 7 } ,
182+ { text : 'e' , value : 8 } ,
169183 ] ;
170184 expect ( actual ) . toEqual ( expected ) ;
171185 } ) ;
172186 } ) ;
173187
174- describe ( " min priority queue" , ( ) => {
188+ describe ( ' min priority queue' , ( ) => {
175189 beforeEach ( ( ) => {
176190 p = new PriorityQueue ( function ( a , b ) {
177191 return a > b ;
@@ -182,7 +196,7 @@ describe("testing priority queue", () => {
182196 p = null ;
183197 } ) ;
184198
185- it ( " should return min heap of test case 1" , ( ) => {
199+ it ( ' should return min heap of test case 1' , ( ) => {
186200 for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
187201 p . push ( testCase1 [ i ] ) ;
188202 }
@@ -191,7 +205,7 @@ describe("testing priority queue", () => {
191205 expect ( actual ) . toEqual ( expected ) ;
192206 } ) ;
193207
194- it ( " should return min heap of test case 2" , ( ) => {
208+ it ( ' should return min heap of test case 2' , ( ) => {
195209 for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
196210 p . push ( testCase2 [ i ] ) ;
197211 }
@@ -200,7 +214,7 @@ describe("testing priority queue", () => {
200214 expect ( actual ) . toEqual ( expected ) ;
201215 } ) ;
202216
203- it ( " should extract the min value & rebuild min heap of test case 2" , ( ) => {
217+ it ( ' should extract the min value & rebuild min heap of test case 2' , ( ) => {
204218 for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
205219 p . push ( testCase2 [ i ] ) ;
206220 }
@@ -212,4 +226,71 @@ describe("testing priority queue", () => {
212226 expect ( actual ) . toEqual ( expected ) ;
213227 } ) ;
214228 } ) ;
229+
230+ describe ( "testing 'contains' method" , ( ) => {
231+ beforeEach ( ( ) => {
232+ p = new PriorityQueue ( ) ;
233+ } ) ;
234+
235+ afterEach ( ( ) => {
236+ p = null ;
237+ } ) ;
238+
239+ it ( 'should return true - with array of numbers' , ( ) => {
240+ for ( let i = 0 ; i < testCase4 . length ; i ++ ) {
241+ p . push ( testCase4 [ i ] ) ;
242+ }
243+ expect ( p . contains ( 9 ) ) . toEqual ( true ) ;
244+ } ) ;
245+
246+ it ( 'should return true - with array of objects' , ( ) => {
247+ for ( let i = 0 ; i < testCase3 . length ; i ++ ) {
248+ p = new PriorityQueue ( function ( a : any , b : any ) {
249+ return a . value < b . value ;
250+ } ) ;
251+
252+ for ( let i = 0 ; i < testCase3 . length ; i ++ ) {
253+ p . push ( testCase3 [ i ] as any ) ;
254+ }
255+ }
256+
257+ var element : any = { text : 'e' , value : 8 } ;
258+
259+ const contain = function ( item : any ) {
260+ return item . value === element . value ;
261+ } ;
262+ expect ( p . contains ( element , contain ) ) . toEqual ( true ) ;
263+ } ) ;
264+
265+ it ( 'should return false' , ( ) => {
266+ for ( let i = 0 ; i < testCase4 . length ; i ++ ) {
267+ p . push ( testCase4 [ i ] ) ;
268+ }
269+ expect ( p . contains ( 10 ) ) . toEqual ( false ) ;
270+ } ) ;
271+
272+ it ( 'should return false - with array of objects' , ( ) => {
273+ for ( let i = 0 ; i < testCase3 . length ; i ++ ) {
274+ p = new PriorityQueue ( function ( a : any , b : any ) {
275+ return a . value < b . value ;
276+ } ) ;
277+
278+ for ( let i = 0 ; i < testCase3 . length ; i ++ ) {
279+ p . push ( testCase3 [ i ] as any ) ;
280+ }
281+ }
282+
283+ var element : any = { text : 'fff' , value : 100 } ;
284+
285+ const contain = function ( item : any ) {
286+ return item . value === element . value ;
287+ } ;
288+
289+ expect ( p . contains ( element , contain ) ) . toEqual ( false ) ;
290+ } ) ;
291+
292+ it ( 'should return false - with empty queue' , ( ) => {
293+ expect ( p . contains ( 10 ) ) . toEqual ( false ) ;
294+ } ) ;
295+ } ) ;
215296} ) ;
0 commit comments