|
| 1 | +package com.thealgorithms.strings; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class ZalgorithmTest { |
| 10 | + /** |
| 11 | + * 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. |
| 15 | + */ |
| 16 | + @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); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test 2: |
| 29 | + * Input: text = "abcdefg", pattern = "xyz" |
| 30 | + * Output: [] |
| 31 | + * Explanation: The pattern "xyz" does not occur in the text. |
| 32 | + */ |
| 33 | + @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); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * 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. |
| 49 | + */ |
| 50 | + @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); |
| 59 | + } |
| 60 | +} |
0 commit comments