mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
add: countSubstrings function implementation (#1091)
This commit is contained in:
52
String/test/CountSubstrings.test.js
Normal file
52
String/test/CountSubstrings.test.js
Normal file
@ -0,0 +1,52 @@
|
||||
import { countSubstrings } from '../CountSubstrings'
|
||||
|
||||
describe('CountSubstrings', () => {
|
||||
it('count multiple occurrences of substring in a string', () => {
|
||||
const str = 'This is a string'
|
||||
const substring = 'is'
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(2)
|
||||
})
|
||||
|
||||
it('should return 0 when input substring has no occurrences', () => {
|
||||
const str = 'Jurassic Park'
|
||||
const substring = 'World'
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(0)
|
||||
})
|
||||
|
||||
it('should return 1 when input substring is of length 1 that is equal to string', () => {
|
||||
const str = 's'
|
||||
const substring = 's'
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(1)
|
||||
})
|
||||
|
||||
it('should return the correct result when input string contains spaces', () => {
|
||||
const str = 'ab cd ef ghi'
|
||||
const substring = ' '
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(4)
|
||||
})
|
||||
|
||||
it('should return the correct result when input substring contains number or special characters', () => {
|
||||
const str = 'abc1@2def1@2'
|
||||
const substring = '1@2'
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(2)
|
||||
})
|
||||
|
||||
it('should return string.length + 1 when the input substring is an empty string', () => {
|
||||
const str = 'empty'
|
||||
const substring = ''
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(6)
|
||||
})
|
||||
|
||||
it('should return correct result when input is overlapping substring', () => {
|
||||
const str = 'aaa'
|
||||
const substring = 'aa'
|
||||
const count = countSubstrings(str, substring)
|
||||
expect(count).toBe(2)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user