@@ -55,8 +55,7 @@ describe('add or insert to heap', function(){
55
55
binaryHeap . insert ( 1 ) ;
56
56
binaryHeap . insert ( 1 ) ;
57
57
binaryHeap . insert ( 1 ) ;
58
- } catch ( e ) {
59
- }
58
+ } catch ( e ) { }
60
59
binaryHeap . size ( ) . should . equal ( 3 ) ;
61
60
} ) ;
62
61
} ) ;
@@ -93,14 +92,13 @@ describe('clear method, isEmpty method, isFull method', function() {
93
92
binaryHeap . isFull ( ) . should . be . true ;
94
93
} ) ;
95
94
} ) ;
96
- /*
97
95
describe ( 'pop method' , function ( ) {
98
96
it ( 'should success' , function ( ) {
99
97
var binaryHeap = new BinaryHeap ( 3 ) ;
100
98
binaryHeap . add ( 1 ) ;
101
99
binaryHeap . add ( 2 ) ;
102
100
binaryHeap . add ( 3 ) ;
103
- binaryHeap.pop().should.equal(3 );
101
+ binaryHeap . pop ( ) . should . equal ( 1 ) ;
104
102
binaryHeap . size ( ) . should . equal ( 2 ) ;
105
103
} ) ;
106
104
} ) ;
@@ -109,11 +107,11 @@ describe('size method, toString method and contains method', function() {
109
107
it ( 'should be a number' , function ( ) {
110
108
var binaryHeap = new BinaryHeap ( 3 ) ;
111
109
binaryHeap . size ( ) . should . equal ( 0 ) ;
112
- binaryHeap.add(true );
113
- binaryHeap.add('xdf' );
110
+ binaryHeap . add ( 1 ) ;
111
+ binaryHeap . add ( 2 ) ;
114
112
binaryHeap . add ( 3 ) ;
115
- binaryHeap.toString().should.equal('[true,"xdf" ,3]');
116
- binaryHeap.contains(true ).should.be.true;
113
+ binaryHeap . toString ( ) . should . equal ( '[1,2 ,3]' ) ;
114
+ binaryHeap . contains ( 1 ) . should . be . true ;
117
115
binaryHeap . contains ( 0 ) . should . be . false ;
118
116
} ) ;
119
117
} ) ;
@@ -135,16 +133,27 @@ describe('grow method', function() {
135
133
binaryHeap . add ( 2 ) ;
136
134
binaryHeap . add ( 3 ) ;
137
135
binaryHeap . add ( 4 ) ;
138
- }catch(e) {
139
- }
136
+ } catch ( e ) { }
140
137
binaryHeap . size ( ) . should . equal ( 3 ) ;
141
138
binaryHeap . grow ( ) ;
142
139
try {
143
140
binaryHeap . add ( 4 ) ;
144
141
binaryHeap . add ( 5 ) ;
145
- }catch(e) {
146
- }
142
+ } catch ( e ) { }
147
143
binaryHeap . size ( ) . should . equal ( 4 ) ;
148
144
} ) ;
149
145
} ) ;
150
- */
146
+
147
+ describe ( 'main function' , function ( ) {
148
+ it ( 'pop should success' , function ( ) {
149
+ var binaryHeap = new BinaryHeap ( ) ;
150
+ var arr = [ 5 , 4 , 3 , 2 , 1 ] ;
151
+ arr . forEach ( function ( i ) {
152
+ binaryHeap . add ( i ) ;
153
+ } ) ;
154
+ binaryHeap . list . toString ( ) . should . equal ( '1,2,4,5,3' ) ;
155
+ var result = [ ] ;
156
+ while ( binaryHeap . size ( ) > 0 ) result . push ( binaryHeap . pop ( ) ) ;
157
+ result . toString ( ) . should . equal ( arr . reverse ( ) . toString ( ) ) ;
158
+ } ) ;
159
+ } ) ;
0 commit comments