Skip to content

Commit e528b62

Browse files
author
Mrinal Chauhan
committed
test: added test cases for Z Algorithm
1 parent 56a96c7 commit e528b62

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

src/test/java/com/thealgorithms/strings/ZalgorithmTest.java

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,42 @@
77
import org.junit.jupiter.api.Test;
88

99
public class ZalgorithmTest {
10+
1011
/**
1112
* Test 1:
12-
* Input: text = "ababcababcabc", pattern = "abc"
13-
* Output: [2, 7, 10]
14-
* Explanation: The pattern "abc" occurs at indices 2, 7, and 10.
13+
* Pattern: "a", Text: "aabaaabaa"
14+
* Expected: [0, 1, 4, 5, 8]
1515
*/
1616
@Test
17-
public void testFindPatternOccurrences() {
18-
String text = "ababcababcabc";
19-
String pattern = "abc";
20-
List<Integer> expected = Arrays.asList(2, 7, 10);
21-
22-
List<Integer> actual = Zalgorithm.findPatternOccurrences(text, pattern);
23-
24-
assertEquals(expected, actual);
17+
public void testFindPatternOccurrencesSingleCharacter() {
18+
String text = "aabaaabaa";
19+
String pattern = "a";
20+
List<Integer> expected = Arrays.asList(0, 1, 4, 5, 8);
21+
assertEquals(expected, Zalgorithm.findPatternOccurrences(text, pattern));
2522
}
2623

2724
/**
2825
* Test 2:
29-
* Input: text = "abcdefg", pattern = "xyz"
30-
* Output: []
31-
* Explanation: The pattern "xyz" does not occur in the text.
26+
* Pattern: "abc", Text: "ababcabcabc"
27+
* Expected: [2, 5, 8]
3228
*/
3329
@Test
34-
public void testFindPatternOccurrencesNoMatch() {
35-
String text = "abcdefg";
36-
String pattern = "xyz";
37-
List<Integer> expected = Arrays.asList();
38-
39-
List<Integer> actual = Zalgorithm.findPatternOccurrences(text, pattern);
40-
41-
assertEquals(expected, actual);
30+
public void testFindPatternOccurrences() {
31+
String text = "ababcabcabc";
32+
String pattern = "abc";
33+
List<Integer> expected = Arrays.asList(2, 5, 8);
34+
assertEquals(expected, Zalgorithm.findPatternOccurrences(text, pattern));
4235
}
43-
4436
/**
4537
* Test 3:
46-
* Input: text = "aabbaabbaaa", pattern = "a"
47-
* Output: [0, 3, 5, 9]
48-
* Explanation: The pattern "a" occurs at indices 0, 3, 5, and 9.
38+
* Pattern: "aa", Text: "aaaaaa"
39+
* Expected: [0, 1, 2, 3, 4]
4940
*/
5041
@Test
51-
public void testFindPatternOccurrencesSingleCharacter() {
52-
String text = "aabbaabbaaa";
53-
String pattern = "a";
54-
List<Integer> expected = Arrays.asList(0, 3, 5, 9);
55-
56-
List<Integer> actual = Zalgorithm.findPatternOccurrences(text, pattern);
57-
58-
assertEquals(expected, actual);
42+
public void testFindPatternOccurrencesRepeated() {
43+
String text = "aaaaaa";
44+
String pattern = "aa";
45+
List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
46+
assertEquals(expected, Zalgorithm.findPatternOccurrences(text, pattern));
5947
}
6048
}

0 commit comments

Comments
 (0)