From be5106a1d7fe21be3645bb091498a1d1efd582bc Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Tue, 16 May 2017 03:04:12 +0000 Subject: [PATCH] github-43: add disabled unit test showing a problem with ROUNDUP(3987*0.2, 2). Thanks to @FishMeat. https://github.com/apache/poi/pull/43 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1795265 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/TestMathX.java | 36 ++++++++----- .../ss/formula/functions/TestRoundFuncs.java | 51 ++++++++++++++++--- 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java b/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java index 18268068f..12c842554 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java @@ -751,25 +751,28 @@ public class TestMathX extends AbstractNumericTestCase { assertEquals("roundDown ", 100, MathX.roundDown(d, p)); d = 0.049999999999999975d; p = 2; - assertEquals("round ", 0.04d, MathX.roundDown(d, p)); + assertEquals("roundDown ", 0.04d, MathX.roundDown(d, p)); d = 0.049999999999999975d; p = 1; - assertEquals("round ", 0.0d, MathX.roundDown(d, p)); + assertEquals("roundDown ", 0.0d, MathX.roundDown(d, p)); d = Double.NaN; p = 1; - assertEquals("round ", Double.NaN, MathX.roundDown(d, p)); + assertEquals("roundDown ", Double.NaN, MathX.roundDown(d, p)); d = Double.POSITIVE_INFINITY; p = 1; - assertEquals("round ", Double.NaN, MathX.roundDown(d, p)); + assertEquals("roundDown ", Double.NaN, MathX.roundDown(d, p)); d = Double.NEGATIVE_INFINITY; p = 1; - assertEquals("round ", Double.NaN, MathX.roundDown(d, p)); + assertEquals("roundDown ", Double.NaN, MathX.roundDown(d, p)); d = Double.MAX_VALUE; p = 1; - assertEquals("round ", Double.MAX_VALUE, MathX.roundDown(d, p)); + assertEquals("roundDown ", Double.MAX_VALUE, MathX.roundDown(d, p)); d = Double.MIN_VALUE; p = 1; - assertEquals("round ", 0.0d, MathX.roundDown(d, p)); + assertEquals("roundDown ", 0.0d, MathX.roundDown(d, p)); + + d = 3987 * 0.2; p = 2; + assertEquals("roundDown ", 797.40, MathX.round(d, p)); } public void testRoundUp() { @@ -819,25 +822,30 @@ public class TestMathX extends AbstractNumericTestCase { assertEquals("roundUp ", 200, MathX.roundUp(d, p)); d = 0.049999999999999975d; p = 2; - assertEquals("round ", 0.05d, MathX.roundUp(d, p)); + assertEquals("roundUp ", 0.05d, MathX.roundUp(d, p)); d = 0.049999999999999975d; p = 1; - assertEquals("round ", 0.1d, MathX.roundUp(d, p)); + assertEquals("roundUp ", 0.1d, MathX.roundUp(d, p)); d = Double.NaN; p = 1; - assertEquals("round ", Double.NaN, MathX.roundUp(d, p)); + assertEquals("roundUp ", Double.NaN, MathX.roundUp(d, p)); d = Double.POSITIVE_INFINITY; p = 1; - assertEquals("round ", Double.NaN, MathX.roundUp(d, p)); + assertEquals("roundUp ", Double.NaN, MathX.roundUp(d, p)); d = Double.NEGATIVE_INFINITY; p = 1; - assertEquals("round ", Double.NaN, MathX.roundUp(d, p)); + assertEquals("roundUp ", Double.NaN, MathX.roundUp(d, p)); d = Double.MAX_VALUE; p = 1; - assertEquals("round ", Double.MAX_VALUE, MathX.roundUp(d, p)); + assertEquals("roundUp ", Double.MAX_VALUE, MathX.roundUp(d, p)); d = Double.MIN_VALUE; p = 1; - assertEquals("round ", 0.1d, MathX.roundUp(d, p)); + assertEquals("roundUp ", 0.1d, MathX.roundUp(d, p)); + + //github-43: https://github.com/apache/poi/pull/43 + //@Ignore("ROUNDUP(3987*0.2, 2) currently fails by returning 797.41") + //d = 3987 * 0.2; p = 2; + //assertEquals("roundUp ", 797.40, MathX.roundUp(d, p)); } public void testCeiling() { diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestRoundFuncs.java b/src/testcases/org/apache/poi/ss/formula/functions/TestRoundFuncs.java index 5d2398d2b..6730cd2d9 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestRoundFuncs.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestRoundFuncs.java @@ -17,7 +17,10 @@ package org.apache.poi.ss.formula.functions; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.Ignore; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ValueEval; @@ -29,22 +32,58 @@ import org.apache.poi.ss.formula.eval.StringEval; * * @author Josh Micich */ -public final class TestRoundFuncs extends TestCase { - private static final NumericFunction F = null; - public void testRounddownWithStringArg() { +public final class TestRoundFuncs { + // github-43 + // https://github.com/apache/poi/pull/43 + @Ignore("ROUNDUP(3987*0.2, 2) currently fails by returning 797.41") + @Test + public void testRoundUp() { + assertRoundUpEquals(797.40, 3987*0.2, 2, 1e-10); + } + @Test + public void testRoundDown() { + assertRoundDownEquals(797.40, 3987*0.2, 2, 1e-10); + } + + @Test + public void testRound() { + assertRoundEquals(797.40, 3987*0.2, 2, 1e-10); + } + + @Test + public void testRoundDownWithStringArg() { ValueEval strArg = new StringEval("abc"); ValueEval[] args = { strArg, new NumberEval(2), }; ValueEval result = NumericFunction.ROUNDDOWN.evaluate(args, -1, (short)-1); assertEquals(ErrorEval.VALUE_INVALID, result); } - public void testRoundupWithStringArg() { - + @Test + public void testRoundUpWithStringArg() { ValueEval strArg = new StringEval("abc"); ValueEval[] args = { strArg, new NumberEval(2), }; ValueEval result = NumericFunction.ROUNDUP.evaluate(args, -1, (short)-1); assertEquals(ErrorEval.VALUE_INVALID, result); } + + + private static void assertRoundFuncEquals(Function func, double expected, double number, double places, double tolerance) { + ValueEval[] args = { new NumberEval( number ), new NumberEval(places), }; + NumberEval result = (NumberEval) func.evaluate(args, -1, (short)-1); + assertEquals(expected, result.getNumberValue(), tolerance); + } + + private static void assertRoundEquals(double expected, double number, double places, double tolerance) { + TestRoundFuncs.assertRoundFuncEquals(NumericFunction.ROUND, expected, number, places, tolerance); + } + + private static void assertRoundUpEquals(double expected, double number, double places, double tolerance) { + TestRoundFuncs.assertRoundFuncEquals(NumericFunction.ROUNDUP, expected, number, places, tolerance); + } + + private static void assertRoundDownEquals(double expected, double number, double places, double tolerance) { + TestRoundFuncs.assertRoundFuncEquals(NumericFunction.ROUNDDOWN, expected, number, places, tolerance); + } }