|
1 | 1 | package com.thealgorithms.strings;
|
2 | 2 |
|
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.*; |
5 | 7 |
|
6 | 8 | 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; |
12 | 9 |
|
| 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") |
13 | 19 | 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"; |
14 | 23 |
|
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 | + } |
19 | 61 | }
|
20 | 62 |
|
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 | + } |
25 | 95 | }
|
26 | 96 |
|
| 97 | + // Test data providers |
27 | 98 | 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 | + ); |
29 | 104 | }
|
30 | 105 |
|
31 | 106 | @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 | + ); |
35 | 117 | }
|
36 | 118 | }
|
0 commit comments