From 41df2de2ce415c8af170e4b11a28c72d1e031347 Mon Sep 17 00:00:00 2001 From: Sourav Hota Date: Mon, 23 Nov 2026 19:59:32 +0530 Subject: [PATCH] Add boundary tests for LongestCommonSubstring Define and verify the supported boundary conditions: null inputs, empty strings, single characters, identical strings, disjoint strings, matches at start/end positions, substring containment, and large-input stress tests. Fixes: #22 --- .../LongestCommonSubstringBoundaryTest.java | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/test/java/com/thealgorithms/strings/LongestCommonSubstringBoundaryTest.java diff --git a/src/test/java/com/thealgorithms/strings/LongestCommonSubstringBoundaryTest.java b/src/test/java/com/thealgorithms/strings/LongestCommonSubstringBoundaryTest.java new file mode 100644 index 000000000000..53e110bded7c --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/LongestCommonSubstringBoundaryTest.java @@ -0,0 +1,178 @@ +package com.thealgorithms.strings; + +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; + +/** + * Boundary tests for {@link LongestCommonSubstring}. + * + * These tests define and verify the supported boundary conditions of the + * longestCommonSubstring() method: + * + * + * + * Fixes: #22 + */ +public class LongestCommonSubstringBoundaryTest { + + // ── B1: Both inputs null ─────────────────────────────────────────── + + @Test + void boundaryBothNull() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring(null, null)); + } + + // ── B2: One input null ───────────────────────────────────────────── + + @Test + void boundaryFirstNull() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring(null, "hello")); + } + + @Test + void boundarySecondNull() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("hello", null)); + } + + // ── B3: Empty string inputs ──────────────────────────────────────── + + @Test + void boundaryBothEmpty() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("", "")); + } + + @Test + void boundaryFirstEmpty() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("", "abc")); + } + + @Test + void boundarySecondEmpty() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", "")); + } + + // ── B4: Single-character inputs ──────────────────────────────────── + + @Test + void boundarySingleCharMatch() { + assertEquals("x", LongestCommonSubstring.longestCommonSubstring("x", "x")); + } + + @Test + void boundarySingleCharMismatch() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("x", "y")); + } + + @Test + void boundarySingleCharVsLongerString() { + assertEquals("a", LongestCommonSubstring.longestCommonSubstring("a", "xyzaz")); + } + + // ── B5: Identical strings ────────────────────────────────────────── + + @Test + void boundaryIdenticalStrings() { + assertEquals("hello", LongestCommonSubstring.longestCommonSubstring("hello", "hello")); + } + + @Test + void boundaryIdenticalSingleChar() { + assertEquals("z", LongestCommonSubstring.longestCommonSubstring("z", "z")); + } + + // ── B6: Completely disjoint strings ──────────────────────────────── + + @Test + void boundaryCompletelyDisjoint() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("abc", "xyz")); + } + + @Test + void boundaryDisjointLongerStrings() { + assertEquals("", LongestCommonSubstring.longestCommonSubstring("abcdef", "ghijkl")); + } + + // ── B7: Match at the very start ──────────────────────────────────── + + @Test + void boundaryMatchAtStart() { + assertEquals("abc", LongestCommonSubstring.longestCommonSubstring("abcXYZ", "abcMNO")); + } + + // ── B8: Match at the very end ────────────────────────────────────── + + @Test + void boundaryMatchAtEnd() { + assertEquals("xyz", LongestCommonSubstring.longestCommonSubstring("ABCxyz", "MNOxyz")); + } + + // ── B9: One string is a complete substring of the other ──────────── + + @Test + void boundaryOneIsSubstringOfOther() { + assertEquals("short", LongestCommonSubstring.longestCommonSubstring("short", "a_short_string")); + } + + @Test + void boundaryLongerContainsShorter() { + assertEquals("inner", LongestCommonSubstring.longestCommonSubstring("the_inner_part", "inner")); + } + + // ── B10: Large input stress test (boundary of practical size) ────── + + @Test + void boundaryLargeIdenticalStrings() { + String large = "A".repeat(5000); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + assertEquals(large, LongestCommonSubstring.longestCommonSubstring(large, large)); + }); + } + + @Test + void boundaryLargeDisjointStrings() { + String a = "A".repeat(5000); + String b = "B".repeat(5000); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + assertEquals("", LongestCommonSubstring.longestCommonSubstring(a, b)); + }); + } + + @Test + void boundaryLargeWithSmallCommonSubstring() { + // Common part is exactly 10 chars, embedded in large surrounding noise + String common = "BOUNDARY10"; + String a = "X".repeat(2000) + common + "X".repeat(2000); + String b = "Y".repeat(2000) + common + "Y".repeat(2000); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + assertEquals(common, LongestCommonSubstring.longestCommonSubstring(a, b)); + }); + } + + // ── Edge: Strings that differ only in the last character ─────────── + + @Test + void boundaryDifferOnlyInLastChar() { + assertEquals("abcde", LongestCommonSubstring.longestCommonSubstring("abcdeX", "abcdeY")); + } + + // ── Edge: Strings that differ only in the first character ────────── + + @Test + void boundaryDifferOnlyInFirstChar() { + assertEquals("bcdef", LongestCommonSubstring.longestCommonSubstring("Xbcdef", "Ybcdef")); + } +}