Skip to content

Commit 8cab588

Browse files
committed
Add tests for KMP
1 parent 401f3f1 commit 8cab588

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var indexOf = require('../../src/searching/knuth-morris-pratt').kmp;
2+
3+
describe('The string searching algorithm of Knuth-Morris-Pratt', function () {
4+
'use strict';
5+
6+
it('should find the empty string in any string', function () {
7+
expect(indexOf('', '')).toBe(0);
8+
expect(indexOf('foo', '')).toBe(0);
9+
});
10+
11+
it('should return negative value for patterns, which are ' +
12+
'not part of the string', function () {
13+
expect(indexOf('foo', 'bar') < 0).toBeTruthy();
14+
expect(indexOf('f', 'foobar') < 0).toBeTruthy();
15+
expect(indexOf('foobar', 'fobar') < 0).toBeTruthy();
16+
});
17+
18+
it('should return the first index of the matching pattern', function () {
19+
expect(indexOf('foo', 'f')).toBe(0);
20+
expect(indexOf('foo', 'oo')).toBe(1);
21+
expect(indexOf('foo', 'o')).toBe(1);
22+
expect(indexOf('foobar', 'foo')).toBe(0);
23+
expect(indexOf('foobar', 'bar')).toBe(3);
24+
});
25+
});

0 commit comments

Comments
 (0)