|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
5 | 5 |
|
| 6 | +import java.util.stream.Stream; |
6 | 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; |
7 | 12 |
|
8 |
| -public class HammingDistanceTest { |
| 13 | +class HammingDistanceTest { |
9 | 14 |
|
10 |
| - @Test |
11 |
| - void testHammingDistance() throws Exception { |
12 |
| - assertEquals(HammingDistance.calculateHammingDistance("", ""), 0); |
13 |
| - assertEquals(HammingDistance.calculateHammingDistance("java", "java"), 0); |
14 |
| - assertEquals(HammingDistance.calculateHammingDistance("karolin", "kathrin"), 3); |
15 |
| - assertEquals(HammingDistance.calculateHammingDistance("kathrin", "kerstin"), 4); |
16 |
| - assertEquals(HammingDistance.calculateHammingDistance("00000", "11111"), 5); |
| 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)); |
| 19 | + } |
| 20 | + |
| 21 | + @ParameterizedTest |
| 22 | + @MethodSource("provideNullInputs") |
| 23 | + void testHammingDistanceWithNullInputs(String input1, String input2) { |
| 24 | + assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance(input1, input2)); |
| 25 | + } |
| 26 | + |
| 27 | + private static Stream<Arguments> provideNullInputs() { |
| 28 | + return Stream.of(Arguments.of(null, "abc"), Arguments.of("abc", null), Arguments.of(null, null)); |
17 | 29 | }
|
18 | 30 |
|
19 | 31 | @Test
|
20 | 32 | void testNotEqualStringLengths() {
|
21 |
| - Exception exception = assertThrows(Exception.class, () -> HammingDistance.calculateHammingDistance("ab", "abc")); |
| 33 | + Exception exception = assertThrows(IllegalArgumentException.class, () -> HammingDistance.calculateHammingDistance("ab", "abc")); |
22 | 34 | assertEquals("String lengths must be equal", exception.getMessage());
|
23 | 35 | }
|
24 | 36 | }
|
0 commit comments