|
| 1 | +package com.thealgorithms.strings; |
| 2 | +// author: Vraj Prajapati @Rosander0 |
| 3 | + |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +public class LongestCommonSubstringTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void testNullOrEmptyInputs() { |
| 12 | + assertEquals("", LongestCommonSubstring.longestCommonSubstring(null, "abc")); |
| 13 | + assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", null)); |
| 14 | + assertEquals("", LongestCommonSubstring.longestCommonSubstring("", "abc")); |
| 15 | + assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", "")); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testNormalSubstrings() { |
| 20 | + assertEquals("cde", LongestCommonSubstring.longestCommonSubstring("abcdef", "zcdemf")); |
| 21 | + assertEquals("abc", LongestCommonSubstring.longestCommonSubstring("abc", "abc")); |
| 22 | + assertEquals("cdef", LongestCommonSubstring.longestCommonSubstring("abcdef", "cdefgh")); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testSingleCharacterAndNoMatch() { |
| 27 | + assertEquals("a", LongestCommonSubstring.longestCommonSubstring("a", "a")); |
| 28 | + assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", "xyz")); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testMultipleMatchesFirstLongest() { |
| 33 | + // Keeps the first matched longest substring when lengths are tied |
| 34 | + assertEquals("abc", LongestCommonSubstring.longestCommonSubstring("abcXdef", "abcYdef")); |
| 35 | + } |
| 36 | +} |
0 commit comments