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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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ä¹°ę¶');
});
});