forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash-table.spec.js
161 lines (160 loc) · 6.17 KB
/
hash-table.spec.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import 'mocha';
import { expect } from 'chai';
import { HashTable } from '../../../src/js/index';
import MyObj from './my-obj';
describe('HashTable', () => {
it('starts empty', () => {
const hashTable = new HashTable();
expect(hashTable.size()).to.equal(0);
expect(hashTable.isEmpty()).to.equal(true);
});
it('generates hashcode', () => {
// numbers
let hashTable = new HashTable();
expect(hashTable.hashCode(1)).to.equal(1);
expect(hashTable.hashCode(10)).to.equal(10);
expect(hashTable.hashCode(100)).to.equal(100);
expect(hashTable.hashCode(1000)).to.equal(1000);
// strings
hashTable = new HashTable();
expect(hashTable.hashCode('1')).to.equal(12);
expect(hashTable.hashCode('10')).to.equal(23);
expect(hashTable.hashCode('100')).to.equal(34);
expect(hashTable.hashCode('1000')).to.equal(8);
expect(hashTable.hashCode('a')).to.equal(23);
expect(hashTable.hashCode('A')).to.equal(28);
expect(hashTable.hashCode('Aba')).to.equal(1);
// objects
hashTable = new HashTable();
const myObjList = [];
for (let i = 1; i <= 5; i++) {
myObjList.push(new MyObj(i, i + 1));
}
expect(hashTable.hashCode(myObjList[0])).to.equal(1);
expect(hashTable.hashCode(myObjList[1])).to.equal(3);
expect(hashTable.hashCode(myObjList[2])).to.equal(5);
expect(hashTable.hashCode(myObjList[3])).to.equal(7);
expect(hashTable.hashCode(myObjList[4])).to.equal(9);
});
it('puts undefined and null keys and values', () => {
const hashTable = new HashTable();
expect(hashTable.put('undefined', undefined)).to.equal(false);
expect(hashTable.get('undefined')).to.equal(undefined);
expect(hashTable.put('undefined', 1)).to.equal(true);
expect(hashTable.get('undefined')).to.equal(1);
expect(hashTable.put('null', null)).to.equal(false);
expect(hashTable.get('null')).to.equal(undefined);
expect(hashTable.put('null', 1)).to.equal(true);
expect(hashTable.get('null')).to.equal(1);
hashTable.clear();
expect(hashTable.put(undefined, undefined)).to.equal(false);
expect(hashTable.get(undefined)).to.equal(undefined);
expect(hashTable.put(undefined, 1)).to.equal(false);
expect(hashTable.get(undefined)).to.equal(undefined);
expect(hashTable.put(null, null)).to.equal(false);
expect(hashTable.get(null)).to.equal(undefined);
expect(hashTable.put(null, 1)).to.equal(false);
expect(hashTable.get(null)).to.equal(undefined);
});
it('puts values with number key', () => {
const min = 1;
const max = 5;
const size = (max - min) + 1;
const hashTable = new HashTable();
for (let i = min; i <= max; i++) {
expect(hashTable.put(i, i)).to.equal(true);
}
expect(hashTable.size()).to.equal(size);
const table = hashTable.getTable();
for (let i = min; i <= max; i++) {
expect(table[i].key).to.equal(i);
expect(table[i].value).to.equal(i);
}
});
it('puts values with string key', () => {
const hashTable = new HashTable();
expect(hashTable.put('1', 1)).to.equal(true);
expect(hashTable.put('10', 10)).to.equal(true);
expect(hashTable.put('100', 100)).to.equal(true);
expect(hashTable.put('1000', 1000)).to.equal(true);
const table = hashTable.getTable();
expect(table[12].key).to.equal('1');
expect(table[12].value).to.equal(1);
expect(table[23].key).to.equal('10');
expect(table[23].value).to.equal(10);
expect(table[34].key).to.equal('100');
expect(table[34].value).to.equal(100);
expect(table[8].key).to.equal('1000');
expect(table[8].value).to.equal(1000);
});
it('puts values with object key', () => {
const hashTable = new HashTable();
const myObjList = [];
for (let i = 1; i <= 5; i++) {
myObjList.push(new MyObj(i, i + 1));
expect(hashTable.put(myObjList[i - 1], myObjList[i - 1])).to.equal(true);
}
const table = hashTable.getTable();
expect(table[1].key).to.equal(myObjList[0]);
expect(table[1].value).to.equal(myObjList[0]);
expect(table[3].key).to.equal(myObjList[1]);
expect(table[3].value).to.equal(myObjList[1]);
expect(table[5].key).to.equal(myObjList[2]);
expect(table[5].value).to.equal(myObjList[2]);
expect(table[7].key).to.equal(myObjList[3]);
expect(table[7].value).to.equal(myObjList[3]);
expect(table[9].key).to.equal(myObjList[4]);
expect(table[9].value).to.equal(myObjList[4]);
});
it('does NOT handle collision, replaces values', () => {
const hashTable = new HashTable();
for (let i = 0; i < 5; i++) {
expect(hashTable.put(1, i)).to.equal(true);
}
expect(hashTable.size()).to.equal(1);
});
it('removes elements', () => {
const min = 1;
const max = 5;
const size = (max - min) + 1;
const hashTable = new HashTable();
for (let i = min; i <= max; i++) {
expect(hashTable.put(i, i)).to.equal(true);
}
expect(hashTable.size()).to.equal(size);
for (let i = min; i <= max; i++) {
expect(hashTable.remove(i)).to.equal(true);
}
// elements do not exist
for (let i = min; i <= max; i++) {
expect(hashTable.remove(i)).to.equal(false);
}
expect(hashTable.isEmpty()).to.equal(true);
});
it('returns toString primitive types', () => {
const hashTable = new HashTable();
expect(hashTable.toString()).to.equal('');
hashTable.put(1, 1);
expect(hashTable.toString()).to.equal('{1 => [#1: 1]}');
hashTable.put(2, 2);
expect(hashTable.toString()).to.equal('{1 => [#1: 1]},{2 => [#2: 2]}');
hashTable.clear();
expect(hashTable.toString()).to.equal('');
});
it('returns toString primitive types', () => {
const hashTable = new HashTable();
hashTable.put('el1', 1);
expect(hashTable.toString()).to.equal('{36 => [#el1: 1]}');
hashTable.put('el2', 2);
expect(hashTable.toString()).to.equal('{0 => [#el2: 2]},{36 => [#el1: 1]}');
});
it('returns toString objects', () => {
const hashTable = new HashTable();
let myObj = new MyObj(1, 2);
hashTable.put(myObj, myObj);
expect(hashTable.toString()).to.equal('{1 => [#1|2: 1|2]}');
myObj = new MyObj(3, 4);
hashTable.put(myObj, myObj);
expect(hashTable.toString()).to.equal('{1 => [#1|2: 1|2]},{5 => [#3|4: 3|4]}');
});
});