From: Cédric Walter Date: Fri, 16 Aug 2013 18:54:35 +0000 (+0000) Subject: Bug 55043: patch for missing function QUOTIENT X-Git-Tag: REL_3_10_BETA2~20 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f75f0ec6124f53c443786ad3db5d47269b8c37e8;p=poi.git Bug 55043: patch for missing function QUOTIENT git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1514847 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index ba3be146b9..d73a31ee8e 100644 --- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -39,8 +39,6 @@ public final class AnalysisToolPak implements UDFFinder { } } - ; - private final Map _functionsByName = createFunctionsMap(); private AnalysisToolPak() { @@ -148,7 +146,7 @@ public final class AnalysisToolPak implements UDFFinder { r(m, "PRICE", null); r(m, "PRICEDISC", null); r(m, "PRICEMAT", null); - r(m, "QUOTIENT", null); + r(m, "QUOTIENT", Quotient.instance); r(m, "RANDBETWEEN", RandBetween.instance); r(m, "RECEIVED", null); r(m, "RTD", null); diff --git a/src/java/org/apache/poi/ss/formula/functions/Quotient.java b/src/java/org/apache/poi/ss/formula/functions/Quotient.java index 44d6286200..f05915bce0 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Quotient.java +++ b/src/java/org/apache/poi/ss/formula/functions/Quotient.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.formula.functions; +import org.apache.poi.ss.formula.OperationEvaluationContext; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.eval.*; @@ -35,8 +36,12 @@ import org.apache.poi.ss.formula.eval.*; * * If either enumerator/denominator is non numeric, QUOTIENT returns the #VALUE! error value. * If denominator is equals to zero, QUOTIENT returns the #DIV/0! error value. + * + * @author Cédric Walter */ -public class Quotient extends Fixed2ArgFunction { +public class Quotient extends Fixed2ArgFunction implements FreeRefFunction { + + public static final FreeRefFunction instance = new Quotient(); public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval venumerator, ValueEval vedenominator) { @@ -58,6 +63,13 @@ public class Quotient extends Fixed2ArgFunction { return ErrorEval.DIV_ZERO; } - return new StringEval(String.valueOf((int)(enumerator / denominator))); + return new NumberEval((int)(enumerator / denominator)); + } + + public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { + if (args.length != 2) { + return ErrorEval.VALUE_INVALID; + } + return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]); } } diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestQuotient.java b/src/testcases/org/apache/poi/ss/formula/functions/TestQuotient.java index d9fef5fdd9..05c6a0aed8 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestQuotient.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestQuotient.java @@ -18,6 +18,7 @@ package org.apache.poi.ss.formula.functions; import junit.framework.TestCase; import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.ValueEval; @@ -34,8 +35,8 @@ public class TestQuotient extends TestCase { private static void confirmValue(String msg, String numerator, String denominator, String expected) { ValueEval result = invokeValue(numerator, denominator); - assertEquals(StringEval.class, result.getClass()); - assertEquals(msg, expected, ((StringEval) result).getStringValue()); + assertEquals(NumberEval.class, result.getClass()); + assertEquals(msg, expected, ((NumberEval) result).getStringValue()); } private static void confirmValueError(String msg, String numerator, String denominator, ErrorEval numError) { @@ -58,6 +59,6 @@ public class TestQuotient extends TestCase { confirmValueError("numerator is nonnumeric", "ABCD", "", ErrorEval.VALUE_INVALID); confirmValueError("denominator is nonnumeric", "", "ABCD", ErrorEval.VALUE_INVALID); - confirmValueError("denominator is nonnumeric", "3.14159", "0", ErrorEval.DIV_ZERO); + confirmValueError("dividing by zero", "3.14159", "0", ErrorEval.DIV_ZERO); } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestQuotientFunctionsFromSpreadsheet.java b/src/testcases/org/apache/poi/ss/formula/functions/TestQuotientFunctionsFromSpreadsheet.java new file mode 100644 index 0000000000..3d68845e50 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestQuotientFunctionsFromSpreadsheet.java @@ -0,0 +1,31 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.formula.functions; + +/** + * Tests QUOTIENT() as loaded from a test data spreadsheet.

+ * + * @author cedric dot walter @ gmail dot com + */ +public class TestQuotientFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet { + + @Override + protected String getFilename() { + return "QuotientFunctionTestCaseData.xls"; + } +} diff --git a/test-data/spreadsheet/FormulaEvalTestData.xls b/test-data/spreadsheet/FormulaEvalTestData.xls index cc3fd41689..434a04f747 100644 Binary files a/test-data/spreadsheet/FormulaEvalTestData.xls and b/test-data/spreadsheet/FormulaEvalTestData.xls differ diff --git a/test-data/spreadsheet/QuotientFunctionTestCaseData.xls b/test-data/spreadsheet/QuotientFunctionTestCaseData.xls new file mode 100644 index 0000000000..729e774850 Binary files /dev/null and b/test-data/spreadsheet/QuotientFunctionTestCaseData.xls differ