Repository navigation
Expand file tree
/
Copy pathLongestCommonSubstringTest.java
More file actions
131 lines (106 loc) · 5.04 KB
/
Copy pathLongestCommonSubstringTest.java
File metadata and controls
131 lines (106 loc) · 5.04 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.thealgorithms.strings;
// author: Vraj Prajapati @Rosander0
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import java.time.Duration;
import org.junit.jupiter.api.Test;
public class LongestCommonSubstringTest {
@Test
public void testNullOrEmptyInputs() {
assertEquals("", LongestCommonSubstring.longestCommonSubstring(null, "abc"));
assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", null));
assertEquals("", LongestCommonSubstring.longestCommonSubstring("", "abc"));
assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", ""));
}
@Test
public void testNormalSubstrings() {
assertEquals("cde", LongestCommonSubstring.longestCommonSubstring("abcdef", "zcdemf"));
assertEquals("abc", LongestCommonSubstring.longestCommonSubstring("abc", "abc"));
assertEquals("cdef", LongestCommonSubstring.longestCommonSubstring("abcdef", "cdefgh"));
}
@Test
public void testSingleCharacterAndNoMatch() {
assertEquals("a", LongestCommonSubstring.longestCommonSubstring("a", "a"));
assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", "xyz"));
}
@Test
public void testMultipleMatchesFirstLongest() {
// Keeps the first matched longest substring when lengths are tied
assertEquals("abc", LongestCommonSubstring.longestCommonSubstring("abcXdef", "abcYdef"));
}
// NEW
@Test
public void testSpacesAndSpecialCharacters() {
assertEquals(" Hello World! ", LongestCommonSubstring.longestCommonSubstring("123 Hello World! 456", "ABC Hello World! XYZ"));
assertEquals("@#$%^", LongestCommonSubstring.longestCommonSubstring("test@#$%^123", "abc@#$%^xyz"));
}
@Test
public void testCoincidenceAtBoundaries() {
// Match at the beginning
assertEquals("PREFIX_", LongestCommonSubstring.longestCommonSubstring("PREFIX_12345", "PREFIX_67890"));
// Match at the end
assertEquals("_SUFFIX", LongestCommonSubstring.longestCommonSubstring("12345_SUFFIX", "67890_SUFFIX"));
}
@Test
public void testRepeatedPatterns() {
assertEquals("anabanana", LongestCommonSubstring.longestCommonSubstring("bananabanana", "anabanana"));
}
@Test
public void testLargeInputsPerformanceAndTimeout() {
// Generate two 3,000-character strings containing a common substring in the middle
int size = 3000;
StringBuilder sb1 = new StringBuilder(size);
StringBuilder sb2 = new StringBuilder(size);
for (int i = 0; i < 1000; i++) {
sb1.append('A');
sb2.append('B');
}
String commonPart = "COMMON_LONG_SUBSTRING_TEST_1234567890";
sb1.append(commonPart);
sb2.append(commonPart);
for (int i = 0; i < 1500; i++) {
sb1.append('X');
sb2.append('Y');
}
// Verify that the algorithm completes within 2 seconds
assertTimeoutPreemptively(Duration.ofSeconds(2), () -> {
String result = LongestCommonSubstring.longestCommonSubstring(sb1.toString(), sb2.toString());
assertEquals(commonPart, result);
});
}
@Test
public void testVeryLargeInputsPerformance() {
// Generate two very large strings (4,000 characters each)
int size = 4000;
StringBuilder sb1 = new StringBuilder(size);
StringBuilder sb2 = new StringBuilder(size);
for (int i = 0; i < size; i++) {
sb1.append('A');
sb2.append('B');
}
assertTimeoutPreemptively(Duration.ofSeconds(2), () -> { assertEquals("", LongestCommonSubstring.longestCommonSubstring(sb1.toString(), sb2.toString())); });
}
@Test
public void testCaseSensitivityAndUnicode() {
// Case sensitivity test
assertEquals("ABC", LongestCommonSubstring.longestCommonSubstring("ABCdef", "123ABCxyz"));
assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", "ABC"));
// Full substring containment
assertEquals("inside", LongestCommonSubstring.longestCommonSubstring("inside", "text_inside_here"));
// Unicode characters
assertEquals("_áéíóú_", LongestCommonSubstring.longestCommonSubstring("hola_áéíóú_mundo", "test_áéíóú_abc"));
}
@Test
public void testWhitespaceAndControlCharacters() {
// Test with newlines and tabs
assertEquals("\t\n", LongestCommonSubstring.longestCommonSubstring("start\t\nend", "begin\t\nfinish"));
// Test with multiple consecutive spaces
assertEquals(" ", LongestCommonSubstring.longestCommonSubstring("a b", "x y"));
}
@Test
public void testOverlappingSubstrings() {
// Test overlapping matches like "AAAA" in "AAAAA" vs "AAAA"
assertEquals("AAAA", LongestCommonSubstring.longestCommonSubstring("AAAAA", "AAAA"));
assertEquals("ABAB", LongestCommonSubstring.longestCommonSubstring("ABABAB", "CABAB"));
}
}