1
1
package com .thealgorithms .strings ;
2
2
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 ;
7
6
7
+ import java .time .Duration ;
8
8
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 ;
9
16
10
17
/**
11
18
* 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
19
*/
18
- @ DisplayName ("Hamming Distance Calculator Tests" )
19
20
class HammingDistanceTest {
20
-
21
+
21
22
private static final String ERROR_MESSAGE_UNEQUAL_LENGTH = "String lengths must be equal" ;
22
23
private static final String ERROR_MESSAGE_NULL_INPUT = "Input strings cannot be null" ;
23
24
24
25
@ Nested
25
- @ DisplayName ("Valid Input Tests" )
26
26
class ValidInputTests {
27
-
28
- @ ParameterizedTest (name = "Calculate distance between \" {0}\" and \" {1}\" = {2}" )
27
+
28
+ @ ParameterizedTest (name = "Calculate distance between {0} and {1} = {2}" )
29
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"
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"
47
38
})
48
39
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
+ );
51
45
}
52
46
53
47
@ Test
54
- @ DisplayName ("Should handle maximum length strings" )
55
48
void shouldHandleMaximumLengthStrings () {
56
49
String str1 = "a" .repeat (1000 );
57
50
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
+ );
60
56
}
61
57
}
62
58
63
59
@ Nested
64
- @ DisplayName ("Invalid Input Tests" )
65
60
class InvalidInputTests {
66
-
61
+
67
62
@ ParameterizedTest (name = "Test null input: first={0}, second={1}" )
68
63
@ MethodSource ("com.thealgorithms.strings.HammingDistanceTest#provideNullInputs" )
69
64
void shouldThrowExceptionForNullInputs (String input1 , String input2 ) {
@@ -72,29 +67,34 @@ void shouldThrowExceptionForNullInputs(String input1, String input2) {
72
67
() -> HammingDistance .calculateHammingDistance (input1 , input2 ),
73
68
"Should throw IllegalArgumentException for null inputs"
74
69
);
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
+ );
77
75
}
78
76
79
- @ ParameterizedTest (name = "Test unequal lengths: \" {0}\" and \" {1}\" " )
77
+ @ ParameterizedTest (name = "Test unequal lengths: {0} and {1}" )
80
78
@ CsvSource ({
81
79
"ab, abc" ,
82
80
"a, aa" ,
83
81
"hello, hi" ,
84
- "'' , a"
82
+ ", a"
85
83
})
86
84
void shouldThrowExceptionForUnequalLengths (String s1 , String s2 ) {
87
85
IllegalArgumentException exception = assertThrows (
88
86
IllegalArgumentException .class ,
89
87
() -> HammingDistance .calculateHammingDistance (s1 , s2 ),
90
88
"Should throw IllegalArgumentException for unequal length strings"
91
89
);
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
+ );
94
95
}
95
96
}
96
97
97
- // Test data providers
98
98
private static Stream <Arguments > provideNullInputs () {
99
99
return Stream .of (
100
100
Arguments .of (null , "abc" ),
@@ -104,13 +104,12 @@ private static Stream<Arguments> provideNullInputs() {
104
104
}
105
105
106
106
@ Test
107
- @ DisplayName ("Performance test with large strings" )
108
107
void performanceTest () {
109
108
String str1 = "a" .repeat (10000 ) + "b" .repeat (10000 );
110
109
String str2 = "a" .repeat (10000 ) + "c" .repeat (10000 );
111
-
110
+
112
111
assertTimeoutPreemptively (
113
- java . time . Duration .ofSeconds (1 ),
112
+ Duration .ofSeconds (1 ),
114
113
() -> HammingDistance .calculateHammingDistance (str1 , str2 ),
115
114
"Should complete calculation within reasonable time"
116
115
);
0 commit comments