Skip to content

Commit 813c215

Browse files
Added the test cases for bisection method euler method and binary to decimal conversion
1 parent 9b477f1 commit 813c215

6 files changed

+144
-5
lines changed

maths/bisection_method.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @function bisectionMethod
32
* @description Bisection method is a root-finding method that applies to any continuous function for which one knows two values with opposite signs.
43
* @param {number} a - The first value
54
* @param {number} b - The second value
@@ -12,6 +11,15 @@
1211
*/
1312

1413
export const bisectionMethod = (a: number, b: number, e: number, f: Function): number => {
14+
15+
if (e <= 0) {
16+
throw new Error('Error threshold must be positive')
17+
}
18+
19+
if (f(a) * f(b) >= 0) {
20+
throw new Error('f(a) and f(b) should have opposite signs')
21+
}
22+
1523
let c = a
1624
while ((b - a) >= e) {
1725
c = (a + b) / 2

maths/decimal_convert.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
2-
* @function decimalConvert
32
* @description Convert the binary to decimal.
43
* @param {string} binary - The input binary
54
* @return {number} - Decimal of binary.
65
* @see [DecimalConvert](https://www.programiz.com/javascript/examples/binary-decimal)
7-
* @example decimalConvert(1100) = 12
8-
* @example decimalConvert(1110) = 14
6+
* @example decimalConvert("1100") = 12
7+
* @example decimalConvert("1110") = 14
98
*/
109

1110
export const decimalConvert = (binary: string): number => {

maths/euler_method.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @function eulerMethod
32
* @description Euler's method is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value.
43
* @param {number} x0 - The initial value of x
54
* @param {number} y0 - The initial value of y
@@ -13,6 +12,15 @@
1312
*/
1413

1514
export const eulerMethod = (x0: number, y0: number, h: number, n: number, f: Function): number => {
15+
16+
if (typeof f !== 'function') {
17+
throw new Error('f must be a function')
18+
}
19+
20+
if (n < 0) {
21+
throw new Error('Number of iterations must be non-negative')
22+
}
23+
1624
let x = x0
1725
let y = y0
1826

maths/test/bisection_method.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { bisectionMethod } from "../bisection_method";
2+
3+
describe('bisectionMethod', () => {
4+
5+
it('should find the root of f(x) = x^2 - 3 between [1, 2]', () => {
6+
const result = bisectionMethod(1, 2, 0.01, (x: number) => x ** 2 - 3);
7+
expect(result).toBeCloseTo(1.732, 2);
8+
});
9+
10+
it('should find the root of f(x) = x^3 - x - 2 between [1, 2]', () => {
11+
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 3 - x - 2);
12+
expect(result).toBeCloseTo(1.521, 3);
13+
});
14+
15+
it('should find the root of f(x) = x^2 + x - 6 between [1, 3]', () => {
16+
const result = bisectionMethod(1, 3, 0.01, (x: number) => x ** 2 + x - 6);
17+
expect(result).toBeCloseTo(1.816, 2);
18+
});
19+
20+
it('should find the root of f(x) = cos(x) - x between [0, 1]', () => {
21+
const result = bisectionMethod(0, 1, 0.001, (x: number) => Math.cos(x) - x);
22+
expect(result).toBeCloseTo(0.739, 3);
23+
});
24+
25+
it('should find the root of f(x) = e^x - 3 between [0, 2]', () => {
26+
const result = bisectionMethod(0, 2, 0.001, (x: number) => Math.exp(x) - 3);
27+
expect(result).toBeCloseTo(1.099, 3);
28+
});
29+
30+
it('should throw an error when f(a) and f(b) have the same sign', () => {
31+
expect(() => bisectionMethod(1, 2, 0.01, (x: number) => x ** 2 + 1)).toThrow(
32+
'f(a) and f(b) should have opposite signs'
33+
);
34+
});
35+
36+
it('should throw an error when error threshold is non-positive', () => {
37+
expect(() => bisectionMethod(1, 2, -0.01, (x: number) => x ** 2 - 2)).toThrow(
38+
'Error threshold must be positive'
39+
);
40+
});
41+
});

maths/test/decimal_convert.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { decimalConvert } from "../decimal_convert";
2+
3+
describe('decimalConvert', () => {
4+
it('should convert "1100" to 12', () => {
5+
expect(decimalConvert("1100")).toBe(12);
6+
});
7+
8+
it('should convert "1110" to 14', () => {
9+
expect(decimalConvert("1110")).toBe(14);
10+
});
11+
12+
it('should convert "0" to 0', () => {
13+
expect(decimalConvert("0")).toBe(0);
14+
});
15+
16+
it('should convert "1" to 1', () => {
17+
expect(decimalConvert("1")).toBe(1);
18+
});
19+
20+
it('should convert "101" to 5', () => {
21+
expect(decimalConvert("101")).toBe(5);
22+
});
23+
24+
it('should handle an empty string by returning 0', () => {
25+
expect(decimalConvert("")).toBe(0);
26+
});
27+
28+
it('should convert a binary string with leading zeros "0001" to 1', () => {
29+
expect(decimalConvert("0001")).toBe(1);
30+
});
31+
32+
it('should throw an error when the input is not a valid binary string', () => {
33+
expect(() => decimalConvert("102")).toThrow('Invalid binary input');
34+
});
35+
36+
it('should throw an error when the input contains non-numeric characters', () => {
37+
expect(() => decimalConvert("abc")).toThrow('Invalid binary input');
38+
});
39+
40+
});

maths/test/euler_method.test.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { eulerMethod } from './eulerMethod';
2+
3+
describe('eulerMethod', () => {
4+
it('should compute y for a linear function (x + y)', () => {
5+
const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x + y);
6+
expect(result).toBeCloseTo(2.5937424601, 5);
7+
});
8+
9+
it('should compute y for a multiplicative function (x * y)', () => {
10+
const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x * y);
11+
expect(result).toBeCloseTo(1.7715614317, 5);
12+
});
13+
14+
it('should return the initial value y0 when there are zero iterations', () => {
15+
const result = eulerMethod(0, 1, 0.1, 0, (x, y) => x + y);
16+
expect(result).toBe(1);
17+
});
18+
19+
it('should return the correct value for a very small step size', () => {
20+
const result = eulerMethod(0, 1, 0.01, 100, (x, y) => x + y);
21+
expect(result).toBeCloseTo(2.7048138294, 5);
22+
});
23+
24+
it('should return the correct value after one iteration', () => {
25+
const result = eulerMethod(0, 1, 0.1, 1, (x, y) => x + y);
26+
expect(result).toBeCloseTo(1.1, 5);
27+
});
28+
29+
it('should return the initial value y0 when step size is zero', () => {
30+
const result = eulerMethod(0, 1, 0, 10, (x, y) => x + y);
31+
expect(result).toBe(1);
32+
});
33+
34+
it('should return correct value for negative step size', () => {
35+
const result = eulerMethod(1, 1, -0.1, 10, (x, y) => x + y);
36+
expect(result).toBeCloseTo(0.3162798676, 5);
37+
});
38+
39+
it('should throw an error when number of iterations is negative', () => {
40+
expect(() => eulerMethod(0, 1, 0.1, -5, (x, y) => x + y)).toThrow('Number of iterations must be non-negative');
41+
});
42+
43+
});

0 commit comments

Comments
 (0)