-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathRabinKarp.test.js
36 lines (31 loc) · 1.08 KB
/
RabinKarp.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { rabinKarp } from '../RabinKarp'
describe('Rabin-Karp Algorithm', () => {
it('should find occurrences of the pattern in the text', () => {
const haystack = 'abracadabra'
const needle = 'abra'
const result = rabinKarp(haystack, needle)
expect(result).toEqual([0, 7])
})
it('should return an empty array if the pattern is absent', () => {
const haystack = 'hello world'
const needle = 'test'
const result = rabinKarp(haystack, needle)
expect(result).toEqual([])
})
it('should throw RangeError for empty input', () => {
expect(() => rabinKarp('', 'pattern')).toThrow(RangeError)
expect(() => rabinKarp('text', '')).toThrow(RangeError)
})
it('should return empty for patterns longer than text', () => {
const haystack = 'short'
const needle = 'longerpattern'
const result = rabinKarp(haystack, needle)
expect(result).toEqual([])
})
it('should handle single-character patterns', () => {
const haystack = 'hello'
const needle = 'e'
const result = rabinKarp(haystack, needle)
expect(result).toEqual([1])
})
})