Skip to content

Commit 3515288

Browse files
Update HammingDistanceTest.java
1 parent 173fcf2 commit 3515288

File tree

1 file changed

+29
-75
lines changed

1 file changed

+29
-75
lines changed
Lines changed: 29 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
package com.thealgorithms.strings;
2-
31
import static org.junit.jupiter.api.Assertions.assertEquals;
42
import static org.junit.jupiter.api.Assertions.assertThrows;
5-
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
63

7-
import java.time.Duration;
84
import java.util.stream.Stream;
95
import org.junit.jupiter.api.DisplayName;
106
import org.junit.jupiter.api.Nested;
@@ -14,104 +10,62 @@
1410
import org.junit.jupiter.params.provider.CsvSource;
1511
import org.junit.jupiter.params.provider.MethodSource;
1612

17-
/**
18-
* Test class for HammingDistance calculator.
19-
*/
2013
class HammingDistanceTest {
2114

22-
private static final String ERROR_MESSAGE_UNEQUAL_LENGTH = "String lengths must be equal";
23-
private static final String ERROR_MESSAGE_NULL_INPUT = "Input strings cannot be null";
24-
2515
@Nested
16+
@DisplayName("Valid Input Tests")
2617
class ValidInputTests {
2718

28-
@ParameterizedTest(name = "Calculate distance between {0} and {1} = {2}")
19+
@ParameterizedTest(name = "Hamming distance between \"{0}\" and \"{1}\" should be {2}")
2920
@CsvSource({
30-
", , 0",
31-
"java, java, 0",
32-
"karolin, kathrin, 3",
33-
"kathrin, kerstin, 4",
34-
"00000, 11111, 5",
35-
"10101, 10100, 1",
36-
"hello!, hello., 1",
37-
"@#$%^&, @#$%^*, 1"
21+
"'', '', 0",
22+
"'java', 'java', 0",
23+
"'karolin', 'kathrin', 3",
24+
"'kathrin', 'kerstin', 4",
25+
"'00000', '11111', 5",
26+
"'10101', '10100', 1"
3827
})
39-
void shouldCalculateHammingDistance(String s1, String s2, int expected) {
28+
void testHammingDistance(String s1, String s2, int expected) {
4029
assertEquals(
4130
expected,
4231
HammingDistance.calculateHammingDistance(s1, s2),
43-
String.format("Failed to calculate correct Hamming distance for '%s' and '%s'", s1, s2)
44-
);
45-
}
46-
47-
@Test
48-
void shouldHandleMaximumLengthStrings() {
49-
String str1 = "a".repeat(1000);
50-
String str2 = "b".repeat(1000);
51-
assertEquals(
52-
1000,
53-
HammingDistance.calculateHammingDistance(str1, str2),
54-
"Should correctly calculate distance for maximum length strings"
32+
String.format("Expected Hamming distance between '%s' and '%s' is %d", s1, s2, expected)
5533
);
5634
}
5735
}
5836

5937
@Nested
38+
@DisplayName("Invalid Input Tests")
6039
class InvalidInputTests {
6140

62-
@ParameterizedTest(name = "Test null input: first={0}, second={1}")
63-
@MethodSource("com.thealgorithms.strings.HammingDistanceTest#provideNullInputs")
64-
void shouldThrowExceptionForNullInputs(String input1, String input2) {
41+
@ParameterizedTest(name = "Hamming distance should throw exception for null inputs: \"{0}\", \"{1}\"")
42+
@MethodSource("provideNullInputs")
43+
void testHammingDistanceWithNullInputs(String input1, String input2) {
6544
IllegalArgumentException exception = assertThrows(
6645
IllegalArgumentException.class,
6746
() -> HammingDistance.calculateHammingDistance(input1, input2),
68-
"Should throw IllegalArgumentException for null inputs"
47+
"Expected IllegalArgumentException for null inputs"
6948
);
70-
assertEquals(
71-
ERROR_MESSAGE_NULL_INPUT,
72-
exception.getMessage(),
73-
"Exception message should match for null inputs"
49+
assertEquals("Input strings cannot be null", exception.getMessage());
50+
}
51+
52+
private static Stream<Arguments> provideNullInputs() {
53+
return Stream.of(
54+
Arguments.of(null, "abc"),
55+
Arguments.of("abc", null),
56+
Arguments.of(null, null)
7457
);
7558
}
7659

77-
@ParameterizedTest(name = "Test unequal lengths: {0} and {1}")
78-
@CsvSource({
79-
"ab, abc",
80-
"a, aa",
81-
"hello, hi",
82-
", a"
83-
})
84-
void shouldThrowExceptionForUnequalLengths(String s1, String s2) {
60+
@Test
61+
@DisplayName("Should throw exception for unequal string lengths")
62+
void testNotEqualStringLengths() {
8563
IllegalArgumentException exception = assertThrows(
8664
IllegalArgumentException.class,
87-
() -> HammingDistance.calculateHammingDistance(s1, s2),
88-
"Should throw IllegalArgumentException for unequal length strings"
89-
);
90-
assertEquals(
91-
ERROR_MESSAGE_UNEQUAL_LENGTH,
92-
exception.getMessage(),
93-
"Exception message should match for unequal lengths"
65+
() -> HammingDistance.calculateHammingDistance("ab", "abc"),
66+
"Expected IllegalArgumentException for unequal length strings"
9467
);
68+
assertEquals("String lengths must be equal", exception.getMessage());
9569
}
9670
}
97-
98-
private static Stream<Arguments> provideNullInputs() {
99-
return Stream.of(
100-
Arguments.of(null, "abc"),
101-
Arguments.of("abc", null),
102-
Arguments.of(null, null)
103-
);
104-
}
105-
106-
@Test
107-
void performanceTest() {
108-
String str1 = "a".repeat(10000) + "b".repeat(10000);
109-
String str2 = "a".repeat(10000) + "c".repeat(10000);
110-
111-
assertTimeoutPreemptively(
112-
Duration.ofSeconds(1),
113-
() -> HammingDistance.calculateHammingDistance(str1, str2),
114-
"Should complete calculation within reasonable time"
115-
);
116-
}
11771
}

0 commit comments

Comments
 (0)