Skip to content

Commit

Permalink
Add unit test for EditDistance (#6108)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentanphat8694 authored Dec 21, 2024
1 parent d102fa7 commit 2fff579
Showing 1 changed file with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

Expand All @@ -12,4 +14,91 @@ public class EditDistanceTest {
void testMinDistance(String str1, String str2, int expected) {
assertEquals(expected, EditDistance.minDistance(str1, str2));
}

@Test
public void testEditDistanceBothEmptyStrings() {
assertEquals(0, EditDistance.editDistance("", ""));
}

@Test
public void testEditDistanceOneEmptyString() {
assertEquals(5, EditDistance.editDistance("", "hello"));
assertEquals(7, EditDistance.editDistance("worldly", ""));
}

@Test
public void testEditDistanceOneEmptyStringMemoization() {
int[][] storage = new int[1][6];
assertAll("String assertions",
()
-> assertEquals(5, EditDistance.editDistance("", "hello", storage)),
() -> assertEquals(0, storage[0][0]), () -> assertEquals(0, storage[0][1]), () -> assertEquals(0, storage[0][2]), () -> assertEquals(0, storage[0][3]), () -> assertEquals(0, storage[0][4]), () -> assertEquals(5, storage[0][5]));
}

@Test
public void testEditDistanceEqualStrings() {
assertEquals(0, EditDistance.editDistance("test", "test"));
assertEquals(0, EditDistance.editDistance("abc", "abc"));
}

@Test
public void testEditDistanceEqualStringsMemoization() {
int[][] storage = new int[4][4];
assertAll("String assertions",
()
-> assertEquals(0, EditDistance.editDistance("abc", "abc", storage)),
()
-> assertEquals(0, storage[0][0]),
()
-> assertEquals(0, storage[0][1]),
()
-> assertEquals(0, storage[0][2]),
()
-> assertEquals(0, storage[0][3]),
()
-> assertEquals(0, storage[1][0]),
()
-> assertEquals(0, storage[1][1]),
()
-> assertEquals(0, storage[1][2]),
()
-> assertEquals(0, storage[1][3]),
()
-> assertEquals(0, storage[2][0]),
() -> assertEquals(0, storage[2][1]), () -> assertEquals(0, storage[2][2]), () -> assertEquals(0, storage[2][3]), () -> assertEquals(0, storage[3][0]), () -> assertEquals(0, storage[3][1]), () -> assertEquals(0, storage[3][2]), () -> assertEquals(0, storage[3][3]));
}

@Test
public void testEditDistanceOneCharacterDifference() {
assertEquals(1, EditDistance.editDistance("cat", "bat"));
assertEquals(1, EditDistance.editDistance("cat", "cats"));
assertEquals(1, EditDistance.editDistance("cats", "cat"));
}

@Test
public void testEditDistanceOneCharacterDifferenceMemoization() {
int[][] storage = new int[3][3];
assertAll("All assertions",
()
-> assertEquals(1, EditDistance.editDistance("at", "it", storage)),
()
-> assertEquals(0, storage[0][0]),
()
-> assertEquals(1, storage[0][1]),
() -> assertEquals(2, storage[0][2]), () -> assertEquals(1, storage[1][0]), () -> assertEquals(0, storage[1][1]), () -> assertEquals(1, storage[1][2]), () -> assertEquals(2, storage[2][0]), () -> assertEquals(1, storage[2][1]), () -> assertEquals(1, storage[2][2]));
}

@Test
public void testEditDistanceGeneralCases() {
assertEquals(3, EditDistance.editDistance("kitten", "sitting"));
assertEquals(2, EditDistance.editDistance("flaw", "lawn"));
assertEquals(5, EditDistance.editDistance("intention", "execution"));
}

@Test
public void testEditDistanceGeneralCasesMemoization() {
int[][] storage = new int[7][8];
assertEquals(3, EditDistance.editDistance("kitten", "sitting", storage));
assertAll("All assertions", () -> assertEquals(0, storage[0][0]), () -> assertEquals(3, storage[6][7]));
}
}

0 comments on commit 2fff579

Please sign in to comment.