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.

TestArrayPtg.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.ptg;
  16. import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook;
  17. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  20. import java.io.IOException;
  21. import org.apache.poi.hssf.record.TestcaseRecordInputStream;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.util.LittleEndianByteArrayOutputStream;
  24. import org.apache.poi.util.LittleEndianInput;
  25. import org.junit.jupiter.api.Test;
  26. /**
  27. * Tests for {@code ArrayPtg}
  28. */
  29. final class TestArrayPtg {
  30. private static final byte[] ENCODED_PTG_DATA = {
  31. 0x40,
  32. 0, 0, 0, 0, 0, 0, 0,
  33. };
  34. private static final byte[] ENCODED_CONSTANT_DATA = {
  35. 2, // 3 columns
  36. 1, 0, // 2 rows
  37. 4, 1, 0, 0, 0, 0, 0, 0, 0, // TRUE
  38. 2, 4, 0, 0, 65, 66, 67, 68, // "ABCD"
  39. 2, 1, 0, 0, 69, // "E"
  40. 1, 0, 0, 0, 0, 0, 0, 0, 0, // 0
  41. 4, 0, 0, 0, 0, 0, 0, 0, 0, // FALSE
  42. 2, 2, 0, 0, 70, 71, // "FG"
  43. };
  44. private static ArrayPtg create(byte[] initialData, byte[] constantData) {
  45. ArrayInitialPtg ptgInit = new ArrayInitialPtg(TestcaseRecordInputStream.createLittleEndian(initialData));
  46. return ptgInit.finishReading(TestcaseRecordInputStream.createLittleEndian(constantData));
  47. }
  48. /**
  49. * Lots of problems with ArrayPtg's decoding and encoding of the element value data
  50. */
  51. @Test
  52. void testReadWriteTokenValueBytes() {
  53. ArrayPtg ptg = create(ENCODED_PTG_DATA, ENCODED_CONSTANT_DATA);
  54. assertEquals(3, ptg.getColumnCount());
  55. assertEquals(2, ptg.getRowCount());
  56. Object[][] values = ptg.getTokenArrayValues();
  57. assertEquals(2, values.length);
  58. assertEquals(Boolean.TRUE, values[0][0]);
  59. assertEquals("ABCD", values[0][1]);
  60. assertEquals(0d, values[1][0]);
  61. assertEquals(Boolean.FALSE, values[1][1]);
  62. assertEquals("FG", values[1][2]);
  63. byte[] outBuf = new byte[ENCODED_CONSTANT_DATA.length];
  64. ptg.writeTokenValueBytes(new LittleEndianByteArrayOutputStream(outBuf, 0));
  65. assertNotEquals(4, outBuf[0], "Identified bug 42564b");
  66. assertArrayEquals(ENCODED_CONSTANT_DATA, outBuf);
  67. }
  68. /**
  69. * Excel stores array elements column by column. This test makes sure POI does the same.
  70. */
  71. @Test
  72. void testElementOrdering() {
  73. ArrayPtg ptg = create(ENCODED_PTG_DATA, ENCODED_CONSTANT_DATA);
  74. assertEquals(3, ptg.getColumnCount());
  75. assertEquals(2, ptg.getRowCount());
  76. assertEquals(0, ptg.getValueIndex(0, 0));
  77. assertEquals(1, ptg.getValueIndex(1, 0));
  78. assertEquals(2, ptg.getValueIndex(2, 0));
  79. assertEquals(3, ptg.getValueIndex(0, 1));
  80. assertEquals(4, ptg.getValueIndex(1, 1));
  81. assertEquals(5, ptg.getValueIndex(2, 1));
  82. }
  83. /**
  84. * Test for a bug which was temporarily introduced by the fix for bug 42564.
  85. * A spreadsheet was added to make the ordering clearer.
  86. */
  87. @Test
  88. void testElementOrderingInSpreadsheet() throws IOException {
  89. String formula;
  90. try (HSSFWorkbook wb = openSampleWorkbook("ex42564-elementOrder.xls")) {
  91. // The formula has an array with 3 rows and 5 columns
  92. formula = wb.getSheetAt(0).getRow(0).getCell(0).getCellFormula();
  93. assertNotEquals("SUM({1,6,11;2,7,12;3,8,13;4,9,14;5,10,15})", formula, "Identified bug 42564 b");
  94. assertEquals("SUM({1,2,3,4,5;6,7,8,9,10;11,12,13,14,15})", formula);
  95. }
  96. }
  97. @Test
  98. void testToFormulaString() {
  99. ArrayPtg ptg = create(ENCODED_PTG_DATA, ENCODED_CONSTANT_DATA);
  100. // bug 45380 - Unexpected constant class (java.lang.Boolean)
  101. String actualFormula = ptg.toFormulaString();
  102. assertEquals("{TRUE,\"ABCD\",\"E\";0,FALSE,\"FG\"}", actualFormula);
  103. }
  104. /**
  105. * worth checking since AttrPtg.sid=0x20 and Ptg.CLASS_* = (0x00, 0x20, and 0x40)
  106. */
  107. @Test
  108. void testOperandClassDecoding() {
  109. confirmOperandClassDecoding(Ptg.CLASS_REF);
  110. confirmOperandClassDecoding(Ptg.CLASS_VALUE);
  111. confirmOperandClassDecoding(Ptg.CLASS_ARRAY);
  112. }
  113. private static void confirmOperandClassDecoding(byte operandClass) {
  114. byte[] fullData = concat(ENCODED_PTG_DATA, ENCODED_CONSTANT_DATA);
  115. // Force encoded operand class for tArray
  116. fullData[0] = (byte) (ArrayPtg.sid + operandClass);
  117. LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(fullData);
  118. Ptg[] ptgs = Ptg.readTokens(ENCODED_PTG_DATA.length, in);
  119. assertEquals(1, ptgs.length);
  120. ArrayPtg aPtg = (ArrayPtg) ptgs[0];
  121. assertEquals(operandClass, aPtg.getPtgClass());
  122. }
  123. private static byte[] concat(byte[] a, byte[] b) {
  124. byte[] result = new byte[a.length + b.length];
  125. System.arraycopy(a, 0, result, 0, a.length);
  126. System.arraycopy(b, 0, result, a.length, b.length);
  127. return result;
  128. }
  129. }