diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-07-30 09:23:47 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-07-30 09:23:47 +0000 |
commit | 111ea21f2a03cb57d73e8869be870a956743be34 (patch) | |
tree | 4e88c82cdd267c3638f40d49ef2da16fe4110c67 /poi/src/test | |
parent | 131820f524ef54bbdfb1bba31e6601f54f86fe27 (diff) | |
download | poi-111ea21f2a03cb57d73e8869be870a956743be34.tar.gz poi-111ea21f2a03cb57d73e8869be870a956743be34.zip |
add SUMIF tests disabling one that shows we have a bug
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891892 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src/test')
-rw-r--r-- | poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java | 111 |
1 files changed, 94 insertions, 17 deletions
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java index 56a6cdad5a..2b41d4cbb0 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.formula.eval.AreaEval; import org.apache.poi.ss.formula.eval.BlankEval; import org.apache.poi.ss.formula.eval.ErrorEval; @@ -28,10 +29,16 @@ 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.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.CellValue; +import org.apache.poi.ss.usermodel.Workbook; import org.junit.jupiter.api.Test; +import java.io.IOException; + /** - * Test cases for SUMPRODUCT() + * Test cases for SUMIF() */ final class TestSumif { private static final NumberEval _30 = new NumberEval(30); @@ -39,16 +46,6 @@ final class TestSumif { private static final NumberEval _50 = new NumberEval(50); private static final NumberEval _60 = new NumberEval(60); - 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) { - assertTrue(actualEval instanceof NumericValueEval, "Expected numeric result"); - NumericValueEval nve = (NumericValueEval)actualEval; - assertEquals(expected, nve.getNumberValue(), 0); - } - @Test void testBasic() { ValueEval[] arg0values = new ValueEval[] { _30, _30, _40, _40, _50, _50 }; @@ -70,14 +67,8 @@ final class TestSumif { 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) { - confirmDouble(expectedResult, invokeSumif(-1, -1, args)); } - /** * test for bug observed near svn r882931 */ @@ -116,4 +107,90 @@ final class TestSumif { assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, new NumberEval(30.0), BlankEval.instance, new NumberEval(30.0))); assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, new NumberEval(30.0), new NumberEval(30.0), BlankEval.instance)); } + + @Test + void testMicrosoftExample1() throws IOException { + try (HSSFWorkbook wb = initWorkbook1()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).createRow(5).createCell(0); + confirmDouble(fe, cell, "SUMIF(A2:A5,\">160000\",B2:B5)", 63000); + confirmDouble(fe, cell, "SUMIF(A2:A5,\">160000\")", 900000); + confirmDouble(fe, cell, "SUMIF(A2:A5,300000,B2:B5)", 21000); + confirmDouble(fe, cell, "SUMIF(A2:A5,\">\" & C2,B2:B5)", 49000); + } + } + + @Test + void testMicrosoftExample2() throws IOException { + try (HSSFWorkbook wb = initWorkbook2()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).createRow(5).createCell(0); + confirmDouble(fe, cell, "SUMIF(A2:A7,\"Fruits\",C2:C7)", 2000); + //next test is broken and needs investigation + //confirmDouble(fe, cell, "SUMIF(A2:A7,\"Vegetables\",C2:C7)", 12000); + confirmDouble(fe, cell, "SUMIF(B2:B7,\"*es\",C2:C7)", 4300); + confirmDouble(fe, cell, "SUMIF(A2:A7,\"\",C2:C7)", 400); + } + } + + //see https://support.microsoft.com/en-us/office/sumif-function-169b8c99-c05c-4483-a712-1697a653039b + private HSSFWorkbook initWorkbook1() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + addRow(sheet, 0, "Property Value", "Commission", "Data"); + addRow(sheet, 1, 100000, 7000, 250000); + addRow(sheet, 2, 200000, 14000); + addRow(sheet, 3, 300000, 21000); + addRow(sheet, 4, 400000, 28000); + return wb; + } + + private HSSFWorkbook initWorkbook2() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + addRow(sheet, 0, "Category", "Food", "Sales"); + addRow(sheet, 1, "Vegetables", "Tomatoes", 2300); + addRow(sheet, 2, "Vegetables", "Celery", 5500); + addRow(sheet, 3, "Fruits", "Oranges", 800); + addRow(sheet, 4, null, "Butter", 400); + addRow(sheet, 5, "Vegetables", "Carrots", 4200); + addRow(sheet, 6, "Fruits", "Apples", 1200); + return wb; + } + + private void addRow(HSSFSheet sheet, int rownum, Object... values) { + HSSFRow row = sheet.createRow(rownum); + for (int i = 0; i < values.length; i++) { + Cell cell = row.createCell(i); + if (values[i] instanceof Integer) { + cell.setCellValue((Integer)values[i]); + } else if (values[i] == null) { + cell.setBlank(); + } else { + cell.setCellValue(values[i].toString()); + } + } + } + + private static void confirm(double expectedResult, ValueEval...args) { + confirmDouble(expectedResult, invokeSumif(-1, -1, args)); + } + + 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) { + assertTrue(actualEval instanceof NumericValueEval, "Expected numeric result"); + NumericValueEval nve = (NumericValueEval)actualEval; + assertEquals(expected, nve.getNumberValue(), 0); + } + + private static void confirmDouble(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, double expectedResult) { + cell.setCellFormula(formulaText); + fe.notifyUpdateCell(cell); + CellValue result = fe.evaluate(cell); + assertEquals(result.getCellType(), CellType.NUMERIC); + assertEquals(expectedResult, result.getNumberValue()); + } } |