|
| 1 | +import PolynomialHash from '../PolynomialHash'; |
| 2 | + |
| 3 | +describe('PolynomialHash', () => { |
| 4 | + it('should calculate new hash based on previous one', () => { |
| 5 | + // const primes = [3, 79, 101, 3251, 13229, 122743, 3583213]; |
| 6 | + // const frameSizes = [5, 20]; |
| 7 | + |
| 8 | + const primes = [3]; |
| 9 | + const frameSizes = [20]; |
| 10 | + |
| 11 | + const text = 'Lorem Ipsum is simply dummy text of the printing and ' |
| 12 | + + 'typesetting industry. Lorem Ipsum has been the industry\'s standard ' |
| 13 | + + 'galley of type and \u{ffff} scrambled it to make a type specimen book. It ' |
| 14 | + + 'electronic 耀 typesetting, remaining essentially unchanged. It was ' |
| 15 | + + 'popularised in the \u{20005} \u{20000}1960s with the release of Letraset sheets ' |
| 16 | + + 'publishing software like Aldus PageMaker 耀 including versions of Lorem.'; |
| 17 | + |
| 18 | + // Check hashing for different prime base. |
| 19 | + primes.forEach((prime) => { |
| 20 | + const polynomialHash = new PolynomialHash(prime); |
| 21 | + |
| 22 | + // Check hashing for different word lengths. |
| 23 | + frameSizes.forEach((frameSize) => { |
| 24 | + let previousWord = text.substr(0, frameSize); |
| 25 | + let previousHash = polynomialHash.hash(previousWord); |
| 26 | + |
| 27 | + // Shift frame through the whole text. |
| 28 | + for (let frameShift = 1; frameShift < (text.length - frameSize); frameShift += 1) { |
| 29 | + const currentWord = text.substr(frameShift, frameSize); |
| 30 | + const currentHash = polynomialHash.hash(currentWord); |
| 31 | + const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord); |
| 32 | + |
| 33 | + // Check that rolling hash is the same as directly calculated hash. |
| 34 | + expect(currentRollingHash).toBe(currentHash); |
| 35 | + |
| 36 | + previousWord = currentWord; |
| 37 | + previousHash = currentHash; |
| 38 | + } |
| 39 | + }); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + // it('should calculate new hash based on previous one', () => { |
| 44 | + // const polynomialHash = new PolynomialHash(); |
| 45 | + // |
| 46 | + // const wordLength = 3; |
| 47 | + // const string = 'Hello World!'; |
| 48 | + // |
| 49 | + // const word1 = string.substr(0, wordLength); |
| 50 | + // const word2 = string.substr(1, wordLength); |
| 51 | + // const word3 = string.substr(2, wordLength); |
| 52 | + // const word4 = string.substr(3, wordLength); |
| 53 | + // |
| 54 | + // const directHash1 = polynomialHash.hash(word1); |
| 55 | + // const directHash2 = polynomialHash.hash(word2); |
| 56 | + // const directHash3 = polynomialHash.hash(word3); |
| 57 | + // const directHash4 = polynomialHash.hash(word4); |
| 58 | + // |
| 59 | + // const rollingHash2 = polynomialHash.roll(directHash1, word1, word2); |
| 60 | + // const rollingHash3 = polynomialHash.roll(directHash2, word2, word3); |
| 61 | + // const rollingHash4 = polynomialHash.roll(directHash3, word3, word4); |
| 62 | + // |
| 63 | + // expect(directHash1).toBe(151661); |
| 64 | + // expect(directHash2).toBe(151949); |
| 65 | + // expect(directHash3).toBe(156063); |
| 66 | + // expect(directHash4).toBe(48023); |
| 67 | + // |
| 68 | + // expect(rollingHash2).toBe(directHash2); |
| 69 | + // expect(rollingHash3).toBe(directHash3); |
| 70 | + // expect(rollingHash4).toBe(directHash4); |
| 71 | + // }); |
| 72 | + // |
| 73 | + // it('should calculate new hash based on previous one with 3 as a primeModulus', () => { |
| 74 | + // const PRIME = 3; |
| 75 | + // const polynomialHash = new PolynomialHash(PRIME); |
| 76 | + // |
| 77 | + // const wordLength = 3; |
| 78 | + // const string = 'Hello World!'; |
| 79 | + // |
| 80 | + // const word1 = string.substr(0, wordLength); |
| 81 | + // const word2 = string.substr(1, wordLength); |
| 82 | + // const word3 = string.substr(2, wordLength); |
| 83 | + // const word4 = string.substr(3, wordLength); |
| 84 | + // |
| 85 | + // const directHash1 = polynomialHash.hash(word1); |
| 86 | + // const directHash2 = polynomialHash.hash(word2); |
| 87 | + // const directHash3 = polynomialHash.hash(word3); |
| 88 | + // const directHash4 = polynomialHash.hash(word4); |
| 89 | + // |
| 90 | + // const rollingHash2 = polynomialHash.roll(directHash1, word1, word2); |
| 91 | + // const rollingHash3 = polynomialHash.roll(directHash2, word2, word3); |
| 92 | + // const rollingHash4 = polynomialHash.roll(directHash3, word3, word4); |
| 93 | + // |
| 94 | + // expect(directHash1).toBe(1347); |
| 95 | + // expect(directHash2).toBe(1397); |
| 96 | + // expect(directHash3).toBe(1431); |
| 97 | + // expect(directHash4).toBe(729); |
| 98 | + // |
| 99 | + // expect(rollingHash2).toBe(directHash2); |
| 100 | + // expect(rollingHash3).toBe(directHash3); |
| 101 | + // expect(rollingHash4).toBe(directHash4); |
| 102 | + // }); |
| 103 | +}); |
0 commit comments