Back to Repositories

Testing Z-Algorithm String Pattern Matching in javascript-algorithms

This test suite validates the Z-Algorithm implementation for string pattern matching in JavaScript. It verifies the algorithm’s ability to find all occurrences of a pattern within a text string, covering various pattern matching scenarios and edge cases.

Test Coverage Overview

The test suite provides comprehensive coverage of the Z-Algorithm string matching functionality.

Key areas tested include:
  • Empty pattern matching scenarios
  • Exact string matches
  • Multiple pattern occurrences
  • Overlapping pattern cases
  • Pattern matching at different positions

Implementation Analysis

The testing approach uses Jest’s describe/it pattern for structured test organization. Each test case utilizes expect().toEqual() assertions to verify pattern matching results, comparing expected position arrays with actual algorithm output.

The implementation focuses on validating both positive and negative matching scenarios with varying pattern lengths and positions.

Technical Details

Testing Framework: Jest
Test Structure: Unit tests
Main Components:
  • Pattern matching validation
  • Array comparison assertions
  • Multiple test cases in single describe block
  • Import-based module testing

Best Practices Demonstrated

The test suite exemplifies clean testing practices with clear test case organization and comprehensive edge case coverage.

Notable practices include:
  • Consistent assertion pattern usage
  • Clear test case descriptions
  • Comprehensive input variations
  • Edge case handling
  • Focused test scope

trekhleb/javascript-algorithms

src/algorithms/string/z-algorithm/__test__/zAlgorithm.test.js

            
import zAlgorithm from '../zAlgorithm';

describe('zAlgorithm', () => {
  it('should find word positions in given text', () => {
    expect(zAlgorithm('abcbcglx', 'abca')).toEqual([]);
    expect(zAlgorithm('abca', 'abca')).toEqual([0]);
    expect(zAlgorithm('abca', 'abcadfd')).toEqual([]);
    expect(zAlgorithm('abcbcglabcx', 'abc')).toEqual([0, 7]);
    expect(zAlgorithm('abcbcglx', 'bcgl')).toEqual([3]);
    expect(zAlgorithm('abcbcglx', 'cglx')).toEqual([4]);
    expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabcy')).toEqual([15]);
    expect(zAlgorithm('abcxabcdabxabcdabcdabcy', 'abcdabca')).toEqual([]);
    expect(zAlgorithm('abcxabcdabxaabcdabcabcdabcdabcy', 'abcdabca')).toEqual([12]);
    expect(zAlgorithm('abcxabcdabxaabaabaaaabcdabcdabcy', 'aabaabaaa')).toEqual([11]);
  });
});