|
| 1 | +import HashTable from '../DynamicHashTable'; |
| 2 | + |
| 3 | +describe('HashTable', () => { |
| 4 | + it('should create hash table of certain size', () => { |
| 5 | + const defaultHashTable = new HashTable(); |
| 6 | + expect(defaultHashTable.buckets.length).toBe(8); |
| 7 | + |
| 8 | + const biggerHashTable = new HashTable(64); |
| 9 | + expect(biggerHashTable.buckets.length).toBe(64); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should generate proper hash for specified keys', () => { |
| 13 | + const hashTable = new HashTable(); |
| 14 | + |
| 15 | + expect(hashTable.hash('a')).toBe(1); |
| 16 | + expect(hashTable.hash('b')).toBe(2); |
| 17 | + expect(hashTable.hash('abc')).toBe(6); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should set, read and delete data with collisions', () => { |
| 21 | + const hashTable = new HashTable(3); |
| 22 | + |
| 23 | + expect(hashTable.hash('a')).toBe(1); |
| 24 | + expect(hashTable.hash('b')).toBe(2); |
| 25 | + expect(hashTable.hash('c')).toBe(0); |
| 26 | + expect(hashTable.hash('d')).toBe(1); |
| 27 | + |
| 28 | + hashTable.set('a', 'sky-old'); |
| 29 | + hashTable.set('a', 'sky'); |
| 30 | + hashTable.set('b', 'sea'); |
| 31 | + hashTable.set('c', 'earth'); |
| 32 | + hashTable.set('d', 'ocean'); |
| 33 | + |
| 34 | + expect(hashTable.has('x')).toBe(false); |
| 35 | + expect(hashTable.has('b')).toBe(true); |
| 36 | + expect(hashTable.has('c')).toBe(true); |
| 37 | + |
| 38 | + // const stringifier = value => `${value.key}:${value.value}`; |
| 39 | + |
| 40 | + // expect(hashTable.buckets[0].toString(stringifier)).toBe('c:earth'); |
| 41 | + // expect(hashTable.buckets[1].toString(stringifier)).toBe('a:sky,d:ocean'); |
| 42 | + // expect(hashTable.buckets[2].toString(stringifier)).toBe('b:sea'); |
| 43 | + |
| 44 | + expect(hashTable.get('a')).toBe('sky'); |
| 45 | + expect(hashTable.get('d')).toBe('ocean'); |
| 46 | + expect(hashTable.get('x')).not.toBeDefined(); |
| 47 | + |
| 48 | + hashTable.delete('a'); |
| 49 | + |
| 50 | + expect(hashTable.delete('not-existing')).toBeNull(); |
| 51 | + |
| 52 | + expect(hashTable.get('a')).not.toBeDefined(); |
| 53 | + expect(hashTable.get('d')).toBe('ocean'); |
| 54 | + |
| 55 | + hashTable.set('d', 'ocean-new'); |
| 56 | + expect(hashTable.get('d')).toBe('ocean-new'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should be possible to add objects to hash table', () => { |
| 60 | + const hashTable = new HashTable(); |
| 61 | + |
| 62 | + hashTable.set('objectKey', { prop1: 'a', prop2: 'b' }); |
| 63 | + |
| 64 | + const object = hashTable.get('objectKey'); |
| 65 | + expect(object).toBeDefined(); |
| 66 | + expect(object.prop1).toBe('a'); |
| 67 | + expect(object.prop2).toBe('b'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should track actual keys', () => { |
| 71 | + const hashTable = new HashTable(3); |
| 72 | + |
| 73 | + hashTable.set('a', 'sky-old'); |
| 74 | + hashTable.set('a', 'sky'); |
| 75 | + hashTable.set('b', 'sea'); |
| 76 | + hashTable.set('c', 'earth'); |
| 77 | + hashTable.set('d', 'ocean'); |
| 78 | + |
| 79 | + expect(hashTable.getKeys()).toEqual(['a', 'b', 'c', 'd']); |
| 80 | + expect(hashTable.has('a')).toBe(true); |
| 81 | + expect(hashTable.has('x')).toBe(false); |
| 82 | + |
| 83 | + hashTable.delete('a'); |
| 84 | + |
| 85 | + expect(hashTable.has('a')).toBe(false); |
| 86 | + expect(hashTable.has('b')).toBe(true); |
| 87 | + expect(hashTable.has('x')).toBe(false); |
| 88 | + }); |
| 89 | +}); |
0 commit comments