|
1 | | -package com.thealgorithms.strings; |
2 | | - |
3 | 1 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 2 | import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | | -import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
6 | 3 |
|
7 | | -import java.time.Duration; |
8 | 4 | import java.util.stream.Stream; |
9 | 5 | import org.junit.jupiter.api.DisplayName; |
10 | 6 | import org.junit.jupiter.api.Nested; |
|
14 | 10 | import org.junit.jupiter.params.provider.CsvSource; |
15 | 11 | import org.junit.jupiter.params.provider.MethodSource; |
16 | 12 |
|
17 | | -/** |
18 | | - * Test class for HammingDistance calculator. |
19 | | - */ |
20 | 13 | class HammingDistanceTest { |
21 | 14 |
|
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 | | - |
25 | 15 | @Nested |
| 16 | + @DisplayName("Valid Input Tests") |
26 | 17 | class ValidInputTests { |
27 | 18 |
|
28 | | - @ParameterizedTest(name = "Calculate distance between {0} and {1} = {2}") |
| 19 | + @ParameterizedTest(name = "Hamming distance between \"{0}\" and \"{1}\" should be {2}") |
29 | 20 | @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" |
38 | 27 | }) |
39 | | - void shouldCalculateHammingDistance(String s1, String s2, int expected) { |
| 28 | + void testHammingDistance(String s1, String s2, int expected) { |
40 | 29 | assertEquals( |
41 | 30 | expected, |
42 | 31 | 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) |
55 | 33 | ); |
56 | 34 | } |
57 | 35 | } |
58 | 36 |
|
59 | 37 | @Nested |
| 38 | + @DisplayName("Invalid Input Tests") |
60 | 39 | class InvalidInputTests { |
61 | 40 |
|
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) { |
65 | 44 | IllegalArgumentException exception = assertThrows( |
66 | 45 | IllegalArgumentException.class, |
67 | 46 | () -> HammingDistance.calculateHammingDistance(input1, input2), |
68 | | - "Should throw IllegalArgumentException for null inputs" |
| 47 | + "Expected IllegalArgumentException for null inputs" |
69 | 48 | ); |
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) |
74 | 57 | ); |
75 | 58 | } |
76 | 59 |
|
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() { |
85 | 63 | IllegalArgumentException exception = assertThrows( |
86 | 64 | 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" |
94 | 67 | ); |
| 68 | + assertEquals("String lengths must be equal", exception.getMessage()); |
95 | 69 | } |
96 | 70 | } |
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 | | - } |
117 | 71 | } |
0 commit comments