mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
Add tests to Math (#423)
* Add prettier config * test: add test to check for absolute function * chore: es5 to es6 * test: add test to check mean function * test: add test for sum of digit * test: add test for factorial * test: add test for fibonnaci * test: add test for find HCF * test: add test for lcm * test: add gridget test * test: add test for mean square error * test: add test for modular binary exponentiation * test: add tests for palindrome * test: add test for pascals triangle * test: add tests for polynomial * test: add tests for prime check * test: add tests for reverse polish notation * test: add tests for sieve of eratosthenes * test: add tests for pi estimation monte carlo method * chore: move tests to test folder * chore: fix standardjs errors
This commit is contained in:
committed by
GitHub
parent
554abf7126
commit
e112434dee
13
Maths/test/Abs.test.js
Normal file
13
Maths/test/Abs.test.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { absVal } from '../Abs'
|
||||
|
||||
describe('absVal', () => {
|
||||
it('should return an absolute value of a negative number', () => {
|
||||
const absOfNegativeNumber = absVal(-34)
|
||||
expect(absOfNegativeNumber).toBe(34)
|
||||
})
|
||||
|
||||
it('should return an absolute value of a positive number', () => {
|
||||
const absOfPositiveNumber = absVal(50)
|
||||
expect(absOfPositiveNumber).toBe(50)
|
||||
})
|
||||
})
|
||||
11
Maths/test/AverageMean.test.js
Normal file
11
Maths/test/AverageMean.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { mean } from '../AverageMean'
|
||||
|
||||
describe('Tests for average mean', () => {
|
||||
it('should be a function', () => {
|
||||
expect(typeof mean).toEqual('function')
|
||||
})
|
||||
it('should return the mean of an array of numbers', () => {
|
||||
const meanFunction = mean([1, 2, 4, 5])
|
||||
expect(meanFunction).toBe(3)
|
||||
})
|
||||
})
|
||||
11
Maths/test/DigitSum.test.js
Normal file
11
Maths/test/DigitSum.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { digitSum } from '../DigitSum'
|
||||
|
||||
describe('digitSum', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof digitSum).toEqual('function')
|
||||
})
|
||||
it('should return the sum of digits of a given number', () => {
|
||||
const sumOfNumber = digitSum(12345)
|
||||
expect(sumOfNumber).toBe(15)
|
||||
})
|
||||
})
|
||||
35
Maths/test/Factorial.test.js
Normal file
35
Maths/test/Factorial.test.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import { calcFactorial } from '../Factorial'
|
||||
|
||||
describe('calcFactorial', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof calcFactorial).toEqual('function')
|
||||
})
|
||||
|
||||
it('should return a statement for value "0"', () => {
|
||||
expect(calcFactorial(0)).toBe('The factorial of 0 is 1.')
|
||||
})
|
||||
|
||||
it('should return a statement for "null" and "undefined"', () => {
|
||||
const nullFactorial = calcFactorial(null)
|
||||
const undefinedFactorial = calcFactorial(undefined)
|
||||
|
||||
expect(nullFactorial).toBe(
|
||||
'Sorry, factorial does not exist for null or undefined numbers.'
|
||||
)
|
||||
expect(undefinedFactorial).toBe(
|
||||
'Sorry, factorial does not exist for null or undefined numbers.'
|
||||
)
|
||||
})
|
||||
|
||||
it('should not support negative numbers', () => {
|
||||
const negativeFactorial = calcFactorial(-5)
|
||||
expect(negativeFactorial).toBe(
|
||||
'Sorry, factorial does not exist for negative numbers.'
|
||||
)
|
||||
})
|
||||
|
||||
it('should return the factorial of a positive number', () => {
|
||||
const positiveFactorial = calcFactorial(3)
|
||||
expect(positiveFactorial).toBe('The factorial of 3 is 6')
|
||||
})
|
||||
})
|
||||
30
Maths/test/Fibonacci.test.js
Normal file
30
Maths/test/Fibonacci.test.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
FibonacciDpWithoutRecursion,
|
||||
FibonacciRecursiveDP,
|
||||
FibonacciIterative,
|
||||
FibonacciRecursive
|
||||
} from '../Fibonacci'
|
||||
|
||||
describe('Fibonanci', () => {
|
||||
it('should return an array of numbers for FibonnaciIterative', () => {
|
||||
expect(FibonacciIterative(5)).toEqual(
|
||||
expect.arrayContaining([1, 1, 2, 3, 5])
|
||||
)
|
||||
})
|
||||
|
||||
it('should return an array of numbers for FibonnaciRecursive', () => {
|
||||
expect(FibonacciRecursive(5)).toEqual(
|
||||
expect.arrayContaining([1, 1, 2, 3, 5])
|
||||
)
|
||||
})
|
||||
|
||||
it('should return number for FibonnaciRecursiveDP', () => {
|
||||
expect(FibonacciRecursiveDP(5)).toBe(5)
|
||||
})
|
||||
|
||||
it('should return an array of numbers for FibonacciDpWithoutRecursion', () => {
|
||||
expect(FibonacciDpWithoutRecursion(5)).toEqual(
|
||||
expect.arrayContaining([1, 1, 2, 3, 5])
|
||||
)
|
||||
})
|
||||
})
|
||||
20
Maths/test/FindHcf.test.js
Normal file
20
Maths/test/FindHcf.test.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { findHCF } from '../FindHcf'
|
||||
|
||||
describe('findHCF', () => {
|
||||
it('should throw a statement for values less than 1', () => {
|
||||
expect(findHCF(0, 0)).toBe('Please enter values greater than zero.')
|
||||
})
|
||||
|
||||
it('should throw a statement for one value less than 1', () => {
|
||||
expect(findHCF(0, 1)).toBe('Please enter values greater than zero.')
|
||||
expect(findHCF(1, 0)).toBe('Please enter values greater than zero.')
|
||||
})
|
||||
|
||||
it('should return an error for values non-integer values', () => {
|
||||
expect(findHCF(2.24, 4.35)).toBe('Please enter whole numbers.')
|
||||
})
|
||||
|
||||
it('should return the HCF of two given integers', () => {
|
||||
expect(findHCF(27, 36)).toBe(9)
|
||||
})
|
||||
})
|
||||
20
Maths/test/FindLcm.test.js
Normal file
20
Maths/test/FindLcm.test.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { findLcm } from '../FindLcm'
|
||||
|
||||
describe('findLcm', () => {
|
||||
it('should throw a statement for values less than 1', () => {
|
||||
expect(findLcm(0, 0)).toBe('Please enter values greater than zero.')
|
||||
})
|
||||
|
||||
it('should throw a statement for one value less than 1', () => {
|
||||
expect(findLcm(1, 0)).toBe('Please enter values greater than zero.')
|
||||
expect(findLcm(0, 1)).toBe('Please enter values greater than zero.')
|
||||
})
|
||||
|
||||
it('should return an error for values non-integer values', () => {
|
||||
expect(findLcm(4.564, 7.39)).toBe('Please enter whole numbers.')
|
||||
})
|
||||
|
||||
it('should return the LCM of two given integers', () => {
|
||||
expect(findLcm(27, 36)).toBe(108)
|
||||
})
|
||||
})
|
||||
16
Maths/test/GridGet.test.js
Normal file
16
Maths/test/GridGet.test.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { gridGetX, gridGetY } from '../GridGet'
|
||||
|
||||
describe('GridGet', () => {
|
||||
it('should have a value of x for the 27th element if the square array has 400 elements', () => {
|
||||
expect(gridGetX(Math.sqrt(400), 27)).toEqual(8)
|
||||
})
|
||||
it('should have a value of x for the 11th element if the square array has 7 columns and 3 rows', () => {
|
||||
expect(gridGetX(7, 11)).toEqual(5)
|
||||
})
|
||||
it('should have a value of y for the 27th element if the square array has 400 elements', () => {
|
||||
expect(gridGetY(Math.sqrt(400), 27)).toEqual(2)
|
||||
})
|
||||
it('should have a value of y for the 11th element if the square array has 7 columns and 3 rows ', () => {
|
||||
expect(gridGetX(7, 11)).toEqual(5)
|
||||
})
|
||||
})
|
||||
21
Maths/test/MeanSquareError.test.js
Normal file
21
Maths/test/MeanSquareError.test.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { meanSquaredError } from '../MeanSquareError'
|
||||
|
||||
describe('meanSquareError', () => {
|
||||
it('should throw an error on non-array arguments', () => {
|
||||
expect(() => meanSquaredError(1, 4)).toThrow('Argument must be an Array')
|
||||
})
|
||||
|
||||
it('should throw an error on non equal length ', () => {
|
||||
const firstArr = [1, 2, 3, 4, 5]
|
||||
const secondArr = [1, 2, 3]
|
||||
expect(() => meanSquaredError(firstArr, secondArr)).toThrow(
|
||||
'The two lists must be of equal length'
|
||||
)
|
||||
})
|
||||
|
||||
it('should return the mean square error of two equal length arrays', () => {
|
||||
const firstArr = [1, 2, 3, 4, 5]
|
||||
const secondArr = [1, 3, 5, 6, 7]
|
||||
expect(meanSquaredError(firstArr, secondArr)).toBe(2.6)
|
||||
})
|
||||
})
|
||||
7
Maths/test/ModularBinaryExponentiationRecursive.test.js
Normal file
7
Maths/test/ModularBinaryExponentiationRecursive.test.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import { modularBinaryExponentiation } from '../ModularBinaryExponentiationRecursive'
|
||||
|
||||
describe('modularBinaryExponentiation', () => {
|
||||
it('should return the binary exponentiation', () => {
|
||||
expect(modularBinaryExponentiation(2, 10, 17)).toBe(4)
|
||||
})
|
||||
})
|
||||
16
Maths/test/Palindrome.test.js
Normal file
16
Maths/test/Palindrome.test.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { PalindromeRecursive, PalindromeIterative } from '../Palindrome'
|
||||
|
||||
describe('Palindrome', () => {
|
||||
it('should return true for a palindrome for PalindromeRecursive', () => {
|
||||
expect(PalindromeRecursive('mom')).toBeTruthy()
|
||||
})
|
||||
it('should return true for a palindrome for PalindromeIterative', () => {
|
||||
expect(PalindromeIterative('mom')).toBeTruthy()
|
||||
})
|
||||
it('should return false for a non-palindrome for PalindromeRecursive', () => {
|
||||
expect(PalindromeRecursive('Algorithms')).toBeFalsy()
|
||||
})
|
||||
it('should return true for a non-palindrome for PalindromeIterative', () => {
|
||||
expect(PalindromeIterative('JavaScript')).toBeFalsy()
|
||||
})
|
||||
})
|
||||
20
Maths/test/PascalTriangle.test.js
Normal file
20
Maths/test/PascalTriangle.test.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { generate } from '../PascalTriangle'
|
||||
|
||||
describe('Pascals Triangle', () => {
|
||||
it('should have the the same length as the number', () => {
|
||||
const pascalsTriangle = generate(5)
|
||||
expect(pascalsTriangle.length).toEqual(5)
|
||||
})
|
||||
it('should have same length as its index in the array', () => {
|
||||
const pascalsTriangle = generate(5)
|
||||
pascalsTriangle.forEach((arr, index) => {
|
||||
expect(arr.length).toEqual(index + 1)
|
||||
})
|
||||
})
|
||||
it('should return an array of arrays', () => {
|
||||
const pascalsTriangle = generate(3)
|
||||
expect(pascalsTriangle).toEqual(
|
||||
expect.arrayContaining([[1], [1, 1], [1, 2, 1]])
|
||||
)
|
||||
})
|
||||
})
|
||||
9
Maths/test/PiApproximationMonteCarlo.test.js
Normal file
9
Maths/test/PiApproximationMonteCarlo.test.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { piEstimation } from '../PiApproximationMonteCarlo'
|
||||
|
||||
describe('PiApproximationMonteCarlo', () => {
|
||||
it('should be between the range of 2 to 4', () => {
|
||||
const pi = piEstimation()
|
||||
const piRange = pi >= 2 && pi <= 4
|
||||
expect(piRange).toBeTruthy()
|
||||
})
|
||||
})
|
||||
37
Maths/test/Polynomial.test.js
Normal file
37
Maths/test/Polynomial.test.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Polynomial } from '../Polynomial'
|
||||
|
||||
describe('Polynomial', () => {
|
||||
it('should not return a expression for zero', () => {
|
||||
const polynomial = new Polynomial([0])
|
||||
expect(polynomial.display()).toBe('')
|
||||
})
|
||||
it('should not return an expression for zero values', () => {
|
||||
const polynomial = new Polynomial([0, 0, 0, 0, 0])
|
||||
expect(polynomial.display()).toBe('')
|
||||
})
|
||||
it('should return an expression for single a non zero value', () => {
|
||||
const polynomial = new Polynomial([9])
|
||||
expect(polynomial.display()).toBe('(9)')
|
||||
})
|
||||
it('should return an expression for two values', () => {
|
||||
const polynomial = new Polynomial([3, 2])
|
||||
expect(polynomial.display()).toBe('(2x) + (3)')
|
||||
})
|
||||
it('should return an expression for values including zero', () => {
|
||||
const polynomial = new Polynomial([0, 2])
|
||||
expect(polynomial.display()).toBe('(2x)')
|
||||
})
|
||||
it('should return an expression and evaluate it', () => {
|
||||
const polynomial = new Polynomial([1, 2, 3, 4])
|
||||
expect(polynomial.display()).toBe('(4x^3) + (3x^2) + (2x) + (1)')
|
||||
expect(polynomial.evaluate(2)).toEqual(49)
|
||||
})
|
||||
it('should evaluate 0 for zero values', () => {
|
||||
const polynomial = new Polynomial([0, 0, 0, 0])
|
||||
expect(polynomial.evaluate(5)).toEqual(0)
|
||||
})
|
||||
it('should evaluate for negative values', () => {
|
||||
const polynomial = new Polynomial([-1, -3, -4, -7])
|
||||
expect(polynomial.evaluate(-5)).toBe(789)
|
||||
})
|
||||
})
|
||||
14
Maths/test/PrimeCheck.test.js
Normal file
14
Maths/test/PrimeCheck.test.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { PrimeCheck } from '../PrimeCheck'
|
||||
|
||||
describe('PrimeCheck', () => {
|
||||
it('should return true for Prime Numbers', () => {
|
||||
expect(PrimeCheck(1000003)).toBeTruthy()
|
||||
})
|
||||
it('should return false for Non Prime Numbers', () => {
|
||||
expect(PrimeCheck(1000001)).toBeFalsy()
|
||||
})
|
||||
it('should return false for 1 and 0', () => {
|
||||
expect(PrimeCheck(1)).toBeFalsy()
|
||||
expect(PrimeCheck(0)).toBeFalsy()
|
||||
})
|
||||
})
|
||||
11
Maths/test/ReversePolishNotation.test.js
Normal file
11
Maths/test/ReversePolishNotation.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { calcRPN } from '../ReversePolishNotation'
|
||||
|
||||
describe('ReversePolishNotation', () => {
|
||||
it('should evaluate correctly for two values', () => {
|
||||
expect(calcRPN('2 3 +')).toEqual(5)
|
||||
})
|
||||
it("should evaluate' for multiple values", () => {
|
||||
expect(calcRPN('2 2 2 * +')).toEqual(6)
|
||||
expect(calcRPN('6 9 7 + 2 / + 3 *')).toEqual(42)
|
||||
})
|
||||
})
|
||||
14
Maths/test/SieveOfEratosthenes.test.js
Normal file
14
Maths/test/SieveOfEratosthenes.test.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { sieveOfEratosthenes } from '../SieveOfEratosthenes'
|
||||
import { PrimeCheck } from '../PrimeCheck'
|
||||
|
||||
describe('should return an array of prime booleans', () => {
|
||||
it('should have each element in the array as a prime boolean', () => {
|
||||
const n = 30
|
||||
const primes = sieveOfEratosthenes(n)
|
||||
primes.forEach((primeBool, index) => {
|
||||
if (primeBool) {
|
||||
expect(PrimeCheck(index)).toBeTruthy()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user