forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.test.js
More file actions
64 lines (47 loc) · 1.96 KB
/
HashTable.test.js
File metadata and controls
64 lines (47 loc) · 1.96 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
import HashTable from '../HashTable';
describe('HashTable', () => {
it('should create hash table of certain size', () => {
const defaultHashTable = new HashTable();
expect(defaultHashTable.buckets.length).toBe(32);
const biggerHashTable = new HashTable(64);
expect(biggerHashTable.buckets.length).toBe(64);
});
it('should generate proper hash for specified keys', () => {
const hashTable = new HashTable();
expect(hashTable.hash('a')).toBe(1);
expect(hashTable.hash('b')).toBe(2);
expect(hashTable.hash('abc')).toBe(6);
});
it('should insert, read and delete data with collisions', () => {
const hashTable = new HashTable(3);
expect(hashTable.hash('a')).toBe(1);
expect(hashTable.hash('b')).toBe(2);
expect(hashTable.hash('c')).toBe(0);
expect(hashTable.hash('d')).toBe(1);
hashTable.insert('a', 'sky-old');
hashTable.insert('a', 'sky');
hashTable.insert('b', 'sea');
hashTable.insert('c', 'earth');
hashTable.insert('d', 'ocean');
const stringifier = value => `${value.key}:${value.value}`;
expect(hashTable.buckets[0].toString(stringifier)).toBe('c:earth');
expect(hashTable.buckets[1].toString(stringifier)).toBe('a:sky,d:ocean');
expect(hashTable.buckets[2].toString(stringifier)).toBe('b:sea');
expect(hashTable.get('a')).toBe('sky');
expect(hashTable.get('d')).toBe('ocean');
hashTable.delete('a');
expect(hashTable.delete('not-existing')).toBeNull();
expect(hashTable.get('a')).toBeNull();
expect(hashTable.get('d')).toBe('ocean');
hashTable.insert('d', 'ocean-new');
expect(hashTable.get('d')).toBe('ocean-new');
});
it('should be possible to add objects to hash table', () => {
const hashTable = new HashTable();
hashTable.insert('objectKey', { prop1: 'a', prop2: 'b' });
const object = hashTable.get('objectKey');
expect(object).toBeDefined();
expect(object.prop1).toBe('a');
expect(object.prop2).toBe('b');
});
});