11package com .thealgorithms .dynamicprogramming ;
22
3+ import static org .junit .jupiter .api .Assertions .assertAll ;
34import static org .junit .jupiter .api .Assertions .assertEquals ;
45
6+ import org .junit .jupiter .api .Test ;
57import org .junit .jupiter .params .ParameterizedTest ;
68import org .junit .jupiter .params .provider .CsvSource ;
79
@@ -12,4 +14,91 @@ public class EditDistanceTest {
1214 void testMinDistance (String str1 , String str2 , int expected ) {
1315 assertEquals (expected , EditDistance .minDistance (str1 , str2 ));
1416 }
17+
18+ @ Test
19+ public void testEditDistanceBothEmptyStrings () {
20+ assertEquals (0 , EditDistance .editDistance ("" , "" ));
21+ }
22+
23+ @ Test
24+ public void testEditDistanceOneEmptyString () {
25+ assertEquals (5 , EditDistance .editDistance ("" , "hello" ));
26+ assertEquals (7 , EditDistance .editDistance ("worldly" , "" ));
27+ }
28+
29+ @ Test
30+ public void testEditDistanceOneEmptyStringMemoization () {
31+ int [][] storage = new int [1 ][6 ];
32+ assertAll ("String assertions" ,
33+ ()
34+ -> assertEquals (5 , EditDistance .editDistance ("" , "hello" , storage )),
35+ () -> 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 ]));
36+ }
37+
38+ @ Test
39+ public void testEditDistanceEqualStrings () {
40+ assertEquals (0 , EditDistance .editDistance ("test" , "test" ));
41+ assertEquals (0 , EditDistance .editDistance ("abc" , "abc" ));
42+ }
43+
44+ @ Test
45+ public void testEditDistanceEqualStringsMemoization () {
46+ int [][] storage = new int [4 ][4 ];
47+ assertAll ("String assertions" ,
48+ ()
49+ -> assertEquals (0 , EditDistance .editDistance ("abc" , "abc" , storage )),
50+ ()
51+ -> assertEquals (0 , storage [0 ][0 ]),
52+ ()
53+ -> assertEquals (0 , storage [0 ][1 ]),
54+ ()
55+ -> assertEquals (0 , storage [0 ][2 ]),
56+ ()
57+ -> assertEquals (0 , storage [0 ][3 ]),
58+ ()
59+ -> assertEquals (0 , storage [1 ][0 ]),
60+ ()
61+ -> assertEquals (0 , storage [1 ][1 ]),
62+ ()
63+ -> assertEquals (0 , storage [1 ][2 ]),
64+ ()
65+ -> assertEquals (0 , storage [1 ][3 ]),
66+ ()
67+ -> assertEquals (0 , storage [2 ][0 ]),
68+ () -> 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 ]));
69+ }
70+
71+ @ Test
72+ public void testEditDistanceOneCharacterDifference () {
73+ assertEquals (1 , EditDistance .editDistance ("cat" , "bat" ));
74+ assertEquals (1 , EditDistance .editDistance ("cat" , "cats" ));
75+ assertEquals (1 , EditDistance .editDistance ("cats" , "cat" ));
76+ }
77+
78+ @ Test
79+ public void testEditDistanceOneCharacterDifferenceMemoization () {
80+ int [][] storage = new int [3 ][3 ];
81+ assertAll ("All assertions" ,
82+ ()
83+ -> assertEquals (1 , EditDistance .editDistance ("at" , "it" , storage )),
84+ ()
85+ -> assertEquals (0 , storage [0 ][0 ]),
86+ ()
87+ -> assertEquals (1 , storage [0 ][1 ]),
88+ () -> 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 ]));
89+ }
90+
91+ @ Test
92+ public void testEditDistanceGeneralCases () {
93+ assertEquals (3 , EditDistance .editDistance ("kitten" , "sitting" ));
94+ assertEquals (2 , EditDistance .editDistance ("flaw" , "lawn" ));
95+ assertEquals (5 , EditDistance .editDistance ("intention" , "execution" ));
96+ }
97+
98+ @ Test
99+ public void testEditDistanceGeneralCasesMemoization () {
100+ int [][] storage = new int [7 ][8 ];
101+ assertEquals (3 , EditDistance .editDistance ("kitten" , "sitting" , storage ));
102+ assertAll ("All assertions" , () -> assertEquals (0 , storage [0 ][0 ]), () -> assertEquals (3 , storage [6 ][7 ]));
103+ }
15104}
0 commit comments