You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestIndex.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.ss.formula.functions;
  16. import java.util.Arrays;
  17. import junit.framework.AssertionFailedError;
  18. import junit.framework.TestCase;
  19. import org.apache.poi.hssf.HSSFTestDataSamples;
  20. import org.apache.poi.hssf.usermodel.HSSFCell;
  21. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  22. import org.apache.poi.hssf.usermodel.HSSFSheet;
  23. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  24. import org.apache.poi.ss.formula.eval.AreaEval;
  25. import org.apache.poi.ss.formula.eval.MissingArgEval;
  26. import org.apache.poi.ss.formula.eval.NumberEval;
  27. import org.apache.poi.ss.formula.eval.ValueEval;
  28. import org.apache.poi.ss.formula.WorkbookEvaluator;
  29. import org.apache.poi.ss.usermodel.*;
  30. import org.apache.poi.ss.util.CellRangeAddress;
  31. /**
  32. * Tests for the INDEX() function.</p>
  33. *
  34. * This class contains just a few specific cases that directly invoke {@link Index},
  35. * with minimum overhead.<br>
  36. * Another test: {@link TestIndexFunctionFromSpreadsheet} operates from a higher level
  37. * and has far greater coverage of input permutations.<br>
  38. *
  39. * @author Josh Micich
  40. */
  41. public final class TestIndex extends TestCase {
  42. private static final Index FUNC_INST = new Index();
  43. private static final double[] TEST_VALUES0 = {
  44. 1, 2,
  45. 3, 4,
  46. 5, 6,
  47. 7, 8,
  48. 9, 10,
  49. 11, 12,
  50. };
  51. /**
  52. * For the case when the first argument to INDEX() is an area reference
  53. */
  54. public void testEvaluateAreaReference() {
  55. double[] values = TEST_VALUES0;
  56. confirmAreaEval("C1:D6", values, 4, 1, 7);
  57. confirmAreaEval("C1:D6", values, 6, 2, 12);
  58. confirmAreaEval("C1:D6", values, 3, 1, 5);
  59. // now treat same data as 3 columns, 4 rows
  60. confirmAreaEval("C10:E13", values, 2, 2, 5);
  61. confirmAreaEval("C10:E13", values, 4, 1, 10);
  62. }
  63. /**
  64. * @param areaRefString in Excel notation e.g. 'D2:E97'
  65. * @param dValues array of evaluated values for the area reference
  66. * @param rowNum 1-based
  67. * @param colNum 1-based, pass -1 to signify argument not present
  68. */
  69. private static void confirmAreaEval(String areaRefString, double[] dValues,
  70. int rowNum, int colNum, double expectedResult) {
  71. ValueEval[] values = new ValueEval[dValues.length];
  72. for (int i = 0; i < values.length; i++) {
  73. values[i] = new NumberEval(dValues[i]);
  74. }
  75. AreaEval arg0 = EvalFactory.createAreaEval(areaRefString, values);
  76. ValueEval[] args;
  77. if (colNum > 0) {
  78. args = new ValueEval[] { arg0, new NumberEval(rowNum), new NumberEval(colNum), };
  79. } else {
  80. args = new ValueEval[] { arg0, new NumberEval(rowNum), };
  81. }
  82. double actual = invokeAndDereference(args);
  83. assertEquals(expectedResult, actual, 0D);
  84. }
  85. private static double invokeAndDereference(ValueEval[] args) {
  86. ValueEval ve = FUNC_INST.evaluate(args, -1, -1);
  87. ve = WorkbookEvaluator.dereferenceResult(ve, -1, -1);
  88. assertEquals(NumberEval.class, ve.getClass());
  89. return ((NumberEval)ve).getNumberValue();
  90. }
  91. /**
  92. * Tests expressions like "INDEX(A1:C1,,2)".<br>
  93. * This problem was found while fixing bug 47048 and is observable up to svn r773441.
  94. */
  95. public void testMissingArg() {
  96. ValueEval[] values = {
  97. new NumberEval(25.0),
  98. new NumberEval(26.0),
  99. new NumberEval(28.0),
  100. };
  101. AreaEval arg0 = EvalFactory.createAreaEval("A10:C10", values);
  102. ValueEval[] args = new ValueEval[] { arg0, MissingArgEval.instance, new NumberEval(2), };
  103. ValueEval actualResult;
  104. try {
  105. actualResult = FUNC_INST.evaluate(args, -1, -1);
  106. } catch (RuntimeException e) {
  107. if (e.getMessage().equals("Unexpected arg eval type (org.apache.poi.hssf.record.formula.eval.MissingArgEval")) {
  108. throw new AssertionFailedError("Identified bug 47048b - INDEX() should support missing-arg");
  109. }
  110. throw e;
  111. }
  112. // result should be an area eval "B10:B10"
  113. AreaEval ae = confirmAreaEval("B10:B10", actualResult);
  114. actualResult = ae.getValue(0, 0);
  115. assertEquals(NumberEval.class, actualResult.getClass());
  116. assertEquals(26.0, ((NumberEval)actualResult).getNumberValue(), 0.0);
  117. }
  118. /**
  119. * When the argument to INDEX is a reference, the result should be a reference
  120. * A formula like "OFFSET(INDEX(A1:B2,2,1),1,1,1,1)" should return the value of cell B3.
  121. * This works because the INDEX() function returns a reference to A2 (not the value of A2)
  122. */
  123. public void testReferenceResult() {
  124. ValueEval[] values = new ValueEval[4];
  125. Arrays.fill(values, NumberEval.ZERO);
  126. AreaEval arg0 = EvalFactory.createAreaEval("A1:B2", values);
  127. ValueEval[] args = new ValueEval[] { arg0, new NumberEval(2), new NumberEval(1), };
  128. ValueEval ve = FUNC_INST.evaluate(args, -1, -1);
  129. confirmAreaEval("A2:A2", ve);
  130. }
  131. /**
  132. * Confirms that the result is an area ref with the specified coordinates
  133. * @return <tt>ve</tt> cast to {@link AreaEval} if it is valid
  134. */
  135. private static AreaEval confirmAreaEval(String refText, ValueEval ve) {
  136. CellRangeAddress cra = CellRangeAddress.valueOf(refText);
  137. assertTrue(ve instanceof AreaEval);
  138. AreaEval ae = (AreaEval) ve;
  139. assertEquals(cra.getFirstRow(), ae.getFirstRow());
  140. assertEquals(cra.getFirstColumn(), ae.getFirstColumn());
  141. assertEquals(cra.getLastRow(), ae.getLastRow());
  142. assertEquals(cra.getLastColumn(), ae.getLastColumn());
  143. return ae;
  144. }
  145. public void test61859(){
  146. Workbook wb = HSSFTestDataSamples.openSampleWorkbook("maxindextest.xls");
  147. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  148. Sheet example1 = wb.getSheetAt(0);
  149. Cell ex1cell1 = example1.getRow(1).getCell(6);
  150. assertEquals("MAX(INDEX(($B$2:$B$11=F2)*$A$2:$A$11,0))", ex1cell1.getCellFormula());
  151. fe.evaluate(ex1cell1);
  152. assertEquals(4.0, ex1cell1.getNumericCellValue());
  153. Cell ex1cell2 = example1.getRow(2).getCell(6);
  154. assertEquals("MAX(INDEX(($B$2:$B$11=F3)*$A$2:$A$11,0))", ex1cell2.getCellFormula());
  155. fe.evaluate(ex1cell2);
  156. assertEquals(10.0, ex1cell2.getNumericCellValue());
  157. Cell ex1cell3 = example1.getRow(3).getCell(6);
  158. assertEquals("MAX(INDEX(($B$2:$B$11=F4)*$A$2:$A$11,0))", ex1cell3.getCellFormula());
  159. fe.evaluate(ex1cell3);
  160. assertEquals(20.0, ex1cell3.getNumericCellValue());
  161. }
  162. /**
  163. * If both the Row_num and Column_num arguments are used,
  164. * INDEX returns the value in the cell at the intersection of Row_num and Column_num
  165. */
  166. public void testReference2DArea(){
  167. Workbook wb = new HSSFWorkbook();
  168. Sheet sheet = wb.createSheet();
  169. /**
  170. * 1 2 3
  171. * 4 5 6
  172. * 7 8 9
  173. */
  174. int val = 0;
  175. for(int i = 0; i < 3; i++){
  176. Row row = sheet.createRow(i);
  177. for(int j = 0; j < 3; j++){
  178. row.createCell(j).setCellValue(++val);
  179. }
  180. }
  181. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  182. Cell c1 = sheet.getRow(0).createCell(5);
  183. c1.setCellFormula("INDEX(A1:C3,2,2)");
  184. Cell c2 = sheet.getRow(0).createCell(6);
  185. c2.setCellFormula("INDEX(A1:C3,3,2)");
  186. assertEquals(5.0, fe.evaluate(c1).getNumberValue());
  187. assertEquals(8.0, fe.evaluate(c2).getNumberValue());
  188. }
  189. /**
  190. * If Column_num is 0 (zero), INDEX returns the array of values for the entire row.
  191. */
  192. public void testArrayArgument_RowLookup(){
  193. Workbook wb = new HSSFWorkbook();
  194. Sheet sheet = wb.createSheet();
  195. /**
  196. * 1 2 3
  197. * 4 5 6
  198. * 7 8 9
  199. */
  200. int val = 0;
  201. for(int i = 0; i < 3; i++){
  202. Row row = sheet.createRow(i);
  203. for(int j = 0; j < 3; j++){
  204. row.createCell(j).setCellValue(++val);
  205. }
  206. }
  207. Cell c1 = sheet.getRow(0).createCell(5);
  208. c1.setCellFormula("SUM(INDEX(A1:C3,1,0))"); // sum of all values in the 1st row: 1 + 2 + 3 = 6
  209. Cell c2 = sheet.getRow(0).createCell(6);
  210. c2.setCellFormula("SUM(INDEX(A1:C3,2,0))"); // sum of all values in the 2nd row: 4 + 5 + 6 = 15
  211. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  212. assertEquals(6.0, fe.evaluate(c1).getNumberValue());
  213. assertEquals(15.0, fe.evaluate(c2).getNumberValue());
  214. }
  215. /**
  216. * If Row_num is 0 (zero), INDEX returns the array of values for the entire column.
  217. */
  218. public void testArrayArgument_ColumnLookup(){
  219. Workbook wb = new HSSFWorkbook();
  220. Sheet sheet = wb.createSheet();
  221. /**
  222. * 1 2 3
  223. * 4 5 6
  224. * 7 8 9
  225. */
  226. int val = 0;
  227. for(int i = 0; i < 3; i++){
  228. Row row = sheet.createRow(i);
  229. for(int j = 0; j < 3; j++){
  230. row.createCell(j).setCellValue(++val);
  231. }
  232. }
  233. Cell c1 = sheet.getRow(0).createCell(5);
  234. c1.setCellFormula("SUM(INDEX(A1:C3,0,1))"); // sum of all values in the 1st column: 1 + 4 + 7 = 12
  235. Cell c2 = sheet.getRow(0).createCell(6);
  236. c2.setCellFormula("SUM(INDEX(A1:C3,0,3))"); // sum of all values in the 3rd column: 3 + 6 + 9 = 18
  237. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  238. assertEquals(12.0, fe.evaluate(c1).getNumberValue());
  239. assertEquals(18.0, fe.evaluate(c2).getNumberValue());
  240. }
  241. /**
  242. * =SUM(B1:INDEX(B1:B3,2))
  243. *
  244. * The sum of the range starting at B1, and ending at the intersection of the 2nd row of the range B1:B3,
  245. * which is the sum of B1:B2.
  246. */
  247. public void testDynamicReference(){
  248. Workbook wb = new HSSFWorkbook();
  249. Sheet sheet = wb.createSheet();
  250. /**
  251. * 1 2 3
  252. * 4 5 6
  253. * 7 8 9
  254. */
  255. int val = 0;
  256. for(int i = 0; i < 3; i++){
  257. Row row = sheet.createRow(i);
  258. for(int j = 0; j < 3; j++){
  259. row.createCell(j).setCellValue(++val);
  260. }
  261. }
  262. Cell c1 = sheet.getRow(0).createCell(5);
  263. c1.setCellFormula("SUM(B1:INDEX(B1:B3,2))"); // B1:INDEX(B1:B3,2) evaluates to B1:B2
  264. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  265. assertEquals(7.0, fe.evaluate(c1).getNumberValue());
  266. }
  267. }