mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Added test for ScrambleStrings and formatted folder structure for other folders
This commit is contained in:
67
String/test/CheckVowels.test.js
Normal file
67
String/test/CheckVowels.test.js
Normal file
@ -0,0 +1,67 @@
|
||||
import { checkVowels } from '../CheckVowels'
|
||||
|
||||
describe('Test the checkVowels function', () => {
|
||||
it('expect throws on use wrong param', () => {
|
||||
expect(() => checkVowels(0)).toThrow()
|
||||
})
|
||||
|
||||
it('count the vowels in a string', () => {
|
||||
const value = 'Mad World'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(2)
|
||||
})
|
||||
|
||||
it('should return 0 when input is a string with no vowels', () => {
|
||||
const value = 'bcdfgh'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(0)
|
||||
})
|
||||
|
||||
it('should return 1 when input is a string of length 1 that is a vowel', () => {
|
||||
const value = 'a'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(1)
|
||||
})
|
||||
|
||||
it('should return the correct result when input is in all uppercase letters', () => {
|
||||
const value = 'ABCDE'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(2)
|
||||
})
|
||||
|
||||
it('should return the correct result when input is in all lowercase letters', () => {
|
||||
const value = 'abcdefghi'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(3)
|
||||
})
|
||||
|
||||
it('should return the correct result when input string contains spaces', () => {
|
||||
const value = 'abc def ghi'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(3)
|
||||
})
|
||||
|
||||
it('should return the correct result when input contains number characters', () => {
|
||||
const value = 'a1b2c3'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(1)
|
||||
})
|
||||
|
||||
it('should return the correct result when input contains punctuation characters', () => {
|
||||
const value = 'a!b.ce)'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(2)
|
||||
})
|
||||
|
||||
it('should return 0 when the input is an empty string', () => {
|
||||
const value = ''
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(0)
|
||||
})
|
||||
|
||||
it('should count multiple occurances of the same vowel in the input', () => {
|
||||
const value = 'aaaaa'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(5)
|
||||
})
|
||||
})
|
33
String/test/CheckWordOcurrence.test.js
Normal file
33
String/test/CheckWordOcurrence.test.js
Normal file
@ -0,0 +1,33 @@
|
||||
import { checkWordOccurrence } from '../CheckWordOccurrence'
|
||||
describe('checkWordOccurrence', () => {
|
||||
it('expects throw on insert wrong string', () => {
|
||||
const value = 123
|
||||
expect(() => checkWordOccurrence(value)).toThrow()
|
||||
})
|
||||
it('expect throw on insert wrong param for case sensitive', () => {
|
||||
const value = 'hello'
|
||||
expect(() => checkWordOccurrence(value, value)).toThrow()
|
||||
})
|
||||
it('check occurrence with case sensitive', () => {
|
||||
const stringToTest = 'A Mad World'
|
||||
const charsOccurrences = checkWordOccurrence(stringToTest, true)
|
||||
const expectResult = { A: 1, M: 1, a: 1, d: 2, W: 1, l: 1, o: 1, r: 1 }
|
||||
const occurrencesObjectKeys = Object.keys(charsOccurrences)
|
||||
const expectObjectKeys = Object.keys(expectResult)
|
||||
expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length)
|
||||
expectObjectKeys.forEach(key => {
|
||||
expect(expectResult[key]).toBe(charsOccurrences[key])
|
||||
})
|
||||
})
|
||||
it('check occurrence with case insensitive', () => {
|
||||
const stringToTest = 'A Mad World'
|
||||
const charsOccurrences = checkWordOccurrence(stringToTest, false)
|
||||
const expectResult = { A: 2, D: 2, L: 1, M: 1, O: 1, R: 1, W: 1 }
|
||||
const occurrencesObjectKeys = Object.keys(charsOccurrences)
|
||||
const expectObjectKeys = Object.keys(expectResult)
|
||||
expect(occurrencesObjectKeys.length).toBe(expectObjectKeys.length)
|
||||
expectObjectKeys.forEach(key => {
|
||||
expect(expectResult[key]).toBe(charsOccurrences[key])
|
||||
})
|
||||
})
|
||||
})
|
23
String/test/FormatPhoneNumber.test.js
Normal file
23
String/test/FormatPhoneNumber.test.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { formatPhoneNumber } from '../FormatPhoneNumber'
|
||||
|
||||
describe('PhoneNumberFormatting', () => {
|
||||
it('expects to return the formatted phone number', () => {
|
||||
expect(formatPhoneNumber('1234567890')).toEqual('(123) 456-7890')
|
||||
})
|
||||
|
||||
it('expects to return the formatted phone number', () => {
|
||||
expect(formatPhoneNumber(1234567890)).toEqual('(123) 456-7890')
|
||||
})
|
||||
|
||||
it('expects to throw a type error', () => {
|
||||
expect(() => { formatPhoneNumber('1234567') }).toThrow('Invalid phone number.')
|
||||
})
|
||||
|
||||
it('expects to throw a type error', () => {
|
||||
expect(() => { formatPhoneNumber('123456text') }).toThrow('Invalid phone number.')
|
||||
})
|
||||
|
||||
it('expects to throw a type error', () => {
|
||||
expect(() => { formatPhoneNumber(12345) }).toThrow('Invalid phone number.')
|
||||
})
|
||||
})
|
26
String/test/LevenshteinDistance.test.js
Normal file
26
String/test/LevenshteinDistance.test.js
Normal file
@ -0,0 +1,26 @@
|
||||
import { levenshteinDistance } from '../LevenshteinDistance'
|
||||
|
||||
describe('levenshteinDistance', () => {
|
||||
it('should calculate edit distance between two strings', () => {
|
||||
expect(levenshteinDistance('', '')).toBe(0)
|
||||
expect(levenshteinDistance('a', '')).toBe(1)
|
||||
expect(levenshteinDistance('', 'a')).toBe(1)
|
||||
expect(levenshteinDistance('abc', '')).toBe(3)
|
||||
expect(levenshteinDistance('', 'abc')).toBe(3)
|
||||
|
||||
// Should just add I to the beginning.
|
||||
expect(levenshteinDistance('igloo', 'gloo')).toBe(1)
|
||||
|
||||
// Should just substitute i with o, m with g and insert e at end
|
||||
expect(levenshteinDistance('firm', 'forge')).toBe(3)
|
||||
|
||||
// Should just substitute f with s, g with t and delete h
|
||||
expect(levenshteinDistance('fighting', 'sitting')).toBe(3)
|
||||
|
||||
// Should add 4 letters b, a, s and e at the beginning.
|
||||
expect(levenshteinDistance('ball', 'baseball')).toBe(4)
|
||||
|
||||
// Should delete 4 letters b, a, s and e at the beginning and replace the last 4 with f, o, o, t
|
||||
expect(levenshteinDistance('baseball', 'foot')).toBe(8)
|
||||
})
|
||||
})
|
12
String/test/MaxCharacter.test.js
Normal file
12
String/test/MaxCharacter.test.js
Normal file
@ -0,0 +1,12 @@
|
||||
import { maxCharacter } from '../MaxCharacter'
|
||||
|
||||
describe('Testing the maxCharacter function', () => {
|
||||
it('Expect throw with wrong arg', () => {
|
||||
expect(() => maxCharacter(123)).toThrow()
|
||||
})
|
||||
it('Check the max character in string', () => {
|
||||
const theString = 'I can\'t do that'
|
||||
const maxChar = maxCharacter(theString)
|
||||
expect(maxChar).toBe('t')
|
||||
})
|
||||
})
|
17
String/test/PermutateString.test.js
Normal file
17
String/test/PermutateString.test.js
Normal file
@ -0,0 +1,17 @@
|
||||
import { permutate } from '../PermutateString'
|
||||
|
||||
describe('Permutate a string', () => {
|
||||
it('expects to throw an Error with an empty string', () => {
|
||||
expect(() => { permutate() }).toThrow('The arg must be a valid, non empty string')
|
||||
})
|
||||
it('expects to permute "no" into [no, on]', () => {
|
||||
expect(['no', 'on']).toEqual(permutate('no'))
|
||||
})
|
||||
it('expects to permute "yes" into [esy, eys, sey, sye, yes, yse]', () => {
|
||||
expect(['esy', 'eys', 'sey', 'sye', 'yes', 'yse']).toEqual(permutate('yes'))
|
||||
})
|
||||
it('expects to permute "good" into [dgoo dogo doog gdoo godo good odgo odog ogdo ogod oodg oogd ]', () => {
|
||||
expect(['dgoo', 'dogo', 'doog', 'gdoo', 'godo', 'good', 'odgo', 'odog', 'ogdo', 'ogod', 'oodg', 'oogd'])
|
||||
.toEqual(permutate('good'))
|
||||
})
|
||||
})
|
15
String/test/ScrambleStrings.test.js
Normal file
15
String/test/ScrambleStrings.test.js
Normal file
@ -0,0 +1,15 @@
|
||||
import { isScramble } from '../ScrambleStrings'
|
||||
|
||||
describe('ScrambleStrings', () => {
|
||||
it('expects to return true for same string', () => {
|
||||
expect(isScramble('a', 'a')).toBe(true)
|
||||
})
|
||||
|
||||
it('expects to return false for non-scrambled strings', () => {
|
||||
expect(isScramble('abcde', 'caebd')).toBe(false)
|
||||
})
|
||||
|
||||
it('expects to return true for scrambled strings', () => {
|
||||
expect(isScramble('great', 'rgeat')).toBe(true)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user