|
| 1 | +import { countSubstrings } from '../CountSubstrings' |
| 2 | + |
| 3 | +describe('CountSubstrings', () => { |
| 4 | + it('count multiple occurrences of substring in a string', () => { |
| 5 | + const str = 'This is a string' |
| 6 | + const substring = 'is' |
| 7 | + const count = countSubstrings(str, substring) |
| 8 | + expect(count).toBe(2) |
| 9 | + }) |
| 10 | + |
| 11 | + it('should return 0 when input substring has no occurrences', () => { |
| 12 | + const str = 'Jurassic Park' |
| 13 | + const substring = 'World' |
| 14 | + const count = countSubstrings(str, substring) |
| 15 | + expect(count).toBe(0) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should return 1 when input substring is of length 1 that is equal to string', () => { |
| 19 | + const str = 's' |
| 20 | + const substring = 's' |
| 21 | + const count = countSubstrings(str, substring) |
| 22 | + expect(count).toBe(1) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should return the correct result when input string contains spaces', () => { |
| 26 | + const str = 'ab cd ef ghi' |
| 27 | + const substring = ' ' |
| 28 | + const count = countSubstrings(str, substring) |
| 29 | + expect(count).toBe(4) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should return the correct result when input substring contains number or special characters', () => { |
| 33 | + const str = 'abc1@2def1@2' |
| 34 | + const substring = '1@2' |
| 35 | + const count = countSubstrings(str, substring) |
| 36 | + expect(count).toBe(2) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return string.length + 1 when the input substring is an empty string', () => { |
| 40 | + const str = 'empty' |
| 41 | + const substring = '' |
| 42 | + const count = countSubstrings(str, substring) |
| 43 | + expect(count).toBe(6) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should return correct result when input is overlapping substring', () => { |
| 47 | + const str = 'aaa' |
| 48 | + const substring = 'aa' |
| 49 | + const count = countSubstrings(str, substring) |
| 50 | + expect(count).toBe(2) |
| 51 | + }) |
| 52 | +}) |
0 commit comments