|
| 1 | +const HashMap = require('./hash-map'); |
| 2 | +// import HashMap from './hash-map'; |
| 3 | + |
| 4 | + |
| 5 | +describe('HashMap Tests', () => { |
| 6 | + let hashMap; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + hashMap = new HashMap(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('set and get basics', () => { |
| 13 | + it('should hold one null key', () => { |
| 14 | + hashMap.set(null, 1); |
| 15 | + hashMap.set(null, 2); |
| 16 | + expect(hashMap.get(null)).toBe(2); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should hold multiple null values', () => { |
| 20 | + hashMap.set(1, null); |
| 21 | + hashMap.set(2, null); |
| 22 | + expect(hashMap.get(1)).toBe(null); |
| 23 | + expect(hashMap.get(2)).toBe(null); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('#keys', () => { |
| 28 | + it('should get keys', () => { |
| 29 | + hashMap.set(0, 'foo'); |
| 30 | + hashMap.set(null, 'fox'); |
| 31 | + hashMap.set('a', 'bar'); |
| 32 | + hashMap.set({}, 'baz'); |
| 33 | + |
| 34 | + const mapIter = hashMap.keys(); |
| 35 | + |
| 36 | + expect(mapIter.next().value).toBe(0); |
| 37 | + expect(mapIter.next().value).toBe(null); |
| 38 | + expect(mapIter.next().value).toBe('a'); |
| 39 | + expect(mapIter.next().value).toEqual({}); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not have holes', () => { |
| 43 | + hashMap.set('0', 'foo'); |
| 44 | + hashMap.set(1, 'bar'); |
| 45 | + hashMap.set({}, 'baz'); |
| 46 | + |
| 47 | + hashMap.delete(1); |
| 48 | + |
| 49 | + expect([...hashMap.keys()]).toEqual(['0', {}]); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('#values', () => { |
| 54 | + it('should get values', () => { |
| 55 | + hashMap.set('0', 'foo'); |
| 56 | + hashMap.set(1, 'bar'); |
| 57 | + hashMap.set({}, 'baz'); |
| 58 | + |
| 59 | + const mapIter = hashMap.values(); |
| 60 | + |
| 61 | + expect(mapIter.next().value).toBe('foo'); |
| 62 | + expect(mapIter.next().value).toBe('bar'); |
| 63 | + expect(mapIter.next().value).toBe('baz'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not have holes', () => { |
| 67 | + hashMap.set('0', 'foo'); |
| 68 | + hashMap.set(1, 'bar'); |
| 69 | + hashMap.set({}, 'baz'); |
| 70 | + |
| 71 | + hashMap.delete(1); |
| 72 | + |
| 73 | + expect(Array.from(hashMap.values())).toEqual(['foo', 'baz']); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('#entries', () => { |
| 78 | + it('should get values', () => { |
| 79 | + hashMap.set('0', 'foo'); |
| 80 | + hashMap.set(1, 'bar'); |
| 81 | + hashMap.set({}, 'baz'); |
| 82 | + |
| 83 | + const mapIter = hashMap.entries(); |
| 84 | + |
| 85 | + expect(mapIter.next().value).toEqual(['0', 'foo']); |
| 86 | + expect(mapIter.next().value).toEqual([1, 'bar']); |
| 87 | + expect(mapIter.next().value).toEqual([{}, 'baz']); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should not have holes', () => { |
| 91 | + hashMap.set('0', 'foo'); |
| 92 | + hashMap.set(1, 'bar'); |
| 93 | + hashMap.set({}, 'baz'); |
| 94 | + |
| 95 | + hashMap.delete(1); |
| 96 | + expect(hashMap.length).toBe(2); |
| 97 | + expect(hashMap.size).toBe(2); |
| 98 | + |
| 99 | + expect(Array.from(hashMap.entries())).toEqual([ |
| 100 | + ['0', 'foo'], |
| 101 | + [{}, 'baz'], |
| 102 | + ]); |
| 103 | + |
| 104 | + expect(Array.from(hashMap)).toEqual([ |
| 105 | + ['0', 'foo'], |
| 106 | + [{}, 'baz'], |
| 107 | + ]); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('without collisions', () => { |
| 112 | + it('set and get values', () => { |
| 113 | + hashMap.set('test', 'one'); |
| 114 | + expect(hashMap.get('test')).toBe('one'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should increase size', () => { |
| 118 | + expect(hashMap.size).toBe(0); |
| 119 | + hashMap.set('test', 'uno'); |
| 120 | + expect(hashMap.size).toBe(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it('has without value', () => { |
| 124 | + expect(hashMap.size).toBe(0); |
| 125 | + hashMap.set('uno'); |
| 126 | + expect(hashMap.size).toBe(1); |
| 127 | + expect(hashMap.has('uno')).toBe(true); |
| 128 | + expect(hashMap.has('dos')).toBe(false); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should overwrite values and keep same size', () => { |
| 132 | + hashMap.set('test', 'uno'); |
| 133 | + expect(hashMap.get('test')).toBe('uno'); |
| 134 | + hashMap.set('test', 'dos'); |
| 135 | + expect(hashMap.get('test')).toBe('dos'); |
| 136 | + expect(hashMap.size).toBe(1); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should increase load factor and size', () => { |
| 140 | + expect(hashMap.getLoadFactor()).toBe(0); |
| 141 | + expect(hashMap.size).toBe(0); |
| 142 | + hashMap.set('test', 'one'); |
| 143 | + expect(hashMap.getLoadFactor()).toBeGreaterThan(0); |
| 144 | + expect(hashMap.size).toBe(1); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should return with has', () => { |
| 148 | + expect(hashMap.has('test')).toBe(false); |
| 149 | + hashMap.set('test', 'uno'); |
| 150 | + expect(hashMap.has('test')).toBe(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should delete', () => { |
| 154 | + hashMap.set('Despacito', 'Luis Fonsi'); |
| 155 | + hashMap.set('Bailando', 'Enrique Iglesias'); |
| 156 | + hashMap.set('Dura', 'Daddy Yankee'); |
| 157 | + |
| 158 | + expect(hashMap.size).toBe(3); |
| 159 | + expect(hashMap.delete('Bailando')).toBe(true); |
| 160 | + expect(hashMap.size).toBe(2); |
| 161 | + expect(hashMap.delete('Bailando')).toBe(false); |
| 162 | + expect(hashMap.get('Bailando')).toBe(undefined); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('with many values (and collisions)', () => { |
| 167 | + beforeEach(() => { |
| 168 | + hashMap = new HashMap(1, Number.MAX_SAFE_INTEGER); |
| 169 | + |
| 170 | + hashMap.set('Pineapple', 'Pen Pineapple Apple Pen'); |
| 171 | + hashMap.set('Despacito', 'Luis Fonsi'); |
| 172 | + hashMap.set('Bailando', 'Enrique Iglesias'); |
| 173 | + hashMap.set('Dura', 'Daddy Yankee'); |
| 174 | + hashMap.set('Lean On', 'Major Lazer'); |
| 175 | + hashMap.set('Hello', 'Adele'); |
| 176 | + hashMap.set('All About That Bass', 'Meghan Trainor'); |
| 177 | + hashMap.set('This Is What You Came For', 'Calvin Harris '); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should count collisions', () => { |
| 181 | + expect(hashMap.collisions).toBe(7); |
| 182 | + }); |
| 183 | + |
| 184 | + it('gets values', () => { |
| 185 | + hashMap.set('test', 'one'); |
| 186 | + expect(hashMap.get('test')).toBe('one'); |
| 187 | + expect(hashMap.get('Dura')).toBe('Daddy Yankee'); |
| 188 | + expect(hashMap.get('Bailando')).toBe('Enrique Iglesias'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should increase load factor and size', () => { |
| 192 | + expect(hashMap.getLoadFactor()).toBe(8); |
| 193 | + expect(hashMap.size).toBe(8); |
| 194 | + hashMap.set('test', 'one'); |
| 195 | + expect(hashMap.getLoadFactor()).toBe(9); |
| 196 | + expect(hashMap.size).toBe(9); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should overwrite values and keep same size', () => { |
| 200 | + hashMap.set('test', 'uno'); |
| 201 | + expect(hashMap.get('test')).toBe('uno'); |
| 202 | + hashMap.set('test', 'dos'); |
| 203 | + expect(hashMap.get('test')).toBe('dos'); |
| 204 | + expect(hashMap.size).toBe(9); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should return with has', () => { |
| 208 | + expect(hashMap.has('test')).toBe(false); |
| 209 | + hashMap.set('test', 'uno'); |
| 210 | + expect(hashMap.has('test')).toBe(true); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should update keys on deletes', () => { |
| 214 | + // expect(hashMap.size).toBe(8); |
| 215 | + // expect(hashMap.has('Hello')).toBe(true); |
| 216 | + expect(hashMap.delete('Hello')).toBe(true); |
| 217 | + // expect(hashMap.has('Hello')).toBe(false); |
| 218 | + // expect(hashMap.size).toBe(7); |
| 219 | + expect(hashMap.delete('Lean On')).toBe(true); |
| 220 | + // expect(hashMap.has('Lean On')).toBe(false); |
| 221 | + // expect(hashMap.size).toBe(6); |
| 222 | + expect(hashMap.delete('Pineapple')).toBe(true); |
| 223 | + // expect(hashMap.has('Pineapple')).toBe(false); |
| 224 | + // expect(hashMap.size).toBe(5); |
| 225 | + expect(hashMap.delete('All About That Bass')).toBe(true); |
| 226 | + expect(hashMap.has('All About That Bass')).toBe(false); |
| 227 | + // expect(hashMap.size).toBe(4); |
| 228 | + expect(hashMap.delete('This Is What You Came For')).toBe(true); |
| 229 | + expect(hashMap.has('This Is What You Came For')).toBe(false); |
| 230 | + expect(hashMap.size).toBe(3); |
| 231 | + |
| 232 | + expect(Array.from(hashMap.keys())).toEqual(['Despacito', 'Bailando', 'Dura']); |
| 233 | + |
| 234 | + expect(hashMap.delete('Bailando')).toBe(true); |
| 235 | + expect(hashMap.delete('Bailando')).toBe(false); |
| 236 | + expect(hashMap.get('Bailando')).toBe(undefined); |
| 237 | + |
| 238 | + expect(hashMap.size).toBe(2); |
| 239 | + expect([...hashMap.keys()]).toEqual(['Despacito', 'Dura']); |
| 240 | + }); |
| 241 | + }); |
| 242 | + |
| 243 | + describe('#rehash', () => { |
| 244 | + beforeEach(() => { |
| 245 | + hashMap = new HashMap(1, 11); |
| 246 | + |
| 247 | + hashMap.set('Pineapple', 'Pen Pineapple Apple Pen'); |
| 248 | + hashMap.set('Despacito', 'Luis Fonsi'); |
| 249 | + hashMap.set('Bailando', 'Enrique Iglesias'); |
| 250 | + hashMap.set('Dura', 'Daddy Yankee'); |
| 251 | + hashMap.set('Lean On', 'Major Lazer'); |
| 252 | + hashMap.set('Hello', 'Adele'); |
| 253 | + hashMap.set('All About That Bass', 'Meghan Trainor'); |
| 254 | + hashMap.set('Wake Me Up', 'Avicii'); |
| 255 | + hashMap.set('Brother', 'Avicii'); |
| 256 | + hashMap.set('Faded', 'Alan Walker'); |
| 257 | + hashMap.set('The Spectre', 'Alan Walker'); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should rehash', () => { |
| 261 | + expect(hashMap.collisions).toBe(10); |
| 262 | + expect(hashMap.getLoadFactor()).toBe(11); |
| 263 | + expect(hashMap.buckets.length).toBe(1); |
| 264 | + |
| 265 | + expect(hashMap.keysTrackerIndex).toBe(11); |
| 266 | + hashMap.delete('All About That Bass'); |
| 267 | + hashMap.set('All About That Bass', 'Meghan Trainor'); |
| 268 | + expect(hashMap.keysTrackerIndex).toBe(12); |
| 269 | + // should hava a hole |
| 270 | + expect(hashMap.keysTrackerArray).toEqual(['Pineapple', 'Despacito', 'Bailando', 'Dura', 'Lean On', 'Hello', |
| 271 | + undefined, |
| 272 | + 'Wake Me Up', 'Brother', 'Faded', 'The Spectre', 'All About That Bass']); |
| 273 | + |
| 274 | + hashMap.loadFactor = 0.75; |
| 275 | + hashMap.set('Alone', 'Alan Walker'); |
| 276 | + |
| 277 | + // rehash |
| 278 | + expect(hashMap.keysTrackerIndex).toBe(12); |
| 279 | + expect(hashMap.collisions).toBeLessThan(10); |
| 280 | + expect(hashMap.buckets.length).toBe(29); // <- next prime |
| 281 | + expect(hashMap.getLoadFactor()).toBe(12 / 29); |
| 282 | + |
| 283 | + hashMap.set('Rolling in the Deep', 'Adele'); |
| 284 | + |
| 285 | + expect(hashMap.get('Dura')).toBe('Daddy Yankee'); |
| 286 | + expect(hashMap.get('Bailando')).toBe('Enrique Iglesias'); |
| 287 | + expect(hashMap.get('Alone')).toBe('Alan Walker'); |
| 288 | + |
| 289 | + expect(Array.from(hashMap.keys()).length).toBe(13); |
| 290 | + expect(hashMap.size).toBe(13); |
| 291 | + expect(hashMap.keysTrackerIndex).toBe(13); |
| 292 | + // after the rehash the hole should have been removed |
| 293 | + expect(hashMap.keysTrackerArray).toEqual(['Pineapple', 'Despacito', |
| 294 | + 'Bailando', 'Dura', 'Lean On', 'Hello', 'Wake Me Up', 'Brother', |
| 295 | + 'Faded', 'The Spectre', 'All About That Bass', 'Alone', |
| 296 | + 'Rolling in the Deep']); |
| 297 | + }); |
| 298 | + }); |
| 299 | +}); |
0 commit comments