public void TestSlowEvaluate45376() { /* * Note - to observe behaviour without caching, disable the call to * updateValue() from FormulaCellCacheEntry.updateFormulaResult(). */ // Firstly set up a sequence of formula cells where each depends on the previous multiple // times. Without caching, each subsequent cell take about 4 times longer to Evaluate. HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = wb.CreateSheet("Sheet1"); IRow row = sheet.CreateRow(0); for (int i = 1; i < 10; i++) { ICell cell = row.CreateCell(i); char prevCol = (char)('A' + i - 1); String prevCell = prevCol + "1"; // this formula is inspired by the offending formula of the attachment for bug 45376 String formula = "IF(DATE(YEAR(" + prevCell + "),MONTH(" + prevCell + ")+1,1)<=$D$3," + "DATE(YEAR(" + prevCell + "),MONTH(" + prevCell + ")+1,1),NA())"; cell.CellFormula = (formula); } row.CreateCell(0).SetCellValue(new DateTime(2000,1,1,0,0,0)); // Choose cell A9, so that the Assert.Failing Test case doesn't take too long to execute. ICell cell1 = row.GetCell(8); EvalListener evalListener = new EvalListener(); WorkbookEvaluator evaluator = WorkbookEvaluatorTestHelper.CreateEvaluator(wb, evalListener); ValueEval ve = evaluator.Evaluate(HSSFEvaluationTestHelper.WrapCell(cell1)); int evalCount = evalListener.GetCountCacheMisses(); if (evalCount > 10) { // Without caching, evaluating cell 'A9' takes 21845 evaluations which consumes // much time (~3 sec on Core 2 Duo 2.2GHz) Console.Error.WriteLine("Cell A9 took " + evalCount + " intermediate evaluations"); throw new AssertionException("Identifed bug 45376 - Formula evaluator should cache values"); } // With caching, the evaluationCount is 8 which is a big improvement // Note - these expected values may change if the WorkbookEvaluator is // ever optimised to short circuit 'if' functions. Assert.AreEqual(8, evalCount); // The cache hits would be 24 if fully evaluating all arguments of the // "IF()" functions (Each of the 8 formulas has 4 refs to formula cells // which result in 1 cache miss and 3 cache hits). However with the // short-circuit-if optimisation, 2 of the cell refs get skipped // reducing this metric 8. Assert.AreEqual(8, evalListener.GetCountCacheHits()); // confirm the evaluation result too Assert.AreEqual(ErrorEval.NA, ve); }