11package com .thealgorithms .dynamicprogramming ;
22
3+ import static org .junit .jupiter .api .Assertions .*;
4+ import static org .junit .jupiter .api .Assertions .assertAll ;
5+
36import org .junit .jupiter .api .Test ;
47import org .junit .jupiter .params .ParameterizedTest ;
58import org .junit .jupiter .params .provider .CsvSource ;
69
7- import static org .junit .jupiter .api .Assertions .*;
8- import static org .junit .jupiter .api .Assertions .assertAll ;
9-
1010public class EditDistanceTest {
1111
1212 @ ParameterizedTest
@@ -17,15 +17,9 @@ void testMinDistance(String str1, String str2, int expected) {
1717
1818 @ Test
1919 void testEditDistanceStringNull () {
20- assertThrows (NullPointerException .class , () -> {
21- EditDistance .editDistance (null , "abc" );
22- });
23- assertThrows (NullPointerException .class , () -> {
24- EditDistance .editDistance ("abc" , null );
25- });
26- assertThrows (NullPointerException .class , () -> {
27- EditDistance .editDistance (null , null );
28- });
20+ assertThrows (NullPointerException .class , () -> { EditDistance .editDistance (null , "abc" ); });
21+ assertThrows (NullPointerException .class , () -> { EditDistance .editDistance ("abc" , null ); });
22+ assertThrows (NullPointerException .class , () -> { EditDistance .editDistance (null , null ); });
2923 }
3024
3125 @ Test
@@ -42,15 +36,9 @@ void testEditDistanceStringEmpty() {
4236
4337 @ Test
4438 void testEditDistance_NullValue () {
45- assertThrows (NullPointerException .class , () -> {
46- EditDistance .editDistance (null , "hello" );
47- });
48- assertThrows (NullPointerException .class , () -> {
49- EditDistance .editDistance ("hello" , null );
50- });
51- assertThrows (NullPointerException .class , () -> {
52- EditDistance .editDistance (null , null );
53- });
39+ assertThrows (NullPointerException .class , () -> { EditDistance .editDistance (null , "hello" ); });
40+ assertThrows (NullPointerException .class , () -> { EditDistance .editDistance ("hello" , null ); });
41+ assertThrows (NullPointerException .class , () -> { EditDistance .editDistance (null , null ); });
5442 }
5543
5644 @ Test
@@ -67,15 +55,10 @@ public void testEditDistance_OneEmptyString() {
6755 @ Test
6856 public void testEditDistance_OneEmptyString_Memoization () {
6957 int [][] storage = new int [1 ][6 ];
70- ;
7158 assertAll ("String assertions" ,
72- () -> assertEquals (5 , EditDistance .editDistance ("" , "hello" , storage )),
73- () -> assertEquals (0 , storage [0 ][0 ]),
74- () -> assertEquals (0 , storage [0 ][1 ]),
75- () -> assertEquals (0 , storage [0 ][2 ]),
76- () -> assertEquals (0 , storage [0 ][3 ]),
77- () -> assertEquals (0 , storage [0 ][4 ]),
78- () -> assertEquals (5 , storage [0 ][5 ])
59+ ()
60+ -> assertEquals (5 , EditDistance .editDistance ("" , "hello" , storage )),
61+ () -> 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 ])
7962 );
8063 }
8164
@@ -89,23 +72,27 @@ public void testEditDistance_EqualStrings() {
8972 public void testEditDistance_EqualStrings_Memoization () {
9073 int [][] storage = new int [4 ][4 ];
9174 assertAll ("String assertions" ,
92- () -> assertEquals (0 , EditDistance .editDistance ("abc" , "abc" , storage )),
93- () -> assertEquals (0 , storage [0 ][0 ]),
94- () -> assertEquals (0 , storage [0 ][1 ]),
95- () -> assertEquals (0 , storage [0 ][2 ]),
96- () -> assertEquals (0 , storage [0 ][3 ]),
97- () -> assertEquals (0 , storage [1 ][0 ]),
98- () -> assertEquals (0 , storage [1 ][1 ]),
99- () -> assertEquals (0 , storage [1 ][2 ]),
100- () -> assertEquals (0 , storage [1 ][3 ]),
101- () -> assertEquals (0 , storage [2 ][0 ]),
102- () -> assertEquals (0 , storage [2 ][1 ]),
103- () -> assertEquals (0 , storage [2 ][2 ]),
104- () -> assertEquals (0 , storage [2 ][3 ]),
105- () -> assertEquals (0 , storage [3 ][0 ]),
106- () -> assertEquals (0 , storage [3 ][1 ]),
107- () -> assertEquals (0 , storage [3 ][2 ]),
108- () -> assertEquals (0 , storage [3 ][3 ])
75+ () ->
76+ assertEquals (0 , EditDistance .editDistance ("abc" , "abc" , storage )),
77+ () ->
78+ assertEquals (0 , storage [0 ][0 ]),
79+ () ->
80+ assertEquals (0 , storage [0 ][1 ]),
81+ () ->
82+ assertEquals (0 , storage [0 ][2 ]),
83+ () ->
84+ assertEquals (0 , storage [0 ][3 ]),
85+ () ->
86+ assertEquals (0 , storage [1 ][0 ]),
87+ () ->
88+ assertEquals (0 , storage [1 ][1 ]),
89+ () ->
90+ assertEquals (0 , storage [1 ][2 ]),
91+ () ->
92+ assertEquals (0 , storage [1 ][3 ]),
93+ () ->
94+ assertEquals (0 , storage [2 ][0 ]),
95+ () -> 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 ])
10996 );
11097 }
11198
@@ -120,16 +107,13 @@ public void testEditDistance_OneCharacterDifference() {
120107 public void testEditDistance_OneCharacterDifference_Memoization () {
121108 int [][] storage = new int [3 ][3 ];
122109 assertAll ("All assertions" ,
123- () -> assertEquals (1 , EditDistance .editDistance ("at" , "it" , storage )),
124- () -> assertEquals (0 , storage [0 ][0 ]),
125- () -> assertEquals (1 , storage [0 ][1 ]),
126- () -> assertEquals (2 , storage [0 ][2 ]),
127- () -> assertEquals (1 , storage [1 ][0 ]),
128- () -> assertEquals (0 , storage [1 ][1 ]),
129- () -> assertEquals (1 , storage [1 ][2 ]),
130- () -> assertEquals (2 , storage [2 ][0 ]),
131- () -> assertEquals (1 , storage [2 ][1 ]),
132- () -> assertEquals (1 , storage [2 ][2 ])
110+ () ->
111+ assertEquals (1 , EditDistance .editDistance ("at" , "it" , storage )),
112+ () ->
113+ assertEquals (0 , storage [0 ][0 ]),
114+ () ->
115+ assertEquals (1 , storage [0 ][1 ]),
116+ () -> 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 ])
133117 );
134118 }
135119
@@ -144,9 +128,6 @@ public void testEditDistance_GeneralCases() {
144128 public void testEditDistance_GeneralCases_Memoization () {
145129 int [][] storage = new int [7 ][8 ];
146130 assertEquals (3 , EditDistance .editDistance ("kitten" , "sitting" , storage ));
147- assertAll ("All assertions" ,
148- () -> assertEquals (0 , storage [0 ][0 ]),
149- () -> assertEquals (3 , storage [6 ][7 ])
150- );
131+ assertAll ("All assertions" , () -> assertEquals (0 , storage [0 ][0 ]), () -> assertEquals (3 , storage [6 ][7 ]));
151132 }
152133}
0 commit comments