Back to Repositories

Testing Polynomial Hash Calculation Workflow in javascript-algorithms

This test suite validates the SimplePolynomialHash implementation, focusing on rolling hash calculations and numeric hash generation for string inputs. The tests ensure correct hash computation for varying base values and frame sizes while maintaining consistency between direct and rolling hash calculations.

Test Coverage Overview

The test suite provides comprehensive coverage of the SimplePolynomialHash functionality.

Key areas tested include:
  • Rolling hash calculation verification
  • Multiple base values (3, 5)
  • Various frame sizes (5, 10)
  • Unicode character handling
  • Basic numeric hash generation for single and multi-character strings

Implementation Analysis

The testing approach employs Jest’s describe/it blocks with nested iterations over different test parameters.

Notable patterns include:
  • Parameterized testing using forEach loops
  • Progressive frame shifting through text
  • Direct vs rolling hash comparison
  • Deterministic hash value verification

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • SimplePolynomialHash class implementation
  • Unicode string handling capabilities
  • Dynamic test data generation
  • Expect assertions for hash verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable examples include:
  • Systematic parameter variation
  • Comprehensive edge case coverage
  • Clear test case organization
  • Efficient test data reuse
  • Isolated test scenarios

trekhleb/javascript-algorithms

src/algorithms/cryptography/polynomial-hash/__test__/SimplePolynomialHash.test.js

            
import SimplePolynomialHash from '../SimplePolynomialHash';

describe('PolynomialHash', () => {
  it('should calculate new hash based on previous one', () => {
    const bases = [3, 5];
    const frameSizes = [5, 10];

    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 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) => {
      const polynomialHash = new SimplePolynomialHash(base);

      // 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', () => {
    const polynomialHash = new SimplePolynomialHash();

    expect(polynomialHash.hash('Test')).toBe(604944);
    expect(polynomialHash.hash('a')).toBe(97);
    expect(polynomialHash.hash('b')).toBe(98);
    expect(polynomialHash.hash('c')).toBe(99);
    expect(polynomialHash.hash('d')).toBe(100);
    expect(polynomialHash.hash('e')).toBe(101);
    expect(polynomialHash.hash('ab')).toBe(1763);
    expect(polynomialHash.hash('abc')).toBe(30374);
  });
});