Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bugs related to unicode support in polynomial-hash functions #304

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify test for PolynomialHash.roll
dubzzz committed Feb 14, 2019
commit c3b695d2cb6528f3a5c7c8b95727ff5e1450cc61
Original file line number Diff line number Diff line change
@@ -7,27 +7,22 @@ describe('PolynomialHash', () => {
fc.property(
fc.constantFrom(3, 79, 101, 3251, 13229, 122743, 3583213),
fc.integer(2, 0x7fffffff),
fc.integer(1, 50),
fc.unicodeString(0, 100), // no surrogate pairs
(base, modulus, frameSize, text) => {
fc.pre(base * modulus < 0x7fffffff); // avoid overflows
const polynomialHash = new PolynomialHash({ base, modulus });

let previousWord = text.substr(0, frameSize);
let previousHash = polynomialHash.hash(previousWord);
fc.string(0, 50),
fc.char(),
fc.char(),
(base, modulus, commonWord, previousChar, newChar) => {
fc.pre(base * modulus + 0x10ffff < 0x7fffffff); // avoid overflows

// 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);
const polynomialHash = new PolynomialHash({ base, modulus });
const previousWord = previousChar + commonWord;
const currentWord = commonWord + newChar;
const previousHash = polynomialHash.hash(previousWord);

// Check that rolling hash is the same as directly calculated hash.
expect(currentRollingHash).toBe(currentHash);
const currentHash = polynomialHash.hash(currentWord);
const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord);

previousWord = currentWord;
previousHash = currentHash;
}
// Check that rolling hash is the same as directly calculated hash.
expect(currentRollingHash).toBe(currentHash);
},
),
);
Original file line number Diff line number Diff line change
@@ -6,27 +6,22 @@ describe('SimplePolynomialHash', () => {
fc.assert(
fc.property(
fc.constantFrom(3, 79, 101, 3251, 13229, 122743, 3583213),
fc.integer(1, 10),
fc.unicodeString(0, 100), // no surrogate pairs
(base, frameSize, text) => {
fc.pre(0xffff * (base ** (frameSize - 1)) < 0x7fffffff); // avoid overflows
const polynomialHash = new SimplePolynomialHash(base);

let previousWord = text.substr(0, frameSize);
let previousHash = polynomialHash.hash(previousWord);
fc.string(0, 50),
fc.char(),
fc.char(),
(base, commonWord, previousChar, newChar) => {
fc.pre(0xffff * (base ** commonWord.length) < 0x7fffffff); // avoid overflows

// 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);
const polynomialHash = new SimplePolynomialHash(base);
const previousWord = previousChar + commonWord;
const currentWord = commonWord + newChar;
const previousHash = polynomialHash.hash(previousWord);

// Check that rolling hash is the same as directly calculated hash.
expect(currentRollingHash).toBe(currentHash);
const currentHash = polynomialHash.hash(currentWord);
const currentRollingHash = polynomialHash.roll(previousHash, previousWord, currentWord);

previousWord = currentWord;
previousHash = currentHash;
}
// Check that rolling hash is the same as directly calculated hash.
expect(currentRollingHash).toBe(currentHash);
},
),
);