|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.util.function.ToIntBiFunction; |
5 | 8 | import java.util.stream.Stream;
|
6 | 9 | import org.junit.jupiter.params.ParameterizedTest;
|
7 | 10 | import org.junit.jupiter.params.provider.Arguments;
|
8 | 11 | import org.junit.jupiter.params.provider.MethodSource;
|
9 | 12 |
|
10 | 13 | public class LevenshteinDistanceTests {
|
11 |
| - private static final Object[][] TEST_CASES |
12 |
| - = {{0, "", ""}, {0, "Hello, World!", "Hello, World!"}, {4, "", "Rust"}, {3, "horse", "ros"}, {6, "tan", "elephant"}, {8, "execute", "intention"}, {0, "", ""}, {0, "Hello, World!", "Hello, World!"}, {4, "", "Rust"}, {3, "horse", "ros"}, {6, "tan", "elephant"}, {8, "execute", "intention"}}; |
13 | 14 |
|
14 | 15 | @ParameterizedTest
|
15 | 16 | @MethodSource("testCases")
|
16 |
| - void testLevenshteinDistance(int expectedDistance, String str1, String str2) { |
17 |
| - assertEquals(expectedDistance, LevenshteinDistance.naiveLevenshteinDistance(str1, str2)); |
18 |
| - assertEquals(expectedDistance, LevenshteinDistance.optimizedLevenshteinDistance(str1, str2)); |
| 17 | + public void testLevenshteinDistance(final int expected, final String str1, final String str2, final ToIntBiFunction<String, String> dist) { |
| 18 | + assertEquals(expected, dist.applyAsInt(str1, str2)); |
| 19 | + assertEquals(expected, dist.applyAsInt(str2, str1)); |
| 20 | + assertEquals(0, dist.applyAsInt(str1, str1)); |
| 21 | + assertEquals(0, dist.applyAsInt(str2, str2)); |
19 | 22 | }
|
20 | 23 |
|
21 | 24 | private static Stream<Arguments> testCases() {
|
22 |
| - return Stream.of(TEST_CASES).map(args -> Arguments.of(args[0], args[1], args[2])); |
| 25 | + final Object[][] testData = { |
| 26 | + {0, "", ""}, |
| 27 | + {0, "Hello, World!", "Hello, World!"}, |
| 28 | + {4, "", "Rust"}, |
| 29 | + {3, "horse", "ros"}, |
| 30 | + {6, "tan", "elephant"}, |
| 31 | + {8, "execute", "intention"}, |
| 32 | + {1, "a", "b"}, |
| 33 | + {1, "a", "aa"}, |
| 34 | + {1, "a", ""}, |
| 35 | + {1, "a", "ab"}, |
| 36 | + {1, "a", "ba"}, |
| 37 | + {2, "a", "bc"}, |
| 38 | + {2, "a", "cb"}, |
| 39 | + }; |
| 40 | + |
| 41 | + final List<ToIntBiFunction<String, String>> methods = Arrays.asList(LevenshteinDistance::naiveLevenshteinDistance, LevenshteinDistance::optimizedLevenshteinDistance); |
| 42 | + |
| 43 | + return Stream.of(testData).flatMap(input -> methods.stream().map(method -> Arguments.of(input[0], input[1], input[2], method))); |
23 | 44 | }
|
24 | 45 | }
|
0 commit comments