-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
Copy pathPolynomialHash.test.js
executable file
·59 lines (49 loc) · 2.44 KB
/
PolynomialHash.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
import PolynomialHash from '../PolynomialHash';
describe('PolynomialHash', () => {
it('should calculate new hash based on previous one', () => {
const bases = [3, 79, 101, 3251, 13229, 122743, 3583213];
const mods = [79, 101];
const frameSizes = [5, 20];
// @TODO: Provide Unicode support.
const text = 'Lorem Ipsum is simply dummy text of the printing and '
+ 'typesetting industry. Lorem Ipsum has been the industry\'s standard '
+ 'galley of type and \u{ffff} scrambled it to make a type specimen book. It '
+ 'electronic 耀 typesetting, remaining essentially unchanged. It was '
// + 'popularised in the \u{20005} \u{20000}1960s with the release of Letraset sheets '
+ 'publishing software like Aldus PageMaker 耀 including versions of Lorem.';
// Check hashing for different prime base.
bases.forEach((base) => {
mods.forEach((modulus) => {
const polynomialHash = new PolynomialHash({ base, modulus });
// Check hashing for different word lengths.
frameSizes.forEach((frameSize) => {
let previousWord = text.substr(0, frameSize);
let previousHash = polynomialHash.hash(previousWord);
// Shift frame through the whole text.
for (let frameShift = 1; frameShift < (text.length - frameSize); frameShift += 1) {
const currentWord = text.substr(frameShift, frameSize);
const currentHash = polynomialHash.hash(currentWord);
const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord);
// Check that rolling hash is the same as directly calculated hash.
expect(currentRollingHash).toBe(currentHash);
previousWord = currentWord;
previousHash = currentHash;
}
});
});
});
});
it('should generate numeric hashed less than 100', () => {
const polynomialHash = new PolynomialHash({ modulus: 100 });
expect(polynomialHash.hash('Some long text that is used as a key')).toBe(41);
expect(polynomialHash.hash('Test')).toBe(92);
expect(polynomialHash.hash('a')).toBe(97);
expect(polynomialHash.hash('b')).toBe(98);
expect(polynomialHash.hash('c')).toBe(99);
expect(polynomialHash.hash('d')).toBe(0);
expect(polynomialHash.hash('e')).toBe(1);
expect(polynomialHash.hash('ab')).toBe(87);
// @TODO: Provide Unicode support.
expect(polynomialHash.hash('\u{20000}')).toBe(92);
});
});