Skip to content

Commit 5874f55

Browse files
committed
add mirr test
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1922096 13f79535-47bb-0310-9956-ffa450edef68
1 parent edb9aea commit 5874f55

File tree

2 files changed

+38
-3
lines changed
  • poi/src
    • main/java/org/apache/poi/ss/formula/functions
    • test/java/org/apache/poi/ss/formula/functions

2 files changed

+38
-3
lines changed

poi/src/main/java/org/apache/poi/ss/formula/functions/Mirr.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ protected int getMaxNumOperands() {
5656
@Override
5757
protected double evaluate(double[] values) throws EvaluationException {
5858

59-
double financeRate = values[values.length-2];
60-
double reinvestRate = values[values.length-1];
59+
final double financeRate = values[values.length-2];
60+
final double reinvestRate = values[values.length-1];
6161

62-
double[] mirrValues = Arrays.copyOf(values, values.length - 2);
62+
final double[] mirrValues = Arrays.copyOf(values, values.length - 2);
6363

6464
boolean mirrValuesAreAllNegatives = true;
6565
boolean mirrValuesAreAllPositives = true;

poi/src/test/java/org/apache/poi/ss/formula/functions/TestMirr.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,41 @@ void testEvaluateInSheet() {
112112
assertEquals(0.18736225093, res, 0.00000001);
113113
}
114114

115+
@Test
116+
void testMicrosoftSample() {
117+
// https://support.microsoft.com/en-us/office/mirr-function-b020f038-7492-4fb4-93c1-35c345b53524
118+
HSSFWorkbook wb = new HSSFWorkbook();
119+
HSSFSheet sheet = wb.createSheet("Sheet1");
120+
121+
int row = 0;
122+
sheet.createRow(row++).createCell(0).setCellValue("Data");
123+
sheet.createRow(row++).createCell(0).setCellValue(-120000);
124+
sheet.createRow(row++).createCell(0).setCellValue(39000);
125+
sheet.createRow(row++).createCell(0).setCellValue(30000);
126+
sheet.createRow(row++).createCell(0).setCellValue(21000);
127+
sheet.createRow(row++).createCell(0).setCellValue(37000);
128+
sheet.createRow(row++).createCell(0).setCellValue(46000);
129+
sheet.createRow(row++).createCell(0).setCellValue(0.1);
130+
sheet.createRow(row++).createCell(0).setCellValue(0.12);
131+
132+
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
133+
HSSFCell cell = sheet.createRow(row).createCell(0);
134+
cell.setCellFormula("MIRR(A2:A7, A8, A9)");
135+
fe.clearAllCachedResultValues();
136+
fe.evaluateFormulaCell(cell);
137+
assertEquals(0.126094, cell.getNumericCellValue(), 0.00000015);
138+
139+
cell.setCellFormula("MIRR(A2:A5, A8, A9)");
140+
fe.clearAllCachedResultValues();
141+
fe.evaluateFormulaCell(cell);
142+
assertEquals(-0.048044655, cell.getNumericCellValue(), 0.00000015);
143+
144+
cell.setCellFormula("MIRR(A2:A7, A8, .14)");
145+
fe.clearAllCachedResultValues();
146+
fe.evaluateFormulaCell(cell);
147+
assertEquals(0.134759111, cell.getNumericCellValue(), 0.00000015);
148+
}
149+
115150
@Test
116151
void testMirrFromSpreadsheet() {
117152
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("mirrTest.xls");

0 commit comments

Comments
 (0)