Back to Repositories

Testing Longest Common Substring Algorithm Implementation in javascript-algorithms

This test suite validates the longestCommonSubstring algorithm implementation, focusing on finding the longest shared sequence of characters between two strings. The tests cover basic string matching, empty strings, and Unicode character handling.

Test Coverage Overview

The test suite provides comprehensive coverage of the longest common substring functionality.

Key areas tested include:
  • Empty string handling
  • Basic ASCII string matching
  • Partial string overlaps
  • Long text comparisons
  • Unicode character support

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for organized test grouping. Two main test blocks validate core functionality and Unicode handling separately.

Testing patterns include:
  • Multiple expect assertions per test case
  • Progressive complexity in test cases
  • Boundary condition testing

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • ES6 module import structure
  • Unicode character testing support
  • String comparison assertions

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable elements include:
  • Logical test case grouping
  • Comprehensive edge case coverage
  • Clear test case descriptions
  • Progressive complexity approach
  • International character support validation

trekhleb/javascript-algorithms

src/algorithms/string/longest-common-substring/__test__/longestCommonSubstring.test.js

            
import longestCommonSubstring from '../longestCommonSubstring';

describe('longestCommonSubstring', () => {
  it('should find longest common substring between two strings', () => {
    expect(longestCommonSubstring('', '')).toBe('');
    expect(longestCommonSubstring('ABC', '')).toBe('');
    expect(longestCommonSubstring('', 'ABC')).toBe('');
    expect(longestCommonSubstring('ABABC', 'BABCA')).toBe('BABC');
    expect(longestCommonSubstring('BABCA', 'ABCBA')).toBe('ABC');
    expect(longestCommonSubstring('sea', 'eat')).toBe('ea');
    expect(longestCommonSubstring('algorithms', 'rithm')).toBe('rithm');
    expect(longestCommonSubstring(
      'Algorithms and data structures implemented in JavaScript',
      'Here you may find Algorithms and data structures that are implemented in JavaScript',
    )).toBe('Algorithms and data structures ');
  });

  it('should handle unicode correctly', () => {
    expect(longestCommonSubstring('𐌵𐌵**ABC', '𐌵𐌵--ABC')).toBe('ABC');
    expect(longestCommonSubstring('𐌵𐌵**A', '𐌵𐌵--A')).toBe('𐌵𐌵');
    expect(longestCommonSubstring('Aä¹°Bę—¶', 'ä¹°Bę—¶GD')).toBe('ä¹°Bę—¶');
    expect(longestCommonSubstring('After testä¹°ę—¶ case', 'another_testä¹°ę—¶')).toBe('testä¹°ę—¶');
  });
});