diff --git a/src/documentation/content/xdocs/spreadsheet/eval-devguide.xml b/src/documentation/content/xdocs/spreadsheet/eval-devguide.xml index 551b2f133..d59b8034b 100644 --- a/src/documentation/content/xdocs/spreadsheet/eval-devguide.xml +++ b/src/documentation/content/xdocs/spreadsheet/eval-devguide.xml @@ -206,6 +206,23 @@

+
Testing Framework +

Automated testing of the implemented Function is easy. + The source code for this is in the file: o.a.p.h.record.formula.GenericFormulaTestCase.java + This class has a reference to the test xls file (not /a/ test xls, /the/ test xls :) + which may need to be changed for your environment. Once you do that, in the test xls, + locate the entry for the function that you have implemented and enter different tests + in a cell in the FORMULA row. Then copy the "value of" the formula that you entered in the + cell just below it (this is easily done in excel as: + [copy the formula cell] > [go to cell below] > Edit > Paste Special > Values > "ok"). + You can enter multiple such formulas and paste their values in the cell below and the + test framework will automatically test if the formula evaluation matches the expected + value (Again, hard to put in words, so if you will, please take time to quickly look + at the code and the currently entered tests in the patch attachment "FormulaEvalTestData.xls" + file). +

+
+
Appendix A @@ -354,4 +371,4 @@
- \ No newline at end of file + diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 7bb0f71fe..3e6792d07 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52057 - updated formula test framework to be aware of recently added Functions 52574 - support setting header / footer page margins in HSSF 52583 - fixed WorkbookUtil#createSafeSheetName to escape colon 51710 - fixed reading shared formulas in XSSF diff --git a/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java b/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java index 623cf65db..38d800653 100644 --- a/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java +++ b/src/java/org/apache/poi/ss/formula/functions/NumericFunction.java @@ -331,7 +331,8 @@ public abstract class NumericFunction implements Function { double d0 = singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex); double d1 = singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex); double multi = Math.pow(10d,d1); - result = Math.floor(d0 * multi) / multi; + if(d0 < 0) result = -Math.floor(-d0 * multi) / multi; + else result = Math.floor(d0 * multi) / multi; checkValue(result); }catch (EvaluationException e) { return e.getErrorEval(); diff --git a/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java index fd0771fdc..fbbe0efdb 100644 --- a/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java +++ b/src/testcases/org/apache/poi/ss/formula/eval/TestFormulasFromSpreadsheet.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.formula.eval; import java.io.PrintStream; +import java.util.Collection; import junit.framework.Assert; import junit.framework.AssertionFailedError; @@ -31,6 +32,8 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; /** * Tests formulas and operators as loaded from a test data spreadsheet.

@@ -43,6 +46,7 @@ import org.apache.poi.ss.usermodel.Sheet; * @author Amol S. Deshmukh < amolweb at ya hoo dot com > */ public final class TestFormulasFromSpreadsheet extends TestCase { + private static final POILogger logger = POILogFactory.getLogger(TestFormulasFromSpreadsheet.class); private static final class Result { public static final int SOME_EVALUATIONS_FAILED = -1; @@ -167,9 +171,7 @@ public final class TestFormulasFromSpreadsheet extends TestCase { + _evaluationFailureCount + " evaluation(s). " + successMsg; throw new AssertionFailedError(msg); } - if(false) { // normally no output for successful tests - System.out.println(getClass().getName() + ": " + successMsg); - } + logger.log(POILogger.INFO, getClass().getName() + ": " + successMsg); } /** @@ -179,6 +181,7 @@ public final class TestFormulasFromSpreadsheet extends TestCase { */ private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) { HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(workbook); + Collection funcs = FunctionEval.getSupportedFunctionNames(); int rowIndex = startRowIndex; while (true) { @@ -208,6 +211,12 @@ public final class TestFormulasFromSpreadsheet extends TestCase { default: throw new RuntimeException("unexpected result"); case Result.NO_EVALUATIONS_FOUND: // do nothing + String uname = targetFunctionName.toUpperCase(); + if(startRowIndex >= SS.START_FUNCTIONS_ROW_INDEX && + funcs.contains(uname)) { + logger.log(POILogger.WARN, uname + ": function is supported but missing test data"); + } + break; } } rowIndex += SS.NUMBER_OF_ROWS_PER_FUNCTION; diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java b/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java index c3ed45974..55360b96c 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestTrunc.java @@ -56,4 +56,11 @@ public final class TestTrunc extends AbstractNumericTestCase { ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1); assertEquals("TRUNC", (new NumberEval(2d)).getNumberValue(), ((NumberEval)result).getNumberValue()); } + + public void testNegative() { + ValueEval[] args = { new NumberEval(-8.9), new NumberEval(0) }; + @SuppressWarnings("static-access") + ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1); + assertEquals("TRUNC", (new NumberEval(-8)).getNumberValue(), ((NumberEval)result).getNumberValue()); + } } diff --git a/test-data/spreadsheet/FormulaEvalTestData.xls b/test-data/spreadsheet/FormulaEvalTestData.xls index 919bf867e..4aa144093 100644 Binary files a/test-data/spreadsheet/FormulaEvalTestData.xls and b/test-data/spreadsheet/FormulaEvalTestData.xls differ diff --git a/test-data/spreadsheet/FormulaEvalTestData_Copy.xlsx b/test-data/spreadsheet/FormulaEvalTestData_Copy.xlsx index 8e491a54e..848ea3f01 100644 Binary files a/test-data/spreadsheet/FormulaEvalTestData_Copy.xlsx and b/test-data/spreadsheet/FormulaEvalTestData_Copy.xlsx differ