Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestOperandClassTransformer.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.model;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import org.apache.poi.ss.formula.eval.BlankEval;
  21. import org.apache.poi.ss.formula.eval.ErrorEval;
  22. import org.apache.poi.ss.formula.eval.NumberEval;
  23. import org.apache.poi.ss.formula.eval.ValueEval;
  24. import org.apache.poi.ss.formula.functions.EvalFactory;
  25. import org.apache.poi.ss.formula.functions.MatrixFunction;
  26. import org.apache.poi.ss.formula.ptg.AbstractFunctionPtg;
  27. import org.apache.poi.ss.formula.ptg.FuncVarPtg;
  28. import org.apache.poi.ss.formula.ptg.Ptg;
  29. import org.junit.jupiter.api.Disabled;
  30. import org.junit.jupiter.api.Test;
  31. /**
  32. * Tests specific formula examples in {@code OperandClassTransformer}.
  33. */
  34. final class TestOperandClassTransformer {
  35. private static Ptg[] parseFormula(String formula) {
  36. Ptg[] result = HSSFFormulaParser.parse(formula, null);
  37. assertNotNull(result, "Ptg array should not be null");
  38. return result;
  39. }
  40. @Test
  41. void testMdeterm() {
  42. String formula = "MDETERM(ABS(A1))";
  43. Ptg[] ptgs = parseFormula(formula);
  44. confirmTokenClass(ptgs, 0, Ptg.CLASS_ARRAY);
  45. confirmFuncClass(ptgs, 1, "ABS", Ptg.CLASS_ARRAY);
  46. confirmFuncClass(ptgs, 2, "MDETERM", Ptg.CLASS_VALUE);
  47. }
  48. @Test
  49. void testMdetermReturnsValueInvalidOnABlankCell() {
  50. ValueEval matrixRef = EvalFactory.createAreaEval("A1:B2",
  51. new ValueEval[]{
  52. BlankEval.instance,
  53. new NumberEval(1),
  54. new NumberEval(2),
  55. new NumberEval(3)
  56. }
  57. );
  58. ValueEval result = MatrixFunction.MDETERM.evaluate(new ValueEval[]{matrixRef} , 0, 0);
  59. assertEquals(ErrorEval.VALUE_INVALID, result);
  60. }
  61. /**
  62. * In the example: {@code INDEX(PI(),1)}, Excel encodes PI() as 'array'. It is not clear
  63. * what rule justifies this. POI currently encodes it as 'value' which Excel(2007) seems to
  64. * tolerate. Changing the metadata for INDEX to have first parameter as 'array' class breaks
  65. * other formulas involving INDEX. It seems like a special case needs to be made. Perhaps an
  66. * important observation is that INDEX is one of very few functions that returns 'reference' type.
  67. * <p>
  68. * This test has been added but disabled in order to document this issue.
  69. */
  70. @Test
  71. @Disabled
  72. void testIndexPi1() {
  73. String formula = "INDEX(PI(),1)";
  74. Ptg[] ptgs = parseFormula(formula);
  75. confirmFuncClass(ptgs, 1, "PI", Ptg.CLASS_ARRAY); // fails as of POI 3.1
  76. confirmFuncClass(ptgs, 2, "INDEX", Ptg.CLASS_VALUE);
  77. }
  78. /**
  79. * Even though count expects args of type R, because A1 is a direct operand of a
  80. * value operator it must get type V
  81. */
  82. @Test
  83. void testDirectOperandOfValueOperator() {
  84. String formula = "COUNT(A1*1)";
  85. Ptg[] ptgs = parseFormula(formula);
  86. assertNotEquals(Ptg.CLASS_REF, ptgs[0].getPtgClass());
  87. confirmTokenClass(ptgs, 0, Ptg.CLASS_VALUE);
  88. confirmTokenClass(ptgs, 3, Ptg.CLASS_VALUE);
  89. }
  90. /**
  91. * A cell ref passed to a function expecting type V should be converted to type V
  92. */
  93. @Test
  94. void testRtoV() {
  95. String formula = "lookup(A1, A3:A52, B3:B52)";
  96. Ptg[] ptgs = parseFormula(formula);
  97. confirmTokenClass(ptgs, 0, Ptg.CLASS_VALUE);
  98. }
  99. @Test
  100. void testComplexIRR_bug45041() {
  101. String formula = "(1+IRR(SUMIF(A:A,ROW(INDIRECT(MIN(A:A)&\":\"&MAX(A:A))),B:B),0))^365-1";
  102. Ptg[] ptgs = parseFormula(formula);
  103. FuncVarPtg rowFunc = (FuncVarPtg) ptgs[10];
  104. FuncVarPtg sumifFunc = (FuncVarPtg) ptgs[12];
  105. assertEquals("ROW", rowFunc.getName());
  106. assertEquals("SUMIF", sumifFunc.getName());
  107. assertNotEquals(Ptg.CLASS_VALUE, rowFunc.getPtgClass());
  108. assertNotEquals(Ptg.CLASS_VALUE, sumifFunc.getPtgClass());
  109. confirmTokenClass(ptgs, 1, Ptg.CLASS_REF);
  110. confirmTokenClass(ptgs, 2, Ptg.CLASS_REF);
  111. confirmFuncClass(ptgs, 3, "MIN", Ptg.CLASS_VALUE);
  112. confirmTokenClass(ptgs, 6, Ptg.CLASS_REF);
  113. confirmFuncClass(ptgs, 7, "MAX", Ptg.CLASS_VALUE);
  114. confirmFuncClass(ptgs, 9, "INDIRECT", Ptg.CLASS_REF);
  115. confirmFuncClass(ptgs, 10, "ROW", Ptg.CLASS_ARRAY);
  116. confirmTokenClass(ptgs, 11, Ptg.CLASS_REF);
  117. confirmFuncClass(ptgs, 12, "SUMIF", Ptg.CLASS_ARRAY);
  118. confirmFuncClass(ptgs, 14, "IRR", Ptg.CLASS_VALUE);
  119. }
  120. private void confirmFuncClass(Ptg[] ptgs, int i, String expectedFunctionName, byte operandClass) {
  121. confirmTokenClass(ptgs, i, operandClass);
  122. AbstractFunctionPtg afp = (AbstractFunctionPtg) ptgs[i];
  123. assertEquals(expectedFunctionName, afp.getName());
  124. }
  125. private void confirmTokenClass(Ptg[] ptgs, int i, byte operandClass) {
  126. Ptg ptg = ptgs[i];
  127. assertFalse(ptg.isBaseToken(), "ptg[" + i + "] is a base token");
  128. assertEquals(operandClass, ptg.getPtgClass());
  129. }
  130. }