|
| 1 | +import SimplePolynomialHash from '../SimplePolynomialHash'; |
| 2 | + |
| 3 | +describe('PolynomialHash', () => { |
| 4 | + it('should calculate new hash based on previous one', () => { |
| 5 | + const bases = [3, 5]; |
| 6 | + const frameSizes = [5, 10]; |
| 7 | + |
| 8 | + const text = 'Lorem Ipsum is simply dummy text of the printing and ' |
| 9 | + + 'typesetting industry. Lorem Ipsum has been the industry\'s standard ' |
| 10 | + + 'galley of type and \u{ffff} scrambled it to make a type specimen book. It ' |
| 11 | + + 'electronic 耀 typesetting, remaining essentially unchanged. It was ' |
| 12 | + + 'popularised in the 1960s with the release of Letraset sheets ' |
| 13 | + + 'publishing software like Aldus 耀 PageMaker including versions of Lorem.'; |
| 14 | + |
| 15 | + // Check hashing for different prime base. |
| 16 | + bases.forEach((base) => { |
| 17 | + const polynomialHash = new SimplePolynomialHash(base); |
| 18 | + |
| 19 | + // Check hashing for different word lengths. |
| 20 | + frameSizes.forEach((frameSize) => { |
| 21 | + let previousWord = text.substr(0, frameSize); |
| 22 | + let previousHash = polynomialHash.hash(previousWord); |
| 23 | + |
| 24 | + // Shift frame through the whole text. |
| 25 | + for (let frameShift = 1; frameShift < (text.length - frameSize); frameShift += 1) { |
| 26 | + const currentWord = text.substr(frameShift, frameSize); |
| 27 | + const currentHash = polynomialHash.hash(currentWord); |
| 28 | + const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord); |
| 29 | + |
| 30 | + // Check that rolling hash is the same as directly calculated hash. |
| 31 | + expect(currentRollingHash).toBe(currentHash); |
| 32 | + |
| 33 | + previousWord = currentWord; |
| 34 | + previousHash = currentHash; |
| 35 | + } |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should generate numeric hashed', () => { |
| 41 | + const polynomialHash = new SimplePolynomialHash(); |
| 42 | + |
| 43 | + expect(polynomialHash.hash('Test')).toBe(604944); |
| 44 | + expect(polynomialHash.hash('a')).toBe(97); |
| 45 | + expect(polynomialHash.hash('b')).toBe(98); |
| 46 | + expect(polynomialHash.hash('c')).toBe(99); |
| 47 | + expect(polynomialHash.hash('d')).toBe(100); |
| 48 | + expect(polynomialHash.hash('e')).toBe(101); |
| 49 | + expect(polynomialHash.hash('ab')).toBe(1763); |
| 50 | + expect(polynomialHash.hash('abc')).toBe(30374); |
| 51 | + }); |
| 52 | +}); |
0 commit comments