From 4ad5c0e2923d8b98bceb3232a49a327372cb3940 Mon Sep 17 00:00:00 2001 From: Karthikn-VR Date: Tue, 26 Aug 2025 21:03:14 +0530 Subject: [PATCH 1/2] test: add unit tests for NaivePatternSearch This commit adds JUnit 5 test cases for the NaivePatternSearch algorithm. - Verifies correct detection of multiple matches in a string - Covers cases where the pattern is not found - Tests matching at the end of the text - Tests when the text and pattern are equal These tests ensure correctness and improve reliability of the algorithm. --- .../strings/NaivePatternSearchTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java diff --git a/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java b/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java new file mode 100644 index 000000000000..f6b1def43974 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java @@ -0,0 +1,41 @@ +package strings; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class NaivePatternSearchTest { + + @Test + void testPatternFound() { + String text = "abcdxabchjikabc"; + String pattern = "abc"; + List result = NaivePatternSearch.search(text, pattern); + assertEquals(List.of(0, 5, 12), result); + } + + @Test + void testPatternNotFound() { + String text = "hello world"; + String pattern = "abc"; + List result = NaivePatternSearch.search(text, pattern); + assertTrue(result.isEmpty()); + } + + @Test + void testPatternAtEnd() { + String text = "xyzabc"; + String pattern = "abc"; + List result = NaivePatternSearch.search(text, pattern); + assertEquals(List.of(3), result); + } + + @Test + void testPatternEqualsText() { + String text = "abc"; + String pattern = "abc"; + List result = NaivePatternSearch.search(text, pattern); + assertEquals(List.of(0), result); + } +} From 3a2274d366dfdebb3b5817375b1fe088c80e95c6 Mon Sep 17 00:00:00 2001 From: Karthikn-VR Date: Tue, 26 Aug 2025 21:48:04 +0530 Subject: [PATCH 2/2] Update NaivePatternSearchTest.java --- .../java/com/thealgorithms/strings/NaivePatternSearchTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java b/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java index f6b1def43974..d6a17e30f9c1 100644 --- a/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java +++ b/src/test/java/com/thealgorithms/strings/NaivePatternSearchTest.java @@ -1,5 +1,5 @@ package strings; - +import com.thealgorithms.strings.NaivePatternSearch; import static org.junit.jupiter.api.Assertions.*; import java.util.List;