@@ -88,14 +88,24 @@ describe('SBTree', function () {
88
88
expect ( right . parent ) . toBe ( Nil ) ;
89
89
checkNil ( ) ;
90
90
} ) ;
91
+ // Returns a random integer between min (included) and max (excluded)
92
+ // Using Math.round() will give you a non-uniform distribution!
93
+ function getRandomInt ( min , max ) {
94
+ return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
95
+ }
96
+ // Returns a random integer between min (included) and max (included)
97
+ // Using Math.round() will give you a non-uniform distribution!
98
+ function getRandomIntInclusive ( min , max ) {
99
+ return Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
100
+ }
91
101
92
102
it ( 'push and get 100000 elements, remove the array by always remove the first/last element' , function ( ) {
93
103
var sTree = new SBTree ( ) ;
94
104
for ( var i = 0 ; i < 2000000 ; ++ i ) {
95
105
sTree . push ( i ) ;
96
106
}
97
107
checkNil ( ) ;
98
- let maxHeight = 0 ;
108
+ var maxHeight = 0 ;
99
109
for ( var i = 0 ; i < 2000000 ; ++ i ) {
100
110
var node = sTree . get ( i ) ;
101
111
maxHeight = Math . max ( maxHeight , node . height ) ;
@@ -119,5 +129,31 @@ describe('SBTree', function () {
119
129
expect ( sTree . size ) . toBe ( count - i - 1 ) ;
120
130
}
121
131
checkNil ( ) ;
132
+ var expectedArray = [ ] ;
133
+ for ( var i = 0 ; i < 10000 ; ++ i ) {
134
+ var isAdded = sTree . size === 0 ;
135
+ if ( ! isAdded ) {
136
+ isAdded = getRandomIntInclusive ( 0 , 3 ) < 3 ;
137
+ }
138
+ if ( isAdded ) {
139
+ var newPos = getRandomIntInclusive ( 0 , sTree . size ) ;
140
+ sTree . insert ( newPos , i ) ;
141
+ expectedArray . splice ( newPos , 0 , i ) ;
142
+ } else {
143
+ var removedPos = getRandomInt ( 0 , sTree . size ) ;
144
+ // sTree.remove(removedPos);
145
+ //expectedArray.splice(removedPos, 1);
146
+ }
147
+ }
148
+ expect ( sTree . size ) . toBe ( expectedArray . length ) ;
149
+ maxHeight = 0 ;
150
+ for ( var i = 0 ; i < sTree . size ; ++ i ) {
151
+ var node = sTree . get ( i ) ;
152
+ maxHeight = Math . max ( maxHeight , node . height ) ;
153
+ expect ( node . value ) . toBe ( expectedArray [ i ] ) ;
154
+ //console.log(node.value, expectedArray[i]);
155
+ }
156
+ console . log ( maxHeight ) ;
157
+ checkNil ( ) ;
122
158
} ) ;
123
159
} ) ;
0 commit comments