forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHashTable.test.js
117 lines (86 loc) · 3.52 KB
/
HashTable.test.js
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
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 set, 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.set('a', 'sky-old');
hashTable.set('a', 'sky');
hashTable.set('b', 'sea');
hashTable.set('c', 'earth');
hashTable.set('d', 'ocean');
expect(hashTable.has('x')).toBe(false);
expect(hashTable.has('b')).toBe(true);
expect(hashTable.has('c')).toBe(true);
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');
expect(hashTable.get('x')).not.toBeDefined();
hashTable.delete('a');
expect(hashTable.delete('not-existing')).toBeNull();
expect(hashTable.get('a')).not.toBeDefined();
expect(hashTable.get('d')).toBe('ocean');
hashTable.set('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.set('objectKey', { prop1: 'a', prop2: 'b' });
const object = hashTable.get('objectKey');
expect(object).toBeDefined();
expect(object.prop1).toBe('a');
expect(object.prop2).toBe('b');
});
it('should track actual keys', () => {
const hashTable = new HashTable(3);
hashTable.set('a', 'sky-old');
hashTable.set('a', 'sky');
hashTable.set('b', 'sea');
hashTable.set('c', 'earth');
hashTable.set('d', 'ocean');
expect(hashTable.getKeys()).toEqual(['a', 'b', 'c', 'd']);
expect(hashTable.has('a')).toBe(true);
expect(hashTable.has('x')).toBe(false);
hashTable.delete('a');
expect(hashTable.has('a')).toBe(false);
expect(hashTable.has('b')).toBe(true);
expect(hashTable.has('x')).toBe(false);
});
it('should get all the values', () => {
const hashTable = new HashTable(3);
hashTable.set('a', 'alpha');
hashTable.set('b', 'beta');
hashTable.set('c', 'gamma');
expect(hashTable.getValues()).toEqual(['gamma', 'alpha', 'beta']);
});
it('should get all the values from empty hash table', () => {
const hashTable = new HashTable();
expect(hashTable.getValues()).toEqual([]);
});
it('should get all the values in case of hash collision', () => {
const hashTable = new HashTable(3);
// Keys `ab` and `ba` in current implementation should result in one hash (one bucket).
// We need to make sure that several items from one bucket will be serialized.
hashTable.set('ab', 'one');
hashTable.set('ba', 'two');
hashTable.set('ac', 'three');
expect(hashTable.getValues()).toEqual(['one', 'two', 'three']);
});
});