@@ -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
/*
@@ -49,7 +49,7 @@ describe('Hash table', function () {
49
49
expect ( hashTable . buckets [ 14 ] . data ) . toBe ( 'value' ) ;
50
50
expect ( hashTable . buckets [ 14 ] . next . data ) . toBe ( 'anotherValue' ) ;
51
51
} ) ;
52
- it ( 'should put() multiple K:Vs with hash collisions in properly (2).' , function ( ) {
52
+ it ( 'should put() multiple K(int) :Vs with hash collisions in properly (2).' , function ( ) {
53
53
var hashTable = new Hashtable ( ) ;
54
54
hashTable . put ( 10 , 'value' , 'someHash' ) ;
55
55
hashTable . put ( 35 , 'anotherValue' , 'someHash' ) ;
@@ -58,46 +58,102 @@ 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 get() a k:v from table properly.' , function ( ) {
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
+ } ) ;
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
} ) ;
66
- it ( 'should get() a k:v with collisions from table properly (1).' , function ( ) {
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
+ } ) ;
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' ) ;
69
98
hashTable . put ( 35 , 'anotherValue' , 'someHash' ) ;
70
99
expect ( hashTable . get ( 35 , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
71
100
} ) ;
72
- it ( 'should get() a k:v with collisions from table properly (2).' , function ( ) {
101
+ it ( 'should get() a K(int):V with collisions from table properly (2).' , function ( ) {
73
102
var hashTable = new Hashtable ( ) ;
74
103
hashTable . put ( 10 , 'value' , 'someHash' ) ;
75
104
hashTable . put ( 35 , 'anotherValue' , 'someHash' ) ;
76
105
hashTable . put ( 77 , 'lastValue' , 'someHash' ) ;
77
106
expect ( hashTable . get ( 77 , 'someHash' ) ) . toBe ( 'lastValue' ) ;
78
107
} ) ;
79
- it ( 'should get() a k:v with collisions from table properly (3).' , function ( ) {
108
+ it ( 'should get() a K(int):V with collisions from table properly (3).' , function ( ) {
80
109
var hashTable = new Hashtable ( ) ;
81
110
hashTable . put ( 10 , 'value' , 'someHash' ) ;
82
111
hashTable . put ( 35 , 'anotherValue' , 'someHash' ) ;
83
112
hashTable . put ( 77 , 'lastValue' , 'someHash' ) ;
84
113
expect ( hashTable . get ( 35 , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
85
114
} ) ;
86
- it ( 'should get() a k:v with collisions from table properly (4).' , function ( ) {
115
+ it ( 'should get() a K(int):V with collisions from table properly (4).' , function ( ) {
87
116
var hashTable = new Hashtable ( ) ;
88
117
hashTable . put ( 10 , 'value' , 'someHash' ) ;
89
118
hashTable . put ( 35 , 'anotherValue' , 'someHash' ) ;
90
119
hashTable . put ( 77 , 'lastValue' , 'someHash' ) ;
91
120
expect ( hashTable . get ( 10 , 'someHash' ) ) . toBe ( 'value' ) ;
92
121
} ) ;
93
- it ( 'should remove() a k: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' ) ;
97
153
hashTable . remove ( 10 ) ;
98
154
expect ( hashTable . get ( 10 ) ) . toBe ( undefined ) ;
99
155
} ) ;
100
- it ( 'should remove() a k:v with collisions from table properly (2).' , function ( ) {
156
+ it ( 'should remove() a K(int):V with collisions from table properly (2).' , function ( ) {
101
157
// remove start node/link in bucket : (B) - A
102
158
var hashTable = new Hashtable ( ) ;
103
159
hashTable . put ( 10 , 'value' , 'someHash' ) ;
@@ -106,7 +162,7 @@ describe('Hash table', function () {
106
162
expect ( hashTable . get ( 35 , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
107
163
expect ( hashTable . get ( 10 , 'someHash' ) ) . toBe ( undefined ) ;
108
164
} ) ;
109
- it ( 'should remove() a k:v with collisions from table properly (3).' , function ( ) {
165
+ it ( 'should remove() a K(int):V with collisions from table properly (3).' , function ( ) {
110
166
// remove start node/link in bucket : (B) - A - C
111
167
var hashTable = new Hashtable ( ) ;
112
168
hashTable . put ( 10 , 'value' , 'someHash' ) ;
@@ -116,7 +172,7 @@ describe('Hash table', function () {
116
172
expect ( hashTable . get ( 35 , 'someHash' ) ) . toBe ( 'anotherValue' ) ;
117
173
expect ( hashTable . get ( 66 , 'someHash' ) ) . toBe ( 'lastValue' ) ;
118
174
} ) ;
119
- it ( 'should remove() a k:v with collisions from table properly (4).' , function ( ) {
175
+ it ( 'should remove() a K(int):V with collisions from table properly (4).' , function ( ) {
120
176
// remove middle node/link in bucket : A - (B) - C
121
177
var hashTable = new Hashtable ( ) ;
122
178
hashTable . put ( 10 , 'value' , 'someHash' ) ;
@@ -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