|
| 1 | +import { stringSearch } from '../StringSearch' |
| 2 | + |
| 3 | +describe('StringSearch', () => { |
| 4 | + // Test basic pattern matching functionality in different positions |
| 5 | + describe('Basic functionality', () => { |
| 6 | + it('should find a pattern at the beginning of the text', () => { |
| 7 | + const text = 'ABCDEFG' |
| 8 | + const pattern = 'ABC' |
| 9 | + expect(stringSearch(text, pattern)).toStrictEqual([0]) |
| 10 | + }) |
| 11 | + |
| 12 | + it('should find a pattern in the middle of the text', () => { |
| 13 | + const text = 'ABCDEFG' |
| 14 | + const pattern = 'CDE' |
| 15 | + expect(stringSearch(text, pattern)).toStrictEqual([2]) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should find a pattern at the end of the text', () => { |
| 19 | + const text = 'ABCDEFG' |
| 20 | + const pattern = 'EFG' |
| 21 | + expect(stringSearch(text, pattern)).toStrictEqual([4]) |
| 22 | + }) |
| 23 | + |
| 24 | + // Test single character pattern matching |
| 25 | + it('should find a single character pattern', () => { |
| 26 | + const text = 'ABCDEFG' |
| 27 | + const pattern = 'D' |
| 28 | + expect(stringSearch(text, pattern)).toStrictEqual([3]) |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + // Test scenarios where pattern appears multiple times in the text |
| 33 | + describe('Multiple occurrences', () => { |
| 34 | + it('should find all occurrences of non-overlapping patterns', () => { |
| 35 | + const text = 'ABC DEF ABC GHI ABC' |
| 36 | + const pattern = 'ABC' |
| 37 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 8, 16]) |
| 38 | + }) |
| 39 | + |
| 40 | + // This tests KMP's ability to find overlapping matches correctly |
| 41 | + // Pattern "ABAB" at position 0 overlaps with pattern at position 2 |
| 42 | + it('should find all occurrences of overlapping patterns', () => { |
| 43 | + const text = 'ABABAB' |
| 44 | + const pattern = 'ABAB' |
| 45 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 2]) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should find multiple single character occurrences', () => { |
| 49 | + const text = 'ABCDABCDABCD' |
| 50 | + const pattern = 'A' |
| 51 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 4, 8]) |
| 52 | + }) |
| 53 | + |
| 54 | + // Test pattern with all same characters - creates many overlapping matches |
| 55 | + it('should find patterns with repeated characters', () => { |
| 56 | + const text = 'AAAAA' |
| 57 | + const pattern = 'AAA' |
| 58 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 1, 2]) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + // Test cases where pattern should not be found |
| 63 | + describe('Pattern not found', () => { |
| 64 | + it('should return empty array when pattern is not in text', () => { |
| 65 | + const text = 'ABCDEFG' |
| 66 | + const pattern = 'XYZ' |
| 67 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 68 | + }) |
| 69 | + |
| 70 | + // Edge case: pattern longer than text should return empty immediately |
| 71 | + it('should return empty array when pattern is longer than text', () => { |
| 72 | + const text = 'ABC' |
| 73 | + const pattern = 'ABCDEFG' |
| 74 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 75 | + }) |
| 76 | + |
| 77 | + // Test partial match that fails - pattern starts similar but differs |
| 78 | + it('should return empty array for similar but different pattern', () => { |
| 79 | + const text = 'ABCDEFG' |
| 80 | + const pattern = 'ABX' |
| 81 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + // Test edge cases and boundary conditions |
| 86 | + describe('Edge cases', () => { |
| 87 | + // Empty text should always return empty array |
| 88 | + it('should handle empty text', () => { |
| 89 | + const text = '' |
| 90 | + const pattern = 'ABC' |
| 91 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 92 | + }) |
| 93 | + |
| 94 | + // Empty pattern should return empty array (no valid matches) |
| 95 | + it('should handle empty pattern', () => { |
| 96 | + const text = 'ABCDEFG' |
| 97 | + const pattern = '' |
| 98 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 99 | + }) |
| 100 | + |
| 101 | + // When text exactly matches pattern, should find at index 0 |
| 102 | + it('should handle text equal to pattern', () => { |
| 103 | + const text = 'ABC' |
| 104 | + const pattern = 'ABC' |
| 105 | + expect(stringSearch(text, pattern)).toStrictEqual([0]) |
| 106 | + }) |
| 107 | + |
| 108 | + // Minimum valid case: single character match |
| 109 | + it('should handle single character text and pattern match', () => { |
| 110 | + const text = 'A' |
| 111 | + const pattern = 'A' |
| 112 | + expect(stringSearch(text, pattern)).toStrictEqual([0]) |
| 113 | + }) |
| 114 | + |
| 115 | + // Minimum invalid case: single character mismatch |
| 116 | + it('should handle single character text and pattern mismatch', () => { |
| 117 | + const text = 'A' |
| 118 | + const pattern = 'B' |
| 119 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + // Test complex patterns that demonstrate KMP algorithm's efficiency |
| 124 | + // KMP excels at patterns with repeating substrings (prefix-suffix overlap) |
| 125 | + describe('Complex patterns (KMP advantage)', () => { |
| 126 | + // Pattern has overlapping prefix and suffix, testing KMP's prefix table efficiency |
| 127 | + it('should handle patterns with prefix-suffix overlap', () => { |
| 128 | + const text = 'ABABCABABABD' |
| 129 | + const pattern = 'ABABCABAB' |
| 130 | + expect(stringSearch(text, pattern)).toStrictEqual([0]) |
| 131 | + }) |
| 132 | + |
| 133 | + // Pattern repeats completely, creates many potential matches |
| 134 | + it('should handle patterns with repeated substrings', () => { |
| 135 | + const text = 'ABCABCABCABC' |
| 136 | + const pattern = 'ABCABC' |
| 137 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 3, 6]) |
| 138 | + }) |
| 139 | + |
| 140 | + // Classic KMP example: pattern "ABCDABD" has prefix "AB" that matches suffix "AB" |
| 141 | + // This tests that KMP correctly uses the prefix table to skip unnecessary comparisons |
| 142 | + it('should find pattern in longer text', () => { |
| 143 | + const text = 'ABC ABCDAB ABCDABCDABDE' |
| 144 | + const pattern = 'ABCDABD' |
| 145 | + expect(stringSearch(text, pattern)).toStrictEqual([15]) |
| 146 | + }) |
| 147 | + |
| 148 | + // Multiple occurrences of complex pattern with prefix-suffix overlap |
| 149 | + it('should find multiple occurrences with prefix-suffix overlap', () => { |
| 150 | + const text = 'ABC ABCDABD ABCDABCDABDE' |
| 151 | + const pattern = 'ABCDABD' |
| 152 | + expect(stringSearch(text, pattern)).toStrictEqual([4, 16]) |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + // Test handling of special characters and case sensitivity |
| 157 | + describe('Special characters and cases', () => { |
| 158 | + // Test that spaces in text don't break pattern matching |
| 159 | + it('should handle patterns with spaces', () => { |
| 160 | + const text = 'Hello World Hello' |
| 161 | + const pattern = 'Hello' |
| 162 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 12]) |
| 163 | + }) |
| 164 | + |
| 165 | + // Test that special characters (@, #, etc.) are treated as regular characters |
| 166 | + it('should handle patterns with special characters', () => { |
| 167 | + const text = 'abc@def@ghi@jkl' |
| 168 | + const pattern = '@def' |
| 169 | + expect(stringSearch(text, pattern)).toStrictEqual([3]) |
| 170 | + }) |
| 171 | + |
| 172 | + // Verify case sensitivity - 'Hello' should not match 'hello' |
| 173 | + it('should be case-sensitive', () => { |
| 174 | + const text = 'Hello World' |
| 175 | + const pattern = 'hello' |
| 176 | + expect(stringSearch(text, pattern)).toStrictEqual([]) |
| 177 | + }) |
| 178 | + |
| 179 | + // Verify correct case-sensitive matches are found |
| 180 | + it('should find case-sensitive matches', () => { |
| 181 | + const text = 'Hello World Hello' |
| 182 | + const pattern = 'Hello' |
| 183 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 12]) |
| 184 | + }) |
| 185 | + }) |
| 186 | + |
| 187 | + // Test realistic use cases for string search algorithms |
| 188 | + describe('Real-world examples', () => { |
| 189 | + // Common use case: searching for DNA subsequences (ATCG nucleotides) |
| 190 | + it('should find DNA sequence patterns', () => { |
| 191 | + const text = 'ATCGATCGATCG' |
| 192 | + const pattern = 'ATCG' |
| 193 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 4, 8]) |
| 194 | + }) |
| 195 | + |
| 196 | + // Common use case: finding word occurrences in text |
| 197 | + it('should find words in a sentence', () => { |
| 198 | + const text = 'the cat in the hat' |
| 199 | + const pattern = 'the' |
| 200 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 12]) |
| 201 | + }) |
| 202 | + |
| 203 | + // Test with numeric strings (e.g., searching for patterns in phone numbers, IDs) |
| 204 | + it('should handle numeric patterns', () => { |
| 205 | + const text = '123456789012345' |
| 206 | + const pattern = '123' |
| 207 | + expect(stringSearch(text, pattern)).toStrictEqual([0, 10]) |
| 208 | + }) |
| 209 | + }) |
| 210 | + |
| 211 | + // Test cases that stress-test the algorithm's performance characteristics |
| 212 | + describe('Performance-related cases', () => { |
| 213 | + // Test with many matches - ensures algorithm handles large result sets efficiently |
| 214 | + it('should handle pattern that appears many times', () => { |
| 215 | + const text = 'A'.repeat(100) + 'B' |
| 216 | + const pattern = 'A' |
| 217 | + const result = stringSearch(text, pattern) |
| 218 | + expect(result.length).toBe(100) |
| 219 | + expect(result[0]).toBe(0) |
| 220 | + expect(result[99]).toBe(99) |
| 221 | + }) |
| 222 | + |
| 223 | + // Test worst-case scenario: pattern at the very end of long text |
| 224 | + // KMP should efficiently skip through repeated characters |
| 225 | + it('should handle long text with pattern at end', () => { |
| 226 | + const text = 'A'.repeat(100) + 'XYZ' |
| 227 | + const pattern = 'XYZ' |
| 228 | + expect(stringSearch(text, pattern)).toStrictEqual([100]) |
| 229 | + }) |
| 230 | + |
| 231 | + // Test with pattern containing no repeated characters |
| 232 | + // In this case, KMP's prefix table will mostly be zeros, but still works correctly |
| 233 | + it('should handle pattern with no repeated characters', () => { |
| 234 | + const text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 235 | + const pattern = 'MNOP' |
| 236 | + expect(stringSearch(text, pattern)).toStrictEqual([12]) |
| 237 | + }) |
| 238 | + }) |
| 239 | +}) |
0 commit comments