Testing Hamming Distance String Comparison in javascript-algorithms
This test suite validates the Hamming Distance algorithm implementation in JavaScript, ensuring accurate calculation of differences between two strings. The tests verify both error handling for invalid inputs and correct distance calculations across various string pairs.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
trekhleb/javascript-algorithms
src/algorithms/string/hamming-distance/__test__/hammingDistance.test.js
import hammingDistance from '../hammingDistance';
describe('hammingDistance', () => {
it('should throw an error when trying to compare the strings of different lengths', () => {
const compareStringsOfDifferentLength = () => {
hammingDistance('a', 'aa');
};
expect(compareStringsOfDifferentLength).toThrowError();
});
it('should calculate difference between two strings', () => {
expect(hammingDistance('a', 'a')).toBe(0);
expect(hammingDistance('a', 'b')).toBe(1);
expect(hammingDistance('abc', 'add')).toBe(2);
expect(hammingDistance('karolin', 'kathrin')).toBe(3);
expect(hammingDistance('karolin', 'kerstin')).toBe(3);
expect(hammingDistance('1011101', '1001001')).toBe(2);
expect(hammingDistance('2173896', '2233796')).toBe(3);
});
});