From 8ed95f7970c584668536fd9132e4ac61ef19fef8 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 10 Oct 2021 13:09:09 +0000 Subject: [PATCH] add test case for IRR function git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894109 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/TestIrr.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIrr.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIrr.java index 6764ebdc2b..289fc826c9 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIrr.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIrr.java @@ -17,6 +17,8 @@ package org.apache.poi.ss.formula.functions; +import static org.apache.poi.ss.util.Utils.addRow; +import static org.apache.poi.ss.util.Utils.assertDouble; import static org.junit.jupiter.api.Assertions.assertEquals; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -29,6 +31,8 @@ import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellValue; import org.junit.jupiter.api.Test; +import java.io.IOException; + /** * Tests for {@link Irr} */ @@ -119,6 +123,26 @@ final class TestIrr { assertEquals(0, failures.length(), "IRR assertions failed"); } + @Test + void testMicrosoftExample() throws IOException { + https://support.microsoft.com/en-us/office/irr-function-64925eaa-9988-495b-b290-3ad0c163c1bc + try (HSSFWorkbook wb = new HSSFWorkbook()) { + HSSFSheet sheet = wb.createSheet(); + addRow(sheet, 0, "Data", "Description"); + addRow(sheet, 1, -70000, "Initial cost of a business"); + addRow(sheet, 2, 12000, "Net income for the first year"); + addRow(sheet, 3, 15000, "Net income for the second year"); + addRow(sheet, 4, 18000, "Net income for the third year"); + addRow(sheet, 5, 21000, "Net income for the fourth year"); + addRow(sheet, 6, 26000, "Net income for the fifth year"); + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100); + assertDouble(fe, cell, "IRR(A2:A6)", -0.02124484827341093); + assertDouble(fe, cell, "IRR(A2:A7)", 0.08663094803653162); + assertDouble(fe, cell, "IRR(A2:A4,-0.1)", -0.44350694133474067); + } + } + private static void assertFormulaResult(CellValue cv, HSSFCell cell){ double actualValue = cv.getNumberValue(); double expectedValue = cell.getNumericCellValue(); // cached formula result calculated by Excel -- 2.39.5