Skip to content

Commit 173fcf2

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

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

src/test/java/com/thealgorithms/strings/HammingDistanceTest.java

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,64 @@
11
package com.thealgorithms.strings;
22

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.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
76

7+
import java.time.Duration;
88
import java.util.stream.Stream;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Nested;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.CsvSource;
15+
import org.junit.jupiter.params.provider.MethodSource;
916

1017
/**
1118
* 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
1719
*/
18-
@DisplayName("Hamming Distance Calculator Tests")
1920
class HammingDistanceTest {
20-
21+
2122
private static final String ERROR_MESSAGE_UNEQUAL_LENGTH = "String lengths must be equal";
2223
private static final String ERROR_MESSAGE_NULL_INPUT = "Input strings cannot be null";
2324

2425
@Nested
25-
@DisplayName("Valid Input Tests")
2626
class ValidInputTests {
27-
28-
@ParameterizedTest(name = "Calculate distance between \"{0}\" and \"{1}\" = {2}")
27+
28+
@ParameterizedTest(name = "Calculate distance between {0} and {1} = {2}")
2929
@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"
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"
4738
})
4839
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));
40+
assertEquals(
41+
expected,
42+
HammingDistance.calculateHammingDistance(s1, s2),
43+
String.format("Failed to calculate correct Hamming distance for '%s' and '%s'", s1, s2)
44+
);
5145
}
5246

5347
@Test
54-
@DisplayName("Should handle maximum length strings")
5548
void shouldHandleMaximumLengthStrings() {
5649
String str1 = "a".repeat(1000);
5750
String str2 = "b".repeat(1000);
58-
assertEquals(1000, HammingDistance.calculateHammingDistance(str1, str2),
59-
"Should correctly calculate distance for maximum length strings");
51+
assertEquals(
52+
1000,
53+
HammingDistance.calculateHammingDistance(str1, str2),
54+
"Should correctly calculate distance for maximum length strings"
55+
);
6056
}
6157
}
6258

6359
@Nested
64-
@DisplayName("Invalid Input Tests")
6560
class InvalidInputTests {
66-
61+
6762
@ParameterizedTest(name = "Test null input: first={0}, second={1}")
6863
@MethodSource("com.thealgorithms.strings.HammingDistanceTest#provideNullInputs")
6964
void shouldThrowExceptionForNullInputs(String input1, String input2) {
@@ -72,29 +67,34 @@ void shouldThrowExceptionForNullInputs(String input1, String input2) {
7267
() -> HammingDistance.calculateHammingDistance(input1, input2),
7368
"Should throw IllegalArgumentException for null inputs"
7469
);
75-
assertEquals(ERROR_MESSAGE_NULL_INPUT, exception.getMessage(),
76-
"Exception message should match for null inputs");
70+
assertEquals(
71+
ERROR_MESSAGE_NULL_INPUT,
72+
exception.getMessage(),
73+
"Exception message should match for null inputs"
74+
);
7775
}
7876

79-
@ParameterizedTest(name = "Test unequal lengths: \"{0}\" and \"{1}\"")
77+
@ParameterizedTest(name = "Test unequal lengths: {0} and {1}")
8078
@CsvSource({
8179
"ab, abc",
8280
"a, aa",
8381
"hello, hi",
84-
"'', a"
82+
", a"
8583
})
8684
void shouldThrowExceptionForUnequalLengths(String s1, String s2) {
8785
IllegalArgumentException exception = assertThrows(
8886
IllegalArgumentException.class,
8987
() -> HammingDistance.calculateHammingDistance(s1, s2),
9088
"Should throw IllegalArgumentException for unequal length strings"
9189
);
92-
assertEquals(ERROR_MESSAGE_UNEQUAL_LENGTH, exception.getMessage(),
93-
"Exception message should match for unequal lengths");
90+
assertEquals(
91+
ERROR_MESSAGE_UNEQUAL_LENGTH,
92+
exception.getMessage(),
93+
"Exception message should match for unequal lengths"
94+
);
9495
}
9596
}
9697

97-
// Test data providers
9898
private static Stream<Arguments> provideNullInputs() {
9999
return Stream.of(
100100
Arguments.of(null, "abc"),
@@ -104,13 +104,12 @@ private static Stream<Arguments> provideNullInputs() {
104104
}
105105

106106
@Test
107-
@DisplayName("Performance test with large strings")
108107
void performanceTest() {
109108
String str1 = "a".repeat(10000) + "b".repeat(10000);
110109
String str2 = "a".repeat(10000) + "c".repeat(10000);
111-
110+
112111
assertTimeoutPreemptively(
113-
java.time.Duration.ofSeconds(1),
112+
Duration.ofSeconds(1),
114113
() -> HammingDistance.calculateHammingDistance(str1, str2),
115114
"Should complete calculation within reasonable time"
116115
);

0 commit comments

Comments
 (0)