Skip to content

Commit 9d4da7c

Browse files
committed
ref: update tests
1 parent 1d759cb commit 9d4da7c

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* edits (insertions, deletions, or substitutions) required to change one string into the other.
1010
*/
1111
public final class LevenshteinDistance {
12+
private LevenshteinDistance() {
13+
}
1214

1315
/**
1416
* Calculates the Levenshtein distance between two strings using a naive dynamic programming approach.

src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,44 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.function.ToIntBiFunction;
58
import java.util.stream.Stream;
69
import org.junit.jupiter.params.ParameterizedTest;
710
import org.junit.jupiter.params.provider.Arguments;
811
import org.junit.jupiter.params.provider.MethodSource;
912

1013
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"}};
1314

1415
@ParameterizedTest
1516
@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));
1922
}
2023

2124
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)));
2344
}
2445
}

0 commit comments

Comments
 (0)