11package com .thealgorithms .strings ;
22
3- import java .util .stream .Stream ;
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import org .junit .jupiter .api .Test ;
45
5- import org .junit .jupiter .api .Assertions ;
6- import org .junit .jupiter .params .ParameterizedTest ;
7- import org .junit .jupiter .params .provider .MethodSource ;
6+ public class LongestCommonSubstringTest {
87
9- public final class LongestCommonSubstringTest {
8+ @ Test
9+ public void testNullOrEmptyInputs () {
10+ assertEquals ("" , LongestCommonSubstring .longestCommonSubstring (null , "abc" ));
11+ assertEquals ("" , LongestCommonSubstring .longestCommonSubstring ("abc" , null ));
12+ assertEquals ("" , LongestCommonSubstring .longestCommonSubstring ("" , "abc" ));
13+ assertEquals ("" , LongestCommonSubstring .longestCommonSubstring ("abc" , "" ));
14+ }
1015
11- private record TestData (String a , String b , String expected ) {
16+ @ Test
17+ public void testNormalSubstrings () {
18+ assertEquals ("cde" , LongestCommonSubstring .longestCommonSubstring ("abcdef" , "zcdemf" ));
19+ assertEquals ("abc" , LongestCommonSubstring .longestCommonSubstring ("abc" , "abc" ));
20+ assertEquals ("cdef" , LongestCommonSubstring .longestCommonSubstring ("abcdef" , "cdefgh" ));
1221 }
1322
14- private static Stream <TestData > testLongestCommonSubstring () {
15- return Stream .of (
16- new TestData (null , "abc" , "" ),
17- new TestData ("abc" , null , "" ),
18- new TestData ("" , "abc" , "" ),
19- new TestData ("abc" , "" , "" ),
20- new TestData ("abcdef" , "zcdemf" , "cde" ),
21- new TestData ("abc" , "abc" , "abc" ),
22- new TestData ("abc" , "xyz" , "" ),
23- new TestData ("abcdef" , "cdefgh" , "cdef" ),
24- new TestData ("a" , "a" , "a" ),
25- new TestData ("abcXdef" , "abcYdef" , "abc" )
26- );
23+ @ Test
24+ public void testSingleCharacterAndNoMatch () {
25+ assertEquals ("a" , LongestCommonSubstring .longestCommonSubstring ("a" , "a" ));
26+ assertEquals ("" , LongestCommonSubstring .longestCommonSubstring ("abc" , "xyz" ));
2727 }
2828
29- @ ParameterizedTest
30- @ MethodSource
31- void testLongestCommonSubstring (TestData testData ) {
32- String actual = LongestCommonSubstring .longestCommonSubstring (testData .a (), testData .b ());
33- Assertions .assertEquals (testData .expected (), actual );
29+ @ Test
30+ public void testMultipleMatchesFirstLongest () {
31+ // Keeps the first matched longest substring when lengths are tied
32+ assertEquals ("abc" , LongestCommonSubstring .longestCommonSubstring ("abcXdef" , "abcYdef" ));
3433 }
3534}
0 commit comments