|
| 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 | +consttext='ABCDEFG' |
| 8 | +constpattern='ABC' |
| 9 | +expect(stringSearch(text,pattern)).toStrictEqual([0]) |
| 10 | +}) |
| 11 | + |
| 12 | +it('should find a pattern in the middle of the text',()=>{ |
| 13 | +consttext='ABCDEFG' |
| 14 | +constpattern='CDE' |
| 15 | +expect(stringSearch(text,pattern)).toStrictEqual([2]) |
| 16 | +}) |
| 17 | + |
| 18 | +it('should find a pattern at the end of the text',()=>{ |
| 19 | +consttext='ABCDEFG' |
| 20 | +constpattern='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 | +consttext='ABCDEFG' |
| 27 | +constpattern='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 | +consttext='ABC DEF ABC GHI ABC' |
| 36 | +constpattern='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 | +consttext='ABABAB' |
| 44 | +constpattern='ABAB' |
| 45 | +expect(stringSearch(text,pattern)).toStrictEqual([0,2]) |
| 46 | +}) |
| 47 | + |
| 48 | +it('should find multiple single character occurrences',()=>{ |
| 49 | +consttext='ABCDABCDABCD' |
| 50 | +constpattern='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 | +consttext='AAAAA' |
| 57 | +constpattern='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 | +consttext='ABCDEFG' |
| 66 | +constpattern='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 | +consttext='ABC' |
| 73 | +constpattern='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 | +consttext='ABCDEFG' |
| 80 | +constpattern='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 | +consttext='' |
| 90 | +constpattern='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 | +consttext='ABCDEFG' |
| 97 | +constpattern='' |
| 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 | +consttext='ABC' |
| 104 | +constpattern='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 | +consttext='A' |
| 111 | +constpattern='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 | +consttext='A' |
| 118 | +constpattern='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 | +consttext='ABABCABABABD' |
| 129 | +constpattern='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 | +consttext='ABCABCABCABC' |
| 136 | +constpattern='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 | +consttext='ABC ABCDAB ABCDABCDABDE' |
| 144 | +constpattern='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 | +consttext='ABC ABCDABD ABCDABCDABDE' |
| 151 | +constpattern='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 | +consttext='Hello World Hello' |
| 161 | +constpattern='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 | +consttext='abc@def@ghi@jkl' |
| 168 | +constpattern='@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 | +consttext='Hello World' |
| 175 | +constpattern='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 | +consttext='Hello World Hello' |
| 182 | +constpattern='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 | +consttext='ATCGATCGATCG' |
| 192 | +constpattern='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 | +consttext='the cat in the hat' |
| 199 | +constpattern='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 | +consttext='123456789012345' |
| 206 | +constpattern='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 | +consttext='A'.repeat(100)+'B' |
| 216 | +constpattern='A' |
| 217 | +constresult=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 | +consttext='A'.repeat(100)+'XYZ' |
| 227 | +constpattern='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 | +consttext='ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 235 | +constpattern='MNOP' |
| 236 | +expect(stringSearch(text,pattern)).toStrictEqual([12]) |
| 237 | +}) |
| 238 | +}) |
| 239 | +}) |