-
-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the test cases and revised the cases
- Loading branch information
1 parent
813c215
commit cf97cac
Showing
6 changed files
with
146 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,28 @@ | ||
import { bisectionMethod } from "../bisection_method"; | ||
import { bisectionMethod } from '../bisection_method' | ||
|
||
describe('bisectionMethod', () => { | ||
|
||
it('should find the root of f(x) = x^2 - 3 between [1, 2]', () => { | ||
const result = bisectionMethod(1, 2, 0.01, (x: number) => x ** 2 - 3); | ||
expect(result).toBeCloseTo(1.732, 2); | ||
}); | ||
|
||
it('should find the root of f(x) = x^3 - x - 2 between [1, 2]', () => { | ||
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 3 - x - 2); | ||
expect(result).toBeCloseTo(1.521, 3); | ||
}); | ||
|
||
it('should find the root of f(x) = x^2 + x - 6 between [1, 3]', () => { | ||
const result = bisectionMethod(1, 3, 0.01, (x: number) => x ** 2 + x - 6); | ||
expect(result).toBeCloseTo(1.816, 2); | ||
}); | ||
|
||
it('should find the root of f(x) = cos(x) - x between [0, 1]', () => { | ||
const result = bisectionMethod(0, 1, 0.001, (x: number) => Math.cos(x) - x); | ||
expect(result).toBeCloseTo(0.739, 3); | ||
}); | ||
|
||
it('should find the root of f(x) = e^x - 3 between [0, 2]', () => { | ||
const result = bisectionMethod(0, 2, 0.001, (x: number) => Math.exp(x) - 3); | ||
expect(result).toBeCloseTo(1.099, 3); | ||
}); | ||
|
||
it('should throw an error when f(a) and f(b) have the same sign', () => { | ||
expect(() => bisectionMethod(1, 2, 0.01, (x: number) => x ** 2 + 1)).toThrow( | ||
'f(a) and f(b) should have opposite signs' | ||
); | ||
}); | ||
|
||
it('should throw an error when error threshold is non-positive', () => { | ||
expect(() => bisectionMethod(1, 2, -0.01, (x: number) => x ** 2 - 2)).toThrow( | ||
'Error threshold must be positive' | ||
); | ||
}); | ||
}); | ||
it('should find the root of f(x) = x^2 - 3 between [1, 2]', () => { | ||
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 2 - 3) | ||
expect(result).toBeCloseTo(1.732, 3) | ||
}) | ||
|
||
it('should find the root of f(x) = x^3 - x - 2 between [1, 2]', () => { | ||
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 3 - x - 2) | ||
expect(result).toBeCloseTo(1.521, 3) | ||
}) | ||
|
||
it('should find the root of f(x) = x^2 + x - 6 between [1, 3]', () => { | ||
const result = bisectionMethod(1, 3, 0.001, (x: number) => x ** 2 + x - 6) | ||
expect(result).toBeCloseTo(2, 3) | ||
}) | ||
|
||
it('should find the root of f(x) = cos(x) - x between [0, 1]', () => { | ||
const result = bisectionMethod(0, 1, 0.001, (x: number) => Math.cos(x) - x) | ||
expect(result).toBeCloseTo(0.739, 2) | ||
}) | ||
|
||
it('should find the root of f(x) = e^x - 3 between [0, 2]', () => { | ||
const result = bisectionMethod(0, 2, 0.001, (x: number) => Math.exp(x) - 3) | ||
expect(result).toBeCloseTo(1.099, 2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,31 @@ | ||
import { decimalConvert } from "../decimal_convert"; | ||
import { decimalConvert } from '../decimal_convert' | ||
|
||
describe('decimalConvert', () => { | ||
it('should convert "1100" to 12', () => { | ||
expect(decimalConvert("1100")).toBe(12); | ||
}); | ||
|
||
it('should convert "1110" to 14', () => { | ||
expect(decimalConvert("1110")).toBe(14); | ||
}); | ||
|
||
it('should convert "0" to 0', () => { | ||
expect(decimalConvert("0")).toBe(0); | ||
}); | ||
|
||
it('should convert "1" to 1', () => { | ||
expect(decimalConvert("1")).toBe(1); | ||
}); | ||
|
||
it('should convert "101" to 5', () => { | ||
expect(decimalConvert("101")).toBe(5); | ||
}); | ||
|
||
it('should handle an empty string by returning 0', () => { | ||
expect(decimalConvert("")).toBe(0); | ||
}); | ||
|
||
it('should convert a binary string with leading zeros "0001" to 1', () => { | ||
expect(decimalConvert("0001")).toBe(1); | ||
}); | ||
|
||
it('should throw an error when the input is not a valid binary string', () => { | ||
expect(() => decimalConvert("102")).toThrow('Invalid binary input'); | ||
}); | ||
|
||
it('should throw an error when the input contains non-numeric characters', () => { | ||
expect(() => decimalConvert("abc")).toThrow('Invalid binary input'); | ||
}); | ||
|
||
}); | ||
it('should convert "1100" to 12', () => { | ||
expect(decimalConvert('1100')).toBe(12) | ||
}) | ||
|
||
it('should convert "1110" to 14', () => { | ||
expect(decimalConvert('1110')).toBe(14) | ||
}) | ||
|
||
it('should convert "0" to 0', () => { | ||
expect(decimalConvert('0')).toBe(0) | ||
}) | ||
|
||
it('should convert "1" to 1', () => { | ||
expect(decimalConvert('1')).toBe(1) | ||
}) | ||
|
||
it('should convert "101" to 5', () => { | ||
expect(decimalConvert('101')).toBe(5) | ||
}) | ||
|
||
it('should handle an empty string by returning 0', () => { | ||
expect(decimalConvert('')).toBe(0) | ||
}) | ||
|
||
it('should convert a binary string with leading zeros "0001" to 1', () => { | ||
expect(decimalConvert('0001')).toBe(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,42 @@ | ||
import { eulerMethod } from './eulerMethod'; | ||
import { eulerMethod } from '../euler_method' | ||
|
||
describe('eulerMethod', () => { | ||
it('should compute y for a linear function (x + y)', () => { | ||
const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x + y); | ||
expect(result).toBeCloseTo(2.5937424601, 5); | ||
}); | ||
|
||
it('should compute y for a multiplicative function (x * y)', () => { | ||
const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x * y); | ||
expect(result).toBeCloseTo(1.7715614317, 5); | ||
}); | ||
|
||
it('should return the initial value y0 when there are zero iterations', () => { | ||
const result = eulerMethod(0, 1, 0.1, 0, (x, y) => x + y); | ||
expect(result).toBe(1); | ||
}); | ||
|
||
it('should return the correct value for a very small step size', () => { | ||
const result = eulerMethod(0, 1, 0.01, 100, (x, y) => x + y); | ||
expect(result).toBeCloseTo(2.7048138294, 5); | ||
}); | ||
|
||
it('should return the correct value after one iteration', () => { | ||
const result = eulerMethod(0, 1, 0.1, 1, (x, y) => x + y); | ||
expect(result).toBeCloseTo(1.1, 5); | ||
}); | ||
|
||
it('should return the initial value y0 when step size is zero', () => { | ||
const result = eulerMethod(0, 1, 0, 10, (x, y) => x + y); | ||
expect(result).toBe(1); | ||
}); | ||
|
||
it('should return correct value for negative step size', () => { | ||
const result = eulerMethod(1, 1, -0.1, 10, (x, y) => x + y); | ||
expect(result).toBeCloseTo(0.3162798676, 5); | ||
}); | ||
|
||
it('should throw an error when number of iterations is negative', () => { | ||
expect(() => eulerMethod(0, 1, 0.1, -5, (x, y) => x + y)).toThrow('Number of iterations must be non-negative'); | ||
}); | ||
|
||
}); | ||
it('should compute y for dy/dx = y with y(0) = 1 at x = 1', () => { | ||
const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => y) | ||
expect(result).toBeCloseTo(2.59374, 5) | ||
}) | ||
|
||
it('should compute y for dy/dx = -2y with y(0) = 1 at x = 1', () => { | ||
const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => -2 * y) | ||
const expectedResult = 1 * Math.pow(0.8, 10) | ||
expect(result).toBeCloseTo(expectedResult, 5) | ||
}) | ||
|
||
it('should compute y for dy/dx = x with y(0) = 0 at x = 1', () => { | ||
const result = eulerMethod(0, 0, 0.1, 10, (x: number, y: number) => x) | ||
expect(result).toBeCloseTo(0.45, 2) | ||
}) | ||
|
||
it('should compute y for dy/dx = x + y with y(0) = 1 at x = 0.5', () => { | ||
const h = 0.1 | ||
const n = 5 | ||
const result = eulerMethod(0, 1, h, n, (x: number, y: number) => x + y) | ||
expect(result).toBeCloseTo(1.72102, 5) | ||
}) | ||
|
||
it('should compute y for dy/dx = x^2 with y(0) = 0 at x = 1', () => { | ||
const result = eulerMethod(0, 0, 0.2, 5, (x: number, y: number) => x ** 2) | ||
expect(result).toBeCloseTo(0.24, 3) | ||
}) | ||
|
||
it('should handle negative step size for dy/dx = y with y(1) = e', () => { | ||
const result = eulerMethod( | ||
1, | ||
Math.E, | ||
-0.001, | ||
1000, | ||
(x: number, y: number) => y | ||
) | ||
expect(result).toBeCloseTo(1, 2) | ||
}) | ||
}) |