@@ -7,16 +7,68 @@ describe(MinimumHeap.name, () => {
7
7
it ( 'iterates in sorted order' , ( ) => {
8
8
const comparator : ( a : number , b : number ) => number = ( a : number , b : number ) => a - b ;
9
9
10
- const heap : MinimumHeap < number > = new MinimumHeap < number > ( comparator ) ;
11
- for ( const x of [ 1 , 3 , - 2 , 9 , 6 , 12 , 11 , 0 , - 5 , 2 , 3 , 1 , - 21 ] ) {
10
+ const inputs : number [ ] = [ ] ;
11
+ for ( let heapSize : number = 1 ; heapSize < 100 ; heapSize ++ ) {
12
+ const heap : MinimumHeap < number > = new MinimumHeap < number > ( comparator ) ;
13
+ inputs . length = 0 ;
14
+ for ( let i = 0 ; i < heapSize ; i ++ ) {
15
+ const x : number = Math . random ( ) ;
16
+ inputs . push ( x ) ;
17
+ heap . push ( x ) ;
18
+ }
19
+
20
+ const iterationResults : number [ ] = [ ] ;
21
+ while ( heap . size > 0 ) {
22
+ iterationResults . push ( heap . poll ( ) ! ) ;
23
+ }
24
+
25
+ expect ( iterationResults ) . toEqual ( inputs . sort ( comparator ) ) ;
26
+ }
27
+ } ) ;
28
+
29
+ it ( 'returns all input objects' , ( ) => {
30
+ const comparator : ( a : { } , b : { } ) => number = ( a : { } , b : { } ) => 0 ;
31
+
32
+ const heap : MinimumHeap < { } > = new MinimumHeap < { } > ( comparator ) ;
33
+ const inputs : Set < { } > = new Set ( [ { } , { } , { } , { } , { } , { } ] ) ;
34
+ for ( const x of inputs ) {
12
35
heap . push ( x ) ;
13
36
}
14
37
15
- const iterationResults : number [ ] = [ ] ;
38
+ const iterationResults : Set < { } > = new Set ( ) ;
16
39
while ( heap . size > 0 ) {
17
- iterationResults . push ( heap . poll ( ) ! ) ;
40
+ iterationResults . add ( heap . poll ( ) ! ) ;
18
41
}
19
42
20
- expect ( iterationResults ) . toEqual ( iterationResults . slice ( ) . sort ( comparator ) ) ;
43
+ expect ( iterationResults . size ) . toEqual ( inputs . size ) ;
44
+ } ) ;
45
+
46
+ it ( 'handles interleaved push and poll' , ( ) => {
47
+ const comparator : ( a : { } , b : { } ) => number = ( a : { } , b : { } ) => 0 ;
48
+
49
+ const heap : MinimumHeap < { } > = new MinimumHeap < { } > ( comparator ) ;
50
+ const input1 : Set < { } > = new Set ( ) ;
51
+ const input2 : Set < { } > = new Set ( ) ;
52
+ for ( let heapSize : number = 1 ; heapSize < 100 ; heapSize ++ ) {
53
+ input1 . add ( { } ) ;
54
+ input2 . add ( { } ) ;
55
+
56
+ const iterationResults : Set < { } > = new Set ( ) ;
57
+
58
+ for ( const x of input1 ) {
59
+ heap . push ( x ) ;
60
+ }
61
+
62
+ for ( const x of input2 ) {
63
+ iterationResults . add ( heap . poll ( ) ! ) ;
64
+ heap . push ( x ) ;
65
+ }
66
+
67
+ while ( heap . size > 0 ) {
68
+ iterationResults . add ( heap . poll ( ) ! ) ;
69
+ }
70
+
71
+ expect ( iterationResults . size ) . toEqual ( input1 . size + input2 . size ) ;
72
+ }
21
73
} ) ;
22
74
} ) ;
0 commit comments