You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestIrr.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.functions;
  16. import junit.framework.TestCase;
  17. import junit.framework.AssertionFailedError;
  18. import org.apache.poi.hssf.usermodel.*;
  19. import org.apache.poi.hssf.HSSFTestDataSamples;
  20. import org.apache.poi.ss.usermodel.CellValue;
  21. /**
  22. * Tests for {@link Irr}
  23. *
  24. * @author Marcel May
  25. */
  26. public final class TestIrr extends TestCase {
  27. public void testIrr() {
  28. // http://en.wikipedia.org/wiki/Internal_rate_of_return#Example
  29. double[] incomes = {-4000d, 1200d, 1410d, 1875d, 1050d};
  30. double irr = Irr.irr(incomes);
  31. double irrRounded = Math.round(irr * 1000d) / 1000d;
  32. assertEquals("irr", 0.143d, irrRounded);
  33. // http://www.techonthenet.com/excel/formulas/irr.php
  34. incomes = new double[]{-7500d, 3000d, 5000d, 1200d, 4000d};
  35. irr = Irr.irr(incomes);
  36. irrRounded = Math.round(irr * 100d) / 100d;
  37. assertEquals("irr", 0.28d, irrRounded);
  38. incomes = new double[]{-10000d, 3400d, 6500d, 1000d};
  39. irr = Irr.irr(incomes);
  40. irrRounded = Math.round(irr * 100d) / 100d;
  41. assertEquals("irr", 0.05, irrRounded);
  42. incomes = new double[]{100d, -10d, -110d};
  43. irr = Irr.irr(incomes);
  44. irrRounded = Math.round(irr * 100d) / 100d;
  45. assertEquals("irr", 0.1, irrRounded);
  46. incomes = new double[]{-70000d, 12000, 15000};
  47. irr = Irr.irr(incomes, -0.1);
  48. irrRounded = Math.round(irr * 100d) / 100d;
  49. assertEquals("irr", -0.44, irrRounded);
  50. }
  51. public void testEvaluateInSheet() {
  52. HSSFWorkbook wb = new HSSFWorkbook();
  53. HSSFSheet sheet = wb.createSheet("Sheet1");
  54. HSSFRow row = sheet.createRow(0);
  55. row.createCell(0).setCellValue(-4000d);
  56. row.createCell(1).setCellValue(1200d);
  57. row.createCell(2).setCellValue(1410d);
  58. row.createCell(3).setCellValue(1875d);
  59. row.createCell(4).setCellValue(1050d);
  60. HSSFCell cell = row.createCell(5);
  61. cell.setCellFormula("IRR(A1:E1)");
  62. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  63. fe.clearAllCachedResultValues();
  64. fe.evaluateFormulaCell(cell);
  65. double res = cell.getNumericCellValue();
  66. assertEquals(0.143d, Math.round(res * 1000d) / 1000d);
  67. }
  68. public void testIrrFromSpreadsheet(){
  69. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("IrrNpvTestCaseData.xls");
  70. HSSFSheet sheet = wb.getSheet("IRR-NPV");
  71. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  72. StringBuffer failures = new StringBuffer();
  73. int failureCount = 0;
  74. // TODO YK: Formulas in rows 16 and 17 operate with ArrayPtg which isn't yet supported
  75. // FormulaEvaluator as of r1041407 throws "Unexpected ptg class (org.apache.poi.ss.formula.ptg.ArrayPtg)"
  76. for(int rownum = 9; rownum <= 15; rownum++){
  77. HSSFRow row = sheet.getRow(rownum);
  78. HSSFCell cellA = row.getCell(0);
  79. try {
  80. CellValue cv = fe.evaluate(cellA);
  81. assertFormulaResult(cv, cellA);
  82. } catch (Throwable e){
  83. if(failures.length() > 0) failures.append('\n');
  84. failures.append("Row[" + (cellA.getRowIndex() + 1)+ "]: " + cellA.getCellFormula() + " ");
  85. failures.append(e.getMessage());
  86. failureCount++;
  87. }
  88. HSSFCell cellC = row.getCell(2); //IRR-NPV relationship: NPV(IRR(values), values) = 0
  89. try {
  90. CellValue cv = fe.evaluate(cellC);
  91. assertEquals(0, cv.getNumberValue(), 0.0001); // should agree within 0.01%
  92. } catch (Throwable e){
  93. if(failures.length() > 0) failures.append('\n');
  94. failures.append("Row[" + (cellC.getRowIndex() + 1)+ "]: " + cellC.getCellFormula() + " ");
  95. failures.append(e.getMessage());
  96. failureCount++;
  97. }
  98. }
  99. if(failures.length() > 0) {
  100. throw new AssertionFailedError(failureCount + " IRR assertions failed:\n" + failures.toString());
  101. }
  102. }
  103. private static void assertFormulaResult(CellValue cv, HSSFCell cell){
  104. double actualValue = cv.getNumberValue();
  105. double expectedValue = cell.getNumericCellValue(); // cached formula result calculated by Excel
  106. assertEquals("Invalid formula result: " + cv.toString(), HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
  107. assertEquals(expectedValue, actualValue, 1E-4); // should agree within 0.01%
  108. }
  109. }