Skip to content

Commit 4ad5c0e

Browse files
authored
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.
1 parent e78d53d commit 4ad5c0e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package strings;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.List;
6+
import org.junit.jupiter.api.Test;
7+
8+
class NaivePatternSearchTest {
9+
10+
@Test
11+
void testPatternFound() {
12+
String text = "abcdxabchjikabc";
13+
String pattern = "abc";
14+
List<Integer> result = NaivePatternSearch.search(text, pattern);
15+
assertEquals(List.of(0, 5, 12), result);
16+
}
17+
18+
@Test
19+
void testPatternNotFound() {
20+
String text = "hello world";
21+
String pattern = "abc";
22+
List<Integer> result = NaivePatternSearch.search(text, pattern);
23+
assertTrue(result.isEmpty());
24+
}
25+
26+
@Test
27+
void testPatternAtEnd() {
28+
String text = "xyzabc";
29+
String pattern = "abc";
30+
List<Integer> result = NaivePatternSearch.search(text, pattern);
31+
assertEquals(List.of(3), result);
32+
}
33+
34+
@Test
35+
void testPatternEqualsText() {
36+
String text = "abc";
37+
String pattern = "abc";
38+
List<Integer> result = NaivePatternSearch.search(text, pattern);
39+
assertEquals(List.of(0), result);
40+
}
41+
}

0 commit comments

Comments
 (0)