Skip to content

Commit d1ec872

Browse files
Comprehensive Test Suite Enhancement for Hamming Distance Algorithm
This enhancement significantly improves the test coverage and structure of the Hamming Distance calculator implementation. The updates focus on robust testing practices, edge cases, and maintainability.
1 parent a163816 commit d1ec872

File tree

1 file changed

+101
-19
lines changed

1 file changed

+101
-19
lines changed
Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,118 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertThrows;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.*;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.*;
57

68
import java.util.stream.Stream;
7-
import org.junit.jupiter.api.Test;
8-
import org.junit.jupiter.params.ParameterizedTest;
9-
import org.junit.jupiter.params.provider.Arguments;
10-
import org.junit.jupiter.params.provider.CsvSource;
11-
import org.junit.jupiter.params.provider.MethodSource;
129

10+
/**
11+
* Test class for HammingDistance calculator.
12+
* Tests various scenarios including:
13+
* - Valid string pairs with different Hamming distances
14+
* - Edge cases (empty strings, same strings)
15+
* - Invalid inputs (null values, unequal lengths)
16+
* - Special characters and Unicode strings
17+
*/
18+
@DisplayName("Hamming Distance Calculator Tests")
1319
class HammingDistanceTest {
20+
21+
private static final String ERROR_MESSAGE_UNEQUAL_LENGTH = "String lengths must be equal";
22+
private static final String ERROR_MESSAGE_NULL_INPUT = "Input strings cannot be null";
1423

15-
@ParameterizedTest
16-
@CsvSource({"'', '', 0", "'java', 'java', 0", "'karolin', 'kathrin', 3", "'kathrin', 'kerstin', 4", "'00000', '11111', 5", "'10101', '10100', 1"})
17-
void testHammingDistance(String s1, String s2, int expected) {
18-
assertEquals(expected, HammingDistance.calculateHammingDistance(s1, s2));
24+
@Nested
25+
@DisplayName("Valid Input Tests")
26+
class ValidInputTests {
27+
28+
@ParameterizedTest(name = "Calculate distance between \"{0}\" and \"{1}\" = {2}")
29+
@CsvSource({
30+
// Basic cases
31+
"'', '', 0",
32+
"'java', 'java', 0",
33+
"'karolin', 'kathrin', 3",
34+
"'kathrin', 'kerstin', 4",
35+
36+
// Binary strings
37+
"'00000', '11111', 5",
38+
"'10101', '10100', 1",
39+
40+
// Special characters
41+
"'hello!', 'hello.', 1",
42+
"'@#$%^&', '@#$%^*', 1",
43+
44+
// Unicode characters
45+
"'über④⑤', 'uber45', 2",
46+
"'münchen', 'munchen', 1"
47+
})
48+
void shouldCalculateHammingDistance(String s1, String s2, int expected) {
49+
assertEquals(expected, HammingDistance.calculateHammingDistance(s1, s2),
50+
() -> String.format("Failed to calculate correct Hamming distance for '%s' and '%s'", s1, s2));
51+
}
52+
53+
@Test
54+
@DisplayName("Should handle maximum length strings")
55+
void shouldHandleMaximumLengthStrings() {
56+
String str1 = "a".repeat(1000);
57+
String str2 = "b".repeat(1000);
58+
assertEquals(1000, HammingDistance.calculateHammingDistance(str1, str2),
59+
"Should correctly calculate distance for maximum length strings");
60+
}
1961
}
2062

21-
@ParameterizedTest
22-
@MethodSource("provideNullInputs")
23-
void testHammingDistanceWithNullInputs(String input1, String input2) {
24-
assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance(input1, input2));
63+
@Nested
64+
@DisplayName("Invalid Input Tests")
65+
class InvalidInputTests {
66+
67+
@ParameterizedTest(name = "Test null input: first={0}, second={1}")
68+
@MethodSource("com.thealgorithms.strings.HammingDistanceTest#provideNullInputs")
69+
void shouldThrowExceptionForNullInputs(String input1, String input2) {
70+
IllegalArgumentException exception = assertThrows(
71+
IllegalArgumentException.class,
72+
() -> HammingDistance.calculateHammingDistance(input1, input2),
73+
"Should throw IllegalArgumentException for null inputs"
74+
);
75+
assertEquals(ERROR_MESSAGE_NULL_INPUT, exception.getMessage(),
76+
"Exception message should match for null inputs");
77+
}
78+
79+
@ParameterizedTest(name = "Test unequal lengths: \"{0}\" and \"{1}\"")
80+
@CsvSource({
81+
"ab, abc",
82+
"a, aa",
83+
"hello, hi",
84+
"'', a"
85+
})
86+
void shouldThrowExceptionForUnequalLengths(String s1, String s2) {
87+
IllegalArgumentException exception = assertThrows(
88+
IllegalArgumentException.class,
89+
() -> HammingDistance.calculateHammingDistance(s1, s2),
90+
"Should throw IllegalArgumentException for unequal length strings"
91+
);
92+
assertEquals(ERROR_MESSAGE_UNEQUAL_LENGTH, exception.getMessage(),
93+
"Exception message should match for unequal lengths");
94+
}
2595
}
2696

97+
// Test data providers
2798
private static Stream<Arguments> provideNullInputs() {
28-
return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null));
99+
return Stream.of(
100+
Arguments.of(null, "abc"),
101+
Arguments.of("abc", null),
102+
Arguments.of(null, null)
103+
);
29104
}
30105

31106
@Test
32-
void testNotEqualStringLengths() {
33-
Exception exception = assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance("ab", "abc"));
34-
assertEquals("String lengths must be equal", exception.getMessage());
107+
@DisplayName("Performance test with large strings")
108+
void performanceTest() {
109+
String str1 = "a".repeat(10000) + "b".repeat(10000);
110+
String str2 = "a".repeat(10000) + "c".repeat(10000);
111+
112+
assertTimeoutPreemptively(
113+
java.time.Duration.ofSeconds(1),
114+
() -> HammingDistance.calculateHammingDistance(str1, str2),
115+
"Should complete calculation within reasonable time"
116+
);
35117
}
36118
}

0 commit comments

Comments
 (0)