add helper functions to make it easier to see which scenarios are being tested

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1784231 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-02-24 05:07:10 +00:00
parent c04e2a314a
commit bb6e94851f

View File

@ -18,49 +18,76 @@
package org.apache.poi.ss.formula.functions; package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.*; import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.util.StringUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/**
* Tests WEEKDAY(serial_number[, return_type]) excep function
* https://support.office.com/en-us/article/WEEKDAY-function-60E44483-2ED1-439F-8BD0-E404C190949A
*/
public class TestWeekdayFunc { public class TestWeekdayFunc {
private static final double TOLERANCE = 0.001;
private void assertEvaluateEquals(double expected, double serial_number) {
String formula = "WEEKDAY(" + serial_number + ")";
ValueEval[] args = new ValueEval[] { new NumberEval(serial_number) };
NumberEval result = (NumberEval) WeekdayFunc.instance.evaluate(args, 0, 0);
assertEquals(formula, expected, result.getNumberValue(), TOLERANCE);
}
private void assertEvaluateEquals(double expected, double serial_number, double return_type) {
String formula = "WEEKDAY(" + serial_number + ", " + return_type + ")";
ValueEval[] args = new ValueEval[] { new NumberEval(serial_number), new NumberEval(return_type) };
NumberEval result = (NumberEval) WeekdayFunc.instance.evaluate(args, 0, 0);
assertEquals(formula, expected, result.getNumberValue(), TOLERANCE);
}
@Test @Test
public void testEvaluate() throws Exception { public void testEvaluate() throws Exception {
assertEquals(2.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(2.0, 1.0);
assertEquals(2.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(1.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(2.0, 1.0, 1.0);
assertEquals(1.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(2.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(1.0, 1.0, 2.0);
assertEquals(0.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(3.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(0.0, 1.0, 3.0);
assertEquals(1.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(11.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(1.0, 1.0, 11.0);
assertEquals(7.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(12.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(7.0, 1.0, 12.0);
assertEquals(6.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(13.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(6.0, 1.0, 13.0);
assertEquals(5.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(14.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(5.0, 1.0, 14.0);
assertEquals(4.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(15.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(4.0, 1.0, 15.0);
assertEquals(3.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(16.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(3.0, 1.0, 16.0);
assertEquals(2.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(17.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(2.0, 1.0, 17.0);
assertEquals(3.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0)}, 0, 0)).getNumberValue(), 0.001);
assertEquals(3.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(1.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(3.0, 39448.0);
assertEquals(2.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(2.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(3.0, 39448.0, 1.0);
assertEquals(1.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(3.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(2.0, 39448.0, 2.0);
assertEquals(2.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(11.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(1.0, 39448.0, 3.0);
assertEquals(1.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(12.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(2.0, 39448.0, 11.0);
assertEquals(7.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(13.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(1.0, 39448.0, 12.0);
assertEquals(6.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(14.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(7.0, 39448.0, 13.0);
assertEquals(5.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(15.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(6.0, 39448.0, 14.0);
assertEquals(4.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(16.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(5.0, 39448.0, 15.0);
assertEquals(3.0, ((NumberEval)WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(39448.0), new NumberEval(17.0)}, 0, 0)).getNumberValue(), 0.001); assertEvaluateEquals(4.0, 39448.0, 16.0);
assertEvaluateEquals(3.0, 39448.0, 17.0);
}
// for testing invalid invocations
private void assertEvaluateEquals(String message, ErrorEval expected, ValueEval... args) {
String formula = "WEEKDAY(" + StringUtil.join(args, ", ") + ")";
ValueEval result = WeekdayFunc.instance.evaluate(args, 0, 0);
assertEquals(formula + ": " + message, expected, result);
} }
@Test @Test
public void testEvaluateInvalid() throws Exception { public void testEvaluateInvalid() throws Exception {
assertEquals(ErrorEval.VALUE_INVALID, WeekdayFunc.instance.evaluate(new ValueEval[]{}, 0, 0)); assertEvaluateEquals("no args", ErrorEval.VALUE_INVALID);
assertEquals(ErrorEval.VALUE_INVALID, WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(1.0), new NumberEval(1.0)}, 0, 0)); assertEvaluateEquals("too many args", ErrorEval.VALUE_INVALID, new NumberEval(1.0), new NumberEval(1.0), new NumberEval(1.0));
assertEvaluateEquals("negative date", ErrorEval.NUM_ERROR, new NumberEval(-1.0));
assertEquals(ErrorEval.NUM_ERROR, WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(-1.0)}, 0, 0)); assertEvaluateEquals("cannot coerce serial_number to number", ErrorEval.VALUE_INVALID, new StringEval(""));
assertEquals(ErrorEval.VALUE_INVALID, WeekdayFunc.instance.evaluate(new ValueEval[]{new StringEval("")}, 0, 0)); assertEvaluateEquals("cannot coerce return_type to number", ErrorEval.VALUE_INVALID, new StringEval("1"), new StringEval(""));
assertEquals(ErrorEval.VALUE_INVALID, WeekdayFunc.instance.evaluate(new ValueEval[]{new StringEval("1"), new StringEval("")}, 0, 0)); assertEvaluateEquals("return_type is blank", ErrorEval.NUM_ERROR, new StringEval("2"), BlankEval.instance);
assertEquals(ErrorEval.NUM_ERROR, WeekdayFunc.instance.evaluate(new ValueEval[]{new StringEval("2"), BlankEval.instance}, 0, 0)); assertEvaluateEquals("return_type is missing", ErrorEval.NUM_ERROR, new StringEval("3"), MissingArgEval.instance);
assertEquals(ErrorEval.NUM_ERROR, WeekdayFunc.instance.evaluate(new ValueEval[]{new StringEval("3"), MissingArgEval.instance}, 0, 0)); assertEvaluateEquals("invalid return_type", ErrorEval.NUM_ERROR, new NumberEval(1.0), new NumberEval(18.0));
assertEquals(ErrorEval.NUM_ERROR, WeekdayFunc.instance.evaluate(new ValueEval[]{new NumberEval(1.0), new NumberEval(18.0)}, 0, 0));
} }
} }