forked from SiZapPaaiGwat/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash-table.spec.js
More file actions
221 lines (218 loc) · 10 KB
/
hash-table.spec.js
File metadata and controls
221 lines (218 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
var mod = require('../../src/data-structures/hash-table.js');
var Node = mod.Node;
var Hashtable = mod.Hashtable;
describe('Node', function () {
it('should be a constructor function', function () {
expect(typeof Node).toBe('function');
});
});
describe('Hash table', function () {
it('should be a constructor function.', function () {
expect(typeof Hashtable).toBe('function');
});
it('should start with empty table.', function () {
expect(new Hashtable().buckets.length).toBe(0);
});
it('should put() K(int):V in table properly.', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value');
expect(hashTable.buckets[10].data).toBe('value');
});
it('should put() K(string):V in table properly.', function () {
var hashTable = new Hashtable();
hashTable.put('key', 'value');
/*
'key' hashCode()'s to 106079. Then the hash is adjusted to fit
the number of configurable buckets (array size).
106079 % 100 (100 is default maxBucketCount)
result is 79.
This is done to avoid using get() since it's untested at this point.
*/
expect(hashTable.buckets[79].data).toBe('value');
});
it('should put() multiple K(int):Vs with hash collisions in properly (1).', function () {
var hashTable = new Hashtable();
// Same hash so going to same bucket, but different keys. Collision.
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
/*
'someHash' hashCode()'s to 1504481314. Then the hash is adjusted to fit
the number of configurable buckets (array size).
1504481314 % 100 (100 is default maxBucketCount)
result is 14.
This is done to avoid using get() since it's untested at this point.
*/
expect(hashTable.buckets[14].data).toBe('value');
expect(hashTable.buckets[14].next.data).toBe('anotherValue');
});
it('should put() multiple K(int):Vs with hash collisions in properly (2).', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
hashTable.put(77, 'lastValue', 'someHash');
expect(hashTable.buckets[14].data).toBe('value');
expect(hashTable.buckets[14].next.data).toBe('anotherValue');
expect(hashTable.buckets[14].next.next.data).toBe('lastValue');
});
it('should put() multiple K(string):Vs with hash collisions in properly (1).', function () {
var hashTable = new Hashtable();
// Same hash so going to same bucket, but different keys. Collision.
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
/*
'someHash' hashCode()'s to 1504481314. Then the hash is adjusted to fit
the number of configurable buckets (array size).
1504481314 % 100 (100 is default maxBucketCount)
result is 14.
This is done to avoid using get() since it's untested at this point.
*/
expect(hashTable.buckets[14].data).toBe('value');
expect(hashTable.buckets[14].next.data).toBe('anotherValue');
});
it('should put() multiple K(string):Vs with hash collisions in properly (2).', function () {
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
hashTable.put('keyC', 'lastValue', 'someHash');
expect(hashTable.buckets[14].data).toBe('value');
expect(hashTable.buckets[14].next.data).toBe('anotherValue');
expect(hashTable.buckets[14].next.next.data).toBe('lastValue');
});
it('should get() a K(int):V from table properly.', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value');
expect(hashTable.get(10)).toBe('value');
});
it('should get() a K(string):V from table properly.', function () {
var hashTable = new Hashtable();
hashTable.put('keyA', 'value');
expect(hashTable.get('keyA')).toBe('value');
});
it('should get() a K(int):V with collisions from table properly (1).', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
expect(hashTable.get(35, 'someHash')).toBe('anotherValue');
});
it('should get() a K(int):V with collisions from table properly (2).', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
hashTable.put(77, 'lastValue', 'someHash');
expect(hashTable.get(77, 'someHash')).toBe('lastValue');
});
it('should get() a K(int):V with collisions from table properly (3).', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
hashTable.put(77, 'lastValue', 'someHash');
expect(hashTable.get(35, 'someHash')).toBe('anotherValue');
});
it('should get() a K(int):V with collisions from table properly (4).', function () {
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
hashTable.put(77, 'lastValue', 'someHash');
expect(hashTable.get(10, 'someHash')).toBe('value');
});
it('should get() a K(string):V with collisions from table properly (1).', function () {
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
expect(hashTable.get('keyB', 'someHash')).toBe('anotherValue');
});
it('should get() a K(string):V with collisions from table properly (2).', function () {
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
hashTable.put('keyC', 'lastValue', 'someHash');
expect(hashTable.get('keyC', 'someHash')).toBe('lastValue');
});
it('should get() a K(string):V with collisions from table properly (3).', function () {
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
hashTable.put('keyC', 'lastValue', 'someHash');
expect(hashTable.get('keyB', 'someHash')).toBe('anotherValue');
});
it('should get() a K(string):V with collisions from table properly (4).', function () {
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
hashTable.put('keyC', 'lastValue', 'someHash');
expect(hashTable.get('keyA', 'someHash')).toBe('value');
});
it('should remove() a K(int):V from table properly (1).', function () {
// remove only node/link in bucket : (B)
var hashTable = new Hashtable();
hashTable.put(10, 'value');
hashTable.remove(10);
expect(hashTable.get(10)).toBe(undefined);
});
it('should remove() a K(int):V with collisions from table properly (2).', function () {
// remove start node/link in bucket : (B) - A
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
expect(hashTable.remove(10, 'someHash')).toBe('value');
expect(hashTable.get(35, 'someHash')).toBe('anotherValue');
expect(hashTable.get(10, 'someHash')).toBe(undefined);
});
it('should remove() a K(int):V with collisions from table properly (3).', function () {
// remove start node/link in bucket : (B) - A - C
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
hashTable.put(66, 'lastValue', 'someHash');
expect(hashTable.remove(10, 'someHash')).toBe('value');
expect(hashTable.get(35, 'someHash')).toBe('anotherValue');
expect(hashTable.get(66, 'someHash')).toBe('lastValue');
});
it('should remove() a K(int):V with collisions from table properly (4).', function () {
// remove middle node/link in bucket : A - (B) - C
var hashTable = new Hashtable();
hashTable.put(10, 'value', 'someHash');
hashTable.put(35, 'anotherValue', 'someHash');
hashTable.put(66, 'lastValue', 'someHash');
expect(hashTable.remove(35, 'someHash')).toBe('anotherValue');
expect(hashTable.get(10, 'someHash')).toBe('value');
expect(hashTable.get(66, 'someHash')).toBe('lastValue');
});
it('should remove() a K(string):V from table properly (1).', function () {
// remove only node/link in bucket : (B)
var hashTable = new Hashtable();
hashTable.put('keyA', 'value');
hashTable.remove('keyA');
expect(hashTable.get('keyA')).toBe(undefined);
});
it('should remove() a K(string):V with collisions from table properly (2).', function () {
// remove start node/link in bucket : (B) - A
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
expect(hashTable.remove('keyA', 'someHash')).toBe('value');
expect(hashTable.get('keyB', 'someHash')).toBe('anotherValue');
expect(hashTable.get('keyA', 'someHash')).toBe(undefined);
});
it('should remove() a K(string):V with collisions from table properly (3).', function () {
// remove start node/link in bucket : (B) - A - C
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
hashTable.put('keyC', 'lastValue', 'someHash');
expect(hashTable.remove('keyA', 'someHash')).toBe('value');
expect(hashTable.get('keyB', 'someHash')).toBe('anotherValue');
expect(hashTable.get('keyC', 'someHash')).toBe('lastValue');
});
it('should remove() a K(string):V with collisions from table properly (4).', function () {
// remove middle node/link in bucket : A - (B) - C
var hashTable = new Hashtable();
hashTable.put('keyA', 'value', 'someHash');
hashTable.put('keyB', 'anotherValue', 'someHash');
hashTable.put('keyC', 'lastValue', 'someHash');
expect(hashTable.remove('keyB', 'someHash')).toBe('anotherValue');
expect(hashTable.get('keyA', 'someHash')).toBe('value');
expect(hashTable.get('keyC', 'someHash')).toBe('lastValue');
});
});