-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathLongestCommonSubsequenceTest.java
89 lines (77 loc) · 2.86 KB
/
LongestCommonSubsequenceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class LongestCommonSubsequenceTest {
@Test
public void testLCSBasic() {
String str1 = "ABCBDAB";
String str2 = "BDCAB";
String expected = "BDAB"; // The longest common subsequence
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSIdenticalStrings() {
String str1 = "AGGTAB";
String str2 = "AGGTAB";
String expected = "AGGTAB"; // LCS is the same as the strings
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSNoCommonCharacters() {
String str1 = "ABC";
String str2 = "XYZ";
String expected = ""; // No common subsequence
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSWithEmptyString() {
String str1 = "";
String str2 = "XYZ";
String expected = ""; // LCS with an empty string should be empty
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSWithBothEmptyStrings() {
String str1 = "";
String str2 = "";
String expected = ""; // LCS with both strings empty should be empty
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSWithNullFirstString() {
String str1 = null;
String str2 = "XYZ";
String expected = null; // Should return null if first string is null
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSWithNullSecondString() {
String str1 = "ABC";
String str2 = null;
String expected = null; // Should return null if second string is null
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSWithNullBothStrings() {
String str1 = null;
String str2 = null;
String expected = null; // Should return null if both strings are null
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
@Test
public void testLCSWithLongerStringContainingCommonSubsequence() {
String str1 = "ABCDEF";
String str2 = "AEBDF";
String expected = "ABDF"; // Common subsequence is "ABDF"
String result = LongestCommonSubsequence.getLCS(str1, str2);
assertEquals(expected, result);
}
}