|
| 1 | +import { HashTable } from './hash-table'; |
| 2 | + |
| 3 | +describe('HashTable', () => { |
| 4 | + it('should create an instance', () => { |
| 5 | + const hashTable = new HashTable<string, number>(); |
| 6 | + expect(hashTable).toBeInstanceOf(HashTable); |
| 7 | + expect(hashTable.size()).toBe(0); |
| 8 | + expect(hashTable.keys()).toEqual([]); |
| 9 | + expect(hashTable.values()).toEqual([]); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should set and get values', () => { |
| 13 | + const hashTable = new HashTable<string, number>(); |
| 14 | + hashTable.set('one', 1); |
| 15 | + expect(hashTable.size()).toBe(1); |
| 16 | + expect(hashTable.get('one')).toBe(1); |
| 17 | + hashTable.set('two', 2); |
| 18 | + expect(hashTable.size()).toBe(2); |
| 19 | + expect(hashTable.get('two')).toBe(2); |
| 20 | + hashTable.set('one', 10); |
| 21 | + expect(hashTable.size()).toBe(2); |
| 22 | + expect(hashTable.get('one')).toBe(10); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should remove values', () => { |
| 26 | + const hashTable = new HashTable<string, number>(); |
| 27 | + hashTable.set('one', 1); |
| 28 | + hashTable.set('two', 2); |
| 29 | + expect(hashTable.size()).toBe(2); |
| 30 | + hashTable.remove('one'); |
| 31 | + expect(hashTable.size()).toBe(1); |
| 32 | + expect(hashTable.get('one')).toBeUndefined(); |
| 33 | + expect(hashTable.get('two')).toBe(2); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should check if key exists', () => { |
| 37 | + const hashTable = new HashTable<string, number>(); |
| 38 | + hashTable.set('one', 1); |
| 39 | + expect(hashTable.has('one')).toBe(true); |
| 40 | + expect(hashTable.has('two')).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should clear the hash table', () => { |
| 44 | + const hashTable = new HashTable<string, number>(); |
| 45 | + hashTable.set('one', 1); |
| 46 | + hashTable.set('two', 2); |
| 47 | + expect(hashTable.size()).toBe(2); |
| 48 | + hashTable.clear(); |
| 49 | + expect(hashTable.size()).toBe(0); |
| 50 | + expect(hashTable.get('one')).toBeUndefined(); |
| 51 | + expect(hashTable.get('two')).toBeUndefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return all keys and values', () => { |
| 55 | + const hashTable = new HashTable<string, number>(); |
| 56 | + hashTable.set('one', 1); |
| 57 | + hashTable.set('two', 2); |
| 58 | + hashTable.set('three', 3); |
| 59 | + expect(hashTable.keys().sort()).toEqual(['one', 'three', 'two']); |
| 60 | + expect(hashTable.values().sort()).toEqual([1, 2, 3]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should increase the size when adding a new key', () => { |
| 64 | + const hashTable = new HashTable<string, number>(1); |
| 65 | + hashTable.set('one', 1); |
| 66 | + hashTable.set('two', 2); |
| 67 | + hashTable.set('three', 3); |
| 68 | + expect(hashTable.size()).toBe(3); |
| 69 | + expect(hashTable.get('one')).toBe(1); |
| 70 | + expect(hashTable.get('two')).toBe(2); |
| 71 | + expect(hashTable.get('three')).toBe(3); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle different key types', () => { |
| 75 | + const hashTable = new HashTable<number | string, string>(); |
| 76 | + hashTable.set(1, 'one'); |
| 77 | + hashTable.set('two', 'two'); |
| 78 | + expect(hashTable.get(1)).toBe('one'); |
| 79 | + expect(hashTable.get('two')).toBe('two'); |
| 80 | + expect(hashTable.size()).toBe(2); |
| 81 | + }); |
| 82 | +}); |
0 commit comments