@@ -22,7 +22,7 @@ describe('Hash table', function () {
22
22
hashTable . put ( 10 , 'value' ) ;
23
23
expect ( hashTable . buckets [ 10 ] . data ) . toBe ( 'value' ) ;
24
24
} ) ;
25
- it ( 'should put() K(str ):V in table properly.' , function ( ) {
25
+ it ( 'should put() K(string ):V in table properly.' , function ( ) {
26
26
var hashTable = new Hashtable ( ) ;
27
27
hashTable . put ( 'key' , 'value' ) ;
28
28
/*
@@ -58,11 +58,40 @@ describe('Hash table', function () {
58
58
expect ( hashTable . buckets [ 14 ] . next . data ) . toBe ( 'anotherValue' ) ;
59
59
expect ( hashTable . buckets [ 14 ] . next . next . data ) . toBe ( 'lastValue' ) ;
60
60
} ) ;
61
+ it ( 'should put() multiple K(string):Vs with hash collisions in properly (1).' , function ( ) {
62
+ var hashTable = new Hashtable ( ) ;
63
+ // Same hash so going to same bucket, but different keys. Collision.
64
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
65
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
66
+ /*
67
+ 'someHash' hashCode()'s to 1504481314. Then the hash is adjusted to fit
68
+ the number of configurable buckets (array size).
69
+ 1504481314 % 100 (100 is default maxBucketCount)
70
+ result is 14.
71
+ This is done to avoid using get() since it's untested at this point.
72
+ */
73
+ expect ( hashTable . buckets [ 14 ] . data ) . toBe ( 'value' ) ;
74
+ expect ( hashTable . buckets [ 14 ] . next . data ) . toBe ( 'anotherValue' ) ;
75
+ } ) ;
76
+ it ( 'should put() multiple K(string):Vs with hash collisions in properly (2).' , function ( ) {
77
+ var hashTable = new Hashtable ( ) ;
78
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
79
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
80
+ hashTable . put ( 'keyC' , 'lastValue' , 'someHash' ) ;
81
+ expect ( hashTable . buckets [ 14 ] . data ) . toBe ( 'value' ) ;
82
+ expect ( hashTable . buckets [ 14 ] . next . data ) . toBe ( 'anotherValue' ) ;
83
+ expect ( hashTable . buckets [ 14 ] . next . next . data ) . toBe ( 'lastValue' ) ;
84
+ } ) ;
61
85
it ( 'should get() a K(int):V from table properly.' , function ( ) {
62
86
var hashTable = new Hashtable ( ) ;
63
87
hashTable . put ( 10 , 'value' ) ;
64
88
expect ( hashTable . get ( 10 ) ) . toBe ( 'value' ) ;
65
89
} ) ;
90
+ it ( 'should get() a K(string):V from table properly.' , function ( ) {
91
+ var hashTable = new Hashtable ( ) ;
92
+ hashTable . put ( 'keyA' , 'value' ) ;
93
+ expect ( hashTable . get ( 'keyA' ) ) . toBe ( 'value' ) ;
94
+ } ) ;
66
95
it ( 'should get() a K(int):V with collisions from table properly (1).' , function ( ) {
67
96
var hashTable = new Hashtable ( ) ;
68
97
hashTable . put ( 10 , 'value' , 'someHash' ) ;
@@ -90,7 +119,34 @@ describe('Hash table', function () {
90
119
hashTable . put ( 77 , 'lastValue' , 'someHash' ) ;
91
120
expect ( hashTable . get ( 10 , 'someHash' ) ) . toBe ( 'value' ) ;
92
121
} ) ;
93
- it ( 'should remove() a K(int):V from table properly.' , function ( ) {
122
+ it ( 'should get() a K(string):V with collisions from table properly (1).' , function ( ) {
123
+ var hashTable = new Hashtable ( ) ;
124
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
125
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
126
+ expect ( hashTable . get ( 'keyB' , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
127
+ } ) ;
128
+ it ( 'should get() a K(string):V with collisions from table properly (2).' , function ( ) {
129
+ var hashTable = new Hashtable ( ) ;
130
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
131
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
132
+ hashTable . put ( 'keyC' , 'lastValue' , 'someHash' ) ;
133
+ expect ( hashTable . get ( 'keyC' , 'someHash' ) ) . toBe ( 'lastValue' ) ;
134
+ } ) ;
135
+ it ( 'should get() a K(string):V with collisions from table properly (3).' , function ( ) {
136
+ var hashTable = new Hashtable ( ) ;
137
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
138
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
139
+ hashTable . put ( 'keyC' , 'lastValue' , 'someHash' ) ;
140
+ expect ( hashTable . get ( 'keyB' , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
141
+ } ) ;
142
+ it ( 'should get() a K(string):V with collisions from table properly (4).' , function ( ) {
143
+ var hashTable = new Hashtable ( ) ;
144
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
145
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
146
+ hashTable . put ( 'keyC' , 'lastValue' , 'someHash' ) ;
147
+ expect ( hashTable . get ( 'keyA' , 'someHash' ) ) . toBe ( 'value' ) ;
148
+ } ) ;
149
+ it ( 'should remove() a K(int):V from table properly (1).' , function ( ) {
94
150
// remove only node/link in bucket : (B)
95
151
var hashTable = new Hashtable ( ) ;
96
152
hashTable . put ( 10 , 'value' ) ;
@@ -126,4 +182,40 @@ describe('Hash table', function () {
126
182
expect ( hashTable . get ( 10 , 'someHash' ) ) . toBe ( 'value' ) ;
127
183
expect ( hashTable . get ( 66 , 'someHash' ) ) . toBe ( 'lastValue' ) ;
128
184
} ) ;
185
+ it ( 'should remove() a K(string):V from table properly (1).' , function ( ) {
186
+ // remove only node/link in bucket : (B)
187
+ var hashTable = new Hashtable ( ) ;
188
+ hashTable . put ( 'keyA' , 'value' ) ;
189
+ hashTable . remove ( 'keyA' ) ;
190
+ expect ( hashTable . get ( 'keyA' ) ) . toBe ( undefined ) ;
191
+ } ) ;
192
+ it ( 'should remove() a K(string):V with collisions from table properly (2).' , function ( ) {
193
+ // remove start node/link in bucket : (B) - A
194
+ var hashTable = new Hashtable ( ) ;
195
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
196
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
197
+ expect ( hashTable . remove ( 'keyA' , 'someHash' ) ) . toBe ( 'value' ) ;
198
+ expect ( hashTable . get ( 'keyB' , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
199
+ expect ( hashTable . get ( 'keyA' , 'someHash' ) ) . toBe ( undefined ) ;
200
+ } ) ;
201
+ it ( 'should remove() a K(string):V with collisions from table properly (3).' , function ( ) {
202
+ // remove start node/link in bucket : (B) - A - C
203
+ var hashTable = new Hashtable ( ) ;
204
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
205
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
206
+ hashTable . put ( 'keyC' , 'lastValue' , 'someHash' ) ;
207
+ expect ( hashTable . remove ( 'keyA' , 'someHash' ) ) . toBe ( 'value' ) ;
208
+ expect ( hashTable . get ( 'keyB' , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
209
+ expect ( hashTable . get ( 'keyC' , 'someHash' ) ) . toBe ( 'lastValue' ) ;
210
+ } ) ;
211
+ it ( 'should remove() a K(string):V with collisions from table properly (4).' , function ( ) {
212
+ // remove middle node/link in bucket : A - (B) - C
213
+ var hashTable = new Hashtable ( ) ;
214
+ hashTable . put ( 'keyA' , 'value' , 'someHash' ) ;
215
+ hashTable . put ( 'keyB' , 'anotherValue' , 'someHash' ) ;
216
+ hashTable . put ( 'keyC' , 'lastValue' , 'someHash' ) ;
217
+ expect ( hashTable . remove ( 'keyB' , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
218
+ expect ( hashTable . get ( 'keyA' , 'someHash' ) ) . toBe ( 'value' ) ;
219
+ expect ( hashTable . get ( 'keyC' , 'someHash' ) ) . toBe ( 'lastValue' ) ;
220
+ } ) ;
129
221
} ) ;
0 commit comments