public void TestShortCircuitIfEvaluation() { // Set up a simple IF() formula that has measurable evaluation cost for its operands. HSSFWorkbook wb = new HSSFWorkbook(); ISheet sheet = wb.CreateSheet("Sheet1"); IRow row = sheet.CreateRow(0); ICell cellA1 = row.CreateCell(0); cellA1.CellFormula = "if(B1,C1,D1+E1+F1)"; // populate cells B1..F1 with simple formulas instead of plain values so we can use // EvaluationListener to check which parts of the first formula get evaluated for (int i = 1; i < 6; i++) { // formulas are just literal constants "1".."5" row.CreateCell(i).CellFormula = i.ToString(); } EvalCountListener evalListener = new EvalCountListener(); WorkbookEvaluator evaluator = WorkbookEvaluatorTestHelper.CreateEvaluator(wb, evalListener); ValueEval ve = evaluator.Evaluate(HSSFEvaluationTestHelper.WrapCell(cellA1)); int evalCount = evalListener.EvalCount; if (evalCount == 6) { // Without short-circuit-if evaluation, evaluating cell 'A1' takes 3 extra evaluations (for D1,E1,F1) Assert.Fail("Identifed bug 48195 - Formula evaluator should short-circuit IF() calculations."); } Assert.AreEqual(3, evalCount); Assert.AreEqual(2.0, ((NumberEval)ve).NumberValue, 0D); wb.Close(); }
public void TestShortCircuitIfEvaluation() { // Set up a simple IF() formula that has measurable evaluation cost for its operands. HSSFWorkbook wb = new HSSFWorkbook(); ISheet sheet = wb.CreateSheet("Sheet1"); IRow row = sheet.CreateRow(0); ICell cellA1 = row.CreateCell(0); cellA1.CellFormula = "if(B1,C1,D1+E1+F1)"; // populate cells B1..F1 with simple formulas instead of plain values so we can use // EvaluationListener to check which parts of the first formula get evaluated for (int i = 1; i < 6; i++) { // formulas are just literal constants "1".."5" row.CreateCell(i).CellFormula = i.ToString(); } EvalCountListener evalListener = new EvalCountListener(); WorkbookEvaluator evaluator = WorkbookEvaluatorTestHelper.CreateEvaluator(wb, evalListener); ValueEval ve = evaluator.Evaluate(HSSFEvaluationTestHelper.WrapCell(cellA1)); int evalCount = evalListener.EvalCount; if (evalCount == 6) { // Without short-circuit-if evaluation, evaluating cell 'A1' takes 3 extra evaluations (for D1,E1,F1) throw new AssertionException("Identifed bug 48195 - Formula evaluator should short-circuit IF() calculations."); } Assert.AreEqual(3, evalCount); Assert.AreEqual(2.0, ((NumberEval)ve).NumberValue, 0D); }