mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@@ -12,18 +12,26 @@ describe('Testing findBinomialCoefficient function', () => {
|
||||
})
|
||||
|
||||
it('should throw error when supplied arguments other than number', () => {
|
||||
expect(() => { findBinomialCoefficient('eight', 'three') }).toThrow(Error)
|
||||
expect(() => {
|
||||
findBinomialCoefficient('eight', 'three')
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error when n is less than zero', () => {
|
||||
expect(() => { findBinomialCoefficient(-1, 3) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findBinomialCoefficient(-1, 3)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error when k is less than zero', () => {
|
||||
expect(() => { findBinomialCoefficient(1, -3) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findBinomialCoefficient(1, -3)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error when n and k are less than zero', () => {
|
||||
expect(() => { findBinomialCoefficient(-1, -3) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findBinomialCoefficient(-1, -3)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,16 +1,37 @@
|
||||
import { findRoot } from '../BisectionMethod'
|
||||
|
||||
test('Equation f(x) = x^2 - 3*x + 2 = 0, has root x = 1 in [a, b] = [0, 1.5]', () => {
|
||||
const root = findRoot(0, 1.5, (x) => { return Math.pow(x, 2) - 3 * x + 2 }, 8)
|
||||
const root = findRoot(
|
||||
0,
|
||||
1.5,
|
||||
(x) => {
|
||||
return Math.pow(x, 2) - 3 * x + 2
|
||||
},
|
||||
8
|
||||
)
|
||||
expect(root).toBe(0.9990234375)
|
||||
})
|
||||
|
||||
test('Equation f(x) = ln(x) + sqrt(x) + π*x^2 = 0, has root x = 0.36247037 in [a, b] = [0, 10]', () => {
|
||||
const root = findRoot(0, 10, (x) => { return Math.log(x) + Math.sqrt(x) + Math.PI * Math.pow(x, 2) }, 32)
|
||||
const root = findRoot(
|
||||
0,
|
||||
10,
|
||||
(x) => {
|
||||
return Math.log(x) + Math.sqrt(x) + Math.PI * Math.pow(x, 2)
|
||||
},
|
||||
32
|
||||
)
|
||||
expect(Number(Number(root).toPrecision(8))).toBe(0.36247037)
|
||||
})
|
||||
|
||||
test('Equation f(x) = sqrt(x) + e^(2*x) - 8*x = 0, has root x = 0.93945851 in [a, b] = [0.5, 100]', () => {
|
||||
const root = findRoot(0.5, 100, (x) => { return Math.exp(2 * x) + Math.sqrt(x) - 8 * x }, 32)
|
||||
const root = findRoot(
|
||||
0.5,
|
||||
100,
|
||||
(x) => {
|
||||
return Math.exp(2 * x) + Math.sqrt(x) - 8 * x
|
||||
},
|
||||
32
|
||||
)
|
||||
expect(Number(Number(root).toPrecision(8))).toBe(0.93945851)
|
||||
})
|
||||
|
||||
@@ -6,7 +6,12 @@ describe('Testing euclideanDistance calculations', () => {
|
||||
expect(euclideanDistance).toBe(15)
|
||||
})
|
||||
it('Should not give any output given non-numeric argument', () => {
|
||||
const euclideanDistance = coordinate.euclideanDistance('ABC', '123', '', '###')
|
||||
const euclideanDistance = coordinate.euclideanDistance(
|
||||
'ABC',
|
||||
'123',
|
||||
'',
|
||||
'###'
|
||||
)
|
||||
expect(euclideanDistance).toBeNaN()
|
||||
})
|
||||
it('Should not give any output given any number of numeric arguments less than 4', () => {
|
||||
@@ -27,7 +32,12 @@ describe('Testing manhattanDistance calculations', () => {
|
||||
expect(manhattanDistance).toBe(21)
|
||||
})
|
||||
it('Should not give any output given non-numeric argument', () => {
|
||||
const manhattanDistance = coordinate.manhattanDistance('ABC', '123', '', '###')
|
||||
const manhattanDistance = coordinate.manhattanDistance(
|
||||
'ABC',
|
||||
'123',
|
||||
'',
|
||||
'###'
|
||||
)
|
||||
expect(manhattanDistance).toBeNaN()
|
||||
})
|
||||
it('Should not give any output given any number of numeric arguments less than 4', () => {
|
||||
|
||||
@@ -7,14 +7,25 @@ describe('Count the numbers divisible', () => {
|
||||
[25, 100, 30, 3],
|
||||
[25, 70, 10, 5],
|
||||
[1, 23, 30, 0]
|
||||
])('Total number(s) divisible between %i to %i by %i is/are %i', (n1, n2, m, expected) => {
|
||||
expect(countNumbersDivisible(n1, n2, m)).toBe(expected)
|
||||
})
|
||||
])(
|
||||
'Total number(s) divisible between %i to %i by %i is/are %i',
|
||||
(n1, n2, m, expected) => {
|
||||
expect(countNumbersDivisible(n1, n2, m)).toBe(expected)
|
||||
}
|
||||
)
|
||||
|
||||
test.each([
|
||||
['test', 23, 10, 'Invalid input, please pass only numbers'],
|
||||
[44, 30, 10, 'Invalid number range, please provide numbers such that num1 < num2']
|
||||
])('Should throw an error for input %i, %i, %i, %i', (n1, n2, m, expected) => {
|
||||
expect(() => countNumbersDivisible(n1, n2, m)).toThrowError(expected)
|
||||
})
|
||||
[
|
||||
44,
|
||||
30,
|
||||
10,
|
||||
'Invalid number range, please provide numbers such that num1 < num2'
|
||||
]
|
||||
])(
|
||||
'Should throw an error for input %i, %i, %i, %i',
|
||||
(n1, n2, m, expected) => {
|
||||
expect(() => countNumbersDivisible(n1, n2, m)).toThrowError(expected)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { eulerFull } from '../EulerMethod'
|
||||
|
||||
function plotLine (label, points, width, height) {
|
||||
function plotLine(label, points, width, height) {
|
||||
// utility function to plot the results
|
||||
|
||||
// container needed to control the size of the canvas
|
||||
@@ -14,17 +14,20 @@ function plotLine (label, points, width, height) {
|
||||
container.append(canvas)
|
||||
|
||||
// Chart-class from chartjs
|
||||
const chart = new Chart(canvas, { // eslint-disable-line
|
||||
const chart = new Chart(canvas, {
|
||||
// eslint-disable-line
|
||||
type: 'scatter',
|
||||
data: {
|
||||
datasets: [{
|
||||
label,
|
||||
data: points,
|
||||
showLine: true,
|
||||
fill: false,
|
||||
tension: 0,
|
||||
borderColor: 'black'
|
||||
}]
|
||||
datasets: [
|
||||
{
|
||||
label,
|
||||
data: points,
|
||||
showLine: true,
|
||||
fill: false,
|
||||
tension: 0,
|
||||
borderColor: 'black'
|
||||
}
|
||||
]
|
||||
},
|
||||
options: {
|
||||
maintainAspectRatio: false,
|
||||
@@ -33,17 +36,17 @@ function plotLine (label, points, width, height) {
|
||||
})
|
||||
}
|
||||
|
||||
function exampleEquation1 (x, y) {
|
||||
function exampleEquation1(x, y) {
|
||||
return x
|
||||
}
|
||||
|
||||
// example from https://en.wikipedia.org/wiki/Euler_method
|
||||
function exampleEquation2 (x, y) {
|
||||
function exampleEquation2(x, y) {
|
||||
return y
|
||||
}
|
||||
|
||||
// example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/
|
||||
function exampleEquation3 (x, y) {
|
||||
function exampleEquation3(x, y) {
|
||||
return x + y + x * y
|
||||
}
|
||||
|
||||
|
||||
@@ -2,17 +2,40 @@ import { eulerFull, eulerStep } from '../EulerMethod'
|
||||
|
||||
describe('eulerStep', () => {
|
||||
it('should calculate the next y value correctly', () => {
|
||||
expect(eulerStep(0, 0.1, 0, function (x, y) { return x })).toBe(0)
|
||||
expect(eulerStep(2, 1, 1, function (x, y) { return x * x })).toBe(5)
|
||||
expect(
|
||||
eulerStep(0, 0.1, 0, function (x, y) {
|
||||
return x
|
||||
})
|
||||
).toBe(0)
|
||||
expect(
|
||||
eulerStep(2, 1, 1, function (x, y) {
|
||||
return x * x
|
||||
})
|
||||
).toBe(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('eulerFull', () => {
|
||||
it('should return all the points found', () => {
|
||||
expect(eulerFull(0, 3, 1, 0, function (x, y) { return x }))
|
||||
.toEqual([{ x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 1 }, { x: 3, y: 3 }])
|
||||
expect(
|
||||
eulerFull(0, 3, 1, 0, function (x, y) {
|
||||
return x
|
||||
})
|
||||
).toEqual([
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 1 },
|
||||
{ x: 3, y: 3 }
|
||||
])
|
||||
|
||||
expect(eulerFull(3, 4, 0.5, 1, function (x, y) { return x * x }))
|
||||
.toEqual([{ x: 3, y: 1 }, { x: 3.5, y: 5.5 }, { x: 4, y: 11.625 }])
|
||||
expect(
|
||||
eulerFull(3, 4, 0.5, 1, function (x, y) {
|
||||
return x * x
|
||||
})
|
||||
).toEqual([
|
||||
{ x: 3, y: 1 },
|
||||
{ x: 3.5, y: 5.5 },
|
||||
{ x: 4, y: 11.625 }
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,11 +6,19 @@ describe('extendedEuclideanGCD', () => {
|
||||
expect(extendedEuclideanGCD(46, 240)).toMatchObject([2, 47, -9])
|
||||
})
|
||||
it('should give error on non-positive arguments', () => {
|
||||
expect(() => extendedEuclideanGCD(0, 240)).toThrowError(new TypeError('Must be positive numbers'))
|
||||
expect(() => extendedEuclideanGCD(46, -240)).toThrowError(new TypeError('Must be positive numbers'))
|
||||
expect(() => extendedEuclideanGCD(0, 240)).toThrowError(
|
||||
new TypeError('Must be positive numbers')
|
||||
)
|
||||
expect(() => extendedEuclideanGCD(46, -240)).toThrowError(
|
||||
new TypeError('Must be positive numbers')
|
||||
)
|
||||
})
|
||||
it('should give error on non-numeric arguments', () => {
|
||||
expect(() => extendedEuclideanGCD('240', 46)).toThrowError(new TypeError('Not a Number'))
|
||||
expect(() => extendedEuclideanGCD([240, 46])).toThrowError(new TypeError('Not a Number'))
|
||||
expect(() => extendedEuclideanGCD('240', 46)).toThrowError(
|
||||
new TypeError('Not a Number')
|
||||
)
|
||||
expect(() => extendedEuclideanGCD([240, 46])).toThrowError(
|
||||
new TypeError('Not a Number')
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,12 +6,18 @@ describe('calcFactorial', () => {
|
||||
})
|
||||
|
||||
it('should throw error for "null" and "undefined"', () => {
|
||||
expect(() => { calcFactorial(null) }).toThrow(Error)
|
||||
expect(() => { calcFactorial(undefined) }).toThrow(Error)
|
||||
expect(() => {
|
||||
calcFactorial(null)
|
||||
}).toThrow(Error)
|
||||
expect(() => {
|
||||
calcFactorial(undefined)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error for negative numbers', () => {
|
||||
expect(() => { calcFactorial(-1) }).toThrow(Error)
|
||||
expect(() => {
|
||||
calcFactorial(-1)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should return the factorial of a positive number', () => {
|
||||
|
||||
@@ -100,7 +100,10 @@ describe('Fibonacci', () => {
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[15, 610]
|
||||
])('should calculate the correct Fibonacci number for n = %i', (n, expected) => {
|
||||
expect(FibonacciUsingFormula(n)).toBe(expected)
|
||||
})
|
||||
])(
|
||||
'should calculate the correct Fibonacci number for n = %i',
|
||||
(n, expected) => {
|
||||
expect(FibonacciUsingFormula(n)).toBe(expected)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
@@ -2,16 +2,24 @@ import { findLcm, findLcmWithHcf } from '../FindLcm'
|
||||
|
||||
describe('findLcm', () => {
|
||||
it('should throw a statement for values less than 1', () => {
|
||||
expect(() => { findLcm(0, 0) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcm(0, 0)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw a statement for one value less than 1', () => {
|
||||
expect(() => { findLcm(1, 0) }).toThrow(Error)
|
||||
expect(() => { findLcm(0, 1) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcm(1, 0)
|
||||
}).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcm(0, 1)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should return an error for values non-integer values', () => {
|
||||
expect(() => { findLcm(4.564, 7.39) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcm(4.564, 7.39)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should return the LCM of two given integers', () => {
|
||||
@@ -21,16 +29,24 @@ describe('findLcm', () => {
|
||||
|
||||
describe('findLcmWithHcf', () => {
|
||||
it('should throw a statement for values less than 1', () => {
|
||||
expect(() => { findLcmWithHcf(0, 0) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcmWithHcf(0, 0)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw a statement for one value less than 1', () => {
|
||||
expect(() => { findLcmWithHcf(1, 0) }).toThrow(Error)
|
||||
expect(() => { findLcmWithHcf(0, 1) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcmWithHcf(1, 0)
|
||||
}).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcmWithHcf(0, 1)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should return an error for values non-integer values', () => {
|
||||
expect(() => { findLcmWithHcf(4.564, 7.39) }).toThrow(Error)
|
||||
expect(() => {
|
||||
findLcmWithHcf(4.564, 7.39)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should return the LCM of two given integers', () => {
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
import { findMaxRecursion } from '../FindMaxRecursion'
|
||||
|
||||
describe('Test findMaxRecursion function', () => {
|
||||
const positiveAndNegativeArray = [1, 2, 4, 5, -1, -2, -4, -5]
|
||||
const positiveAndNegativeArray1 = [10, 40, 100, 20, -10, -40, -100, -20]
|
||||
|
||||
const positiveArray = [1, 2, 4, 5]
|
||||
const positiveArray1 = [10, 40, 100, 20]
|
||||
|
||||
const negativeArray = [-1, -2, -4, -5]
|
||||
const negativeArray1 = [-10, -40, -100, -20]
|
||||
|
||||
const zeroArray = [0, 0, 0, 0]
|
||||
const emptyArray = []
|
||||
|
||||
it('Testing with positive arrays', () => {
|
||||
expect(findMaxRecursion(positiveArray, 0, positiveArray.length - 1)).toBe(5)
|
||||
expect(findMaxRecursion(positiveArray1, 0, positiveArray1.length - 1)).toBe(
|
||||
100
|
||||
)
|
||||
})
|
||||
|
||||
it('Testing with negative arrays', () => {
|
||||
expect(findMaxRecursion(negativeArray, 0, negativeArray.length - 1)).toBe(
|
||||
-1
|
||||
)
|
||||
expect(findMaxRecursion(negativeArray1, 0, negativeArray1.length - 1)).toBe(
|
||||
-10
|
||||
)
|
||||
})
|
||||
|
||||
it('Testing with positive and negative arrays', () => {
|
||||
expect(
|
||||
findMaxRecursion(
|
||||
positiveAndNegativeArray,
|
||||
0,
|
||||
positiveAndNegativeArray.length - 1
|
||||
)
|
||||
).toBe(5)
|
||||
expect(
|
||||
findMaxRecursion(
|
||||
positiveAndNegativeArray1,
|
||||
0,
|
||||
positiveAndNegativeArray1.length - 1
|
||||
)
|
||||
).toBe(100)
|
||||
})
|
||||
|
||||
it('Testing with zero arrays', () => {
|
||||
expect(findMaxRecursion(zeroArray, 0, zeroArray.length - 1)).toBe(0)
|
||||
})
|
||||
|
||||
it('Testing with empty arrays', () => {
|
||||
expect(findMaxRecursion(emptyArray, 0, emptyArray.length - 1)).toBe(
|
||||
undefined
|
||||
)
|
||||
})
|
||||
})
|
||||
import { findMaxRecursion } from '../FindMaxRecursion'
|
||||
|
||||
describe('Test findMaxRecursion function', () => {
|
||||
const positiveAndNegativeArray = [1, 2, 4, 5, -1, -2, -4, -5]
|
||||
const positiveAndNegativeArray1 = [10, 40, 100, 20, -10, -40, -100, -20]
|
||||
|
||||
const positiveArray = [1, 2, 4, 5]
|
||||
const positiveArray1 = [10, 40, 100, 20]
|
||||
|
||||
const negativeArray = [-1, -2, -4, -5]
|
||||
const negativeArray1 = [-10, -40, -100, -20]
|
||||
|
||||
const zeroArray = [0, 0, 0, 0]
|
||||
const emptyArray = []
|
||||
|
||||
it('Testing with positive arrays', () => {
|
||||
expect(findMaxRecursion(positiveArray, 0, positiveArray.length - 1)).toBe(5)
|
||||
expect(findMaxRecursion(positiveArray1, 0, positiveArray1.length - 1)).toBe(
|
||||
100
|
||||
)
|
||||
})
|
||||
|
||||
it('Testing with negative arrays', () => {
|
||||
expect(findMaxRecursion(negativeArray, 0, negativeArray.length - 1)).toBe(
|
||||
-1
|
||||
)
|
||||
expect(findMaxRecursion(negativeArray1, 0, negativeArray1.length - 1)).toBe(
|
||||
-10
|
||||
)
|
||||
})
|
||||
|
||||
it('Testing with positive and negative arrays', () => {
|
||||
expect(
|
||||
findMaxRecursion(
|
||||
positiveAndNegativeArray,
|
||||
0,
|
||||
positiveAndNegativeArray.length - 1
|
||||
)
|
||||
).toBe(5)
|
||||
expect(
|
||||
findMaxRecursion(
|
||||
positiveAndNegativeArray1,
|
||||
0,
|
||||
positiveAndNegativeArray1.length - 1
|
||||
)
|
||||
).toBe(100)
|
||||
})
|
||||
|
||||
it('Testing with zero arrays', () => {
|
||||
expect(findMaxRecursion(zeroArray, 0, zeroArray.length - 1)).toBe(0)
|
||||
})
|
||||
|
||||
it('Testing with empty arrays', () => {
|
||||
expect(findMaxRecursion(emptyArray, 0, emptyArray.length - 1)).toBe(
|
||||
undefined
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -16,16 +16,19 @@ describe('FindMinIterator', () => {
|
||||
expect(FindMinIterator([-1, 10])).toBe(-1)
|
||||
expect(FindMinIterator([0, 100])).toBe(0)
|
||||
expect(FindMinIterator([100, 0])).toBe(0)
|
||||
expect(FindMinIterator([100, 50, 20, 0, -100, 0, 2, 30, 45, 99, 104, 23])).toBe(-100)
|
||||
expect(
|
||||
FindMinIterator([100, 50, 20, 0, -100, 0, 2, 30, 45, 99, 104, 23])
|
||||
).toBe(-100)
|
||||
})
|
||||
|
||||
test('given empty generator then min is undefined', () => {
|
||||
const src = function* () { } // eslint-disable-line
|
||||
const src = function* () {} // eslint-disable-line
|
||||
expect(FindMinIterator(src())).toBeUndefined()
|
||||
})
|
||||
|
||||
test('given generator then min is found', () => {
|
||||
const src = function* () { // eslint-disable-line
|
||||
const src = function* () {
|
||||
// eslint-disable-line
|
||||
yield 1
|
||||
yield -1
|
||||
yield 0
|
||||
@@ -34,12 +37,13 @@ describe('FindMinIterator', () => {
|
||||
})
|
||||
|
||||
test('given string generator then min string length is found', () => {
|
||||
const src = function* () { // eslint-disable-line
|
||||
const src = function* () {
|
||||
// eslint-disable-line
|
||||
yield 'abc'
|
||||
yield 'de'
|
||||
yield 'qwerty'
|
||||
}
|
||||
expect(FindMinIterator(src(), _x => _x.length)).toBe(2)
|
||||
expect(FindMinIterator(src(), (_x) => _x.length)).toBe(2)
|
||||
})
|
||||
|
||||
test('given array of objects then min accessor is found', () => {
|
||||
@@ -48,7 +52,7 @@ describe('FindMinIterator', () => {
|
||||
{ name: 'Item #2', price: 0.0 },
|
||||
{ name: 'Item #3', price: -1.0 }
|
||||
]
|
||||
expect(FindMinIterator(array, _x => _x.price)).toBe(-1)
|
||||
expect(FindMinIterator(array, _x => _x.name)).toBe('Item #1')
|
||||
expect(FindMinIterator(array, (_x) => _x.price)).toBe(-1)
|
||||
expect(FindMinIterator(array, (_x) => _x.name)).toBe('Item #1')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { GetEuclidGCD } from '../GetEuclidGCD'
|
||||
|
||||
function testEuclidGCD (n, m, expected) {
|
||||
function testEuclidGCD(n, m, expected) {
|
||||
test('Testing on ' + n + ' and ' + m + '!', () => {
|
||||
expect(GetEuclidGCD(n, m)).toBe(expected)
|
||||
})
|
||||
|
||||
@@ -1,19 +1,29 @@
|
||||
import { hexagonalNumber } from '../HexagonalNumber'
|
||||
|
||||
const expectedValuesArray = [1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378, 435, 496, 561, 630, 703, 780, 861, 946]
|
||||
const expectedValuesArray = [
|
||||
1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378, 435, 496, 561,
|
||||
630, 703, 780, 861, 946
|
||||
]
|
||||
|
||||
describe('Testing hexagonalNumber', () => {
|
||||
for (let i = 1; i <= 22; i++) {
|
||||
it('Testing for number = ' + i + ', should return ' + expectedValuesArray[i], () => {
|
||||
expect(hexagonalNumber(i)).toBe(expectedValuesArray[i - 1])
|
||||
})
|
||||
it(
|
||||
'Testing for number = ' + i + ', should return ' + expectedValuesArray[i],
|
||||
() => {
|
||||
expect(hexagonalNumber(i)).toBe(expectedValuesArray[i - 1])
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
it('should throw error when supplied negative numbers', () => {
|
||||
expect(() => { hexagonalNumber(-1) }).toThrow(Error)
|
||||
expect(() => {
|
||||
hexagonalNumber(-1)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error when supplied zero', () => {
|
||||
expect(() => { hexagonalNumber(0) }).toThrow(Error)
|
||||
expect(() => {
|
||||
hexagonalNumber(0)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -17,9 +17,12 @@ describe('isDivisible', () => {
|
||||
[5, -0, false]
|
||||
]
|
||||
|
||||
test.each(testCases)('if parameters are (%i, %i) it returns %p', (dividend, divisor, expected) => {
|
||||
expect(isDivisible(dividend, divisor)).toBe(expected)
|
||||
})
|
||||
test.each(testCases)(
|
||||
'if parameters are (%i, %i) it returns %p',
|
||||
(dividend, divisor, expected) => {
|
||||
expect(isDivisible(dividend, divisor)).toBe(expected)
|
||||
}
|
||||
)
|
||||
|
||||
const errorCases = [
|
||||
[NaN, NaN],
|
||||
@@ -31,9 +34,12 @@ describe('isDivisible', () => {
|
||||
[false, 2]
|
||||
]
|
||||
|
||||
test.each(errorCases)('throws an error if parameters are (%p, %p)', (dividend, divisor) => {
|
||||
expect(() => {
|
||||
isDivisible(dividend, divisor)
|
||||
}).toThrow()
|
||||
})
|
||||
test.each(errorCases)(
|
||||
'throws an error if parameters are (%p, %p)',
|
||||
(dividend, divisor) => {
|
||||
expect(() => {
|
||||
isDivisible(dividend, divisor)
|
||||
}).toThrow()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { isPronic } from '../IsPronic'
|
||||
|
||||
const pronicNumbers = [0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056, 1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070, 2162, 2256, 2352, 2450, 2550]
|
||||
const pronicNumbers = [
|
||||
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306,
|
||||
342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056,
|
||||
1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070,
|
||||
2162, 2256, 2352, 2450, 2550
|
||||
]
|
||||
|
||||
describe('Testing isPronic function', () => {
|
||||
for (let i = 0; i <= 2500; i++) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -3,7 +3,10 @@ import { PrimeCheck } from '../PrimeCheck'
|
||||
|
||||
describe('LinearSieve', () => {
|
||||
it('should return primes below 100', () => {
|
||||
expect(LinearSieve(100)).toEqual([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
|
||||
expect(LinearSieve(100)).toEqual([
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
|
||||
71, 73, 79, 83, 89, 97
|
||||
])
|
||||
})
|
||||
|
||||
it('should return primes only', () => {
|
||||
|
||||
@@ -1,19 +1,32 @@
|
||||
import { liouvilleFunction } from '../LiouvilleFunction'
|
||||
|
||||
const expectedValuesArray = [1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1]
|
||||
const expectedValuesArray = [
|
||||
1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, 1, 1,
|
||||
-1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, -1, -1, -1,
|
||||
-1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1,
|
||||
-1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1,
|
||||
-1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1
|
||||
]
|
||||
|
||||
describe('Testing liouville function', () => {
|
||||
for (let i = 1; i <= 100; i++) {
|
||||
it('Testing for number = ' + i + ', should return ' + expectedValuesArray[i], () => {
|
||||
expect(liouvilleFunction(i)).toBe(expectedValuesArray[i - 1])
|
||||
})
|
||||
it(
|
||||
'Testing for number = ' + i + ', should return ' + expectedValuesArray[i],
|
||||
() => {
|
||||
expect(liouvilleFunction(i)).toBe(expectedValuesArray[i - 1])
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
it('should throw error when supplied negative numbers', () => {
|
||||
expect(() => { liouvilleFunction(-1) }).toThrow(Error)
|
||||
expect(() => {
|
||||
liouvilleFunction(-1)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error when supplied zero', () => {
|
||||
expect(() => { liouvilleFunction(0) }).toThrow(Error)
|
||||
expect(() => {
|
||||
liouvilleFunction(0)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
import { integralEvaluation } from '../MidpointIntegration'
|
||||
|
||||
test('Should return the integral of f(x) = sqrt(x) in [1, 3] to be equal 2.797434', () => {
|
||||
const result = integralEvaluation(10000, 1, 3, (x) => { return Math.sqrt(x) })
|
||||
const result = integralEvaluation(10000, 1, 3, (x) => {
|
||||
return Math.sqrt(x)
|
||||
})
|
||||
expect(Number(result.toPrecision(6))).toBe(2.79743)
|
||||
})
|
||||
|
||||
test('Should return the integral of f(x) = sqrt(x) + x^2 in [1, 3] to be equal 11.46410161', () => {
|
||||
const result = integralEvaluation(10000, 1, 3, (x) => { return Math.sqrt(x) + Math.pow(x, 2) })
|
||||
const result = integralEvaluation(10000, 1, 3, (x) => {
|
||||
return Math.sqrt(x) + Math.pow(x, 2)
|
||||
})
|
||||
expect(Number(result.toPrecision(10))).toBe(11.46410161)
|
||||
})
|
||||
|
||||
test('Should return the integral of f(x) = log(x) + Pi*x^3 in [5, 12] to be equal 15809.9141543', () => {
|
||||
const result = integralEvaluation(20000, 5, 12, (x) => { return Math.log(x) + Math.PI * Math.pow(x, 3) })
|
||||
const result = integralEvaluation(20000, 5, 12, (x) => {
|
||||
return Math.log(x) + Math.PI * Math.pow(x, 3)
|
||||
})
|
||||
expect(Number(result.toPrecision(10))).toBe(15809.91415)
|
||||
})
|
||||
|
||||
@@ -1,19 +1,32 @@
|
||||
import { mobiusFunction } from '../MobiusFunction'
|
||||
|
||||
const expectedValuesArray = [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 0, -1, 0, 1, 0, 1, 1, 1, 0, -1, 0, 0, 0]
|
||||
const expectedValuesArray = [
|
||||
1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1,
|
||||
0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1,
|
||||
-1, 0, 0, 0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1,
|
||||
-1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 0, -1, 0, 1, 0, 1, 1,
|
||||
1, 0, -1, 0, 0, 0
|
||||
]
|
||||
|
||||
describe('Testing mobius function', () => {
|
||||
for (let i = 1; i <= 100; i++) {
|
||||
it('Testing for number = ' + i + ', should return ' + expectedValuesArray[i], () => {
|
||||
expect(mobiusFunction(i)).toBe(expectedValuesArray[i - 1])
|
||||
})
|
||||
it(
|
||||
'Testing for number = ' + i + ', should return ' + expectedValuesArray[i],
|
||||
() => {
|
||||
expect(mobiusFunction(i)).toBe(expectedValuesArray[i - 1])
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
it('should throw error when supplied negative numbers', () => {
|
||||
expect(() => { mobiusFunction(-1) }).toThrow(Error)
|
||||
expect(() => {
|
||||
mobiusFunction(-1)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
|
||||
it('should throw error when supplied zero', () => {
|
||||
expect(() => { mobiusFunction(0) }).toThrow(Error)
|
||||
expect(() => {
|
||||
mobiusFunction(0)
|
||||
}).toThrow(Error)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,7 +14,10 @@ describe('NumberOfDigits', () => {
|
||||
[123423232, 9],
|
||||
[-123423232, 9],
|
||||
[9999, 4]
|
||||
])('should return the correct number of digits in an integer', (value, expected) => {
|
||||
expect(numberOfDigitsUsingLog(value)).toBe(expected)
|
||||
})
|
||||
])(
|
||||
'should return the correct number of digits in an integer',
|
||||
(value, expected) => {
|
||||
expect(numberOfDigitsUsingLog(value)).toBe(expected)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { PalindromeRecursive, PalindromeIterative, checkPalindrome } from '../Palindrome'
|
||||
import {
|
||||
PalindromeRecursive,
|
||||
PalindromeIterative,
|
||||
checkPalindrome
|
||||
} from '../Palindrome'
|
||||
|
||||
describe('Palindrome', () => {
|
||||
it('should return true for a palindrome for PalindromeRecursive', () => {
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { factorial, permutation, combination } from '../PermutationAndCombination'
|
||||
import {
|
||||
factorial,
|
||||
permutation,
|
||||
combination
|
||||
} from '../PermutationAndCombination'
|
||||
|
||||
describe('Factorial', () => {
|
||||
it('factorial(5)', () => {
|
||||
|
||||
@@ -5,7 +5,7 @@ describe('should return an array of prime numbers', () => {
|
||||
it('should have each element in the array as a prime numbers', () => {
|
||||
const n = 100
|
||||
const primes = sieveOfEratosthenes(n)
|
||||
primes.forEach(prime => {
|
||||
primes.forEach((prime) => {
|
||||
expect(PrimeCheck(prime)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
import { integralEvaluation } from '../SimpsonIntegration'
|
||||
|
||||
test('Should return the integral of f(x) = sqrt(x) in [1, 3] to be equal 2.797434', () => {
|
||||
const result = integralEvaluation(16, 1, 3, (x) => { return Math.sqrt(x) })
|
||||
const result = integralEvaluation(16, 1, 3, (x) => {
|
||||
return Math.sqrt(x)
|
||||
})
|
||||
expect(Number(result.toPrecision(7))).toBe(2.797434)
|
||||
})
|
||||
|
||||
test('Should return the integral of f(x) = sqrt(x) + x^2 in [1, 3] to be equal 11.46410161', () => {
|
||||
const result = integralEvaluation(64, 1, 3, (x) => { return Math.sqrt(x) + Math.pow(x, 2) })
|
||||
const result = integralEvaluation(64, 1, 3, (x) => {
|
||||
return Math.sqrt(x) + Math.pow(x, 2)
|
||||
})
|
||||
expect(Number(result.toPrecision(10))).toBe(11.46410161)
|
||||
})
|
||||
|
||||
test('Should return the integral of f(x) = log(x) + Pi*x^3 in [5, 12] to be equal 15809.9141543', () => {
|
||||
const result = integralEvaluation(128, 5, 12, (x) => { return Math.log(x) + Math.PI * Math.pow(x, 3) })
|
||||
const result = integralEvaluation(128, 5, 12, (x) => {
|
||||
return Math.log(x) + Math.PI * Math.pow(x, 3)
|
||||
})
|
||||
expect(Number(result.toPrecision(12))).toBe(15809.9141543)
|
||||
})
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { sumOfDigitsUsingLoop, sumOfDigitsUsingRecursion, sumOfDigitsUsingString } from '../SumOfDigits'
|
||||
import {
|
||||
sumOfDigitsUsingLoop,
|
||||
sumOfDigitsUsingRecursion,
|
||||
sumOfDigitsUsingString
|
||||
} from '../SumOfDigits'
|
||||
|
||||
test('Testing on sumOfDigitsUsingLoop', () => {
|
||||
const sum = sumOfDigitsUsingLoop(123)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { factorialize } from '../WhileLoopFactorial'
|
||||
|
||||
function testFactorial (n, expected) {
|
||||
function testFactorial(n, expected) {
|
||||
test('Testing on ' + n + '!', () => {
|
||||
expect(factorialize(n)).toBe(expected)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { zellersCongruenceAlgorithm } from '../ZellersCongruenceAlgorithm'
|
||||
|
||||
function testZeller (day, month, year, expected) {
|
||||
function testZeller(day, month, year, expected) {
|
||||
test('Testing on ' + day + '/' + month + '/' + year, () => {
|
||||
expect(zellersCongruenceAlgorithm(day, month, year)).toBe(expected)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user