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' ;
4
9
5
- describe ( " testing priority queue" , ( ) => {
10
+ describe ( ' testing priority queue' , ( ) => {
6
11
let p : IPriorityQueue < number > ;
7
12
8
- describe ( " max priority queue" , ( ) => {
13
+ describe ( ' max priority queue' , ( ) => {
9
14
beforeEach ( ( ) => {
10
15
p = new PriorityQueue ( ) ;
11
16
} ) ;
@@ -14,7 +19,7 @@ describe("testing priority queue", () => {
14
19
p = null ;
15
20
} ) ;
16
21
17
- it ( " should return max heap of test case 1" , ( ) => {
22
+ it ( ' should return max heap of test case 1' , ( ) => {
18
23
for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
19
24
p . push ( testCase1 [ i ] ) ;
20
25
}
@@ -23,7 +28,7 @@ describe("testing priority queue", () => {
23
28
expect ( actual ) . toEqual ( expected ) ;
24
29
} ) ;
25
30
26
- it ( " should return max heap of test case 2" , ( ) => {
31
+ it ( ' should return max heap of test case 2' , ( ) => {
27
32
for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
28
33
p . push ( testCase2 [ i ] ) ;
29
34
}
@@ -32,7 +37,7 @@ describe("testing priority queue", () => {
32
37
expect ( actual ) . toEqual ( expected ) ;
33
38
} ) ;
34
39
35
- it ( " should get the max value without remove" , ( ) => {
40
+ it ( ' should get the max value without remove' , ( ) => {
36
41
for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
37
42
p . push ( testCase2 [ i ] ) ;
38
43
}
@@ -44,7 +49,7 @@ describe("testing priority queue", () => {
44
49
expect ( actual ) . toEqual ( expected ) ;
45
50
} ) ;
46
51
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' , ( ) => {
48
53
for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
49
54
p . push ( testCase2 [ i ] ) ;
50
55
}
@@ -58,7 +63,7 @@ describe("testing priority queue", () => {
58
63
expect ( actual ) . toEqual ( expected ) ;
59
64
} ) ;
60
65
61
- it ( " should call empty & call pop and top with empty queue" , ( ) => {
66
+ it ( ' should call empty & call pop and top with empty queue' , ( ) => {
62
67
for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
63
68
p . push ( testCase1 [ i ] ) ;
64
69
}
@@ -77,10 +82,19 @@ describe("testing priority queue", () => {
77
82
item = p . pop ( ) ;
78
83
expect ( item ) . toEqual ( null ) ;
79
84
} ) ;
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
+ } ) ;
80
94
} ) ;
81
95
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' , ( ) => {
84
98
p = new PriorityQueue ( function ( a : any , b : any ) {
85
99
return a . value < b . value ;
86
100
} ) ;
@@ -90,17 +104,17 @@ describe("testing priority queue", () => {
90
104
}
91
105
const actual = p . toArray ( ) ;
92
106
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 } ,
99
113
] ;
100
114
expect ( actual ) . toEqual ( expected ) ;
101
115
} ) ;
102
116
103
- it ( " should return min heap of test case 3" , ( ) => {
117
+ it ( ' should return min heap of test case 3' , ( ) => {
104
118
p = new PriorityQueue ( function ( a : any , b : any ) {
105
119
return a . value > b . value ;
106
120
} ) ;
@@ -110,12 +124,12 @@ describe("testing priority queue", () => {
110
124
}
111
125
const actual = p . toArray ( ) ;
112
126
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 } ,
119
133
] ;
120
134
expect ( actual ) . toEqual ( expected ) ;
121
135
} ) ;
@@ -131,16 +145,16 @@ describe("testing priority queue", () => {
131
145
132
146
expect ( p . size ( ) ) . toEqual ( testCase3 . length ) ;
133
147
const max = p . pop ( ) ;
134
- expect ( max ) . toEqual ( { text : "e" , value : 8 } ) ;
148
+ expect ( max ) . toEqual ( { text : 'e' , value : 8 } ) ;
135
149
expect ( p . size ( ) ) . toEqual ( testCase3 . length - 1 ) ;
136
150
137
151
const actual = p . toArray ( ) ;
138
152
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 } ,
144
158
] ;
145
159
expect ( actual ) . toEqual ( expected ) ;
146
160
} ) ;
@@ -156,22 +170,22 @@ describe("testing priority queue", () => {
156
170
157
171
expect ( p . size ( ) ) . toEqual ( testCase3 . length ) ;
158
172
const min = p . pop ( ) ;
159
- expect ( min ) . toEqual ( { text : "d" , value : 1 } ) ;
173
+ expect ( min ) . toEqual ( { text : 'd' , value : 1 } ) ;
160
174
expect ( p . size ( ) ) . toEqual ( testCase3 . length - 1 ) ;
161
175
162
176
const actual = p . toArray ( ) ;
163
177
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 } ,
169
183
] ;
170
184
expect ( actual ) . toEqual ( expected ) ;
171
185
} ) ;
172
186
} ) ;
173
187
174
- describe ( " min priority queue" , ( ) => {
188
+ describe ( ' min priority queue' , ( ) => {
175
189
beforeEach ( ( ) => {
176
190
p = new PriorityQueue ( function ( a , b ) {
177
191
return a > b ;
@@ -182,7 +196,7 @@ describe("testing priority queue", () => {
182
196
p = null ;
183
197
} ) ;
184
198
185
- it ( " should return min heap of test case 1" , ( ) => {
199
+ it ( ' should return min heap of test case 1' , ( ) => {
186
200
for ( let i = 0 ; i < testCase1 . length ; i ++ ) {
187
201
p . push ( testCase1 [ i ] ) ;
188
202
}
@@ -191,7 +205,7 @@ describe("testing priority queue", () => {
191
205
expect ( actual ) . toEqual ( expected ) ;
192
206
} ) ;
193
207
194
- it ( " should return min heap of test case 2" , ( ) => {
208
+ it ( ' should return min heap of test case 2' , ( ) => {
195
209
for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
196
210
p . push ( testCase2 [ i ] ) ;
197
211
}
@@ -200,7 +214,7 @@ describe("testing priority queue", () => {
200
214
expect ( actual ) . toEqual ( expected ) ;
201
215
} ) ;
202
216
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' , ( ) => {
204
218
for ( let i = 0 ; i < testCase2 . length ; i ++ ) {
205
219
p . push ( testCase2 [ i ] ) ;
206
220
}
@@ -212,4 +226,71 @@ describe("testing priority queue", () => {
212
226
expect ( actual ) . toEqual ( expected ) ;
213
227
} ) ;
214
228
} ) ;
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
+ } ) ;
215
296
} ) ;
0 commit comments