22// author: Vraj Prajapati @Rosander0
33
44import static org .junit .jupiter .api .Assertions .assertEquals ;
5-
6- import org .junit .jupiter .api .Test ;
75import static org .junit .jupiter .api .Assertions .assertTimeoutPreemptively ;
6+
87import java .time .Duration ;
8+ import org .junit .jupiter .api .Test ;
9+
910public class LongestCommonSubstringTest {
1011
1112 @ Test
@@ -34,6 +35,7 @@ public void testMultipleMatchesFirstLongest() {
3435 // Keeps the first matched longest substring when lengths are tied
3536 assertEquals ("abc" , LongestCommonSubstring .longestCommonSubstring ("abcXdef" , "abcYdef" ));
3637 }
38+
3739 // NEW
3840
3941 @ Test
@@ -84,7 +86,7 @@ public void testLargeInputsPerformanceAndTimeout() {
8486 }
8587
8688 @ Test
87- public void testVeryLargeInputsTimeoutFailure () {
89+ public void testVeryLargeInputsPerformance () {
8890 // Generate two very large strings (4,000 characters each)
8991 int size = 4000 ;
9092 StringBuilder sb1 = new StringBuilder (size );
@@ -95,9 +97,8 @@ public void testVeryLargeInputsTimeoutFailure() {
9597 sb2 .append ('B' );
9698 }
9799
98- // Enforce a strict 50ms time limit which this O(N * M) computation will exceed
99- assertTimeoutPreemptively (Duration .ofMillis (50 ), () -> {
100- LongestCommonSubstring .longestCommonSubstring (sb1 .toString (), sb2 .toString ());
100+ assertTimeoutPreemptively (Duration .ofSeconds (2 ), () -> {
101+ assertEquals ("" , LongestCommonSubstring .longestCommonSubstring (sb1 .toString (), sb2 .toString ()));
101102 });
102103 }
103104
@@ -113,11 +114,12 @@ public void testCaseSensitivityAndUnicode() {
113114 // Unicode characters
114115 assertEquals ("_áéíóú_" , LongestCommonSubstring .longestCommonSubstring ("hola_áéíóú_mundo" , "test_áéíóú_abc" ));
115116 }
117+
116118 @ Test
117119 public void testWhitespaceAndControlCharacters () {
118120 // Test with newlines and tabs
119121 assertEquals ("\t \n " , LongestCommonSubstring .longestCommonSubstring ("start\t \n end" , "begin\t \n finish" ));
120-
122+
121123 // Test with multiple consecutive spaces
122124 assertEquals (" " , LongestCommonSubstring .longestCommonSubstring ("a b" , "x y" ));
123125 }
@@ -128,4 +130,4 @@ public void testOverlappingSubstrings() {
128130 assertEquals ("AAAA" , LongestCommonSubstring .longestCommonSubstring ("AAAAA" , "AAAA" ));
129131 assertEquals ("ABAB" , LongestCommonSubstring .longestCommonSubstring ("ABABAB" , "CABAB" ));
130132 }
131- }
133+ }
0 commit comments