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.

TestNpv.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Cell;
  21. import org.apache.poi.ss.usermodel.Row;
  22. import org.apache.poi.ss.usermodel.CellValue;
  23. /**
  24. * Tests for {@link Npv}
  25. *
  26. * @author Marcel May
  27. * @see <a href="http://office.microsoft.com/en-us/excel-help/npv-HP005209199.aspx">Excel Help</a>
  28. */
  29. public final class TestNpv extends TestCase {
  30. public void testEvaluateInSheetExample2() {
  31. HSSFWorkbook wb = new HSSFWorkbook();
  32. HSSFSheet sheet = wb.createSheet("Sheet1");
  33. HSSFRow row = sheet.createRow(0);
  34. sheet.createRow(1).createCell(0).setCellValue(0.08d);
  35. sheet.createRow(2).createCell(0).setCellValue(-40000d);
  36. sheet.createRow(3).createCell(0).setCellValue(8000d);
  37. sheet.createRow(4).createCell(0).setCellValue(9200d);
  38. sheet.createRow(5).createCell(0).setCellValue(10000d);
  39. sheet.createRow(6).createCell(0).setCellValue(12000d);
  40. sheet.createRow(7).createCell(0).setCellValue(14500d);
  41. HSSFCell cell = row.createCell(8);
  42. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  43. // Enumeration
  44. cell.setCellFormula("NPV(A2, A4,A5,A6,A7,A8)+A3");
  45. fe.clearAllCachedResultValues();
  46. fe.evaluateFormulaCell(cell);
  47. double res = cell.getNumericCellValue();
  48. assertEquals(1922.06d, Math.round(res * 100d) / 100d);
  49. // Range
  50. cell.setCellFormula("NPV(A2, A4:A8)+A3");
  51. fe.clearAllCachedResultValues();
  52. fe.evaluateFormulaCell(cell);
  53. res = cell.getNumericCellValue();
  54. assertEquals(1922.06d, Math.round(res * 100d) / 100d);
  55. }
  56. /**
  57. * evaluate formulas with NPV and compare the result with
  58. * the cached formula result pre-calculated by Excel
  59. */
  60. public void testNpvFromSpreadsheet(){
  61. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("IrrNpvTestCaseData.xls");
  62. HSSFSheet sheet = wb.getSheet("IRR-NPV");
  63. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  64. StringBuffer failures = new StringBuffer();
  65. int failureCount = 0;
  66. // TODO YK: Formulas in rows 16 and 17 operate with ArrayPtg which isn't yet supported
  67. // FormulaEvaluator as of r1041407 throws "Unexpected ptg class (org.apache.poi.ss.formula.ptg.ArrayPtg)"
  68. for(int rownum = 9; rownum <= 15; rownum++){
  69. HSSFRow row = sheet.getRow(rownum);
  70. HSSFCell cellB = row.getCell(1);
  71. try {
  72. CellValue cv = fe.evaluate(cellB);
  73. assertFormulaResult(cv, cellB);
  74. } catch (Throwable e){
  75. if(failures.length() > 0) failures.append('\n');
  76. failures.append("Row[" + (cellB.getRowIndex() + 1)+ "]: " + cellB.getCellFormula() + " ");
  77. failures.append(e.getMessage());
  78. failureCount++;
  79. }
  80. }
  81. if(failures.length() > 0) {
  82. throw new AssertionFailedError(failureCount + " IRR evaluations failed:\n" + failures.toString());
  83. }
  84. }
  85. private static void assertFormulaResult(CellValue cv, HSSFCell cell){
  86. double actualValue = cv.getNumberValue();
  87. double expectedValue = cell.getNumericCellValue(); // cached formula result calculated by Excel
  88. assertEquals(expectedValue, actualValue, 1E-4); // should agree within 0.01%
  89. }
  90. }