Skip to content

Commit 5f4eadc

Browse files
committed
Add tests to cover uncovered lines in irrCore
- Add test for derivative too small condition (line 869) Uses cash flows {-0.5, 2, -1} with guess=0 where dnpv=0 but npv!=0 - Add test for max iterations reached (line 888) Uses cash flows {-1, 2, -1.0001} with guess=0.9 causing oscillation
1 parent cec439c commit 5f4eadc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

test/unit/interpreter/function-irr.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ describe('Function IRR', () => {
5656
expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.NUM))
5757
})
5858

59+
it('should return #NUM! when derivative is too small', () => {
60+
// At r=0 with cash flows {-0.5, 2, -1}:
61+
// dnpv = -1*2/(1+0)^2 - 2*(-1)/(1+0)^3 = -2 + 2 = 0
62+
// npv = -0.5 + 2 - 1 = 0.5 != 0
63+
// So derivative is zero but NPV is not, Newton-Raphson cannot proceed
64+
const engine = HyperFormula.buildFromArray([
65+
['=IRR({-0.5, 2, -1}, 0)'],
66+
])
67+
68+
expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NUM))
69+
})
70+
71+
it('should return #NUM! when max iterations reached', () => {
72+
// Cash flows that cause oscillation without convergence
73+
const engine = HyperFormula.buildFromArray([
74+
['=IRR({-1, 2, -1.0001}, 0.9)'],
75+
])
76+
77+
expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NUM))
78+
})
79+
5980
it('should return #NUM! when values do not contain at least one positive and one negative value', () => {
6081
const engine = HyperFormula.buildFromArray([
6182
['=IRR({1, 2, 3})'],

0 commit comments

Comments
 (0)