|
| 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