Migrate tests to Junit 4

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871067 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2019-12-09 00:37:21 +00:00
parent cdd945b0da
commit d8bfbaffdf
47 changed files with 843 additions and 564 deletions

View File

@ -20,27 +20,19 @@
*/
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertTrue;
/**
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
*
*/
public abstract class AbstractNumericTestCase extends TestCase {
public abstract class AbstractNumericTestCase {
public static final double POS_ZERO = 1E-4;
public static final double DIFF_TOLERANCE_FACTOR = 1E-8;
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
/**
* Why doesnt JUnit have a method like this for doubles?
* Why doesnt JUnit have a method like this for doubles?
* The current impl (3.8.1) of Junit has a retar*** method
* for comparing doubles. DO NOT use that.
* TODO: This class should really be in an abstract super class
@ -72,4 +64,8 @@ public abstract class AbstractNumericTestCase extends TestCase {
assertEquals(msg, baseval, checkval, POS_ZERO, DIFF_TOLERANCE_FACTOR);
}
public static void assertEquals(double baseval, double checkval) {
assertEquals("", baseval, checkval, POS_ZERO, DIFF_TOLERANCE_FACTOR);
}
}

View File

@ -16,16 +16,18 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test;
import junit.framework.TestCase;
public final class TestAddress extends TestCase {
public final class TestAddress {
@Test
public void testAddress() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);

View File

@ -17,17 +17,18 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test;
public final class TestAreas extends TestCase {
public final class TestAreas {
@Test
public void testAreas() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
@ -54,6 +55,6 @@ public final class TestAreas extends TestCase {
fe.notifyUpdateCell(cell);
CellValue result = fe.evaluate(cell);
assertEquals(result.getCellTypeEnum(), CellType.NUMERIC);
assertEquals(expectedResult, result.getNumberValue());
assertEquals(expectedResult, result.getNumberValue(), 0);
}
}

View File

@ -17,19 +17,19 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for Excel function AVERAGE()
*
* @author Josh Micich
*/
public final class TestAverage extends TestCase {
public final class TestAverage {
private static ValueEval invokeAverage(ValueEval[] args) {
return AggregateFunction.AVERAGE.evaluate(args, -1, (short)-1);
@ -47,6 +47,7 @@ public final class TestAverage extends TestCase {
assertEquals(expectedError.getErrorCode(), ((ErrorEval)result).getErrorCode());
}
@Test
public void testBasic() {
ValueEval[] values = {
@ -74,6 +75,7 @@ public final class TestAverage extends TestCase {
/**
* Valid cases where values are not pure numbers
*/
@Test
public void testUnusualArgs() {
ValueEval[] values = {
new NumberEval(1),
@ -86,6 +88,7 @@ public final class TestAverage extends TestCase {
}
@Test
public void testErrors() {
ValueEval[] values = {
new NumberEval(1),

View File

@ -17,7 +17,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -28,13 +28,14 @@ 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;
import org.junit.Test;
/**
* Tests for {@link Bin2Dec}
*
* @author cedric dot walter @ gmail dot com
*/
public final class TestBin2Dec extends TestCase {
public final class TestBin2Dec {
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[] { new StringEval(number1) };
@ -53,6 +54,7 @@ public final class TestBin2Dec extends TestCase {
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Converts binary '00101' to decimal (5)", "00101", "5");
confirmValue("Converts binary '1111111111' to decimal (-1)", "1111111111", "-1");
@ -60,25 +62,28 @@ public final class TestBin2Dec extends TestCase {
confirmValue("Converts binary '0111111111' to decimal (511)", "0111111111", "511");
}
@Test
public void testErrors() {
confirmValueError("does not support more than 10 digits","01010101010", ErrorEval.NUM_ERROR);
confirmValueError("not a valid binary number","GGGGGGG", ErrorEval.NUM_ERROR);
confirmValueError("not a valid binary number","3.14159", ErrorEval.NUM_ERROR);
}
@Test
public void testEvalOperationEvaluationContext() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Bin2Dec().evaluate(args, ctx);
assertEquals(NumberEval.class, result.getClass());
assertEquals("0", ((NumberEval) result).getStringValue());
}
@Test
public void testEvalOperationEvaluationContextFails() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 0) };
ValueEval result = new Bin2Dec().evaluate(args, ctx);
@ -91,7 +96,7 @@ public final class TestBin2Dec extends TestCase {
wb.createSheet();
HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
@Override
public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
return true;
@ -101,9 +106,10 @@ public final class TestBin2Dec extends TestCase {
workbook, 0, 0, 0, null);
}
@Test
public void testRefs() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Bin2Dec().evaluate(args, -1, -1);

View File

@ -17,25 +17,27 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
/**
* Test for YEAR / MONTH / DAY / HOUR / MINUTE / SECOND
*/
public final class TestCalendarFieldFunction extends TestCase {
public final class TestCalendarFieldFunction {
private HSSFCell cell11;
private HSSFFormulaEvaluator evaluator;
@Override
@Before
public void setUp() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
@ -43,6 +45,7 @@ public final class TestCalendarFieldFunction extends TestCase {
evaluator = new HSSFFormulaEvaluator(wb);
}
@Test
public void testValid() {
confirm("YEAR(2.26)", 1900);
confirm("MONTH(2.26)", 1);
@ -50,7 +53,7 @@ public final class TestCalendarFieldFunction extends TestCase {
confirm("HOUR(2.26)", 6);
confirm("MINUTE(2.26)", 14);
confirm("SECOND(2.26)", 24);
confirm("YEAR(40627.4860417)", 2011);
confirm("MONTH(40627.4860417)", 3);
confirm("DAY(40627.4860417)", 25);
@ -59,6 +62,7 @@ public final class TestCalendarFieldFunction extends TestCase {
confirm("SECOND(40627.4860417)", 54);
}
@Test
public void testRounding() {
// 41484.999994200 = 23:59:59,499
// 41484.9999942129 = 23:59:59,500 (but sub-milliseconds are below 0.5 (0.49999453965575), XLS-second results in 59)
@ -71,13 +75,14 @@ public final class TestCalendarFieldFunction extends TestCase {
confirm("HOUR(41484.9999942129)", 23);
confirm("MINUTE(41484.9999942129)", 59);
confirm("SECOND(41484.9999942129)", 59);
confirm("DAY(41484.9999942130)", 30);
confirm("HOUR(41484.9999942130)", 0);
confirm("MINUTE(41484.9999942130)", 0);
confirm("SECOND(41484.9999942130)", 0);
}
@Test
public void testDaylightSaving() {
confirm("HOUR(41364.08263888890000)", 1); // 31.03.2013 01:59:00,000
confirm("HOUR(41364.08333333330000)", 2); // 31.03.2013 02:00:00,000 (this time does not exist in TZ CET, but EXCEL does not care)
@ -86,11 +91,12 @@ public final class TestCalendarFieldFunction extends TestCase {
confirm("HOUR(41364.12500000000000)", 3); // 31.03.2013 03:00:00,000
}
@Test
public void testBugDate() {
confirm("YEAR(0.0)", 1900);
confirm("MONTH(0.0)", 1);
confirm("DAY(0.0)", 0);
confirm("YEAR(0.26)", 1900);
confirm("MONTH(0.26)", 1);
confirm("DAY(0.26)", 0);

View File

@ -16,16 +16,18 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test;
import junit.framework.TestCase;
public final class TestClean extends TestCase {
public final class TestClean {
@Test
public void testClean() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCell cell = wb.createSheet().createRow(0).createCell(0);

View File

@ -16,18 +16,19 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for {@link Code}
*
* @author cedric dot walter @ gmail dot com
*/
public class TestCode extends TestCase
{
public class TestCode {
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[]{new StringEval(number1),};
return new Code().evaluate(args, -1, -1);
@ -45,7 +46,7 @@ public class TestCode extends TestCase
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Displays the numeric code for A (65)", "A", "65");
confirmValue("Displays the numeric code for the first character in text ABCDEFGHI (65)", "ABCDEFGHI", "65");
@ -53,6 +54,7 @@ public class TestCode extends TestCase
confirmValue("Displays the numeric code for ! (33)", "!", "33");
}
@Test
public void testErrors() {
confirmValueError("Empty text", "", ErrorEval.VALUE_INVALID);
}

View File

@ -16,18 +16,19 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for {@link Complex}
*
* @author cedric dot walter @ gmail dot com
*/
public class TestComplex extends TestCase
{
public class TestComplex {
private static ValueEval invokeValue(String real_num, String i_num, String suffix) {
ValueEval[] args = new ValueEval[]{new StringEval(real_num), new StringEval(i_num), new StringEval(suffix)};
return new Complex().evaluate(args, -1, -1);
@ -45,6 +46,7 @@ public class TestComplex extends TestCase
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Complex number with 3 and 4 as the real and imaginary coefficients (3 + 4i)", "3","4", "", "3+4i");
confirmValue("Complex number with 3 and 4 as the real and imaginary coefficients, and j as the suffix (3 + 4j)", "3","4", "j", "3+4j");
@ -58,6 +60,7 @@ public class TestComplex extends TestCase
confirmValue("Complex number with -2 and -3 as the real and imaginary coefficients (-0.5-3.2i)", "-0.5","-3.2", "", "-0.5-3.2i");
}
@Test
public void testErrors() {
confirmValueError("argument is nonnumeric", "ABCD", "","", ErrorEval.VALUE_INVALID);
confirmValueError("argument is nonnumeric", "1", "ABCD","", ErrorEval.VALUE_INVALID);

View File

@ -17,6 +17,12 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
@ -36,17 +42,16 @@ import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.util.CellReference;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.junit.Test;
/**
* Test cases for COUNT(), COUNTA() COUNTIF(), COUNTBLANK()
*/
public final class TestCountFuncs extends TestCase {
public final class TestCountFuncs {
private static final String NULL = null;
@Test
public void testCountBlank() {
AreaEval range;
ValueEval[] values;
@ -74,6 +79,7 @@ public final class TestCountFuncs extends TestCase {
confirmCountBlank(3, range);
}
@Test
public void testCountA() {
ValueEval[] args;
@ -103,6 +109,7 @@ public final class TestCountFuncs extends TestCase {
confirmCountA(59, args);
}
@Test
public void testCountIf() {
AreaEval range;
ValueEval[] values;
@ -139,6 +146,7 @@ public final class TestCountFuncs extends TestCase {
confirmCountIf(2, range, new StringEval(">0.5"));
}
@Test
public void testCriteriaPredicateNe_Bug46647() {
I_MatchPredicate mp = Countif.createCriteriaPredicate(new StringEval("<>aa"), 0, 0);
assertNotNull(mp);
@ -195,6 +203,7 @@ public final class TestCountFuncs extends TestCase {
* String criteria in COUNTIF are case insensitive;
* for example, the string "apples" and the string "APPLES" will match the same cells.
*/
@Test
public void testCaseInsensitiveStringComparison() {
AreaEval range;
ValueEval[] values;
@ -215,6 +224,7 @@ public final class TestCountFuncs extends TestCase {
/**
* special case where the criteria argument is a cell reference
*/
@Test
public void testCountIfWithCriteriaReference() {
ValueEval[] values = {
@ -258,6 +268,7 @@ public final class TestCountFuncs extends TestCase {
/**
* the criteria arg is mostly handled by {@link OperandResolver#getSingleValue(org.apache.poi.ss.formula.eval.ValueEval, int, int)}}
*/
@Test
public void testCountifAreaCriteria() {
int srcColIx = 2; // anything but column A
@ -293,6 +304,7 @@ public final class TestCountFuncs extends TestCase {
confirmPredicate(true, mp, ErrorEval.VALUE_INVALID);
}
@Test
public void testCountifEmptyStringCriteria() {
I_MatchPredicate mp;
@ -312,6 +324,7 @@ public final class TestCountFuncs extends TestCase {
confirmPredicate(true, mp, "");
}
@Test
public void testCountifComparisons() {
I_MatchPredicate mp;
@ -353,6 +366,7 @@ public final class TestCountFuncs extends TestCase {
* the criteria arg value can be an error code (the error does not
* propagate to the COUNTIF result).
*/
@Test
public void testCountifErrorCriteria() {
I_MatchPredicate mp;
@ -381,10 +395,11 @@ public final class TestCountFuncs extends TestCase {
* Bug #51498 - Check that CountIf behaves correctly for GTE, LTE
* and NEQ cases
*/
@Test
public void testCountifBug51498() throws Exception {
final int REF_COL = 4;
final int EVAL_COL = 3;
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("51498.xls");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
HSSFSheet sheet = workbook.getSheetAt(0);
@ -425,6 +440,7 @@ public final class TestCountFuncs extends TestCase {
}
}
@Test
public void testWildCards() {
I_MatchPredicate mp;
@ -456,6 +472,8 @@ public final class TestCountFuncs extends TestCase {
confirmPredicate(true, mp, "12812");
confirmPredicate(false, mp, "128812");
}
@Test
public void testNotQuiteWildCards() {
I_MatchPredicate mp;
@ -489,6 +507,7 @@ public final class TestCountFuncs extends TestCase {
assertEquals(expectedResult, matchPredicate.matches(value));
}
@Test
public void testCountifFromSpreadsheet() {
testCountFunctionFromSpreadsheet("countifExamples.xls", 1, 2, 3, "countif");
}
@ -497,6 +516,7 @@ public final class TestCountFuncs extends TestCase {
* Two COUNTIF examples taken from
* http://office.microsoft.com/en-us/excel-help/countif-function-HP010069840.aspx?CTT=5&origin=HA010277524
*/
@Test
public void testCountifExamples() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("countifExamples.xls");
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
@ -540,6 +560,7 @@ public final class TestCountFuncs extends TestCase {
}
}
@Test
public void testCountBlankFromSpreadsheet() {
testCountFunctionFromSpreadsheet("countblankExamples.xls", 1, 3, 4, "countblank");
}

View File

@ -17,25 +17,28 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
/**
* @author Pavel Krupets (pkrupets at palmtreebusiness dot com)
*/
public final class TestDate extends TestCase {
public final class TestDate {
private HSSFCell cell11;
private HSSFFormulaEvaluator evaluator;
@Override
@Before
public void setUp() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
@ -47,11 +50,13 @@ public final class TestDate extends TestCase {
* Test disabled pending a fix in the formula evaluator
* TODO - create MissingArgEval and modify the formula evaluator to handle this
*/
public void DISABLEDtestSomeArgumentsMissing() {
@Ignore
public void testSomeArgumentsMissing() {
confirm("DATE(, 1, 0)", 0.0);
confirm("DATE(, 1, 1)", 1.0);
}
@Test
public void testValid() {
confirm("DATE(1900, 1, 1)", 1);
@ -62,6 +67,7 @@ public final class TestDate extends TestCase {
confirm("DATE(2007, 1, 1)", 39083);
}
@Test
public void testBugDate() {
confirm("DATE(1900, 2, 29)", 60);
confirm("DATE(1900, 2, 30)", 61);
@ -70,6 +76,7 @@ public final class TestDate extends TestCase {
confirm("DATE(1900, 1, 22222)", 22222);
}
@Test
public void testPartYears() {
confirm("DATE(4, 1, 1)", 1462.00);
confirm("DATE(14, 1, 1)", 5115.00);

View File

@ -17,7 +17,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -31,13 +31,14 @@ import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.Test;
/**
* Tests for {@link Dec2Bin}
*
* @author cedric dot walter @ gmail dot com
*/
public final class TestDec2Bin extends TestCase {
public final class TestDec2Bin {
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[] { new StringEval(number1) };
@ -61,6 +62,7 @@ public final class TestDec2Bin extends TestCase {
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Converts binary '00101' from binary (5)", "5", "101");
confirmValue("Converts binary '1111111111' from binary (-1)", "-1", "1111111111");
@ -69,6 +71,7 @@ public final class TestDec2Bin extends TestCase {
confirmValue("Converts binary '1000000000' from binary (511)", "-512", "1000000000");
}
@Test
public void testErrors() {
confirmValueError("fails for >= 512 or < -512","512", ErrorEval.NUM_ERROR);
confirmValueError("fails for >= 512 or < -512","-513", ErrorEval.NUM_ERROR);
@ -76,9 +79,10 @@ public final class TestDec2Bin extends TestCase {
confirmValueError("not a valid decimal number","3.14159a", ErrorEval.VALUE_INVALID);
}
@Test
public void testEvalOperationEvaluationContext() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Dec2Bin().evaluate(args, ctx);
@ -86,9 +90,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals("1101", ((StringEval) result).getStringValue());
}
@Test
public void testEvalOperationEvaluationContextFails() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ErrorEval.VALUE_INVALID };
ValueEval result = new Dec2Bin().evaluate(args, ctx);
@ -111,7 +116,7 @@ public final class TestDec2Bin extends TestCase {
HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
@Override
public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
return true;
@ -121,9 +126,10 @@ public final class TestDec2Bin extends TestCase {
workbook, 0, 0, 0, null);
}
@Test
public void testRefs() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -131,9 +137,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals("1101", ((StringEval) result).getStringValue());
}
@Test
public void testWithPlacesIntInt() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -142,9 +149,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals("1101", ((StringEval) result).getStringValue());
}
@Test
public void testWithPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Bin().evaluate(args, ctx);
@ -153,9 +161,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals("1101", ((StringEval) result).getStringValue());
}
@Test
public void testWithToShortPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 3) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -163,9 +172,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals(ErrorEval.NUM_ERROR, result);
}
@Test
public void testWithTooManyParamsIntInt() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -173,9 +183,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testWithTooManyParams() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Bin().evaluate(args, ctx);
@ -183,9 +194,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testWithErrorPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ErrorEval.NULL_INTERSECTION };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -193,9 +205,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals(ErrorEval.NULL_INTERSECTION, result);
}
@Test
public void testWithNegativePlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 2) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -203,9 +216,10 @@ public final class TestDec2Bin extends TestCase {
assertEquals(ErrorEval.NUM_ERROR, result);
}
@Test
public void testWithZeroPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), new NumberEval(0.0) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
@ -213,16 +227,18 @@ public final class TestDec2Bin extends TestCase {
assertEquals(ErrorEval.NUM_ERROR, result);
}
@Test
public void testWithEmptyPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(1, 0) };
ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
assertEquals(ErrorEval.class, result.getClass());
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testBackAndForth() {
for (int i = -512; i < 512; i++) {
ValueEval result = invokeValue(Integer.toString(i));

View File

@ -17,7 +17,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -31,13 +31,14 @@ import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.Test;
/**
* Tests for {@link Dec2Hex}
*
* @author cedric dot walter @ gmail dot com
*/
public final class TestDec2Hex extends TestCase {
public final class TestDec2Hex {
private static ValueEval invokeValue(String number1, String number2) {
ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), };
@ -72,6 +73,7 @@ public final class TestDec2Hex extends TestCase {
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Converts decimal 100 to hexadecimal with 0 characters (64)", "100","0", "64");
confirmValue("Converts decimal 100 to hexadecimal with 4 characters (0064)", "100","4", "0064");
@ -81,9 +83,9 @@ public final class TestDec2Hex extends TestCase {
confirmValue("Converts decimal -54 to hexadecimal, 2 is ignored","-54", "2", "FFFFFFFFCA");
confirmValue("places is optionnal","-54", "FFFFFFFFCA");
confirmValue("Converts normal decimal number to hexadecimal", "100", "64");
String maxInt = Integer.toString(Integer.MAX_VALUE);
assertEquals("2147483647", maxInt);
confirmValue("Converts INT_MAX to hexadecimal", maxInt, "7FFFFFFF");
@ -95,16 +97,17 @@ public final class TestDec2Hex extends TestCase {
String maxIntPlusOne = Long.toString(((long)Integer.MAX_VALUE)+1);
assertEquals("2147483648", maxIntPlusOne);
confirmValue("Converts INT_MAX + 1 to hexadecimal", maxIntPlusOne, "80000000");
String maxLong = Long.toString(549755813887l);
assertEquals("549755813887", maxLong);
confirmValue("Converts the max supported value to hexadecimal", maxLong, "7FFFFFFFFF");
String minLong = Long.toString(-549755813888l);
assertEquals("-549755813888", minLong);
confirmValue("Converts the min supported value to hexadecimal", minLong, "FF80000000");
}
@Test
public void testErrors() {
confirmValueError("Out of range min number","-549755813889","0", ErrorEval.NUM_ERROR);
confirmValueError("Out of range max number","549755813888","0", ErrorEval.NUM_ERROR);
@ -113,9 +116,10 @@ public final class TestDec2Hex extends TestCase {
confirmValueError("non number places not allowed","ABCDEF","0", ErrorEval.VALUE_INVALID);
}
@Test
public void testEvalOperationEvaluationContextFails() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ErrorEval.VALUE_INVALID };
ValueEval result = new Dec2Hex().evaluate(args, ctx);
@ -136,7 +140,7 @@ public final class TestDec2Hex extends TestCase {
HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
@Override
public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
return true;
@ -146,9 +150,10 @@ public final class TestDec2Hex extends TestCase {
workbook, 0, 0, 0, null);
}
@Test
public void testRefs() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
@ -156,9 +161,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals("7B", ((StringEval) result).getStringValue());
}
@Test
public void testWithPlacesIntInt() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
@ -166,9 +172,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals("0000007B", ((StringEval) result).getStringValue());
}
@Test
public void testWithPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Hex().evaluate(args, ctx);
@ -176,9 +183,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals("0000007B", ((StringEval) result).getStringValue());
}
@Test
public void testWithTooManyParamsIntInt() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
@ -186,9 +194,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testWithTooManyParams() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
ValueEval result = new Dec2Hex().evaluate(args, ctx);
@ -196,9 +205,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testWithErrorPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ErrorEval.NULL_INTERSECTION };
ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
@ -206,9 +216,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals(ErrorEval.NULL_INTERSECTION, result);
}
@Test
public void testWithNegativePlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 2) };
ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
@ -216,9 +227,10 @@ public final class TestDec2Hex extends TestCase {
assertEquals(ErrorEval.NUM_ERROR, result);
}
@Test
public void testWithEmptyPlaces() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(1, 0) };
ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
@ -226,6 +238,7 @@ public final class TestDec2Hex extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testBackAndForth() {
for (int i = -512; i < 512; i++) {
ValueEval result = invokeValue(Integer.toString(i));

View File

@ -17,18 +17,20 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
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;
import org.junit.Test;
/**
* Tests for {@link org.apache.poi.ss.formula.functions.Delta}
*
* @author cedric dot walter @ gmail dot com
*/
public final class TestDelta extends TestCase {
public final class TestDelta {
private static ValueEval invokeValue(String number1, String number2) {
ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), };
@ -47,6 +49,7 @@ public final class TestDelta extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testBasic() {
confirmValue("5","4", 0); // Checks whether 5 equals 4 (0)
confirmValue("5","5", 1); // Checks whether 5 equals 5 (1)
@ -56,6 +59,7 @@ public final class TestDelta extends TestCase {
confirmValue("0.5000000000","0.5", 1);
}
@Test
public void testErrors() {
confirmValueError("A1","B2");
confirmValueError("AAAA","BBBB");

View File

@ -21,54 +21,59 @@
package org.apache.poi.ss.formula.functions;
import org.junit.Test;
/**
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*
*/
public class TestFinanceLib extends AbstractNumericTestCase {
@Test
public void testFv() {
double f, r, y, p, x;
int n;
boolean t = false;
r = 0; n = 3; y = 2; p = 7; t = true;
f = FinanceLib.fv(r, n, y, p, t);
x = -13;
assertEquals("fv ", x, f);
r = 1; n = 10; y = 100; p = 10000; t = false;
f = FinanceLib.fv(r, n, y, p, t);
x = -10342300;
assertEquals("fv ", x, f);
r = 1; n = 10; y = 100; p = 10000; t = true;
f = FinanceLib.fv(r, n, y, p, t);
x = -10444600;
assertEquals("fv ", x, f);
r = 2; n = 12; y = 120; p = 12000; t = false;
f = FinanceLib.fv(r, n, y, p, t);
x = -6409178400d;
assertEquals("fv ", x, f);
r = 2; n = 12; y = 120; p = 12000; t = true;
f = FinanceLib.fv(r, n, y, p, t);
x = -6472951200d;
assertEquals("fv ", x, f);
// cross tests with pv
r = 2.95; n = 13; y = 13000; p = -4406.78544294496; t = false;
f = FinanceLib.fv(r, n, y, p, t);
x = 333891.230010986; // as returned by excel
assertEquals("fv ", x, f);
r = 2.95; n = 13; y = 13000; p = -17406.7852148156; t = true;
f = FinanceLib.fv(r, n, y, p, t);
x = 333891.230102539; // as returned by excel
assertEquals("fv ", x, f);
}
@Test
public void testNpv() {
double r;
double[] v;
@ -79,22 +84,24 @@ public class TestFinanceLib extends AbstractNumericTestCase {
npv = FinanceLib.npv(r, v);
x = 162.5;
assertEquals("npv ", x, npv);
r = 2.5; v = new double[]{1000, 666.66666, 333.33, 12.2768416};
npv = FinanceLib.npv(r, v);
x = 347.99232604144827;
assertEquals("npv ", x, npv);
r = 12.33333; v = new double[]{1000, 0, -900, -7777.5765};
npv = FinanceLib.npv(r, v);
x = 74.3742433377061;
assertEquals("npv ", x, npv);
r = 0.05; v = new double[]{200000, 300000.55, 400000, 1000000, 6000000, 7000000, -300000};
npv = FinanceLib.npv(r, v);
x = 11342283.4233124;
assertEquals("npv ", x, npv);
}
@Test
public void testPmt() {
double f, r, y, p, x;
int n;
@ -103,31 +110,32 @@ public class TestFinanceLib extends AbstractNumericTestCase {
r = 0; n = 3; p = 2; f = 7; t = true;
y = FinanceLib.pmt(r, n, p, f, t);
x = -3;
assertEquals("pmt ", x, y);
assertEquals("pmt ", x, y);
// cross check with pv
r = 1; n = 10; p = -109.66796875; f = 10000; t = false;
y = FinanceLib.pmt(r, n, p, f, t);
x = 100;
assertEquals("pmt ", x, y);
assertEquals("pmt ", x, y);
r = 1; n = 10; p = -209.5703125; f = 10000; t = true;
y = FinanceLib.pmt(r, n, p, f, t);
x = 100;
assertEquals("pmt ", x, y);
// cross check with fv
r = 2; n = 12; f = -6409178400d; p = 12000; t = false;
y = FinanceLib.pmt(r, n, p, f, t);
x = 120;
assertEquals("pmt ", x, y);
assertEquals("pmt ", x, y);
r = 2; n = 12; f = -6472951200d; p = 12000; t = true;
y = FinanceLib.pmt(r, n, p, f, t);
x = 120;
assertEquals("pmt ", x, y);
}
@Test
public void testPv() {
double f, r, y, p, x;
int n;
@ -141,65 +149,66 @@ public class TestFinanceLib extends AbstractNumericTestCase {
r = 1; n = 10; y = 100; f = 10000; t = false;
p = FinanceLib.pv(r, n, y, f, t);
x = -109.66796875;
assertEquals("pv ", x, p);
assertEquals("pv ", x, p);
r = 1; n = 10; y = 100; f = 10000; t = true;
p = FinanceLib.pv(r, n, y, f, t);
x = -209.5703125;
assertEquals("pv ", x, p);
assertEquals("pv ", x, p);
r = 2.95; n = 13; y = 13000; f = 333891.23; t = false;
p = FinanceLib.pv(r, n, y, f, t);
x = -4406.78544294496;
assertEquals("pv ", x, p);
r = 2.95; n = 13; y = 13000; f = 333891.23; t = true;
p = FinanceLib.pv(r, n, y, f, t);
x = -17406.7852148156;
assertEquals("pv ", x, p);
// cross tests with fv
r = 2; n = 12; y = 120; f = -6409178400d; t = false;
p = FinanceLib.pv(r, n, y, f, t);
x = 12000;
assertEquals("pv ", x, p);
r = 2; n = 12; y = 120; f = -6472951200d; t = true;
p = FinanceLib.pv(r, n, y, f, t);
x = 12000;
x = 12000;
assertEquals("pv ", x, p);
}
@Test
public void testNper() {
double f, r, y, p, x, n;
boolean t = false;
r = 0; y = 7; p = 2; f = 3; t = false;
n = FinanceLib.nper(r, y, p, f, t);
x = -0.71428571429; // can you believe it? excel returns nper as a fraction!??
assertEquals("nper ", x, n);
assertEquals("nper ", x, n);
// cross check with pv
r = 1; y = 100; p = -109.66796875; f = 10000; t = false;
n = FinanceLib.nper(r, y, p, f, t);
x = 10;
assertEquals("nper ", x, n);
assertEquals("nper ", x, n);
r = 1; y = 100; p = -209.5703125; f = 10000; t = true;
n = FinanceLib.nper(r, y, p, f, t);
x = 10;
assertEquals("nper ", x, n);
// cross check with fv
r = 2; y = 120; f = -6409178400d; p = 12000; t = false;
n = FinanceLib.nper(r, y, p, f, t);
x = 12;
assertEquals("nper ", x, n);
assertEquals("nper ", x, n);
r = 2; y = 120; f = -6472951200d; p = 12000; t = true;
n = FinanceLib.nper(r, y, p, f, t);
x = 12;
x = 12;
assertEquals("nper ", x, n);
}
}

View File

@ -17,24 +17,24 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.IStabilityClassifier;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.WorkbookEvaluator;
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;
import org.junit.Test;
/**
* Tests for {@link Hex2Dec}
*
* @author cedric dot walter @ gmail dot com
*/
public final class TestHex2Dec extends TestCase {
public final class TestHex2Dec {
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[] { new StringEval(number1) };
@ -53,30 +53,34 @@ public final class TestHex2Dec extends TestCase {
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Converts hex 'A5' to decimal (165)", "A5", "165");
confirmValue("Converts hex FFFFFFFF5B to decimal (-165)", "FFFFFFFF5B", "-165");
confirmValue("Converts hex 3DA408B9 to decimal (-165)", "3DA408B9", "1034160313");
}
@Test
public void testErrors() {
confirmValueError("not a valid hex number","GGGGGGG", ErrorEval.NUM_ERROR);
confirmValueError("not a valid hex number","3.14159", ErrorEval.NUM_ERROR);
}
@Test
public void testEvalOperationEvaluationContext() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Hex2Dec().evaluate(args, ctx);
assertEquals(NumberEval.class, result.getClass());
assertEquals("0", ((NumberEval) result).getStringValue());
}
@Test
public void testEvalOperationEvaluationContextFails() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 0) };
ValueEval result = new Hex2Dec().evaluate(args, ctx);
@ -88,20 +92,15 @@ public final class TestHex2Dec extends TestCase {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet();
HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
@Override
public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
return true;
}
}, null);
WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, (sheetIndex, rowIndex, columnIndex) -> true, null);
return new OperationEvaluationContext(workbookEvaluator,
workbook, 0, 0, 0, null);
}
@Test
public void testRefs() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
ValueEval result = new Hex2Dec().evaluate(args, -1, -1);

View File

@ -19,24 +19,26 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import junit.framework.TestCase;
import org.junit.Test;
/**
* Test cases for IPMT()
*
*/
public final class TestIPMT extends TestCase {
public final class TestIPMT {
/**
* from http://office.microsoft.com/en-001/excel-help/ipmt-HP005209145.aspx
*/
@Test
public void testFromFile() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("finance.xls");

View File

@ -17,24 +17,26 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.MissingArgEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.junit.Test;
/**
* Tests for the INDEX() function.</p>
@ -46,7 +48,7 @@ import org.apache.poi.ss.util.CellReference;
*
* @author Josh Micich
*/
public final class TestIndex extends TestCase {
public final class TestIndex {
private static final Index FUNC_INST = new Index();
private static final double[] TEST_VALUES0 = {
@ -61,6 +63,7 @@ public final class TestIndex extends TestCase {
/**
* For the case when the first argument to INDEX() is an area reference
*/
@Test
public void testEvaluateAreaReference() {
double[] values = TEST_VALUES0;
@ -109,6 +112,7 @@ public final class TestIndex extends TestCase {
* Tests expressions like "INDEX(A1:C1,,2)".<br>
* This problem was found while fixing bug 47048 and is observable up to svn r773441.
*/
@Test
public void testMissingArg() {
ValueEval[] values = {
new NumberEval(25.0),
@ -138,6 +142,7 @@ public final class TestIndex extends TestCase {
* A formula like "OFFSET(INDEX(A1:B2,2,1),1,1,1,1)" should return the value of cell B3.
* This works because the INDEX() function returns a reference to A2 (not the value of A2)
*/
@Test
public void testReferenceResult() {
ValueEval[] values = new ValueEval[4];
Arrays.fill(values, NumberEval.ZERO);
@ -162,6 +167,7 @@ public final class TestIndex extends TestCase {
return ae;
}
@Test
public void test61859(){
Workbook wb = HSSFTestDataSamples.openSampleWorkbook("maxindextest.xls");
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
@ -170,41 +176,43 @@ public final class TestIndex extends TestCase {
Cell ex1cell1 = example1.getRow(1).getCell(6);
assertEquals("MAX(INDEX(($B$2:$B$11=F2)*$A$2:$A$11,0))", ex1cell1.getCellFormula());
fe.evaluate(ex1cell1);
assertEquals(4.0, ex1cell1.getNumericCellValue());
assertEquals(4.0, ex1cell1.getNumericCellValue(), 0);
Cell ex1cell2 = example1.getRow(2).getCell(6);
assertEquals("MAX(INDEX(($B$2:$B$11=F3)*$A$2:$A$11,0))", ex1cell2.getCellFormula());
fe.evaluate(ex1cell2);
assertEquals(10.0, ex1cell2.getNumericCellValue());
assertEquals(10.0, ex1cell2.getNumericCellValue(), 0);
Cell ex1cell3 = example1.getRow(3).getCell(6);
assertEquals("MAX(INDEX(($B$2:$B$11=F4)*$A$2:$A$11,0))", ex1cell3.getCellFormula());
fe.evaluate(ex1cell3);
assertEquals(20.0, ex1cell3.getNumericCellValue());
assertEquals(20.0, ex1cell3.getNumericCellValue(), 0);
}
@Test
public void test61116(){
Workbook workbook = HSSFTestDataSamples.openSampleWorkbook("61116.xls");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheet("sample2");
Row row = sheet.getRow(1);
assertEquals(3.0, evaluator.evaluate(row.getCell(1)).getNumberValue());
assertEquals(3.0, evaluator.evaluate(row.getCell(2)).getNumberValue());
assertEquals(3.0, evaluator.evaluate(row.getCell(1)).getNumberValue(), 0);
assertEquals(3.0, evaluator.evaluate(row.getCell(2)).getNumberValue(), 0);
row = sheet.getRow(2);
assertEquals(5.0, evaluator.evaluate(row.getCell(1)).getNumberValue());
assertEquals(5.0, evaluator.evaluate(row.getCell(2)).getNumberValue());
assertEquals(5.0, evaluator.evaluate(row.getCell(1)).getNumberValue(), 0);
assertEquals(5.0, evaluator.evaluate(row.getCell(2)).getNumberValue(), 0);
}
/**
* If both the Row_num and Column_num arguments are used,
* INDEX returns the value in the cell at the intersection of Row_num and Column_num
*/
@Test
public void testReference2DArea(){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
/**
/*
* 1 2 3
* 4 5 6
* 7 8 9
@ -223,17 +231,18 @@ public final class TestIndex extends TestCase {
Cell c2 = sheet.getRow(0).createCell(6);
c2.setCellFormula("INDEX(A1:C3,3,2)");
assertEquals(5.0, fe.evaluate(c1).getNumberValue());
assertEquals(8.0, fe.evaluate(c2).getNumberValue());
assertEquals(5.0, fe.evaluate(c1).getNumberValue(), 0);
assertEquals(8.0, fe.evaluate(c2).getNumberValue(), 0);
}
/**
* If Column_num is 0 (zero), INDEX returns the array of values for the entire row.
*/
@Test
public void testArrayArgument_RowLookup(){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
/**
/*
* 1 2 3
* 4 5 6
* 7 8 9
@ -253,18 +262,19 @@ public final class TestIndex extends TestCase {
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
assertEquals(6.0, fe.evaluate(c1).getNumberValue());
assertEquals(15.0, fe.evaluate(c2).getNumberValue());
assertEquals(6.0, fe.evaluate(c1).getNumberValue(), 0);
assertEquals(15.0, fe.evaluate(c2).getNumberValue(), 0);
}
/**
* If Row_num is 0 (zero), INDEX returns the array of values for the entire column.
*/
@Test
public void testArrayArgument_ColumnLookup(){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
/**
/*
* 1 2 3
* 4 5 6
* 7 8 9
@ -284,8 +294,8 @@ public final class TestIndex extends TestCase {
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
assertEquals(12.0, fe.evaluate(c1).getNumberValue());
assertEquals(18.0, fe.evaluate(c2).getNumberValue());
assertEquals(12.0, fe.evaluate(c1).getNumberValue(), 0);
assertEquals(18.0, fe.evaluate(c2).getNumberValue(), 0);
}
/**
@ -294,10 +304,11 @@ public final class TestIndex extends TestCase {
* The sum of the range starting at B1, and ending at the intersection of the 2nd row of the range B1:B3,
* which is the sum of B1:B2.
*/
@Test
public void testDynamicReference(){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
/**
/*
* 1 2 3
* 4 5 6
* 7 8 9
@ -314,6 +325,6 @@ public final class TestIndex extends TestCase {
FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
assertEquals(7.0, fe.evaluate(c1).getNumberValue());
assertEquals(7.0, fe.evaluate(c1).getNumberValue(), 0);
}
}

View File

@ -19,7 +19,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
@ -29,12 +29,14 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Test for Excel function INTERCEPT()
*
* @author Johan Karlsteen
*/
public final class TestIntercept extends TestCase {
public final class TestIntercept {
private static final Function INTERCEPT = new Intercept();
private static ValueEval invoke(Function function, ValueEval xArray, ValueEval yArray) {
@ -57,6 +59,7 @@ public final class TestIntercept extends TestCase {
confirmError(INTERCEPT, xArray, yArray, expectedError);
}
@Test
public void testBasic() {
Double exp = Math.pow(10, 7.5);
ValueEval[] yValues = {
@ -85,6 +88,7 @@ public final class TestIntercept extends TestCase {
/**
* number of items in array is not limited to 30
*/
@Test
public void testLargeArrays() {
ValueEval[] yValues = createMockNumberArray(100, 3); // [1,2,0,1,2,0,...,0,1]
yValues[0] = new NumberEval(2.0); // Changes first element to 2
@ -107,6 +111,7 @@ public final class TestIntercept extends TestCase {
return EvalFactory.createAreaEval(refStr, values);
}
@Test
public void testErrors() {
ValueEval[] xValues = {
ErrorEval.REF_INVALID,
@ -144,6 +149,7 @@ public final class TestIntercept extends TestCase {
* Example from
* http://office.microsoft.com/en-us/excel-help/intercept-function-HP010062512.aspx?CTT=5&origin=HA010277524
*/
@Test
public void testFromFile() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("intercept.xls");

View File

@ -17,6 +17,9 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
@ -25,46 +28,46 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.junit.Test;
/**
* Tests for {@link Irr}
*
* @author Marcel May
*/
public final class TestIrr extends TestCase {
public final class TestIrr {
@Test
public void testIrr() {
// http://en.wikipedia.org/wiki/Internal_rate_of_return#Example
double[] incomes = {-4000d, 1200d, 1410d, 1875d, 1050d};
double irr = Irr.irr(incomes);
double irrRounded = Math.round(irr * 1000d) / 1000d;
assertEquals("irr", 0.143d, irrRounded);
assertEquals("irr", 0.143d, irrRounded, 0);
// http://www.techonthenet.com/excel/formulas/irr.php
incomes = new double[]{-7500d, 3000d, 5000d, 1200d, 4000d};
irr = Irr.irr(incomes);
irrRounded = Math.round(irr * 100d) / 100d;
assertEquals("irr", 0.28d, irrRounded);
assertEquals("irr", 0.28d, irrRounded, 0);
incomes = new double[]{-10000d, 3400d, 6500d, 1000d};
irr = Irr.irr(incomes);
irrRounded = Math.round(irr * 100d) / 100d;
assertEquals("irr", 0.05, irrRounded);
assertEquals("irr", 0.05, irrRounded, 0);
incomes = new double[]{100d, -10d, -110d};
irr = Irr.irr(incomes);
irrRounded = Math.round(irr * 100d) / 100d;
assertEquals("irr", 0.1, irrRounded);
assertEquals("irr", 0.1, irrRounded, 0);
incomes = new double[]{-70000d, 12000, 15000};
irr = Irr.irr(incomes, -0.1);
irrRounded = Math.round(irr * 100d) / 100d;
assertEquals("irr", -0.44, irrRounded);
assertEquals("irr", -0.44, irrRounded, 0);
}
@Test
public void testEvaluateInSheet() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
@ -83,9 +86,10 @@ public final class TestIrr extends TestCase {
fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cell);
double res = cell.getNumericCellValue();
assertEquals(0.143d, Math.round(res * 1000d) / 1000d);
assertEquals(0.143d, Math.round(res * 1000d) / 1000d, 0);
}
@Test
public void testIrrFromSpreadsheet(){
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("IrrNpvTestCaseData.xls");
HSSFSheet sheet = wb.getSheet("IRR-NPV");

View File

@ -17,6 +17,8 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
@ -24,16 +26,17 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test;
import junit.framework.TestCase;
/**
* Tests for Excel function ISBLANK()
*
*
* @author Josh Micich
*/
public final class TestIsBlank extends TestCase {
public final class TestIsBlank {
public void test3DArea() {
@Test
public void test3DArea() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet();
wb.setSheetName(0, "Sheet1");
@ -42,18 +45,18 @@ public final class TestIsBlank extends TestCase {
HSSFRow row = sheet1.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellFormula("isblank(Sheet2!A1:A1)");
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
CellValue result = fe.evaluate(cell);
assertEquals(CellType.BOOLEAN, result.getCellType());
assertEquals(true, result.getBooleanValue());
cell.setCellFormula("isblank(D7:D7)");
result = fe.evaluate(cell);
assertEquals(CellType.BOOLEAN, result.getCellType());
assertEquals(true, result.getBooleanValue());
}
}
}

View File

@ -17,57 +17,46 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
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;
import junit.framework.TestCase;
import org.junit.Test;
/**
*
*
* Test cases for {@link TextFunction#LEFT} and {@link TextFunction#RIGHT}
*
*
* @author Brendan Nolan
*
*/
public class TestLeftRight extends TestCase {
public class TestLeftRight {
private static final NumberEval NEGATIVE_OPERAND = new NumberEval(-1.0);
private static final StringEval ANY_STRING_VALUE = new StringEval("ANYSTRINGVALUE");
private static ValueEval invokeLeft(ValueEval text, ValueEval operand) {
ValueEval[] args = new ValueEval[] { text, operand };
return TextFunction.LEFT.evaluate(args, -1, (short)-1);
}
private static ValueEval invokeRight(ValueEval text, ValueEval operand) {
ValueEval[] args = new ValueEval[] { text, operand };
return TextFunction.RIGHT.evaluate(args, -1, (short)-1);
}
@Test
public void testLeftRight_bug49841() {
try {
invokeLeft(ANY_STRING_VALUE, NEGATIVE_OPERAND);
invokeRight(ANY_STRING_VALUE, NEGATIVE_OPERAND);
} catch (StringIndexOutOfBoundsException e) {
fail("Identified bug 49841");
}
invokeLeft(ANY_STRING_VALUE, NEGATIVE_OPERAND);
invokeRight(ANY_STRING_VALUE, NEGATIVE_OPERAND);
}
@Test
public void testLeftRightNegativeOperand() {
assertEquals(ErrorEval.VALUE_INVALID, invokeRight(ANY_STRING_VALUE, NEGATIVE_OPERAND));
assertEquals(ErrorEval.VALUE_INVALID, invokeRight(ANY_STRING_VALUE, NEGATIVE_OPERAND));
assertEquals(ErrorEval.VALUE_INVALID, invokeLeft(ANY_STRING_VALUE, NEGATIVE_OPERAND));
}
}

View File

@ -17,7 +17,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.BoolEval;
@ -25,12 +25,14 @@ 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;
import org.junit.Test;
/**
* Tests for Excel function LEN()
*
* @author Josh Micich
*/
public final class TestLen extends TestCase {
public final class TestLen {
private static ValueEval invokeLen(ValueEval text) {
ValueEval[] args = new ValueEval[] { text, };
@ -49,14 +51,15 @@ public final class TestLen extends TestCase {
assertEquals(expectedError.getErrorCode(), ((ErrorEval)result).getErrorCode());
}
@Test
public void testBasic() {
confirmLen(new StringEval("galactic"), 8);
}
/**
* Valid cases where text arg is not exactly a string
*/
@Test
public void testUnusualArgs() {
// text (first) arg type is number, other args are strings with fractional digits
@ -66,6 +69,7 @@ public final class TestLen extends TestCase {
confirmLen(BlankEval.instance, 0);
}
@Test
public void testErrors() {
confirmLen(ErrorEval.NAME_INVALID, ErrorEval.NAME_INVALID);
}

View File

@ -17,6 +17,9 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -28,26 +31,23 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
/**
* LogicalFunction unit tests.
*/
public class TestLogicalFunction extends TestCase {
public class TestLogicalFunction {
private FormulaEvaluator evaluator;
private Row row3;
private Cell cell1;
private Cell cell2;
@Override
@Before
public void setUp() throws IOException {
Workbook wb = new HSSFWorkbook();
try {
try (Workbook wb = new HSSFWorkbook()) {
buildWorkbook(wb);
} finally {
wb.close();
}
}
@ -79,6 +79,7 @@ public class TestLogicalFunction extends TestCase {
evaluator = wb.getCreationHelper().createFormulaEvaluator();
}
@Test
public void testIsErr() {
cell1 = row3.createCell(0);
cell1.setCellFormula("ISERR(B1)"); // produces #DIV/0!
@ -88,10 +89,11 @@ public class TestLogicalFunction extends TestCase {
CellValue cell1Value = evaluator.evaluate(cell1);
CellValue cell2Value = evaluator.evaluate(cell2);
assertEquals(true, cell1Value.getBooleanValue());
assertEquals(false, cell2Value.getBooleanValue());
assertTrue(cell1Value.getBooleanValue());
assertFalse(cell2Value.getBooleanValue());
}
@Test
public void testIsError() {
cell1 = row3.createCell(0);
cell1.setCellFormula("ISERROR(B1)"); // produces #DIV/0!
@ -101,7 +103,7 @@ public class TestLogicalFunction extends TestCase {
CellValue cell1Value = evaluator.evaluate(cell1);
CellValue cell2Value = evaluator.evaluate(cell2);
assertEquals(true, cell1Value.getBooleanValue());
assertEquals(true, cell2Value.getBooleanValue());
assertTrue(cell1Value.getBooleanValue());
assertTrue(cell2Value.getBooleanValue());
}
}

View File

@ -17,6 +17,9 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
@ -24,13 +27,12 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import junit.framework.TestCase;
import org.junit.Test;
/**
* Test cases for MATCH()
*/
public final class TestMatch extends TestCase {
public final class TestMatch {
/** less than or equal to */
private static final NumberEval MATCH_LARGEST_LTE = new NumberEval(1);
private static final NumberEval MATCH_EXACT = new NumberEval(0);
@ -57,6 +59,7 @@ public final class TestMatch extends TestCase {
assertEquals(expected, nve.getNumberValue(), 0);
}
@Test
public void testSimpleNumber() {
ValueEval[] values = {
@ -78,6 +81,7 @@ public final class TestMatch extends TestCase {
assertEquals(ErrorEval.NA, invokeMatch(new NumberEval(20), ae, MATCH_EXACT));
}
@Test
public void testReversedNumber() {
ValueEval[] values = {
@ -99,6 +103,7 @@ public final class TestMatch extends TestCase {
assertEquals(ErrorEval.NA, invokeMatch(new NumberEval(26), ae, MATCH_SMALLEST_GTE));
}
@Test
public void testSimpleString() {
// Arrange
ValueEval[] values = {
@ -120,6 +125,7 @@ public final class TestMatch extends TestCase {
assertEquals(ErrorEval.NA, invokeMatch(new StringEval("Hugh"), ae, MATCH_EXACT));
}
@Test
public void testSimpleWildcardValuesString() {
// Arrange
ValueEval[] values = {
@ -149,6 +155,7 @@ public final class TestMatch extends TestCase {
confirmInt(5, invokeMatch(new StringEval("*Ian*"), ae, MATCH_LARGEST_LTE));
}
@Test
public void testTildeString() {
ValueEval[] values = {
@ -162,6 +169,7 @@ public final class TestMatch extends TestCase {
confirmInt(2, invokeMatch(new StringEval("all~*"), ae, MATCH_EXACT));
}
@Test
public void testSimpleBoolean() {
ValueEval[] values = {
@ -180,6 +188,7 @@ public final class TestMatch extends TestCase {
confirmInt(3, invokeMatch(BoolEval.TRUE, ae, MATCH_EXACT));
}
@Test
public void testHeterogeneous() {
ValueEval[] values = {
@ -232,6 +241,7 @@ public final class TestMatch extends TestCase {
* Ensures that the match_type argument can be an <tt>AreaEval</tt>.<br>
* Bugzilla 44421
*/
@Test
public void testMatchArgTypeArea() {
ValueEval[] values = {
@ -257,7 +267,8 @@ public final class TestMatch extends TestCase {
throw e;
}
}
@Test
public void testInvalidMatchType() {
ValueEval[] values = {
@ -271,8 +282,8 @@ public final class TestMatch extends TestCase {
AreaEval ae = EvalFactory.createAreaEval("A1:A5", values);
confirmInt(2, invokeMatch(new NumberEval(5), ae, MATCH_LARGEST_LTE));
assertEquals("Should return #REF! for invalid match type",
assertEquals("Should return #REF! for invalid match type",
ErrorEval.REF_INVALID, invokeMatch(new StringEval("Ben"), ae, MATCH_INVALID));
}
}

View File

@ -20,16 +20,19 @@
*/
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertTrue;
import org.apache.poi.ss.formula.functions.XYNumericFunction.Accumulator;
import org.junit.Test;
/**
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*
*
*/
@SuppressWarnings("ConstantConditions")
public class TestMathX extends AbstractNumericTestCase {
@Test
public void testAcosh() {
double d;
@ -53,6 +56,7 @@ public class TestMathX extends AbstractNumericTestCase {
}
@Test
public void testAsinh() {
double d;
@ -79,6 +83,7 @@ public class TestMathX extends AbstractNumericTestCase {
}
@Test
public void testAtanh() {
double d;
d = MathX.atanh(0);
@ -110,6 +115,7 @@ public class TestMathX extends AbstractNumericTestCase {
}
@Test
public void testCosh() {
double d;
d = MathX.cosh(0);
@ -141,6 +147,7 @@ public class TestMathX extends AbstractNumericTestCase {
}
@Test
public void testTanh() {
double d;
d = MathX.tanh(0);
@ -172,83 +179,86 @@ public class TestMathX extends AbstractNumericTestCase {
}
@Test
public void testMax() {
double[] d = new double[100];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
double m = MathX.max(d);
assertEquals("Max ", 20.1, m);
d = new double[1000];
m = MathX.max(d);
assertEquals("Max ", 0, m);
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[4] = -5.1; d[5] = 6.1; d[6] = -7.1; d[7] = 8.1;
d[8] = -9.1; d[9] = 10.1; d[10] = -11.1; d[11] = 12.1;
d[12] = -13.1; d[13] = 14.1; d[14] = -15.1; d[15] = 16.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
m = MathX.max(d);
assertEquals("Max ", 20.1, m);
d = new double[20];
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[4] = -5.1; d[5] = -6.1; d[6] = -7.1; d[7] = -8.1;
d[8] = -9.1; d[9] = -10.1; d[10] = -11.1; d[11] = -12.1;
d[12] = -13.1; d[13] = -14.1; d[14] = -15.1; d[15] = -16.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
m = MathX.max(d);
assertEquals("Max ", -1.1, m);
}
@Test
public void testMin() {
double[] d = new double[100];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
double m = MathX.min(d);
assertEquals("Min ", 0, m);
d = new double[20];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
m = MathX.min(d);
assertEquals("Min ", 1.1, m);
d = new double[1000];
m = MathX.min(d);
assertEquals("Min ", 0, m);
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[4] = -5.1; d[5] = 6.1; d[6] = -7.1; d[7] = 8.1;
d[8] = -9.1; d[9] = 10.1; d[10] = -11.1; d[11] = 12.1;
d[12] = -13.1; d[13] = 14.1; d[14] = -15.1; d[15] = 16.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
m = MathX.min(d);
assertEquals("Min ", -19.1, m);
d = new double[20];
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[4] = -5.1; d[5] = -6.1; d[6] = -7.1; d[7] = -8.1;
d[8] = -9.1; d[9] = -10.1; d[10] = -11.1; d[11] = -12.1;
d[12] = -13.1; d[13] = -14.1; d[14] = -15.1; d[15] = -16.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
m = MathX.min(d);
assertEquals("Min ", -20.1, m);
}
@Test
public void testProduct() {
assertEquals("Product ", 0, MathX.product(null));
assertEquals("Product ", 0, MathX.product(new double[] {}));
@ -259,41 +269,42 @@ public class TestMathX extends AbstractNumericTestCase {
assertEquals("Product ", 10, MathX.product(new double[] { 10, 1 }));
assertEquals("Product ", -2, MathX.product(new double[] { 2, -1 }));
assertEquals("Product ", 99988000209999d, MathX.product(new double[] { 99999, 99999, 9999 }));
double[] d = new double[100];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
double m = MathX.product(d);
assertEquals("Product ", 0, m);
d = new double[20];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
m = MathX.product(d);
assertEquals("Product ", 3459946360003355534d, m);
d = new double[1000];
m = MathX.product(d);
assertEquals("Product ", 0, m);
d = new double[20];
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[4] = -5.1; d[5] = -6.1; d[6] = -7.1; d[7] = -8.1;
d[8] = -9.1; d[9] = -10.1; d[10] = -11.1; d[11] = -12.1;
d[12] = -13.1; d[13] = -14.1; d[14] = -15.1; d[15] = -16.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
m = MathX.product(d);
assertEquals("Product ", 3459946360003355534d, m);
}
@Test
public void testMod() {
//example from Excel help
@ -315,106 +326,109 @@ public class TestMathX extends AbstractNumericTestCase {
assertEquals(1.0, MathX.mod(13, 12));
}
@Test
public void testNChooseK() {
int n=100;
int k=50;
double d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 1.00891344545564E29, d);
n = -1; k = 1;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", Double.NaN, d);
n = 1; k = -1;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", Double.NaN, d);
n = 0; k = 1;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", Double.NaN, d);
n = 1; k = 0;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 1, d);
n = 10; k = 9;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 10, d);
n = 10; k = 10;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 1, d);
n = 10; k = 1;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 10, d);
n = 1000; k = 1;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 1000, d); // awesome ;)
n = 1000; k = 2;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 499500, d); // awesome ;)
n = 13; k = 7;
d = MathX.nChooseK(n, k);
assertEquals("NChooseK ", 1716, d);
}
@Test
public void testSign() {
final short minus = -1;
final short zero = 0;
final short plus = 1;
double d;
assertEquals("Sign ", minus, MathX.sign(minus));
assertEquals("Sign ", plus, MathX.sign(plus));
assertEquals("Sign ", zero, MathX.sign(zero));
d = 0;
assertEquals("Sign ", zero, MathX.sign(d));
d = -1.000001;
assertEquals("Sign ", minus, MathX.sign(d));
d = -.000001;
assertEquals("Sign ", minus, MathX.sign(d));
d = -1E-200;
assertEquals("Sign ", minus, MathX.sign(d));
d = Double.NEGATIVE_INFINITY;
assertEquals("Sign ", minus, MathX.sign(d));
d = -200.11;
assertEquals("Sign ", minus, MathX.sign(d));
d = -2000000000000.11;
assertEquals("Sign ", minus, MathX.sign(d));
d = 1.000001;
assertEquals("Sign ", plus, MathX.sign(d));
d = .000001;
assertEquals("Sign ", plus, MathX.sign(d));
d = 1E-200;
assertEquals("Sign ", plus, MathX.sign(d));
d = Double.POSITIVE_INFINITY;
assertEquals("Sign ", plus, MathX.sign(d));
d = 200.11;
assertEquals("Sign ", plus, MathX.sign(d));
d = 2000000000000.11;
assertEquals("Sign ", plus, MathX.sign(d));
}
@Test
public void testSinh() {
double d;
d = MathX.sinh(0);
@ -446,170 +460,176 @@ public class TestMathX extends AbstractNumericTestCase {
}
@Test
public void testSum() {
double[] d = new double[100];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
double s = MathX.sum(d);
assertEquals("Sum ", 212, s);
d = new double[1000];
s = MathX.sum(d);
assertEquals("Sum ", 0, s);
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[4] = -5.1; d[5] = 6.1; d[6] = -7.1; d[7] = 8.1;
d[8] = -9.1; d[9] = 10.1; d[10] = -11.1; d[11] = 12.1;
d[12] = -13.1; d[13] = 14.1; d[14] = -15.1; d[15] = 16.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
s = MathX.sum(d);
assertEquals("Sum ", 10, s);
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[4] = -5.1; d[5] = -6.1; d[6] = -7.1; d[7] = -8.1;
d[8] = -9.1; d[9] = -10.1; d[10] = -11.1; d[11] = -12.1;
d[12] = -13.1; d[13] = -14.1; d[14] = -15.1; d[15] = -16.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
s = MathX.sum(d);
assertEquals("Sum ", -212, s);
}
@Test
public void testSumsq() {
double[] d = new double[100];
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[0] = 1.1; d[1] = 2.1; d[2] = 3.1; d[3] = 4.1;
d[4] = 5.1; d[5] = 6.1; d[6] = 7.1; d[7] = 8.1;
d[8] = 9.1; d[9] = 10.1; d[10] = 11.1; d[11] = 12.1;
d[12] = 13.1; d[13] = 14.1; d[14] = 15.1; d[15] = 16.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
d[16] = 17.1; d[17] = 18.1; d[18] = 19.1; d[19] = 20.1;
double s = MathX.sumsq(d);
assertEquals("Sumsq ", 2912.2, s);
d = new double[1000];
s = MathX.sumsq(d);
assertEquals("Sumsq ", 0, s);
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[0] = -1.1; d[1] = 2.1; d[2] = -3.1; d[3] = 4.1;
d[4] = -5.1; d[5] = 6.1; d[6] = -7.1; d[7] = 8.1;
d[8] = -9.1; d[9] = 10.1; d[10] = -11.1; d[11] = 12.1;
d[12] = -13.1; d[13] = 14.1; d[14] = -15.1; d[15] = 16.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
d[16] = -17.1; d[17] = 18.1; d[18] = -19.1; d[19] = 20.1;
s = MathX.sumsq(d);
assertEquals("Sumsq ", 2912.2, s);
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[0] = -1.1; d[1] = -2.1; d[2] = -3.1; d[3] = -4.1;
d[4] = -5.1; d[5] = -6.1; d[6] = -7.1; d[7] = -8.1;
d[8] = -9.1; d[9] = -10.1; d[10] = -11.1; d[11] = -12.1;
d[12] = -13.1; d[13] = -14.1; d[14] = -15.1; d[15] = -16.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
d[16] = -17.1; d[17] = -18.1; d[18] = -19.1; d[19] = -20.1;
s = MathX.sumsq(d);
assertEquals("Sumsq ", 2912.2, s);
}
@Test
public void testFactorial() {
int n;
double s;
n = 0;
s = MathX.factorial(n);
assertEquals("Factorial ", 1, s);
n = 1;
s = MathX.factorial(n);
assertEquals("Factorial ", 1, s);
n = 10;
s = MathX.factorial(n);
assertEquals("Factorial ", 3628800, s);
n = 99;
s = MathX.factorial(n);
assertEquals("Factorial ", 9.33262154439E+155, s);
n = -1;
s = MathX.factorial(n);
assertEquals("Factorial ", Double.NaN, s);
n = Integer.MAX_VALUE;
s = MathX.factorial(n);
assertEquals("Factorial ", Double.POSITIVE_INFINITY, s);
}
@Test
public void testSumx2my2() {
double[] xarr;
double[] yarr;
xarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
yarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
confirmSumx2my2(xarr, yarr, 100);
xarr = new double[]{-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
yarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
confirmSumx2my2(xarr, yarr, 100);
xarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
yarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
confirmSumx2my2(xarr, yarr, -100);
xarr = new double[]{10};
yarr = new double[]{9};
confirmSumx2my2(xarr, yarr, 19);
xarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
yarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
confirmSumx2my2(xarr, yarr, 0);
}
@Test
public void testSumx2py2() {
double[] xarr;
double[] yarr;
xarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
yarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
confirmSumx2py2(xarr, yarr, 670);
xarr = new double[]{-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
yarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
confirmSumx2py2(xarr, yarr, 670);
xarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
yarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
confirmSumx2py2(xarr, yarr, 670);
xarr = new double[]{10};
yarr = new double[]{9};
confirmSumx2py2(xarr, yarr, 181);
xarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
yarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
confirmSumx2py2(xarr, yarr, 770);
}
@Test
public void testSumxmy2() {
double[] xarr;
double[] yarr;
xarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
yarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
confirmSumxmy2(xarr, yarr, 10);
xarr = new double[]{-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
yarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
confirmSumxmy2(xarr, yarr, 1330);
xarr = new double[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
yarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
confirmSumxmy2(xarr, yarr, 10);
xarr = new double[]{10};
yarr = new double[]{9};
confirmSumxmy2(xarr, yarr, 1);
xarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
yarr = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
confirmSumxmy2(xarr, yarr, 0);
@ -631,64 +651,65 @@ public class TestMathX extends AbstractNumericTestCase {
for (int i = 0; i < xarr.length; i++) {
result += acc.accumulate(xarr[i], yarr[i]);
}
assertEquals(expectedResult, result, 0.0);
assertEquals(expectedResult, result);
}
@Test
public void testRound() {
double d;
int p;
d = 0; p = 0;
assertEquals("round ", 0, MathX.round(d, p));
d = 10; p = 0;
assertEquals("round ", 10, MathX.round(d, p));
d = 123.23; p = 0;
assertEquals("round ", 123, MathX.round(d, p));
d = -123.23; p = 0;
assertEquals("round ", -123, MathX.round(d, p));
d = 123.12; p = 2;
assertEquals("round ", 123.12, MathX.round(d, p));
d = 88.123459; p = 5;
assertEquals("round ", 88.12346, MathX.round(d, p));
d = 0; p = 2;
assertEquals("round ", 0, MathX.round(d, p));
d = 0; p = -1;
assertEquals("round ", 0, MathX.round(d, p));
d = 0.01; p = -1;
assertEquals("round ", 0, MathX.round(d, p));
d = 123.12; p = -2;
assertEquals("round ", 100, MathX.round(d, p));
d = 88.123459; p = -3;
assertEquals("round ", 0, MathX.round(d, p));
d = 49.00000001; p = -1;
assertEquals("round ", 50, MathX.round(d, p));
d = 149.999999; p = -2;
assertEquals("round ", 100, MathX.round(d, p));
d = 150.0; p = -2;
assertEquals("round ", 200, MathX.round(d, p));
d = 2162.615d; p = 2;
assertEquals("round ", 2162.62d, MathX.round(d, p));
d = 0.049999999999999975d; p = 2;
assertEquals("round ", 0.05d, MathX.round(d, p));
d = 0.049999999999999975d; p = 1;
assertEquals("round ", 0.1d, MathX.round(d, p));
d = Double.NaN; p = 1;
assertEquals("round ", Double.NaN, MathX.round(d, p));
@ -705,58 +726,59 @@ public class TestMathX extends AbstractNumericTestCase {
assertEquals("round ", 0.0d, MathX.round(d, p));
}
@Test
public void testRoundDown() {
double d;
int p;
d = 0; p = 0;
assertEquals("roundDown ", 0, MathX.roundDown(d, p));
d = 10; p = 0;
assertEquals("roundDown ", 10, MathX.roundDown(d, p));
d = 123.99; p = 0;
assertEquals("roundDown ", 123, MathX.roundDown(d, p));
d = -123.99; p = 0;
assertEquals("roundDown ", -123, MathX.roundDown(d, p));
d = 123.99; p = 2;
assertEquals("roundDown ", 123.99, MathX.roundDown(d, p));
d = 88.123459; p = 5;
assertEquals("roundDown ", 88.12345, MathX.roundDown(d, p));
d = 0; p = 2;
assertEquals("roundDown ", 0, MathX.roundDown(d, p));
d = 0; p = -1;
assertEquals("roundDown ", 0, MathX.roundDown(d, p));
d = 0.01; p = -1;
assertEquals("roundDown ", 0, MathX.roundDown(d, p));
d = 199.12; p = -2;
assertEquals("roundDown ", 100, MathX.roundDown(d, p));
d = 88.123459; p = -3;
assertEquals("roundDown ", 0, MathX.roundDown(d, p));
d = 99.00000001; p = -1;
assertEquals("roundDown ", 90, MathX.roundDown(d, p));
d = 100.00001; p = -2;
assertEquals("roundDown ", 100, MathX.roundDown(d, p));
d = 150.0; p = -2;
assertEquals("roundDown ", 100, MathX.roundDown(d, p));
d = 0.0499999999999975d; p = 2;
assertEquals("roundDown ", 0.04d, MathX.roundDown(d, p));
d = 0.049999999999999975d; p = 1;
assertEquals("roundDown ", 0.0d, MathX.roundDown(d, p));
d = Double.NaN; p = 1;
assertEquals("roundDown ", Double.NaN, MathX.roundDown(d, p));
@ -776,58 +798,59 @@ public class TestMathX extends AbstractNumericTestCase {
assertEquals("roundDown ", 797.40, MathX.round(d, p));
}
@Test
public void testRoundUp() {
double d;
int p;
d = 0; p = 0;
assertEquals("roundUp ", 0, MathX.roundUp(d, p));
d = 10; p = 0;
assertEquals("roundUp ", 10, MathX.roundUp(d, p));
d = 123.23; p = 0;
assertEquals("roundUp ", 124, MathX.roundUp(d, p));
d = -123.23; p = 0;
assertEquals("roundUp ", -124, MathX.roundUp(d, p));
d = 123.12; p = 2;
assertEquals("roundUp ", 123.12, MathX.roundUp(d, p));
d = 88.123459; p = 5;
assertEquals("roundUp ", 88.12346, MathX.roundUp(d, p));
d = 0; p = 2;
assertEquals("roundUp ", 0, MathX.roundUp(d, p));
d = 0; p = -1;
assertEquals("roundUp ", 0, MathX.roundUp(d, p));
d = 0.01; p = -1;
assertEquals("roundUp ", 10, MathX.roundUp(d, p));
d = 123.12; p = -2;
assertEquals("roundUp ", 200, MathX.roundUp(d, p));
d = 88.123459; p = -3;
assertEquals("roundUp ", 1000, MathX.roundUp(d, p));
d = 49.00000001; p = -1;
assertEquals("roundUp ", 50, MathX.roundUp(d, p));
d = 149.999999; p = -2;
assertEquals("roundUp ", 200, MathX.roundUp(d, p));
d = 150.0; p = -2;
assertEquals("roundUp ", 200, MathX.roundUp(d, p));
d = 0.049999999999999975d; p = 2;
assertEquals("roundUp ", 0.05d, MathX.roundUp(d, p));
d = 0.049999999999999975d; p = 1;
assertEquals("roundUp ", 0.1d, MathX.roundUp(d, p));
d = Double.NaN; p = 1;
assertEquals("roundUp ", Double.NaN, MathX.roundUp(d, p));
@ -852,55 +875,56 @@ public class TestMathX extends AbstractNumericTestCase {
assertEquals("roundUp ", 797.40, MathX.roundUp(d, p));
}
@Test
public void testCeiling() {
double d;
double s;
d = 0; s = 0;
assertEquals("ceiling ", 0, MathX.ceiling(d, s));
d = 1; s = 0;
assertEquals("ceiling ", 0, MathX.ceiling(d, s));
d = 0; s = 1;
assertEquals("ceiling ", 0, MathX.ceiling(d, s));
d = -1; s = 0;
assertEquals("ceiling ", 0, MathX.ceiling(d, s));
d = 0; s = -1;
assertEquals("ceiling ", 0, MathX.ceiling(d, s));
d = 10; s = 1.11;
assertEquals("ceiling ", 11.1, MathX.ceiling(d, s));
d = 11.12333; s = 0.03499;
assertEquals("ceiling ", 11.12682, MathX.ceiling(d, s));
d = -11.12333; s = 0.03499;
assertEquals("ceiling ", Double.NaN, MathX.ceiling(d, s));
d = 11.12333; s = -0.03499;
assertEquals("ceiling ", Double.NaN, MathX.ceiling(d, s));
d = -11.12333; s = -0.03499;
assertEquals("ceiling ", -11.12682, MathX.ceiling(d, s));
d = 100; s = 0.001;
assertEquals("ceiling ", 100, MathX.ceiling(d, s));
d = -0.001; s = -9.99;
assertEquals("ceiling ", -9.99, MathX.ceiling(d, s));
d = 4.42; s = 0.05;
assertEquals("ceiling ", 4.45, MathX.ceiling(d, s));
d = 0.05; s = 4.42;
assertEquals("ceiling ", 4.42, MathX.ceiling(d, s));
d = 0.6666; s = 3.33;
assertEquals("ceiling ", 3.33, MathX.ceiling(d, s));
d = 2d/3; s = 3.33;
assertEquals("ceiling ", 3.33, MathX.ceiling(d, s));
@ -937,55 +961,56 @@ public class TestMathX extends AbstractNumericTestCase {
assertEquals("ceiling ", -11.09183, MathX.ceiling(d, s));
}
@Test
public void testFloor() {
double d;
double s;
d = 0; s = 0;
assertEquals("floor ", 0, MathX.floor(d, s));
d = 1; s = 0;
assertEquals("floor ", Double.NaN, MathX.floor(d, s));
d = 0; s = 1;
assertEquals("floor ", 0, MathX.floor(d, s));
d = -1; s = 0;
assertEquals("floor ", Double.NaN, MathX.floor(d, s));
d = 0; s = -1;
assertEquals("floor ", 0, MathX.floor(d, s));
d = 10; s = 1.11;
assertEquals("floor ", 9.99, MathX.floor(d, s));
d = 11.12333; s = 0.03499;
assertEquals("floor ", 11.09183, MathX.floor(d, s));
d = -11.12333; s = 0.03499;
assertEquals("floor ", Double.NaN, MathX.floor(d, s));
d = 11.12333; s = -0.03499;
assertEquals("floor ", Double.NaN, MathX.floor(d, s));
d = -11.12333; s = -0.03499;
assertEquals("floor ", -11.09183, MathX.floor(d, s));
d = 100; s = 0.001;
assertEquals("floor ", 100, MathX.floor(d, s));
d = -0.001; s = -9.99;
assertEquals("floor ", 0, MathX.floor(d, s));
d = 4.42; s = 0.05;
assertEquals("floor ", 4.4, MathX.floor(d, s));
d = 0.05; s = 4.42;
assertEquals("floor ", 0, MathX.floor(d, s));
d = 0.6666; s = 3.33;
assertEquals("floor ", 0, MathX.floor(d, s));
d = 2d/3; s = 3.33;
assertEquals("floor ", 0, MathX.floor(d, s));

View File

@ -17,7 +17,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.BlankEval;
@ -27,12 +27,14 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.RefEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for Excel function MID()
*
* @author Josh Micich
*/
public final class TestMid extends TestCase {
public final class TestMid {
private static ValueEval invokeMid(ValueEval text, ValueEval startPos, ValueEval numChars) {
@ -52,6 +54,7 @@ public final class TestMid extends TestCase {
assertEquals(expectedError.getErrorCode(), ((ErrorEval)result).getErrorCode());
}
@Test
public void testBasic() {
confirmMid(new StringEval("galactic"), new NumberEval(3), new NumberEval(4), "lact");
@ -60,6 +63,7 @@ public final class TestMid extends TestCase {
/**
* Valid cases where args are not precisely (string, int, int) but can be resolved OK.
*/
@Test
public void testUnusualArgs() {
// startPos with fractional digits
confirmMid(new StringEval("galactic"), new NumberEval(3.1), new NumberEval(4), "lact");
@ -86,6 +90,7 @@ public final class TestMid extends TestCase {
/**
* Extreme values for startPos and numChars
*/
@Test
public void testExtremes() {
confirmMid(new StringEval("galactic"), new NumberEval(4), new NumberEval(400), "actic");
@ -96,6 +101,7 @@ public final class TestMid extends TestCase {
/**
* All sorts of ways to make MID return defined errors.
*/
@Test
public void testErrors() {
confirmMid(ErrorEval.NAME_INVALID, new NumberEval(3), new NumberEval(4), ErrorEval.NAME_INVALID);
confirmMid(new StringEval("galactic"), ErrorEval.NAME_INVALID, new NumberEval(4), ErrorEval.NAME_INVALID);

View File

@ -17,6 +17,9 @@
package org.apache.poi.ss.formula.functions;
import static org.junit.Assert.assertEquals;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
@ -27,19 +30,18 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.junit.Test;
/**
* Tests for {@link org.apache.poi.ss.formula.functions.Mirr}
*
* @author Carlos Delgado (carlos dot del dot est at gmail dot com)
* @author Cedric Walter (cedric dot walter at gmail dot com)
* @see {@link org.apache.poi.ss.formula.functions.TestIrr}
* @see org.apache.poi.ss.formula.functions.TestIrr
*/
public final class TestMirr extends TestCase {
public final class TestMirr {
@Test
public void testMirr() {
Mirr mirr = new Mirr();
double mirrValue;
@ -86,6 +88,7 @@ public final class TestMirr extends TestCase {
}
@Test
public void testMirrErrors_expectDIV0() {
Mirr mirr = new Mirr();
@ -101,7 +104,7 @@ public final class TestMirr extends TestCase {
throw new AssertionFailedError("MIRR should failed with all these positives values");
}
@Test
public void testEvaluateInSheet() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
@ -126,6 +129,7 @@ public final class TestMirr extends TestCase {
assertEquals(0.18736225093, res, 0.00000001);
}
@Test
public void testMirrFromSpreadsheet() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("mirrTest.xls");
HSSFSheet sheet = wb.getSheet("Mirr");

View File

@ -17,11 +17,17 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellValue;
import org.junit.Test;
/**
* Tests for {@link Npv}
@ -29,8 +35,9 @@ import org.apache.poi.ss.usermodel.CellValue;
* @author Marcel May
* @see <a href="http://office.microsoft.com/en-us/excel-help/npv-HP005209199.aspx">Excel Help</a>
*/
public final class TestNpv extends TestCase {
public final class TestNpv {
@Test
public void testEvaluateInSheetExample2() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
@ -52,7 +59,7 @@ public final class TestNpv extends TestCase {
fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cell);
double res = cell.getNumericCellValue();
assertEquals(1922.06d, Math.round(res * 100d) / 100d);
assertEquals(1922.06d, Math.round(res * 100d) / 100d, 0);
// Range
cell.setCellFormula("NPV(A2, A4:A8)+A3");
@ -60,13 +67,14 @@ public final class TestNpv extends TestCase {
fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cell);
res = cell.getNumericCellValue();
assertEquals(1922.06d, Math.round(res * 100d) / 100d);
assertEquals(1922.06d, Math.round(res * 100d) / 100d, 0);
}
/**
* evaluate formulas with NPV and compare the result with
* the cached formula result pre-calculated by Excel
*/
@Test
public void testNpvFromSpreadsheet(){
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("IrrNpvTestCaseData.xls");
HSSFSheet sheet = wb.getSheet("IRR-NPV");

View File

@ -17,18 +17,20 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
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;
import org.junit.Test;
/**
* Tests for {@link org.apache.poi.ss.formula.functions.Oct2Dec}
*
* @author cedric dot walter @ gmail dot com
*/
public final class TestOct2Dec extends TestCase {
public final class TestOct2Dec {
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[] { new StringEval(number1) };
@ -47,6 +49,7 @@ public final class TestOct2Dec extends TestCase {
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Converts octal '' to decimal (0)", "", "0");
confirmValue("Converts octal 54 to decimal (44)", "54", "44");
@ -55,6 +58,7 @@ public final class TestOct2Dec extends TestCase {
confirmValue("Converts octal 7776667533 to decimal (-299173)", "7776667533", "-299173");
}
@Test
public void testErrors() {
confirmValueError("not a valid octal number","ABCDEFGH", ErrorEval.NUM_ERROR);
confirmValueError("not a valid octal number","99999999", ErrorEval.NUM_ERROR);

View File

@ -17,10 +17,12 @@
package org.apache.poi.ss.formula.functions;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.eval.EvaluationException;
@ -29,19 +31,20 @@ import org.apache.poi.ss.formula.functions.Offset.LinearOffsetRange;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
/**
* Tests for OFFSET function implementation
*
* @author Josh Micich
*/
public final class TestOffset extends TestCase {
public final class TestOffset {
private static void confirmDoubleConvert(double doubleVal, int expected) {
try {
assertEquals(expected, Offset.evaluateIntArg(new NumberEval(doubleVal), -1, -1));
} catch (EvaluationException e) {
throw new AssertionFailedError("Unexpected error '" + e.getErrorEval() + "'.");
fail("Unexpected error '" + e.getErrorEval() + "'.");
}
}
/**
@ -50,6 +53,7 @@ public final class TestOffset extends TestCase {
* Fractional values are silently truncated.
* Truncation is toward negative infinity.
*/
@Test
public void testDoubleConversion() {
confirmDoubleConvert(100.09, 100);
@ -74,6 +78,7 @@ public final class TestOffset extends TestCase {
confirmDoubleConvert(-2.01, -3);
}
@Test
public void testLinearOffsetRange() {
LinearOffsetRange lor;
@ -103,6 +108,7 @@ public final class TestOffset extends TestCase {
assertFalse(lor.isOutOfBounds(0, 65535));
}
@Test
public void testOffsetWithEmpty23Arguments() throws IOException {
try (Workbook workbook = new HSSFWorkbook()) {
Cell cell = workbook.createSheet().createRow(0).createCell(0);

View File

@ -19,23 +19,26 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
/**
* Test cases for PPMT()
*
*/
public final class TestPPMT extends TestCase {
public final class TestPPMT {
/**
* http://office.microsoft.com/en-001/excel-help/ppmt-function-HP010342774.aspx
*/
@Test
public void testFromFile() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("finance.xls");

View File

@ -17,18 +17,19 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for Excel function POISSON(x,mean,cumulative)
* @author Kalpesh Parmar
*/
public class TestPoisson extends TestCase {
public class TestPoisson {
private static final double DELTA = 1E-15;
@ -43,6 +44,7 @@ public class TestPoisson extends TestCase {
return NumericFunction.POISSON.evaluate(valueEvals,-1,-1);
}
@Test
public void testCumulativeProbability()
{
double x = 1;
@ -54,17 +56,19 @@ public class TestPoisson extends TestCase {
assertEquals(myResult.getNumberValue(), result, DELTA);
}
@Test
public void testNonCumulativeProbability()
{
double x = 0;
double mean = 0.2;
double result = 0.8187307530779818; // known result
NumberEval myResult = (NumberEval)invokePoisson(x,mean,false);
assertEquals(myResult.getNumberValue(), result, DELTA);
}
@Test
public void testNegativeMean()
{
double x = 0;
@ -75,6 +79,7 @@ public class TestPoisson extends TestCase {
assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), myResult.getErrorCode());
}
@Test
public void testNegativeX()
{
double x = -1;
@ -82,11 +87,10 @@ public class TestPoisson extends TestCase {
ErrorEval myResult = (ErrorEval)invokePoisson(x,mean,false);
assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), myResult.getErrorCode());
assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), myResult.getErrorCode());
}
@Test
public void testXAsDecimalNumber()
{
double x = 1.1;
@ -98,6 +102,7 @@ public class TestPoisson extends TestCase {
assertEquals(myResult.getNumberValue(), result, DELTA);
}
@Test
public void testXZeroMeanZero()
{
double x = 0;

View File

@ -16,18 +16,20 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
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;
import org.junit.Test;
/**
* Tests for {@link Quotient}
*
* @author cedric dot walter @ gmail dot com
*/
public class TestQuotient extends TestCase {
public class TestQuotient {
private static ValueEval invokeValue(String numerator, String denominator) {
ValueEval[] args = new ValueEval[]{new StringEval(numerator), new StringEval(denominator)};
return new Quotient().evaluate(args, -1, -1);
@ -45,6 +47,7 @@ public class TestQuotient extends TestCase {
assertEquals(msg, numError, result);
}
@Test
public void testBasic() {
confirmValue("Integer portion of 5/2 (2)", "5", "2", "2");
confirmValue("Integer portion of 4.5/3.1 (1)", "4.5", "3.1", "1");
@ -55,6 +58,7 @@ public class TestQuotient extends TestCase {
confirmValue("Integer portion of Pi/Avogadro (0)", "3.14159", "6.02214179E+23", "0");
}
@Test
public void testErrors() {
confirmValueError("numerator is nonnumeric", "ABCD", "", ErrorEval.VALUE_INVALID);
confirmValueError("denominator is nonnumeric", "", "ABCD", ErrorEval.VALUE_INVALID);

View File

@ -19,19 +19,22 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellReference;
import org.junit.Test;
/**
* Test cases for RANK()
*/
public final class TestRank extends TestCase {
public final class TestRank {
@Test
public void testFromFile() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("rank.xls");
@ -39,30 +42,30 @@ public final class TestRank extends TestCase {
HSSFSheet example1 = wb.getSheet("Example 1");
HSSFCell ex1cell1 = example1.getRow(7).getCell(0);
assertEquals(3.0, fe.evaluate(ex1cell1).getNumberValue());
assertEquals(3.0, fe.evaluate(ex1cell1).getNumberValue(), 0);
HSSFCell ex1cell2 = example1.getRow(8).getCell(0);
assertEquals(5.0, fe.evaluate(ex1cell2).getNumberValue());
assertEquals(5.0, fe.evaluate(ex1cell2).getNumberValue(), 0);
HSSFSheet example2 = wb.getSheet("Example 2");
for(int rownum = 1; rownum<= 10; rownum ++){
HSSFCell cell = example2.getRow(rownum).getCell(2);
double cachedResult = cell.getNumericCellValue(); //cached formula result
assertEquals(cachedResult, fe.evaluate(cell).getNumberValue());
assertEquals(cachedResult, fe.evaluate(cell).getNumberValue(), 0);
}
HSSFSheet example3 = wb.getSheet("Example 3");
for(int rownum = 1; rownum<= 10; rownum ++){
HSSFCell cellD = example3.getRow(rownum).getCell(3);
double cachedResultD = cellD.getNumericCellValue(); //cached formula result
assertEquals(new CellReference(cellD).formatAsString(), cachedResultD, fe.evaluate(cellD).getNumberValue());
assertEquals(new CellReference(cellD).formatAsString(), cachedResultD, fe.evaluate(cellD).getNumberValue(), 0);
HSSFCell cellE = example3.getRow(rownum).getCell(4);
double cachedResultE = cellE.getNumericCellValue(); //cached formula result
assertEquals(new CellReference(cellE).formatAsString(), cachedResultE, fe.evaluate(cellE).getNumberValue());
assertEquals(new CellReference(cellE).formatAsString(), cachedResultE, fe.evaluate(cellE).getNumberValue(), 0);
HSSFCell cellF = example3.getRow(rownum).getCell(5);
double cachedResultF = cellF.getNumericCellValue(); //cached formula result
assertEquals(new CellReference(cellF).formatAsString(), cachedResultF, fe.evaluate(cellF).getNumberValue());
assertEquals(new CellReference(cellF).formatAsString(), cachedResultF, fe.evaluate(cellF).getNumberValue(), 0);
}
}
}

View File

@ -16,19 +16,18 @@
==================================================================== */
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.CacheAreaEval;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.usermodel.CellValue;
import static org.junit.Assert.assertEquals;
public class TestRelationalOperations extends TestCase {
import org.apache.poi.ss.formula.CacheAreaEval;
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.RelationalOperationEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
public class TestRelationalOperations {
/**
* (1, 1)(1, 1) = 1
@ -38,6 +37,7 @@ public class TestRelationalOperations extends TestCase {
* (TRUE, TRUE)(TRUE, TRUE)
*
*/
@Test
public void testEqMatrixByScalar_Numbers() {
ValueEval[] values = new ValueEval[4];
for (int i = 0; i < values.length; i++) {
@ -61,6 +61,7 @@ public class TestRelationalOperations extends TestCase {
}
}
@Test
public void testEqMatrixByScalar_String() {
ValueEval[] values = new ValueEval[4];
for (int i = 0; i < values.length; i++) {
@ -83,6 +84,7 @@ public class TestRelationalOperations extends TestCase {
}
}
@Test
public void testEqMatrixBy_Row() {
ValueEval[] matrix = {
new NumberEval(-1), new NumberEval(1),
@ -116,6 +118,7 @@ public class TestRelationalOperations extends TestCase {
}
}
@Test
public void testEqMatrixBy_Column() {
ValueEval[] matrix = {
new NumberEval(-1), new NumberEval(1),
@ -152,6 +155,7 @@ public class TestRelationalOperations extends TestCase {
}
}
@Test
public void testEqMatrixBy_Matrix() {
// A1:B2
ValueEval[] matrix1 = {

View File

@ -17,17 +17,19 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for ROW(), ROWS(), COLUMN(), COLUMNS()
*
* @author Josh Micich
*/
public final class TestRowCol extends TestCase {
public final class TestRowCol {
@Test
public void testCol() {
Function target = new Column();
{
@ -42,6 +44,7 @@ public final class TestRowCol extends TestCase {
}
}
@Test
public void testRow() {
Function target = new RowFunc();
{
@ -56,6 +59,7 @@ public final class TestRowCol extends TestCase {
}
}
@Test
public void testColumns() {
confirmColumnsFunc("A1:F1", 6, 1);
@ -68,6 +72,7 @@ public final class TestRowCol extends TestCase {
assertEquals(1, actual, 0D);
}
@Test
public void testRows() {
confirmRowsFunc("A1:F1", 6, 1);

View File

@ -19,17 +19,19 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Test for Excel function SLOPE()
*
* @author Johan Karlsteen
*/
public final class TestSlope extends TestCase {
public final class TestSlope {
private static final Function SLOPE = new Slope();
private static ValueEval invoke(Function function, ValueEval xArray, ValueEval yArray) {
@ -52,6 +54,7 @@ public final class TestSlope extends TestCase {
confirmError(SLOPE, xArray, yArray, expectedError);
}
@Test
public void testBasic() {
Double exp = Math.pow(10, 7.5);
ValueEval[] yValues = {
@ -80,6 +83,7 @@ public final class TestSlope extends TestCase {
/**
* number of items in array is not limited to 30
*/
@Test
public void testLargeArrays() {
ValueEval[] yValues = createMockNumberArray(100, 3); // [1,2,0,1,2,0,...,0,1]
yValues[0] = new NumberEval(2.0); // Changes first element to 2
@ -102,6 +106,7 @@ public final class TestSlope extends TestCase {
return EvalFactory.createAreaEval(refStr, values);
}
@Test
public void testErrors() {
ValueEval[] xValues = {
ErrorEval.REF_INVALID,

View File

@ -21,9 +21,10 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.AssertionFailedError;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.junit.Assert;
import org.junit.Test;
/**
@ -32,70 +33,72 @@ import org.apache.poi.ss.formula.eval.EvaluationException;
*/
public class TestStatsLib extends AbstractNumericTestCase {
@Test
public void testDevsq() {
double[] v = null;
double d, x = 0;
v = new double[] {1,2,3,4,5,6,7,8,9,10};
d = StatsLib.devsq(v);
x = 82.5;
assertEquals("devsq ", x, d);
v = new double[] {1,1,1,1,1,1,1,1,1,1};
d = StatsLib.devsq(v);
x = 0;
assertEquals("devsq ", x, d);
v = new double[] {0,0,0,0,0,0,0,0,0,0};
d = StatsLib.devsq(v);
x = 0;
assertEquals("devsq ", x, d);
v = new double[] {1,2,1,2,1,2,1,2,1,2};
d = StatsLib.devsq(v);
x = 2.5;
assertEquals("devsq ", x, d);
v = new double[] {123.12,33.3333,2d/3d,5.37828,0.999};
d = StatsLib.devsq(v);
x = 10953.7416965767;
assertEquals("devsq ", x, d);
v = new double[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
d = StatsLib.devsq(v);
x = 82.5;
assertEquals("devsq ", x, d);
}
@Test
public void testKthLargest() {
double[] v = null;
double d, x = 0;
v = new double[] {1,2,3,4,5,6,7,8,9,10};
d = StatsLib.kthLargest(v, 3);
x = 8;
assertEquals("kthLargest ", x, d);
v = new double[] {1,1,1,1,1,1,1,1,1,1};
d = StatsLib.kthLargest(v, 3);
x = 1;
assertEquals("kthLargest ", x, d);
v = new double[] {0,0,0,0,0,0,0,0,0,0};
d = StatsLib.kthLargest(v, 3);
x = 0;
assertEquals("kthLargest ", x, d);
v = new double[] {1,2,1,2,1,2,1,2,1,2};
d = StatsLib.kthLargest(v, 3);
x = 2;
assertEquals("kthLargest ", x, d);
v = new double[] {123.12,33.3333,2d/3d,5.37828,0.999};
d = StatsLib.kthLargest(v, 3);
x = 5.37828;
assertEquals("kthLargest ", x, d);
v = new double[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
d = StatsLib.kthLargest(v, 3);
x = -3;
@ -105,117 +108,122 @@ public class TestStatsLib extends AbstractNumericTestCase {
public void testKthSmallest() {
}
@Test
public void testAvedev() {
double[] v = null;
double d, x = 0;
v = new double[] {1,2,3,4,5,6,7,8,9,10};
d = StatsLib.avedev(v);
x = 2.5;
assertEquals("avedev ", x, d);
v = new double[] {1,1,1,1,1,1,1,1,1,1};
d = StatsLib.avedev(v);
x = 0;
assertEquals("avedev ", x, d);
v = new double[] {0,0,0,0,0,0,0,0,0,0};
d = StatsLib.avedev(v);
x = 0;
assertEquals("avedev ", x, d);
v = new double[] {1,2,1,2,1,2,1,2,1,2};
d = StatsLib.avedev(v);
x = 0.5;
assertEquals("avedev ", x, d);
v = new double[] {123.12,33.3333,2d/3d,5.37828,0.999};
d = StatsLib.avedev(v);
x = 36.42176053333;
assertEquals("avedev ", x, d);
v = new double[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
d = StatsLib.avedev(v);
x = 2.5;
assertEquals("avedev ", x, d);
}
@Test
public void testMedian() {
double[] v = null;
double d, x = 0;
v = new double[] {1,2,3,4,5,6,7,8,9,10};
d = StatsLib.median(v);
x = 5.5;
assertEquals("median ", x, d);
v = new double[] {1,1,1,1,1,1,1,1,1,1};
d = StatsLib.median(v);
x = 1;
assertEquals("median ", x, d);
v = new double[] {0,0,0,0,0,0,0,0,0,0};
d = StatsLib.median(v);
x = 0;
assertEquals("median ", x, d);
v = new double[] {1,2,1,2,1,2,1,2,1,2};
d = StatsLib.median(v);
x = 1.5;
assertEquals("median ", x, d);
v = new double[] {123.12,33.3333,2d/3d,5.37828,0.999};
d = StatsLib.median(v);
x = 5.37828;
assertEquals("median ", x, d);
v = new double[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
d = StatsLib.median(v);
x = -5.5;
assertEquals("median ", x, d);
v = new double[] {-2,-3,-4,-5,-6,-7,-8,-9,-10};
d = StatsLib.median(v);
x = -6;
assertEquals("median ", x, d);
v = new double[] {1,2,3,4,5,6,7,8,9};
d = StatsLib.median(v);
x = 5;
assertEquals("median ", x, d);
}
@Test
public void testMode() {
double[] v;
double d, x = 0;
v = new double[] {1,2,3,4,5,6,7,8,9,10};
confirmMode(v, null);
v = new double[] {1,1,1,1,1,1,1,1,1,1};
confirmMode(v, 1.0);
v = new double[] {0,0,0,0,0,0,0,0,0,0};
confirmMode(v, 0.0);
v = new double[] {1,2,1,2,1,2,1,2,1,2};
confirmMode(v, 1.0);
v = new double[] {123.12,33.3333,2d/3d,5.37828,0.999};
confirmMode(v, null);
v = new double[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
confirmMode(v, null);
v = new double[] {1,2,3,4,1,1,1,1,0,0,0,0,0};
confirmMode(v, 1.0);
v = new double[] {0,1,2,3,4,1,1,1,0,0,0,0,1};
confirmMode(v, 0.0);
}
private static void confirmMode(double[] v, double expectedResult) {
confirmMode(v, new Double(expectedResult));
}
private static void confirmMode(double[] v, Double expectedResult) {
double actual;
try {
@ -225,50 +233,51 @@ public class TestStatsLib extends AbstractNumericTestCase {
}
} catch (EvaluationException e) {
if (expectedResult == null) {
assertEquals(ErrorEval.NA, e.getErrorEval());
Assert.assertEquals(ErrorEval.NA, e.getErrorEval());
return;
}
throw new RuntimeException(e);
}
assertEquals("mode", expectedResult.doubleValue(), actual);
}
@Test
public void testStddev() {
double[] v = null;
double d, x = 0;
v = new double[] {1,2,3,4,5,6,7,8,9,10};
d = StatsLib.stdev(v);
x = 3.02765035410;
assertEquals("stdev ", x, d);
v = new double[] {1,1,1,1,1,1,1,1,1,1};
d = StatsLib.stdev(v);
x = 0;
assertEquals("stdev ", x, d);
v = new double[] {0,0,0,0,0,0,0,0,0,0};
d = StatsLib.stdev(v);
x = 0;
assertEquals("stdev ", x, d);
v = new double[] {1,2,1,2,1,2,1,2,1,2};
d = StatsLib.stdev(v);
x = 0.52704627669;
assertEquals("stdev ", x, d);
v = new double[] {123.12,33.3333,2d/3d,5.37828,0.999};
d = StatsLib.stdev(v);
x = 52.33006233652;
assertEquals("stdev ", x, d);
v = new double[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10};
d = StatsLib.stdev(v);
x = 3.02765035410;
assertEquals("stdev ", x, d);
}
@Test
public void testVar() {
double[] v = null;
double d, x = 0;
@ -294,6 +303,7 @@ public class TestStatsLib extends AbstractNumericTestCase {
assertEquals("var ", x, d);
}
@Test
public void testVarp() {
double[] v = null;
double d, x = 0;

View File

@ -17,9 +17,9 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import junit.framework.AssertionFailedError;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
@ -27,13 +27,14 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Test cases for SUMPRODUCT()
*
* @author Josh Micich
*/
public final class TestSumif extends TestCase {
public final class TestSumif {
private static final NumberEval _30 = new NumberEval(30);
private static final NumberEval _40 = new NumberEval(40);
private static final NumberEval _50 = new NumberEval(50);
@ -42,6 +43,7 @@ public final class TestSumif extends TestCase {
private static ValueEval invokeSumif(int rowIx, int colIx, ValueEval...args) {
return new Sumif().evaluate(args, rowIx, colIx);
}
private static void confirmDouble(double expected, ValueEval actualEval) {
if(!(actualEval instanceof NumericValueEval)) {
throw new AssertionFailedError("Expected numeric result");
@ -50,6 +52,7 @@ public final class TestSumif extends TestCase {
assertEquals(expected, nve.getNumberValue(), 0);
}
@Test
public void testBasic() {
ValueEval[] arg0values = new ValueEval[] { _30, _30, _40, _40, _50, _50 };
ValueEval[] arg2values = new ValueEval[] { _30, _40, _50, _60, _60, _60 };
@ -70,7 +73,7 @@ public final class TestSumif extends TestCase {
confirm(140.0, arg0, new StringEval("<=40.0"));
confirm(160.0, arg0, new StringEval("<>40.0"));
confirm(80.0, arg0, new StringEval("=40.0"));
}
private static void confirm(double expectedResult, ValueEval...args) {
@ -81,6 +84,7 @@ public final class TestSumif extends TestCase {
/**
* test for bug observed near svn r882931
*/
@Test
public void testCriteriaArgRange() {
ValueEval[] arg0values = new ValueEval[] { _50, _60, _50, _50, _50, _30, };
ValueEval[] arg1values = new ValueEval[] { _30, _40, _50, _60, };
@ -110,6 +114,7 @@ public final class TestSumif extends TestCase {
confirmDouble(60, ve);
}
@Test
public void testEvaluateException() {
assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, BlankEval.instance, new NumberEval(30.0)));
assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, BlankEval.instance, new NumberEval(30.0), new NumberEval(30.0)));

View File

@ -19,26 +19,36 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Test cases for SUMIFS()
*
* @author Yegor Kozlov
*/
public final class TestSumifs extends TestCase {
public final class TestSumifs {
private static final OperationEvaluationContext EC = new OperationEvaluationContext(null, null, 0, 1, 0, null);
private static ValueEval invokeSumifs(ValueEval[] args, OperationEvaluationContext ec) {
return new Sumifs().evaluate(args, EC);
}
private static void confirmDouble(double expected, ValueEval actualEval) {
if(!(actualEval instanceof NumericValueEval)) {
throw new AssertionFailedError("Expected numeric result");
@ -55,6 +65,7 @@ public final class TestSumifs extends TestCase {
* Example 1 from
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx
*/
@Test
public void testExample1() {
// mimic test sample from http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx
ValueEval[] a2a9 = new ValueEval[] {
@ -137,6 +148,7 @@ public final class TestSumifs extends TestCase {
* Example 2 from
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx
*/
@Test
public void testExample2() {
ValueEval[] b2e2 = new ValueEval[] {
new NumberEval(100),
@ -185,6 +197,7 @@ public final class TestSumifs extends TestCase {
* Example 3 from
* http://office.microsoft.com/en-us/excel-help/sumifs-function-HA010047504.aspx
*/
@Test
public void testExample3() {
//3.3 0.8 5.5 5.5
ValueEval[] b2e2 = new ValueEval[] {
@ -228,6 +241,7 @@ public final class TestSumifs extends TestCase {
*
* Criteria entered as reference and by using wildcard characters
*/
@Test
public void testFromFile() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("sumifs.xls");
@ -236,18 +250,18 @@ public final class TestSumifs extends TestCase {
HSSFSheet example1 = wb.getSheet("Example 1");
HSSFCell ex1cell1 = example1.getRow(10).getCell(2);
fe.evaluate(ex1cell1);
assertEquals(20.0, ex1cell1.getNumericCellValue());
assertEquals(20.0, ex1cell1.getNumericCellValue(), 0);
HSSFCell ex1cell2 = example1.getRow(11).getCell(2);
fe.evaluate(ex1cell2);
assertEquals(30.0, ex1cell2.getNumericCellValue());
assertEquals(30.0, ex1cell2.getNumericCellValue(), 0);
HSSFSheet example2 = wb.getSheet("Example 2");
HSSFCell ex2cell1 = example2.getRow(6).getCell(2);
fe.evaluate(ex2cell1);
assertEquals(500.0, ex2cell1.getNumericCellValue());
assertEquals(500.0, ex2cell1.getNumericCellValue(), 0);
HSSFCell ex2cell2 = example2.getRow(7).getCell(2);
fe.evaluate(ex2cell2);
assertEquals(8711.0, ex2cell2.getNumericCellValue());
assertEquals(8711.0, ex2cell2.getNumericCellValue(), 0);
HSSFSheet example3 = wb.getSheet("Example 3");
HSSFCell ex3cell = example3.getRow(5).getCell(2);
@ -257,15 +271,16 @@ public final class TestSumifs extends TestCase {
HSSFSheet example4 = wb.getSheet("Example 4");
HSSFCell ex4cell = example4.getRow(8).getCell(2);
fe.evaluate(ex4cell);
assertEquals(3.5, ex4cell.getNumericCellValue());
assertEquals(3.5, ex4cell.getNumericCellValue(), 0);
HSSFSheet example5 = wb.getSheet("Example 5");
HSSFCell ex5cell = example5.getRow(8).getCell(2);
fe.evaluate(ex5cell);
assertEquals(625000., ex5cell.getNumericCellValue());
assertEquals(625000., ex5cell.getNumericCellValue(), 0);
}
@Test
public void testBug56655() {
ValueEval[] a2a9 = new ValueEval[] {
new NumberEval(5),
@ -283,7 +298,7 @@ public final class TestSumifs extends TestCase {
ErrorEval.VALUE_INVALID,
new StringEval("A*"),
};
ValueEval result = invokeSumifs(args, EC);
assertTrue("Expect to have an error when an input is an invalid value, but had: " + result.getClass(), result instanceof ErrorEval);
@ -292,11 +307,12 @@ public final class TestSumifs extends TestCase {
EvalFactory.createAreaEval("A2:A9", a2a9),
ErrorEval.VALUE_INVALID,
};
result = invokeSumifs(args, EC);
assertTrue("Expect to have an error when an input is an invalid value, but had: " + result.getClass(), result instanceof ErrorEval);
}
@Test
public void testBug56655b() {
/*
setCellFormula(sheet, 0, 0, "B1*C1");
@ -315,13 +331,13 @@ public final class TestSumifs extends TestCase {
EvalFactory.createAreaEval("A0:A1", a0a1),
ErrorEval.VALUE_INVALID
};
ValueEval result = invokeSumifs(args, EC);
assertTrue("Expect to have an error when an input is an invalid value, but had: " + result.getClass(), result instanceof ErrorEval);
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testBug56655c() {
/*
setCellFormula(sheet, 0, 0, "B1*C1");
@ -340,7 +356,7 @@ public final class TestSumifs extends TestCase {
EvalFactory.createAreaEval("A0:A1", a0a1),
ErrorEval.NAME_INVALID
};
ValueEval result = invokeSumifs(args, EC);
assertTrue("Expect to have an error when an input is an invalid value, but had: " + result.getClass(), result instanceof ErrorEval);
assertEquals(ErrorEval.NAME_INVALID, result);

View File

@ -17,7 +17,8 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
@ -25,18 +26,20 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.RefEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Test cases for SUMPRODUCT()
*
* @author Josh Micich
*/
public final class TestSumproduct extends TestCase {
public final class TestSumproduct {
private static ValueEval invokeSumproduct(ValueEval[] args) {
// srcCellRow and srcCellColumn are ignored by SUMPRODUCT
return new Sumproduct().evaluate(args, -1, (short)-1);
}
private static void confirmDouble(double expected, ValueEval actualEval) {
if(!(actualEval instanceof NumericValueEval)) {
fail("Expected numeric result");
@ -45,6 +48,7 @@ public final class TestSumproduct extends TestCase {
assertEquals(expected, nve.getNumberValue(), 0);
}
@Test
public void testScalarSimple() {
RefEval refEval = EvalFactory.createRefEval("A1", new NumberEval(3));
@ -56,6 +60,7 @@ public final class TestSumproduct extends TestCase {
confirmDouble(6D, result);
}
@Test
public void testAreaSimple() {
ValueEval[] aValues = {
new NumberEval(2),
@ -78,6 +83,7 @@ public final class TestSumproduct extends TestCase {
/**
* For scalar products, the terms may be 1x1 area refs
*/
@Test
public void testOneByOneArea() {
AreaEval ae = EvalFactory.createAreaEval("A1:A1", new ValueEval[] { new NumberEval(7), });
@ -90,6 +96,7 @@ public final class TestSumproduct extends TestCase {
confirmDouble(14D, result);
}
@Test
public void testMismatchAreaDimensions() {
AreaEval aeA = EvalFactory.createAreaEval("A1:A3", new ValueEval[3]);
@ -103,6 +110,7 @@ public final class TestSumproduct extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, invokeSumproduct(args));
}
@Test
public void testAreaWithErrorCell() {
ValueEval[] aValues = {
ErrorEval.REF_INVALID,

View File

@ -17,16 +17,26 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.BoolEval;
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;
import org.junit.Test;
/**
* Test cases for Excel function T()
*
* @author Josh Micich
*/
public final class TestTFunc extends TestCase {
public final class TestTFunc {
/**
* @return the result of calling function T() with the specified argument
@ -54,6 +64,7 @@ public final class TestTFunc extends TestCase {
assertEquals(text, se.getStringValue());
}
@Test
public void testTextValues() {
confirmText("abc");
confirmText("");
@ -65,9 +76,10 @@ public final class TestTFunc extends TestCase {
private static void confirmError(ValueEval arg) {
ValueEval eval = invokeT(arg);
assertTrue(arg == eval);
assertSame(arg, eval);
}
@Test
public void testErrorValues() {
confirmError(ErrorEval.VALUE_INVALID);
@ -85,12 +97,14 @@ public final class TestTFunc extends TestCase {
confirmString(eval, "");
}
@Test
public void testOtherValues() {
confirmOther(new NumberEval(2));
confirmOther(BoolEval.FALSE);
confirmOther(BlankEval.instance); // can this particular case be verified?
}
@Test
public void testRefValues() {
ValueEval eval;
@ -105,9 +119,10 @@ public final class TestTFunc extends TestCase {
confirmString(eval, "");
eval = invokeTWithReference(ErrorEval.NAME_INVALID);
assertTrue(eval == ErrorEval.NAME_INVALID);
assertSame(eval, ErrorEval.NAME_INVALID);
}
@Test
public void testAreaArg() {
ValueEval[] areaValues = new ValueEval[] {
new StringEval("abc"), new StringEval("def"),

View File

@ -17,7 +17,7 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.BoolEval;
@ -25,12 +25,14 @@ 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;
import org.junit.Test;
/**
* Tests for Excel function TRIM()
*
* @author Josh Micich
*/
public final class TestTrim extends TestCase {
public final class TestTrim {
private static ValueEval invokeTrim(ValueEval text) {
@ -50,6 +52,7 @@ public final class TestTrim extends TestCase {
assertEquals(expectedError.getErrorCode(), ((ErrorEval)result).getErrorCode());
}
@Test
public void testBasic() {
confirmTrim(new StringEval(" hi "), "hi");
@ -63,6 +66,7 @@ public final class TestTrim extends TestCase {
/**
* Valid cases where text arg is not exactly a string
*/
@Test
public void testUnusualArgs() {
// text (first) arg type is number, other args are strings with fractional digits
@ -72,6 +76,7 @@ public final class TestTrim extends TestCase {
confirmTrim(BlankEval.instance, "");
}
@Test
public void testErrors() {
confirmTrim(ErrorEval.NAME_INVALID, ErrorEval.NAME_INVALID);
}

View File

@ -18,9 +18,11 @@
package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Assert;
import org.junit.Test;
/**
* Test case for TRUNC()
@ -29,34 +31,40 @@ import org.apache.poi.ss.formula.eval.StringEval;
*/
public final class TestTrunc extends AbstractNumericTestCase {
private static final NumericFunction F = null;
@Test
public void testTruncWithStringArg() {
ValueEval strArg = new StringEval("abc");
ValueEval[] args = { strArg, new NumberEval(2) };
ValueEval result = NumericFunction.TRUNC.evaluate(args, -1, (short)-1);
assertEquals(ErrorEval.VALUE_INVALID, result);
Assert.assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testTruncWithWholeNumber() {
ValueEval[] args = { new NumberEval(200), new NumberEval(2) };
@SuppressWarnings("static-access")
ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1);
assertEquals("TRUNC", (new NumberEval(200d)).getNumberValue(), ((NumberEval)result).getNumberValue());
}
@Test
public void testTruncWithDecimalNumber() {
ValueEval[] args = { new NumberEval(2.612777), new NumberEval(3) };
@SuppressWarnings("static-access")
ValueEval result = F.TRUNC.evaluate(args, -1, (short)-1);
assertEquals("TRUNC", (new NumberEval(2.612d)).getNumberValue(), ((NumberEval)result).getNumberValue());
}
@Test
public void testTruncWithDecimalNumberOneArg() {
ValueEval[] args = { new NumberEval(2.612777) };
ValueEval result = NumericFunction.TRUNC.evaluate(args, -1, (short)-1);
assertEquals("TRUNC", (new NumberEval(2d)).getNumberValue(), ((NumberEval)result).getNumberValue());
}
@Test
public void testNegative() {
ValueEval[] args = { new NumberEval(-8.9), new NumberEval(0) };
@SuppressWarnings("static-access")

View File

@ -17,17 +17,18 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
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;
import org.junit.Test;
/**
* Tests for {@link Value}
*/
public final class TestValue extends TestCase {
public final class TestValue {
private static ValueEval invokeValue(String strText) {
ValueEval[] args = new ValueEval[] { new StringEval(strText), };
@ -46,6 +47,7 @@ public final class TestValue extends TestCase {
assertEquals(ErrorEval.VALUE_INVALID, result);
}
@Test
public void testBasic() {
confirmValue("100", 100);
@ -73,6 +75,7 @@ public final class TestValue extends TestCase {
confirmValue("30 %", 0.3);
}
@Test
public void testErrors() {
confirmValueError("1+1");
confirmValueError("1 1");

View File

@ -17,17 +17,19 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.junit.Test;
/**
* Tests for Excel functions SUMX2MY2(), SUMX2PY2(), SUMXMY2()
*
* @author Josh Micich
*/
public final class TestXYNumericFunction extends TestCase {
public final class TestXYNumericFunction {
private static final Function SUM_SQUARES = new Sumx2py2();
private static final Function DIFF_SQUARES = new Sumx2my2();
private static final Function SUM_SQUARES_OF_DIFFS = new Sumxmy2();
@ -54,6 +56,7 @@ public final class TestXYNumericFunction extends TestCase {
confirmError(SUM_SQUARES_OF_DIFFS, xArray, yArray, expectedError);
}
@Test
public void testBasic() {
ValueEval[] xValues = {
new NumberEval(1),
@ -77,6 +80,7 @@ public final class TestXYNumericFunction extends TestCase {
/**
* number of items in array is not limited to 30
*/
@Test
public void testLargeArrays() {
ValueEval[] xValues = createMockNumberArray(100, 3);
ValueEval[] yValues = createMockNumberArray(100, 2);
@ -100,6 +104,7 @@ public final class TestXYNumericFunction extends TestCase {
return EvalFactory.createAreaEval(refStr, values);
}
@Test
public void testErrors() {
ValueEval[] xValues = {
ErrorEval.REF_INVALID,