You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2021/05/22 20:56:49 UTC
svn commit: r1890120 [37/43] - in /poi/trunk/poi/src:
main/java/org/apache/poi/ main/java/org/apache/poi/ddf/
main/java/org/apache/poi/extractor/ main/java/org/apache/poi/hpsf/
main/java/org/apache/poi/hssf/ main/java/org/apache/poi/hssf/dev/
main/java...
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/TestSheetNameFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/TestSheetNameFormatter.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/TestSheetNameFormatter.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/TestSheetNameFormatter.java Sat May 22 20:56:44 2021
@@ -28,164 +28,164 @@ import org.junit.jupiter.api.Test;
* Tests for {@link SheetNameFormatter}
*/
final class TestSheetNameFormatter {
- /**
- * Tests main public method 'format'
- */
- @Test
- void testFormat() {
-
- confirmFormat("abc", "abc");
- confirmFormat("123", "'123'");
-
- confirmFormat("my sheet", "'my sheet'"); // space
- confirmFormat("A:MEM", "'A:MEM'"); // colon
-
- confirmFormat("O'Brian", "'O''Brian'"); // single quote gets doubled
-
- confirmFormat("3rdTimeLucky", "'3rdTimeLucky'"); // digit in first pos
- confirmFormat("_", "_"); // plain underscore OK
- confirmFormat("my_3rd_sheet", "my_3rd_sheet"); // underscores and digits OK
- confirmFormat("A12220", "'A12220'");
- confirmFormat("TAXRETURN19980415", "TAXRETURN19980415");
-
- confirmFormat(null, "#REF");
- }
-
- private static void confirmFormat(String rawSheetName, String expectedSheetNameEncoding) {
- // test all variants
-
- assertEquals(expectedSheetNameEncoding, SheetNameFormatter.format(rawSheetName));
-
- StringBuilder sb = new StringBuilder();
- SheetNameFormatter.appendFormat(sb, rawSheetName);
- assertEquals(expectedSheetNameEncoding, sb.toString());
-
- sb = new StringBuilder();
- SheetNameFormatter.appendFormat((Appendable)sb, rawSheetName);
- assertEquals(expectedSheetNameEncoding, sb.toString());
-
- StringBuffer sbf = new StringBuffer();
- //noinspection deprecation
- SheetNameFormatter.appendFormat(sbf, rawSheetName);
- assertEquals(expectedSheetNameEncoding, sbf.toString());
- }
-
- @Test
- void testFormatWithWorkbookName() {
-
- confirmFormat("abc", "abc", "[abc]abc");
- confirmFormat("abc", "123", "'[abc]123'");
-
- confirmFormat("abc", "my sheet", "'[abc]my sheet'"); // space
- confirmFormat("abc", "A:MEM", "'[abc]A:MEM'"); // colon
-
- confirmFormat("abc", "O'Brian", "'[abc]O''Brian'"); // single quote gets doubled
-
- confirmFormat("abc", "3rdTimeLucky", "'[abc]3rdTimeLucky'"); // digit in first pos
- confirmFormat("abc", "_", "[abc]_"); // plain underscore OK
- confirmFormat("abc", "my_3rd_sheet", "[abc]my_3rd_sheet"); // underscores and digits OK
- confirmFormat("abc", "A12220", "'[abc]A12220'");
- confirmFormat("abc", "TAXRETURN19980415", "[abc]TAXRETURN19980415");
-
- confirmFormat("abc", null, "[abc]#REF");
- confirmFormat(null, "abc", "[#REF]abc");
- confirmFormat(null, null, "[#REF]#REF");
- }
-
- private static void confirmFormat(String workbookName, String rawSheetName, String expectedSheetNameEncoding) {
- // test all variants
-
- StringBuilder sb = new StringBuilder();
- SheetNameFormatter.appendFormat(sb, workbookName, rawSheetName);
- assertEquals(expectedSheetNameEncoding, sb.toString());
-
- sb = new StringBuilder();
- SheetNameFormatter.appendFormat((Appendable)sb, workbookName, rawSheetName);
- assertEquals(expectedSheetNameEncoding, sb.toString());
-
- StringBuffer sbf = new StringBuffer();
- //noinspection deprecation
- SheetNameFormatter.appendFormat(sbf, workbookName, rawSheetName);
- assertEquals(expectedSheetNameEncoding, sbf.toString());
- }
-
- @Test
- void testFormatException() {
- Appendable mock = new Appendable() {
- @Override
- public Appendable append(CharSequence csq) throws IOException {
- throw new IOException("Test exception");
- }
-
- @Override
- public Appendable append(CharSequence csq, int start, int end) throws IOException {
- throw new IOException("Test exception");
- }
-
- @Override
- public Appendable append(char c) throws IOException {
- throw new IOException("Test exception");
- }
- };
-
- assertThrows(RuntimeException.class, () -> SheetNameFormatter.appendFormat(mock, null, null));
- assertThrows(RuntimeException.class, () -> SheetNameFormatter.appendFormat(mock, null));
- }
-
- @Test
- void testBooleanLiterals() {
- confirmFormat("TRUE", "'TRUE'");
- confirmFormat("FALSE", "'FALSE'");
- confirmFormat("True", "'True'");
- confirmFormat("fAlse", "'fAlse'");
-
- confirmFormat("Yes", "Yes");
- confirmFormat("No", "No");
- }
-
- private static void confirmCellNameMatch(String rawSheetName, boolean expected) {
- assertEquals(expected, SheetNameFormatter.nameLooksLikePlainCellReference(rawSheetName));
- }
-
- /**
- * Tests functionality to determine whether a sheet name containing only letters and digits
- * would look (to Excel) like a cell name.
- */
- @Test
- void testLooksLikePlainCellReference() {
-
- confirmCellNameMatch("A1", true);
- confirmCellNameMatch("a111", true);
- confirmCellNameMatch("AA", false);
- confirmCellNameMatch("aa1", true);
- confirmCellNameMatch("A1A", false);
- confirmCellNameMatch("A1A1", false);
- confirmCellNameMatch("Sh3", false);
- confirmCellNameMatch("SALES20080101", false); // out of range
- }
-
- private static void confirmCellRange(String text, int numberOfPrefixLetters, boolean expected) {
- String prefix = text.substring(0, numberOfPrefixLetters);
- String suffix = text.substring(numberOfPrefixLetters);
- assertEquals(expected, SheetNameFormatter.cellReferenceIsWithinRange(prefix, suffix));
- }
-
- /**
- * Tests exact boundaries for names that look very close to cell names (i.e. contain 1 or more
- * letters followed by one or more digits).
- */
- @Test
- void testCellRange() {
- confirmCellRange("A1", 1, true);
- confirmCellRange("a111", 1, true);
- confirmCellRange("A65536", 1, true);
- confirmCellRange("A65537", 1, false);
- confirmCellRange("iv1", 2, true);
- confirmCellRange("IW1", 2, false);
- confirmCellRange("AAA1", 3, false);
- confirmCellRange("a111", 1, true);
- confirmCellRange("Sheet1", 6, false);
- confirmCellRange("iV65536", 2, true); // max cell in Excel 97-2003
- confirmCellRange("IW65537", 2, false);
- }
+ /**
+ * Tests main public method 'format'
+ */
+ @Test
+ void testFormat() {
+
+ confirmFormat("abc", "abc");
+ confirmFormat("123", "'123'");
+
+ confirmFormat("my sheet", "'my sheet'"); // space
+ confirmFormat("A:MEM", "'A:MEM'"); // colon
+
+ confirmFormat("O'Brian", "'O''Brian'"); // single quote gets doubled
+
+ confirmFormat("3rdTimeLucky", "'3rdTimeLucky'"); // digit in first pos
+ confirmFormat("_", "_"); // plain underscore OK
+ confirmFormat("my_3rd_sheet", "my_3rd_sheet"); // underscores and digits OK
+ confirmFormat("A12220", "'A12220'");
+ confirmFormat("TAXRETURN19980415", "TAXRETURN19980415");
+
+ confirmFormat(null, "#REF");
+ }
+
+ private static void confirmFormat(String rawSheetName, String expectedSheetNameEncoding) {
+ // test all variants
+
+ assertEquals(expectedSheetNameEncoding, SheetNameFormatter.format(rawSheetName));
+
+ StringBuilder sb = new StringBuilder();
+ SheetNameFormatter.appendFormat(sb, rawSheetName);
+ assertEquals(expectedSheetNameEncoding, sb.toString());
+
+ sb = new StringBuilder();
+ SheetNameFormatter.appendFormat((Appendable)sb, rawSheetName);
+ assertEquals(expectedSheetNameEncoding, sb.toString());
+
+ StringBuffer sbf = new StringBuffer();
+ //noinspection deprecation
+ SheetNameFormatter.appendFormat(sbf, rawSheetName);
+ assertEquals(expectedSheetNameEncoding, sbf.toString());
+ }
+
+ @Test
+ void testFormatWithWorkbookName() {
+
+ confirmFormat("abc", "abc", "[abc]abc");
+ confirmFormat("abc", "123", "'[abc]123'");
+
+ confirmFormat("abc", "my sheet", "'[abc]my sheet'"); // space
+ confirmFormat("abc", "A:MEM", "'[abc]A:MEM'"); // colon
+
+ confirmFormat("abc", "O'Brian", "'[abc]O''Brian'"); // single quote gets doubled
+
+ confirmFormat("abc", "3rdTimeLucky", "'[abc]3rdTimeLucky'"); // digit in first pos
+ confirmFormat("abc", "_", "[abc]_"); // plain underscore OK
+ confirmFormat("abc", "my_3rd_sheet", "[abc]my_3rd_sheet"); // underscores and digits OK
+ confirmFormat("abc", "A12220", "'[abc]A12220'");
+ confirmFormat("abc", "TAXRETURN19980415", "[abc]TAXRETURN19980415");
+
+ confirmFormat("abc", null, "[abc]#REF");
+ confirmFormat(null, "abc", "[#REF]abc");
+ confirmFormat(null, null, "[#REF]#REF");
+ }
+
+ private static void confirmFormat(String workbookName, String rawSheetName, String expectedSheetNameEncoding) {
+ // test all variants
+
+ StringBuilder sb = new StringBuilder();
+ SheetNameFormatter.appendFormat(sb, workbookName, rawSheetName);
+ assertEquals(expectedSheetNameEncoding, sb.toString());
+
+ sb = new StringBuilder();
+ SheetNameFormatter.appendFormat((Appendable)sb, workbookName, rawSheetName);
+ assertEquals(expectedSheetNameEncoding, sb.toString());
+
+ StringBuffer sbf = new StringBuffer();
+ //noinspection deprecation
+ SheetNameFormatter.appendFormat(sbf, workbookName, rawSheetName);
+ assertEquals(expectedSheetNameEncoding, sbf.toString());
+ }
+
+ @Test
+ void testFormatException() {
+ Appendable mock = new Appendable() {
+ @Override
+ public Appendable append(CharSequence csq) throws IOException {
+ throw new IOException("Test exception");
+ }
+
+ @Override
+ public Appendable append(CharSequence csq, int start, int end) throws IOException {
+ throw new IOException("Test exception");
+ }
+
+ @Override
+ public Appendable append(char c) throws IOException {
+ throw new IOException("Test exception");
+ }
+ };
+
+ assertThrows(RuntimeException.class, () -> SheetNameFormatter.appendFormat(mock, null, null));
+ assertThrows(RuntimeException.class, () -> SheetNameFormatter.appendFormat(mock, null));
+ }
+
+ @Test
+ void testBooleanLiterals() {
+ confirmFormat("TRUE", "'TRUE'");
+ confirmFormat("FALSE", "'FALSE'");
+ confirmFormat("True", "'True'");
+ confirmFormat("fAlse", "'fAlse'");
+
+ confirmFormat("Yes", "Yes");
+ confirmFormat("No", "No");
+ }
+
+ private static void confirmCellNameMatch(String rawSheetName, boolean expected) {
+ assertEquals(expected, SheetNameFormatter.nameLooksLikePlainCellReference(rawSheetName));
+ }
+
+ /**
+ * Tests functionality to determine whether a sheet name containing only letters and digits
+ * would look (to Excel) like a cell name.
+ */
+ @Test
+ void testLooksLikePlainCellReference() {
+
+ confirmCellNameMatch("A1", true);
+ confirmCellNameMatch("a111", true);
+ confirmCellNameMatch("AA", false);
+ confirmCellNameMatch("aa1", true);
+ confirmCellNameMatch("A1A", false);
+ confirmCellNameMatch("A1A1", false);
+ confirmCellNameMatch("Sh3", false);
+ confirmCellNameMatch("SALES20080101", false); // out of range
+ }
+
+ private static void confirmCellRange(String text, int numberOfPrefixLetters, boolean expected) {
+ String prefix = text.substring(0, numberOfPrefixLetters);
+ String suffix = text.substring(numberOfPrefixLetters);
+ assertEquals(expected, SheetNameFormatter.cellReferenceIsWithinRange(prefix, suffix));
+ }
+
+ /**
+ * Tests exact boundaries for names that look very close to cell names (i.e. contain 1 or more
+ * letters followed by one or more digits).
+ */
+ @Test
+ void testCellRange() {
+ confirmCellRange("A1", 1, true);
+ confirmCellRange("a111", 1, true);
+ confirmCellRange("A65536", 1, true);
+ confirmCellRange("A65537", 1, false);
+ confirmCellRange("iv1", 2, true);
+ confirmCellRange("IW1", 2, false);
+ confirmCellRange("AAA1", 3, false);
+ confirmCellRange("a111", 1, true);
+ confirmCellRange("Sheet1", 6, false);
+ confirmCellRange("iV65536", 2, true); // max cell in Excel 97-2003
+ confirmCellRange("IW65537", 2, false);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java Sat May 22 20:56:44 2021
@@ -25,11 +25,11 @@ import org.apache.poi.hssf.usermodel.HSS
*/
public final class WorkbookEvaluatorTestHelper {
- private WorkbookEvaluatorTestHelper() {
- // no instances of this class
- }
+ private WorkbookEvaluatorTestHelper() {
+ // no instances of this class
+ }
- public static WorkbookEvaluator createEvaluator(HSSFWorkbook wb, EvaluationListener listener) {
- return new WorkbookEvaluator(HSSFEvaluationWorkbook.create(wb), listener, null, null);
- }
+ public static WorkbookEvaluator createEvaluator(HSSFWorkbook wb, EvaluationListener listener) {
+ return new WorkbookEvaluator(HSSFEvaluationWorkbook.create(wb), listener, null, null);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestMRound.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestMRound.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestMRound.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestMRound.java Sat May 22 20:56:44 2021
@@ -32,10 +32,10 @@ import org.junit.jupiter.api.Test;
class TestMRound {
/**
- =MROUND(10, 3) Rounds 10 to a nearest multiple of 3 (9)
- =MROUND(-10, -3) Rounds -10 to a nearest multiple of -3 (-9)
- =MROUND(1.3, 0.2) Rounds 1.3 to a nearest multiple of 0.2 (1.4)
- =MROUND(5, -2) Returns an error, because -2 and 5 have different signs (#NUM!) *
+ =MROUND(10, 3) Rounds 10 to a nearest multiple of 3 (9)
+ =MROUND(-10, -3) Rounds -10 to a nearest multiple of -3 (-9)
+ =MROUND(1.3, 0.2) Rounds 1.3 to a nearest multiple of 0.2 (1.4)
+ =MROUND(5, -2) Returns an error, because -2 and 5 have different signs (#NUM!) *
*/
@Test
void testEvaluate(){
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestRandBetween.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestRandBetween.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestRandBetween.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestRandBetween.java Sat May 22 20:56:44 2021
@@ -35,41 +35,41 @@ import org.junit.jupiter.api.Test;
*/
class TestRandBetween {
- private FormulaEvaluator evaluator;
- private Cell bottomValueCell;
- private Cell topValueCell;
- private Cell formulaCell;
-
- @BeforeEach
- void setUp() {
- Workbook wb = HSSFTestDataSamples.openSampleWorkbook("TestRandBetween.xls");
- evaluator = wb.getCreationHelper().createFormulaEvaluator();
-
- Sheet sheet = wb.createSheet("RandBetweenSheet");
- Row row = sheet.createRow(0);
- bottomValueCell = row.createCell(0);
- topValueCell = row.createCell(1);
- formulaCell = row.createCell(2, CellType.FORMULA);
- }
-
- /**
- * Check where values are the same
- */
- @Test
- void testRandBetweenSameValues() {
- evaluator.clearAllCachedResultValues();
- formulaCell.setCellFormula("RANDBETWEEN(1,1)");
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(1, formulaCell.getNumericCellValue(), 0);
- evaluator.clearAllCachedResultValues();
- formulaCell.setCellFormula("RANDBETWEEN(-1,-1)");
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(-1, formulaCell.getNumericCellValue(), 0);
+ private FormulaEvaluator evaluator;
+ private Cell bottomValueCell;
+ private Cell topValueCell;
+ private Cell formulaCell;
+
+ @BeforeEach
+ void setUp() {
+ Workbook wb = HSSFTestDataSamples.openSampleWorkbook("TestRandBetween.xls");
+ evaluator = wb.getCreationHelper().createFormulaEvaluator();
+
+ Sheet sheet = wb.createSheet("RandBetweenSheet");
+ Row row = sheet.createRow(0);
+ bottomValueCell = row.createCell(0);
+ topValueCell = row.createCell(1);
+ formulaCell = row.createCell(2, CellType.FORMULA);
+ }
+
+ /**
+ * Check where values are the same
+ */
+ @Test
+ void testRandBetweenSameValues() {
+ evaluator.clearAllCachedResultValues();
+ formulaCell.setCellFormula("RANDBETWEEN(1,1)");
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(1, formulaCell.getNumericCellValue(), 0);
+ evaluator.clearAllCachedResultValues();
+ formulaCell.setCellFormula("RANDBETWEEN(-1,-1)");
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(-1, formulaCell.getNumericCellValue(), 0);
- }
+ }
- @Test
- void testRandBetweenLargeLongs() {
+ @Test
+ void testRandBetweenLargeLongs() {
for (int i = 0; i < 100; i++) {
evaluator.clearAllCachedResultValues();
formulaCell.setCellFormula("RANDBETWEEN(0,9999999999)");
@@ -78,119 +78,119 @@ class TestRandBetween {
assertTrue(value >= 0.0, "rand is greater than or equal to lowerbound");
assertTrue(value <= 9999999999.0, "rand is less than or equal to upperbound");
}
- }
+ }
- /**
- * Check special case where rounded up bottom value is greater than
- * top value.
- */
- @Test
- void testRandBetweenSpecialCase() {
- bottomValueCell.setCellValue(0.05);
- topValueCell.setCellValue(0.1);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(1, formulaCell.getNumericCellValue(), 0);
- bottomValueCell.setCellValue(-0.1);
- topValueCell.setCellValue(-0.05);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(0, formulaCell.getNumericCellValue(), 0);
- bottomValueCell.setCellValue(-1.1);
- topValueCell.setCellValue(-1.05);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(-1, formulaCell.getNumericCellValue(), 0);
- bottomValueCell.setCellValue(-1.1);
- topValueCell.setCellValue(-1.1);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(-1, formulaCell.getNumericCellValue(), 0);
- }
-
- /**
- * Check top value of BLANK which Excel will evaluate as 0
- */
- @Test
- void testRandBetweenTopBlank() {
- bottomValueCell.setCellValue(-1);
- topValueCell.setBlank();
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertTrue(formulaCell.getNumericCellValue() == 0 || formulaCell.getNumericCellValue() == -1);
-
- }
- /**
- * Check where input values are of wrong type
- */
- @Test
- void testRandBetweenWrongInputTypes() {
- // Check case where bottom input is of the wrong type
- bottomValueCell.setCellValue("STRING");
- topValueCell.setCellValue(1);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
- assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), formulaCell.getErrorCellValue());
-
-
- // Check case where top input is of the wrong type
- bottomValueCell.setCellValue(1);
- topValueCell.setCellValue("STRING");
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
- assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), formulaCell.getErrorCellValue());
-
- // Check case where both inputs are of wrong type
- bottomValueCell.setCellValue("STRING");
- topValueCell.setCellValue("STRING");
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
- assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), formulaCell.getErrorCellValue());
- }
-
- /**
- * Check case where bottom is greater than top
- */
- @Test
- void testRandBetweenBottomGreaterThanTop() {
- // Check case where bottom is greater than top
- bottomValueCell.setCellValue(1);
- topValueCell.setCellValue(0);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
- assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), formulaCell.getErrorCellValue());
- bottomValueCell.setCellValue(1);
- topValueCell.setBlank();
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
- assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), formulaCell.getErrorCellValue());
- }
-
- /**
- * Boundary check of Double MIN and MAX values
- */
- @Test
- void testRandBetweenBoundaryCheck() {
- bottomValueCell.setCellValue(Double.MIN_VALUE);
- topValueCell.setCellValue(Double.MAX_VALUE);
- formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
- evaluator.clearAllCachedResultValues();
- evaluator.evaluateFormulaCell(formulaCell);
- assertTrue(formulaCell.getNumericCellValue() >= Double.MIN_VALUE && formulaCell.getNumericCellValue() <= Double.MAX_VALUE);
- }
+ /**
+ * Check special case where rounded up bottom value is greater than
+ * top value.
+ */
+ @Test
+ void testRandBetweenSpecialCase() {
+ bottomValueCell.setCellValue(0.05);
+ topValueCell.setCellValue(0.1);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(1, formulaCell.getNumericCellValue(), 0);
+ bottomValueCell.setCellValue(-0.1);
+ topValueCell.setCellValue(-0.05);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(0, formulaCell.getNumericCellValue(), 0);
+ bottomValueCell.setCellValue(-1.1);
+ topValueCell.setCellValue(-1.05);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(-1, formulaCell.getNumericCellValue(), 0);
+ bottomValueCell.setCellValue(-1.1);
+ topValueCell.setCellValue(-1.1);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(-1, formulaCell.getNumericCellValue(), 0);
+ }
+
+ /**
+ * Check top value of BLANK which Excel will evaluate as 0
+ */
+ @Test
+ void testRandBetweenTopBlank() {
+ bottomValueCell.setCellValue(-1);
+ topValueCell.setBlank();
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertTrue(formulaCell.getNumericCellValue() == 0 || formulaCell.getNumericCellValue() == -1);
+
+ }
+ /**
+ * Check where input values are of wrong type
+ */
+ @Test
+ void testRandBetweenWrongInputTypes() {
+ // Check case where bottom input is of the wrong type
+ bottomValueCell.setCellValue("STRING");
+ topValueCell.setCellValue(1);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
+ assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), formulaCell.getErrorCellValue());
+
+
+ // Check case where top input is of the wrong type
+ bottomValueCell.setCellValue(1);
+ topValueCell.setCellValue("STRING");
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
+ assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), formulaCell.getErrorCellValue());
+
+ // Check case where both inputs are of wrong type
+ bottomValueCell.setCellValue("STRING");
+ topValueCell.setCellValue("STRING");
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
+ assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), formulaCell.getErrorCellValue());
+ }
+
+ /**
+ * Check case where bottom is greater than top
+ */
+ @Test
+ void testRandBetweenBottomGreaterThanTop() {
+ // Check case where bottom is greater than top
+ bottomValueCell.setCellValue(1);
+ topValueCell.setCellValue(0);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
+ assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), formulaCell.getErrorCellValue());
+ bottomValueCell.setCellValue(1);
+ topValueCell.setBlank();
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertEquals(CellType.ERROR, formulaCell.getCachedFormulaResultType());
+ assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), formulaCell.getErrorCellValue());
+ }
+
+ /**
+ * Boundary check of Double MIN and MAX values
+ */
+ @Test
+ void testRandBetweenBoundaryCheck() {
+ bottomValueCell.setCellValue(Double.MIN_VALUE);
+ topValueCell.setCellValue(Double.MAX_VALUE);
+ formulaCell.setCellFormula("RANDBETWEEN($A$1,$B$1)");
+ evaluator.clearAllCachedResultValues();
+ evaluator.evaluateFormulaCell(formulaCell);
+ assertTrue(formulaCell.getNumericCellValue() >= Double.MIN_VALUE && formulaCell.getNumericCellValue() <= Double.MAX_VALUE);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayCalculator.java Sat May 22 20:56:44 2021
@@ -67,28 +67,28 @@ class TestWorkdayCalculator {
assertEquals(4, WorkdayCalculator.instance.calculateWorkdays(A_FRIDAY, A_WEDNESDAY, new double[]{ A_SATURDAY, A_SUNDAY }));
}
- @Test
- void testCalculateWorkdaysOnSameDayShouldReturn1ForWeekdays() {
- final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
- assertEquals(1, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[0]));
- }
+ @Test
+ void testCalculateWorkdaysOnSameDayShouldReturn1ForWeekdays() {
+ final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
+ assertEquals(1, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[0]));
+ }
- @Test
- void testCalculateWorkdaysOnSameDayShouldReturn0ForHolidays() {
- final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
- assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[]{ A_MONDAY }));
- }
+ @Test
+ void testCalculateWorkdaysOnSameDayShouldReturn0ForHolidays() {
+ final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
+ assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[]{ A_MONDAY }));
+ }
- @Test
- void testCalculateWorkdaysOnSameDayShouldReturn0ForWeekends() {
- final double A_SUNDAY = DateUtil.getExcelDate(d(2017, 1, 1));
- assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_SUNDAY, A_SUNDAY, new double[0]));
- }
+ @Test
+ void testCalculateWorkdaysOnSameDayShouldReturn0ForWeekends() {
+ final double A_SUNDAY = DateUtil.getExcelDate(d(2017, 1, 1));
+ assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_SUNDAY, A_SUNDAY, new double[0]));
+ }
@Test
void testCalculateWorkdaysNumberOfDays() {
- double start = 41553.0;
- int days = 1;
+ double start = 41553.0;
+ int days = 1;
assertEquals(d(2013, 10, 7), WorkdayCalculator.instance.calculateWorkdays(start, days, new double[0]));
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayFunction.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestWorkdayFunction.java Sat May 22 20:56:44 2021
@@ -87,10 +87,10 @@ class TestWorkdayFunction {
Date expDate = expCal.getTime();
ValueEval[] ve = {new StringEval("2013/09/30"), new NumberEval(-1)};
- double numberValue = ((NumberEval) WorkdayFunction.instance.evaluate(ve, EC)).getNumberValue();
- assertEquals(41544.0, numberValue, 0);
+ double numberValue = ((NumberEval) WorkdayFunction.instance.evaluate(ve, EC)).getNumberValue();
+ assertEquals(41544.0, numberValue, 0);
- Date actDate = DateUtil.getJavaDate(numberValue);
+ Date actDate = DateUtil.getJavaDate(numberValue);
assertEquals(expDate, actDate);
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculator.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculator.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculator.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculator.java Sat May 22 20:56:44 2021
@@ -32,35 +32,35 @@ import org.junit.jupiter.api.Test;
final class TestYearFracCalculator {
@Test
- void testBasis1() {
- confirm(md(1999, 1, 1), md(1999, 4, 5), 1, 0.257534247);
- confirm(md(1999, 4, 1), md(1999, 4, 5), 1, 0.010958904);
- confirm(md(1999, 4, 1), md(1999, 4, 4), 1, 0.008219178);
- confirm(md(1999, 4, 2), md(1999, 4, 5), 1, 0.008219178);
- confirm(md(1999, 3, 31), md(1999, 4, 3), 1, 0.008219178);
- confirm(md(1999, 4, 5), md(1999, 4, 8), 1, 0.008219178);
- confirm(md(1999, 4, 4), md(1999, 4, 7), 1, 0.008219178);
- confirm(md(2000, 2, 5), md(2000, 6, 1), 0, 0.322222222);
- }
+ void testBasis1() {
+ confirm(md(1999, 1, 1), md(1999, 4, 5), 1, 0.257534247);
+ confirm(md(1999, 4, 1), md(1999, 4, 5), 1, 0.010958904);
+ confirm(md(1999, 4, 1), md(1999, 4, 4), 1, 0.008219178);
+ confirm(md(1999, 4, 2), md(1999, 4, 5), 1, 0.008219178);
+ confirm(md(1999, 3, 31), md(1999, 4, 3), 1, 0.008219178);
+ confirm(md(1999, 4, 5), md(1999, 4, 8), 1, 0.008219178);
+ confirm(md(1999, 4, 4), md(1999, 4, 7), 1, 0.008219178);
+ confirm(md(2000, 2, 5), md(2000, 6, 1), 0, 0.322222222);
+ }
- private void confirm(double startDate, double endDate, int basis, double expectedValue) {
- double actualValue;
- try {
- actualValue = YearFracCalculator.calculate(startDate, endDate, basis);
- } catch (EvaluationException e) {
- throw new RuntimeException(e);
- }
- double diff = actualValue - expectedValue;
- if (Math.abs(diff) > 0.000000001) {
- double hours = diff * 365 * 24;
- System.out.println(startDate + " " + endDate + " off by " + hours + " hours");
- assertEquals(expectedValue, actualValue, 0.000000001);
- }
+ private void confirm(double startDate, double endDate, int basis, double expectedValue) {
+ double actualValue;
+ try {
+ actualValue = YearFracCalculator.calculate(startDate, endDate, basis);
+ } catch (EvaluationException e) {
+ throw new RuntimeException(e);
+ }
+ double diff = actualValue - expectedValue;
+ if (Math.abs(diff) > 0.000000001) {
+ double hours = diff * 365 * 24;
+ System.out.println(startDate + " " + endDate + " off by " + hours + " hours");
+ assertEquals(expectedValue, actualValue, 0.000000001);
+ }
- }
+ }
- private static double md(int year, int month, int day) {
- Calendar c = LocaleUtil.getLocaleCalendar(year, month-1, day);
- return DateUtil.getExcelDate(c.getTime());
- }
+ private static double md(int year, int month, int day) {
+ Calendar c = LocaleUtil.getLocaleCalendar(year, month-1, day);
+ return DateUtil.getExcelDate(c.getTime());
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculatorFromSpreadsheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculatorFromSpreadsheet.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculatorFromSpreadsheet.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/atp/TestYearFracCalculatorFromSpreadsheet.java Sat May 22 20:56:44 2021
@@ -42,74 +42,74 @@ import org.junit.jupiter.api.Test;
*/
final class TestYearFracCalculatorFromSpreadsheet {
- private static final class SS {
+ private static final class SS {
- public static final int BASIS_COLUMN = 1; // "B"
- public static final int START_YEAR_COLUMN = 2; // "C"
- public static final int END_YEAR_COLUMN = 5; // "F"
- public static final int YEARFRAC_FORMULA_COLUMN = 11; // "L"
- public static final int EXPECTED_RESULT_COLUMN = 13; // "N"
- }
-
- @Test
- void testAll() throws Exception {
-
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("yearfracExamples.xls");
- HSSFSheet sheet = wb.getSheetAt(0);
- HSSFFormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator(wb);
- int nSuccess = 0;
- Iterator<Row> rowIterator = sheet.rowIterator();
- while(rowIterator.hasNext()) {
- HSSFRow row = (HSSFRow) rowIterator.next();
-
- HSSFCell cell = row.getCell(SS.YEARFRAC_FORMULA_COLUMN);
- if (cell == null || cell.getCellType() != CellType.FORMULA) {
- continue;
- }
+ public static final int BASIS_COLUMN = 1; // "B"
+ public static final int START_YEAR_COLUMN = 2; // "C"
+ public static final int END_YEAR_COLUMN = 5; // "F"
+ public static final int YEARFRAC_FORMULA_COLUMN = 11; // "L"
+ public static final int EXPECTED_RESULT_COLUMN = 13; // "N"
+ }
+
+ @Test
+ void testAll() throws Exception {
+
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("yearfracExamples.xls");
+ HSSFSheet sheet = wb.getSheetAt(0);
+ HSSFFormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator(wb);
+ int nSuccess = 0;
+ Iterator<Row> rowIterator = sheet.rowIterator();
+ while(rowIterator.hasNext()) {
+ HSSFRow row = (HSSFRow) rowIterator.next();
+
+ HSSFCell cell = row.getCell(SS.YEARFRAC_FORMULA_COLUMN);
+ if (cell == null || cell.getCellType() != CellType.FORMULA) {
+ continue;
+ }
processRow(row, cell, formulaEvaluator);
nSuccess++;
- }
+ }
- assertTrue(nSuccess > 0, "No test sample cases found");
- wb.close();
- }
-
- private void processRow(HSSFRow row, HSSFCell cell, HSSFFormulaEvaluator formulaEvaluator)
- throws EvaluationException {
-
- double startDate = makeDate(row, SS.START_YEAR_COLUMN);
- double endDate = makeDate(row, SS.END_YEAR_COLUMN);
-
- int basis = getIntCell(row, SS.BASIS_COLUMN);
-
- double expectedValue = getDoubleCell(row, SS.EXPECTED_RESULT_COLUMN);
-
- double actualValue = YearFracCalculator.calculate(startDate, endDate, basis);
-
- String loc = " - row " + (row.getRowNum()+1);
- assertEquals(expectedValue, actualValue, 0, "Direct calculate failed"+loc);
- actualValue = formulaEvaluator.evaluate(cell).getNumberValue();
- assertEquals(expectedValue, actualValue, 0, "Formula evaluate failed"+loc);
- }
-
- private static double makeDate(HSSFRow row, int yearColumn) {
- int year = getIntCell(row, yearColumn + 0);
- int month = getIntCell(row, yearColumn + 1);
- int day = getIntCell(row, yearColumn + 2);
- Calendar c = LocaleUtil.getLocaleCalendar(year, month-1, day);
- return DateUtil.getExcelDate(c.getTime());
- }
-
- private static int getIntCell(HSSFRow row, int colIx) {
- double dVal = getDoubleCell(row, colIx);
- String msg = "Non integer value (" + dVal + ") cell found at column " + (char)('A' + colIx);
- assertEquals(Math.floor(dVal), dVal, 0, msg);
- return (int)dVal;
- }
-
- private static double getDoubleCell(HSSFRow row, int colIx) {
- HSSFCell cell = row.getCell(colIx);
- assertNotNull(cell, "No cell found at column " + colIx);
+ assertTrue(nSuccess > 0, "No test sample cases found");
+ wb.close();
+ }
+
+ private void processRow(HSSFRow row, HSSFCell cell, HSSFFormulaEvaluator formulaEvaluator)
+ throws EvaluationException {
+
+ double startDate = makeDate(row, SS.START_YEAR_COLUMN);
+ double endDate = makeDate(row, SS.END_YEAR_COLUMN);
+
+ int basis = getIntCell(row, SS.BASIS_COLUMN);
+
+ double expectedValue = getDoubleCell(row, SS.EXPECTED_RESULT_COLUMN);
+
+ double actualValue = YearFracCalculator.calculate(startDate, endDate, basis);
+
+ String loc = " - row " + (row.getRowNum()+1);
+ assertEquals(expectedValue, actualValue, 0, "Direct calculate failed"+loc);
+ actualValue = formulaEvaluator.evaluate(cell).getNumberValue();
+ assertEquals(expectedValue, actualValue, 0, "Formula evaluate failed"+loc);
+ }
+
+ private static double makeDate(HSSFRow row, int yearColumn) {
+ int year = getIntCell(row, yearColumn + 0);
+ int month = getIntCell(row, yearColumn + 1);
+ int day = getIntCell(row, yearColumn + 2);
+ Calendar c = LocaleUtil.getLocaleCalendar(year, month-1, day);
+ return DateUtil.getExcelDate(c.getTime());
+ }
+
+ private static int getIntCell(HSSFRow row, int colIx) {
+ double dVal = getDoubleCell(row, colIx);
+ String msg = "Non integer value (" + dVal + ") cell found at column " + (char)('A' + colIx);
+ assertEquals(Math.floor(dVal), dVal, 0, msg);
+ return (int)dVal;
+ }
+
+ private static double getDoubleCell(HSSFRow row, int colIx) {
+ HSSFCell cell = row.getCell(colIx);
+ assertNotNull(cell, "No cell found at column " + colIx);
return cell.getNumericCellValue();
- }
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/constant/TestConstantValueParser.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/constant/TestConstantValueParser.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/constant/TestConstantValueParser.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/constant/TestConstantValueParser.java Sat May 22 20:56:44 2021
@@ -30,49 +30,49 @@ import org.apache.poi.util.LittleEndianI
import org.junit.jupiter.api.Test;
final class TestConstantValueParser {
- private static final Object[] SAMPLE_VALUES = {
- Boolean.TRUE,
- null,
- 1.1,
- "Sample text",
- ErrorConstant.valueOf(FormulaError.DIV0.getCode()),
- };
- private static final byte[] SAMPLE_ENCODING = HexRead.readFromString(
- "04 01 00 00 00 00 00 00 00 " +
- "00 00 00 00 00 00 00 00 00 " +
- "01 9A 99 99 99 99 99 F1 3F " +
- "02 0B 00 00 53 61 6D 70 6C 65 20 74 65 78 74 " +
- "10 07 00 00 00 00 00 00 00");
-
- @Test
- void testGetEncodedSize() {
- int actual = ConstantValueParser.getEncodedSize(SAMPLE_VALUES);
- assertEquals(51, actual);
- }
-
- @Test
- void testEncode() {
- int size = ConstantValueParser.getEncodedSize(SAMPLE_VALUES);
- byte[] data = new byte[size];
-
- ConstantValueParser.encode(new LittleEndianByteArrayOutputStream(data, 0), SAMPLE_VALUES);
-
- assertArrayEquals(SAMPLE_ENCODING, data, "Encoding differs");
- }
-
- @Test
- void testDecode() {
- LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(SAMPLE_ENCODING);
-
- Object[] values = ConstantValueParser.parse(in, 4);
- for (int i = 0; i < values.length; i++) {
- assertTrue(isEqual(SAMPLE_VALUES[i], values[i]), "Decoded result differs");
- }
- }
- private static boolean isEqual(Object a, Object b) {
- if (a == null) {
- return b == null;
- }
- return a.equals(b);
- }
+ private static final Object[] SAMPLE_VALUES = {
+ Boolean.TRUE,
+ null,
+ 1.1,
+ "Sample text",
+ ErrorConstant.valueOf(FormulaError.DIV0.getCode()),
+ };
+ private static final byte[] SAMPLE_ENCODING = HexRead.readFromString(
+ "04 01 00 00 00 00 00 00 00 " +
+ "00 00 00 00 00 00 00 00 00 " +
+ "01 9A 99 99 99 99 99 F1 3F " +
+ "02 0B 00 00 53 61 6D 70 6C 65 20 74 65 78 74 " +
+ "10 07 00 00 00 00 00 00 00");
+
+ @Test
+ void testGetEncodedSize() {
+ int actual = ConstantValueParser.getEncodedSize(SAMPLE_VALUES);
+ assertEquals(51, actual);
+ }
+
+ @Test
+ void testEncode() {
+ int size = ConstantValueParser.getEncodedSize(SAMPLE_VALUES);
+ byte[] data = new byte[size];
+
+ ConstantValueParser.encode(new LittleEndianByteArrayOutputStream(data, 0), SAMPLE_VALUES);
+
+ assertArrayEquals(SAMPLE_ENCODING, data, "Encoding differs");
+ }
+
+ @Test
+ void testDecode() {
+ LittleEndianInput in = TestcaseRecordInputStream.createLittleEndian(SAMPLE_ENCODING);
+
+ Object[] values = ConstantValueParser.parse(in, 4);
+ for (int i = 0; i < values.length; i++) {
+ assertTrue(isEqual(SAMPLE_VALUES[i], values[i]), "Decoded result differs");
+ }
+ }
+ private static boolean isEqual(Object a, Object b) {
+ if (a == null) {
+ return b == null;
+ }
+ return a.equals(b);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/EvalInstances.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/EvalInstances.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/EvalInstances.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/EvalInstances.java Sat May 22 20:56:44 2021
@@ -23,29 +23,29 @@ import org.apache.poi.ss.formula.functio
* Collects eval instances for easy access by tests in this package
*/
final class EvalInstances {
- private EvalInstances() {
- // no instances of this class
- }
-
- public static final Function Add = TwoOperandNumericOperation.AddEval;
- public static final Function Subtract = TwoOperandNumericOperation.SubtractEval;
- public static final Function Multiply = TwoOperandNumericOperation.MultiplyEval;
- public static final Function Divide = TwoOperandNumericOperation.DivideEval;
-
- public static final Function Power = TwoOperandNumericOperation.PowerEval;
-
- public static final Function Percent = PercentEval.instance;
-
- public static final Function UnaryMinus = UnaryMinusEval.instance;
- public static final Function UnaryPlus = UnaryPlusEval.instance;
-
- public static final Function Equal = RelationalOperationEval.EqualEval;
- public static final Function LessThan = RelationalOperationEval.LessThanEval;
- public static final Function LessEqual = RelationalOperationEval.LessEqualEval;
- public static final Function GreaterThan = RelationalOperationEval.GreaterThanEval;
- public static final Function GreaterEqual = RelationalOperationEval.GreaterEqualEval;
- public static final Function NotEqual = RelationalOperationEval.NotEqualEval;
+ private EvalInstances() {
+ // no instances of this class
+ }
+
+ public static final Function Add = TwoOperandNumericOperation.AddEval;
+ public static final Function Subtract = TwoOperandNumericOperation.SubtractEval;
+ public static final Function Multiply = TwoOperandNumericOperation.MultiplyEval;
+ public static final Function Divide = TwoOperandNumericOperation.DivideEval;
+
+ public static final Function Power = TwoOperandNumericOperation.PowerEval;
+
+ public static final Function Percent = PercentEval.instance;
+
+ public static final Function UnaryMinus = UnaryMinusEval.instance;
+ public static final Function UnaryPlus = UnaryPlusEval.instance;
+
+ public static final Function Equal = RelationalOperationEval.EqualEval;
+ public static final Function LessThan = RelationalOperationEval.LessThanEval;
+ public static final Function LessEqual = RelationalOperationEval.LessEqualEval;
+ public static final Function GreaterThan = RelationalOperationEval.GreaterThanEval;
+ public static final Function GreaterEqual = RelationalOperationEval.GreaterEqualEval;
+ public static final Function NotEqual = RelationalOperationEval.NotEqualEval;
- public static final Function Range = RangeEval.instance;
- public static final Function Concat = ConcatEval.instance;
+ public static final Function Range = RangeEval.instance;
+ public static final Function Concat = ConcatEval.instance;
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestAreaEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestAreaEval.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestAreaEval.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestAreaEval.java Sat May 22 20:56:44 2021
@@ -32,32 +32,32 @@ import org.junit.jupiter.api.Test;
*/
final class TestAreaEval {
- @Test
- void testGetValue_bug44950() {
- // TODO - this test probably isn't testing much anymore
- AreaPtg ptg = new AreaPtg(new AreaReference("B2:D3", SpreadsheetVersion.EXCEL97));
- NumberEval one = new NumberEval(1);
- ValueEval[] values = {
- one,
- new NumberEval(2),
- new NumberEval(3),
- new NumberEval(4),
- new NumberEval(5),
- new NumberEval(6),
- };
- AreaEval ae = EvalFactory.createAreaEval(ptg, values);
- assertNotEquals(one, ae.getAbsoluteValue(1, 2), "Identified bug 44950 a");
- confirm(1, ae, 1, 1);
- confirm(2, ae, 1, 2);
- confirm(3, ae, 1, 3);
- confirm(4, ae, 2, 1);
- confirm(5, ae, 2, 2);
- confirm(6, ae, 2, 3);
+ @Test
+ void testGetValue_bug44950() {
+ // TODO - this test probably isn't testing much anymore
+ AreaPtg ptg = new AreaPtg(new AreaReference("B2:D3", SpreadsheetVersion.EXCEL97));
+ NumberEval one = new NumberEval(1);
+ ValueEval[] values = {
+ one,
+ new NumberEval(2),
+ new NumberEval(3),
+ new NumberEval(4),
+ new NumberEval(5),
+ new NumberEval(6),
+ };
+ AreaEval ae = EvalFactory.createAreaEval(ptg, values);
+ assertNotEquals(one, ae.getAbsoluteValue(1, 2), "Identified bug 44950 a");
+ confirm(1, ae, 1, 1);
+ confirm(2, ae, 1, 2);
+ confirm(3, ae, 1, 3);
+ confirm(4, ae, 2, 1);
+ confirm(5, ae, 2, 2);
+ confirm(6, ae, 2, 3);
- }
+ }
- private static void confirm(int expectedValue, AreaEval ae, int row, int col) {
- NumberEval v = (NumberEval) ae.getAbsoluteValue(row, col);
- assertEquals(expectedValue, v.getNumberValue(), 0.0);
- }
+ private static void confirm(int expectedValue, AreaEval ae, int row, int col) {
+ NumberEval v = (NumberEval) ae.getAbsoluteValue(row, col);
+ assertEquals(expectedValue, v.getNumberValue(), 0.0);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestDivideEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestDivideEval.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestDivideEval.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestDivideEval.java Sat May 22 20:56:44 2021
@@ -28,38 +28,38 @@ import org.junit.jupiter.api.Test;
*/
final class TestDivideEval {
- private static void confirm(ValueEval arg0, ValueEval arg1, double expectedResult) {
- ValueEval[] args = {
- arg0, arg1,
- };
-
- double result = NumericFunctionInvoker.invoke(EvalInstances.Divide, args, 0, 0);
-
- assertEquals(expectedResult, result, 0);
- }
-
- @Test
- void testBasic() {
- confirm(new NumberEval(5), new NumberEval(2), 2.5);
- confirm(new NumberEval(3), new NumberEval(16), 0.1875);
- confirm(new NumberEval(-150), new NumberEval(-15), 10.0);
- confirm(new StringEval("0.2"), new NumberEval(0.05), 4.0);
- confirm(BoolEval.TRUE, new StringEval("-0.2"), -5.0);
- }
-
- @Test
- void test1x1Area() {
- AreaEval ae0 = EvalFactory.createAreaEval("B2:B2", new ValueEval[] { new NumberEval(50), });
- AreaEval ae1 = EvalFactory.createAreaEval("C2:C2", new ValueEval[] { new NumberEval(10), });
- confirm(ae0, ae1, 5);
- }
-
- @Test
- void testDivZero() {
- ValueEval[] args = {
- new NumberEval(5), NumberEval.ZERO,
- };
- ValueEval result = EvalInstances.Divide.evaluate(args, 0, (short) 0);
- assertEquals(ErrorEval.DIV_ZERO, result);
- }
+ private static void confirm(ValueEval arg0, ValueEval arg1, double expectedResult) {
+ ValueEval[] args = {
+ arg0, arg1,
+ };
+
+ double result = NumericFunctionInvoker.invoke(EvalInstances.Divide, args, 0, 0);
+
+ assertEquals(expectedResult, result, 0);
+ }
+
+ @Test
+ void testBasic() {
+ confirm(new NumberEval(5), new NumberEval(2), 2.5);
+ confirm(new NumberEval(3), new NumberEval(16), 0.1875);
+ confirm(new NumberEval(-150), new NumberEval(-15), 10.0);
+ confirm(new StringEval("0.2"), new NumberEval(0.05), 4.0);
+ confirm(BoolEval.TRUE, new StringEval("-0.2"), -5.0);
+ }
+
+ @Test
+ void test1x1Area() {
+ AreaEval ae0 = EvalFactory.createAreaEval("B2:B2", new ValueEval[] { new NumberEval(50), });
+ AreaEval ae1 = EvalFactory.createAreaEval("C2:C2", new ValueEval[] { new NumberEval(10), });
+ confirm(ae0, ae1, 5);
+ }
+
+ @Test
+ void testDivZero() {
+ ValueEval[] args = {
+ new NumberEval(5), NumberEval.ZERO,
+ };
+ ValueEval result = EvalInstances.Divide.evaluate(args, 0, (short) 0);
+ assertEquals(ErrorEval.DIV_ZERO, result);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestEqualEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestEqualEval.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestEqualEval.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestEqualEval.java Sat May 22 20:56:44 2021
@@ -29,132 +29,132 @@ import org.junit.jupiter.api.Test;
* Test for EqualEval
*/
final class TestEqualEval {
- /**
- * Test for bug observable at svn revision 692218 (Sep 2008)<br>
- * The value from a 1x1 area should be taken immediately, regardless of srcRow and srcCol
- */
- @Test
- void test1x1AreaOperand() {
-
- ValueEval[] values = { BoolEval.FALSE, };
- ValueEval[] args = {
- EvalFactory.createAreaEval("B1:B1", values),
- BoolEval.FALSE,
- };
- ValueEval result = evaluate(EvalInstances.Equal, args, 10, 10);
- assertNotEquals(ErrorEval.VALUE_INVALID, result, "Identified bug in evaluation of 1x1 area");
- assertTrue(result instanceof BoolEval);
- assertTrue(((BoolEval)result).getBooleanValue());
- }
-
- /**
- * Empty string is equal to blank
- */
- @Test
- void testBlankEqualToEmptyString() {
-
- ValueEval[] args = {
- new StringEval(""),
- BlankEval.instance,
- };
- ValueEval result = evaluate(EvalInstances.Equal, args, 10, 10);
- assertEquals(BoolEval.class, result.getClass());
- BoolEval be = (BoolEval) result;
- assertTrue(be.getBooleanValue(), "Identified bug blank/empty string equality");
- }
-
- /**
- * Test for bug 46613 (observable at svn r737248)
- */
- @Test
- void testStringInsensitive_bug46613() {
- assertTrue(evalStringCmp("abc", "aBc", EvalInstances.Equal));
- assertTrue(evalStringCmp("ABC", "azz", EvalInstances.LessThan));
- assertTrue(evalStringCmp("abc", "AZZ", EvalInstances.LessThan));
- assertTrue(evalStringCmp("ABC", "aaa", EvalInstances.GreaterThan));
- assertTrue(evalStringCmp("abc", "AAA", EvalInstances.GreaterThan));
- }
-
- private static boolean evalStringCmp(String a, String b, Function cmpOp) {
- ValueEval[] args = {
- new StringEval(a),
- new StringEval(b),
- };
- ValueEval result = evaluate(cmpOp, args, 10, 20);
- assertEquals(BoolEval.class, result.getClass());
- BoolEval be = (BoolEval) result;
- return be.getBooleanValue();
- }
-
- @Test
- void testBooleanCompares() {
- confirmCompares(BoolEval.TRUE, new StringEval("TRUE"), +1);
- confirmCompares(BoolEval.TRUE, new NumberEval(1.0), +1);
- confirmCompares(BoolEval.TRUE, BoolEval.TRUE, 0);
- confirmCompares(BoolEval.TRUE, BoolEval.FALSE, +1);
-
- confirmCompares(BoolEval.FALSE, new StringEval("TRUE"), +1);
- confirmCompares(BoolEval.FALSE, new StringEval("FALSE"), +1);
- confirmCompares(BoolEval.FALSE, new NumberEval(0.0), +1);
- confirmCompares(BoolEval.FALSE, BoolEval.FALSE, 0);
- }
-
- private static void confirmCompares(ValueEval a, ValueEval b, int expRes) {
- confirm(a, b, expRes>0, EvalInstances.GreaterThan);
- confirm(a, b, expRes>=0, EvalInstances.GreaterEqual);
- confirm(a, b, expRes==0, EvalInstances.Equal);
- confirm(a, b, expRes<=0, EvalInstances.LessEqual);
- confirm(a, b, expRes<0, EvalInstances.LessThan);
-
- confirm(b, a, expRes<0, EvalInstances.GreaterThan);
- confirm(b, a, expRes<=0, EvalInstances.GreaterEqual);
- confirm(b, a, expRes==0, EvalInstances.Equal);
- confirm(b, a, expRes>=0, EvalInstances.LessEqual);
- confirm(b, a, expRes>0, EvalInstances.LessThan);
- }
-
- private static void confirm(ValueEval a, ValueEval b, boolean expectedResult, Function cmpOp) {
- ValueEval[] args = { a, b, };
- ValueEval result = evaluate(cmpOp, args, 10, 20);
- assertEquals(BoolEval.class, result.getClass());
- assertEquals(expectedResult, ((BoolEval) result).getBooleanValue());
- }
-
- /**
- * Bug 47198 involved a formula "-A1=0" where cell A1 was 0.0.
- * Excel evaluates "-A1=0" to TRUE, not because it thinks -0.0==0.0
- * but because "-A1" evaluated to +0.0
- * <p>
- * Note - the original diagnosis of bug 47198 was that
- * "Excel considers -0.0 to be equal to 0.0" which is NQR
- * See {@link TestMinusZeroResult} for more specific tests regarding -0.0.
- */
- @Test
- void testZeroEquality_bug47198() {
- NumberEval zero = new NumberEval(0.0);
- NumberEval mZero = (NumberEval) evaluate(UnaryMinusEval.instance, new ValueEval[] { zero, }, 0, 0);
- assertNotEquals(0x8000000000000000L, Double.doubleToLongBits(mZero.getNumberValue()),
- "Identified bug 47198: unary minus should convert -0.0 to 0.0");
- ValueEval[] args = { zero, mZero, };
- BoolEval result = (BoolEval) evaluate(EvalInstances.Equal, args, 0, 0);
- assertTrue(result.getBooleanValue(), "Identified bug 47198: -0.0 != 0.0");
- }
-
- @Test
- void testRounding_bug47598() {
- double x = 1+1.0028-0.9973; // should be 1.0055, but has IEEE rounding
- assertNotEquals(1.0055, x, 0.0);
-
- NumberEval a = new NumberEval(x);
- NumberEval b = new NumberEval(1.0055);
- assertEquals("1.0055", b.getStringValue());
-
- ValueEval[] args = { a, b, };
- BoolEval result = (BoolEval) evaluate(EvalInstances.Equal, args, 0, 0);
- assertTrue(result.getBooleanValue(), "Identified bug 47598: 1+1.0028-0.9973 != 1.0055");
- }
-
- private static ValueEval evaluate(Function oper, ValueEval[] args, int srcRowIx, int srcColIx) {
- return oper.evaluate(args, srcRowIx, (short) srcColIx);
- }
+ /**
+ * Test for bug observable at svn revision 692218 (Sep 2008)<br>
+ * The value from a 1x1 area should be taken immediately, regardless of srcRow and srcCol
+ */
+ @Test
+ void test1x1AreaOperand() {
+
+ ValueEval[] values = { BoolEval.FALSE, };
+ ValueEval[] args = {
+ EvalFactory.createAreaEval("B1:B1", values),
+ BoolEval.FALSE,
+ };
+ ValueEval result = evaluate(EvalInstances.Equal, args, 10, 10);
+ assertNotEquals(ErrorEval.VALUE_INVALID, result, "Identified bug in evaluation of 1x1 area");
+ assertTrue(result instanceof BoolEval);
+ assertTrue(((BoolEval)result).getBooleanValue());
+ }
+
+ /**
+ * Empty string is equal to blank
+ */
+ @Test
+ void testBlankEqualToEmptyString() {
+
+ ValueEval[] args = {
+ new StringEval(""),
+ BlankEval.instance,
+ };
+ ValueEval result = evaluate(EvalInstances.Equal, args, 10, 10);
+ assertEquals(BoolEval.class, result.getClass());
+ BoolEval be = (BoolEval) result;
+ assertTrue(be.getBooleanValue(), "Identified bug blank/empty string equality");
+ }
+
+ /**
+ * Test for bug 46613 (observable at svn r737248)
+ */
+ @Test
+ void testStringInsensitive_bug46613() {
+ assertTrue(evalStringCmp("abc", "aBc", EvalInstances.Equal));
+ assertTrue(evalStringCmp("ABC", "azz", EvalInstances.LessThan));
+ assertTrue(evalStringCmp("abc", "AZZ", EvalInstances.LessThan));
+ assertTrue(evalStringCmp("ABC", "aaa", EvalInstances.GreaterThan));
+ assertTrue(evalStringCmp("abc", "AAA", EvalInstances.GreaterThan));
+ }
+
+ private static boolean evalStringCmp(String a, String b, Function cmpOp) {
+ ValueEval[] args = {
+ new StringEval(a),
+ new StringEval(b),
+ };
+ ValueEval result = evaluate(cmpOp, args, 10, 20);
+ assertEquals(BoolEval.class, result.getClass());
+ BoolEval be = (BoolEval) result;
+ return be.getBooleanValue();
+ }
+
+ @Test
+ void testBooleanCompares() {
+ confirmCompares(BoolEval.TRUE, new StringEval("TRUE"), +1);
+ confirmCompares(BoolEval.TRUE, new NumberEval(1.0), +1);
+ confirmCompares(BoolEval.TRUE, BoolEval.TRUE, 0);
+ confirmCompares(BoolEval.TRUE, BoolEval.FALSE, +1);
+
+ confirmCompares(BoolEval.FALSE, new StringEval("TRUE"), +1);
+ confirmCompares(BoolEval.FALSE, new StringEval("FALSE"), +1);
+ confirmCompares(BoolEval.FALSE, new NumberEval(0.0), +1);
+ confirmCompares(BoolEval.FALSE, BoolEval.FALSE, 0);
+ }
+
+ private static void confirmCompares(ValueEval a, ValueEval b, int expRes) {
+ confirm(a, b, expRes>0, EvalInstances.GreaterThan);
+ confirm(a, b, expRes>=0, EvalInstances.GreaterEqual);
+ confirm(a, b, expRes==0, EvalInstances.Equal);
+ confirm(a, b, expRes<=0, EvalInstances.LessEqual);
+ confirm(a, b, expRes<0, EvalInstances.LessThan);
+
+ confirm(b, a, expRes<0, EvalInstances.GreaterThan);
+ confirm(b, a, expRes<=0, EvalInstances.GreaterEqual);
+ confirm(b, a, expRes==0, EvalInstances.Equal);
+ confirm(b, a, expRes>=0, EvalInstances.LessEqual);
+ confirm(b, a, expRes>0, EvalInstances.LessThan);
+ }
+
+ private static void confirm(ValueEval a, ValueEval b, boolean expectedResult, Function cmpOp) {
+ ValueEval[] args = { a, b, };
+ ValueEval result = evaluate(cmpOp, args, 10, 20);
+ assertEquals(BoolEval.class, result.getClass());
+ assertEquals(expectedResult, ((BoolEval) result).getBooleanValue());
+ }
+
+ /**
+ * Bug 47198 involved a formula "-A1=0" where cell A1 was 0.0.
+ * Excel evaluates "-A1=0" to TRUE, not because it thinks -0.0==0.0
+ * but because "-A1" evaluated to +0.0
+ * <p>
+ * Note - the original diagnosis of bug 47198 was that
+ * "Excel considers -0.0 to be equal to 0.0" which is NQR
+ * See {@link TestMinusZeroResult} for more specific tests regarding -0.0.
+ */
+ @Test
+ void testZeroEquality_bug47198() {
+ NumberEval zero = new NumberEval(0.0);
+ NumberEval mZero = (NumberEval) evaluate(UnaryMinusEval.instance, new ValueEval[] { zero, }, 0, 0);
+ assertNotEquals(0x8000000000000000L, Double.doubleToLongBits(mZero.getNumberValue()),
+ "Identified bug 47198: unary minus should convert -0.0 to 0.0");
+ ValueEval[] args = { zero, mZero, };
+ BoolEval result = (BoolEval) evaluate(EvalInstances.Equal, args, 0, 0);
+ assertTrue(result.getBooleanValue(), "Identified bug 47198: -0.0 != 0.0");
+ }
+
+ @Test
+ void testRounding_bug47598() {
+ double x = 1+1.0028-0.9973; // should be 1.0055, but has IEEE rounding
+ assertNotEquals(1.0055, x, 0.0);
+
+ NumberEval a = new NumberEval(x);
+ NumberEval b = new NumberEval(1.0055);
+ assertEquals("1.0055", b.getStringValue());
+
+ ValueEval[] args = { a, b, };
+ BoolEval result = (BoolEval) evaluate(EvalInstances.Equal, args, 0, 0);
+ assertTrue(result.getBooleanValue(), "Identified bug 47598: 1+1.0028-0.9973 != 1.0055");
+ }
+
+ private static ValueEval evaluate(Function oper, ValueEval[] args, int srcRowIx, int srcColIx) {
+ return oper.evaluate(args, srcRowIx, (short) srcColIx);
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestExternalFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestExternalFunction.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestExternalFunction.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestExternalFunction.java Sat May 22 20:56:44 2021
@@ -36,48 +36,48 @@ import org.junit.jupiter.api.Test;
final class TestExternalFunction {
- private static ValueEval myFunc1(ValueEval[] args, OperationEvaluationContext ec) {
- if (args.length != 1 || !(args[0] instanceof StringEval)) {
- return ErrorEval.VALUE_INVALID;
- }
- StringEval input = (StringEval) args[0];
- return new StringEval(input.getStringValue() + "abc");
- }
-
- private static ValueEval myFunc2(ValueEval[] args, OperationEvaluationContext ec) {
- if (args.length != 1 || !(args[0] instanceof StringEval)) {
- return ErrorEval.VALUE_INVALID;
- }
- StringEval input = (StringEval) args[0];
- return new StringEval(input.getStringValue() + "abc2");
- }
-
- /**
- * Checks that an external function can get invoked from the formula evaluator.
- */
- @Test
- void testInvoke() throws IOException {
- try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls")) {
- HSSFSheet sheet = wb.getSheetAt(0);
-
- /*
- * register the two test UDFs in a UDF finder, to be passed to the evaluator
- */
- UDFFinder udff1 = new DefaultUDFFinder(new String[]{"myFunc",},
- new FreeRefFunction[]{TestExternalFunction::myFunc1});
- UDFFinder udff2 = new DefaultUDFFinder(new String[]{"myFunc2",},
- new FreeRefFunction[]{TestExternalFunction::myFunc2,});
- UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
-
-
- HSSFRow row = sheet.getRow(0);
- HSSFCell myFuncCell = row.getCell(1); // =myFunc("_")
-
- HSSFCell myFunc2Cell = row.getCell(2); // =myFunc2("_")
-
- HSSFFormulaEvaluator fe = HSSFFormulaEvaluator.create(wb, null, udff);
- assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
- assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
- }
- }
+ private static ValueEval myFunc1(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length != 1 || !(args[0] instanceof StringEval)) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ StringEval input = (StringEval) args[0];
+ return new StringEval(input.getStringValue() + "abc");
+ }
+
+ private static ValueEval myFunc2(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length != 1 || !(args[0] instanceof StringEval)) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ StringEval input = (StringEval) args[0];
+ return new StringEval(input.getStringValue() + "abc2");
+ }
+
+ /**
+ * Checks that an external function can get invoked from the formula evaluator.
+ */
+ @Test
+ void testInvoke() throws IOException {
+ try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls")) {
+ HSSFSheet sheet = wb.getSheetAt(0);
+
+ /*
+ * register the two test UDFs in a UDF finder, to be passed to the evaluator
+ */
+ UDFFinder udff1 = new DefaultUDFFinder(new String[]{"myFunc",},
+ new FreeRefFunction[]{TestExternalFunction::myFunc1});
+ UDFFinder udff2 = new DefaultUDFFinder(new String[]{"myFunc2",},
+ new FreeRefFunction[]{TestExternalFunction::myFunc2,});
+ UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
+
+
+ HSSFRow row = sheet.getRow(0);
+ HSSFCell myFuncCell = row.getCell(1); // =myFunc("_")
+
+ HSSFCell myFunc2Cell = row.getCell(2); // =myFunc2("_")
+
+ HSSFFormulaEvaluator fe = HSSFFormulaEvaluator.create(wb, null, udff);
+ assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
+ assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
+ }
+ }
}
Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java?rev=1890120&r1=1890119&r2=1890120&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/eval/TestFormulaBugs.java Sat May 22 20:56:44 2021
@@ -41,221 +41,221 @@ import org.junit.jupiter.api.Test;
*/
final class TestFormulaBugs {
- /**
- * Bug 27349 - VLOOKUP with reference to another sheet.<p> This test was
- * added <em>long</em> after the relevant functionality was fixed.
- */
- @Test
- void test27349() throws Exception {
- // 27349-vlookupAcrossSheets.xls is bugzilla/attachment.cgi?id=10622
- InputStream is = HSSFTestDataSamples.openSampleFileStream("27349-vlookupAcrossSheets.xls");
- // original bug may have thrown exception here,
- // or output warning to stderr
- Workbook wb = new HSSFWorkbook(is);
-
- Sheet sheet = wb.getSheetAt(0);
- Row row = sheet.getRow(1);
- Cell cell = row.getCell(0);
-
- // this definitely would have failed due to 27349
- assertEquals("VLOOKUP(1,'DATA TABLE'!$A$8:'DATA TABLE'!$B$10,2)", cell
- .getCellFormula());
-
- // We might as well evaluate the formula
- FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
- CellValue cv = fe.evaluate(cell);
-
- assertEquals(CellType.NUMERIC, cv.getCellType());
- assertEquals(3.0, cv.getNumberValue(), 0.0);
-
- wb.close();
- is.close();
- }
-
- /**
- * Bug 27405 - isnumber() formula always evaluates to false in if statement<p>
- *
- * seems to be a duplicate of 24925
- */
- @Test
- void test27405() throws Exception {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("input");
- // input row 0
- Row row = sheet.createRow(0);
- /*Cell cell =*/ row.createCell(0);
- Cell cell = row.createCell(1);
- cell.setCellValue(1); // B1
- // input row 1
- row = sheet.createRow(1);
- cell = row.createCell(1);
- cell.setCellValue(999); // B2
-
- int rno = 4;
- row = sheet.createRow(rno);
- cell = row.createCell(1); // B5
- cell.setCellFormula("isnumber(b1)");
- cell = row.createCell(3); // D5
- cell.setCellFormula("IF(ISNUMBER(b1),b1,b2)");
-
-// if (false) { // set true to check excel file manually
-// // bug report mentions 'Editing the formula in excel "fixes" the problem.'
-// try {
-// FileOutputStream fileOut = new FileOutputStream("27405output.xls");
-// wb.write(fileOut);
-// fileOut.close();
-// } catch (IOException e) {
-// throw new RuntimeException(e);
-// }
-// }
-
- // use POI's evaluator as an extra sanity check
- FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
- CellValue cv;
- cv = fe.evaluate(cell);
- assertEquals(CellType.NUMERIC, cv.getCellType());
- assertEquals(1.0, cv.getNumberValue(), 0.0);
+ /**
+ * Bug 27349 - VLOOKUP with reference to another sheet.<p> This test was
+ * added <em>long</em> after the relevant functionality was fixed.
+ */
+ @Test
+ void test27349() throws Exception {
+ // 27349-vlookupAcrossSheets.xls is bugzilla/attachment.cgi?id=10622
+ InputStream is = HSSFTestDataSamples.openSampleFileStream("27349-vlookupAcrossSheets.xls");
+ // original bug may have thrown exception here,
+ // or output warning to stderr
+ Workbook wb = new HSSFWorkbook(is);
+
+ Sheet sheet = wb.getSheetAt(0);
+ Row row = sheet.getRow(1);
+ Cell cell = row.getCell(0);
+
+ // this definitely would have failed due to 27349
+ assertEquals("VLOOKUP(1,'DATA TABLE'!$A$8:'DATA TABLE'!$B$10,2)", cell
+ .getCellFormula());
+
+ // We might as well evaluate the formula
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+ CellValue cv = fe.evaluate(cell);
+
+ assertEquals(CellType.NUMERIC, cv.getCellType());
+ assertEquals(3.0, cv.getNumberValue(), 0.0);
+
+ wb.close();
+ is.close();
+ }
+
+ /**
+ * Bug 27405 - isnumber() formula always evaluates to false in if statement<p>
+ *
+ * seems to be a duplicate of 24925
+ */
+ @Test
+ void test27405() throws Exception {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("input");
+ // input row 0
+ Row row = sheet.createRow(0);
+ /*Cell cell =*/ row.createCell(0);
+ Cell cell = row.createCell(1);
+ cell.setCellValue(1); // B1
+ // input row 1
+ row = sheet.createRow(1);
+ cell = row.createCell(1);
+ cell.setCellValue(999); // B2
+
+ int rno = 4;
+ row = sheet.createRow(rno);
+ cell = row.createCell(1); // B5
+ cell.setCellFormula("isnumber(b1)");
+ cell = row.createCell(3); // D5
+ cell.setCellFormula("IF(ISNUMBER(b1),b1,b2)");
+
+// if (false) { // set true to check excel file manually
+// // bug report mentions 'Editing the formula in excel "fixes" the problem.'
+// try {
+// FileOutputStream fileOut = new FileOutputStream("27405output.xls");
+// wb.write(fileOut);
+// fileOut.close();
+// } catch (IOException e) {
+// throw new RuntimeException(e);
+// }
+// }
+
+ // use POI's evaluator as an extra sanity check
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+ CellValue cv;
+ cv = fe.evaluate(cell);
+ assertEquals(CellType.NUMERIC, cv.getCellType());
+ assertEquals(1.0, cv.getNumberValue(), 0.0);
- cv = fe.evaluate(row.getCell(1));
- assertEquals(CellType.BOOLEAN, cv.getCellType());
+ cv = fe.evaluate(row.getCell(1));
+ assertEquals(CellType.BOOLEAN, cv.getCellType());
assertTrue(cv.getBooleanValue());
- wb.close();
- }
+ wb.close();
+ }
- /**
- * Bug 42448 - Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69 <p>
- */
- @Test
- void test42448() throws IOException {
- try (Workbook wb = new HSSFWorkbook()) {
- Sheet sheet1 = wb.createSheet("Sheet1");
-
- Row row = sheet1.createRow(0);
- Cell cell = row.createCell(0);
-
- // it's important to create the referenced sheet first
- Sheet sheet2 = wb.createSheet("A"); // note name 'A'
- // TODO - POI crashes if the formula is added before this sheet
- // RuntimeException("Zero length string is an invalid sheet name")
- // Excel doesn't crash but the formula doesn't work until it is
- // re-entered
-
- String inputFormula = "SUMPRODUCT(A!C7:A!C67, B8:B68) / B69"; // as per bug report
- try {
- cell.setCellFormula(inputFormula);
- } catch (StringIndexOutOfBoundsException e) {
- fail("Identified bug 42448");
- }
-
- assertEquals("SUMPRODUCT(A!C7:A!C67,B8:B68)/B69", cell.getCellFormula());
-
- // might as well evaluate the sucker...
-
- addCell(sheet2, 5, 2, 3.0); // A!C6
- addCell(sheet2, 6, 2, 4.0); // A!C7
- addCell(sheet2, 66, 2, 5.0); // A!C67
- addCell(sheet2, 67, 2, 6.0); // A!C68
-
- addCell(sheet1, 6, 1, 7.0); // B7
- addCell(sheet1, 7, 1, 8.0); // B8
- addCell(sheet1, 67, 1, 9.0); // B68
- addCell(sheet1, 68, 1, 10.0); // B69
-
- double expectedResult = (4.0 * 8.0 + 5.0 * 9.0) / 10.0;
-
- FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
- CellValue cv = fe.evaluate(cell);
-
- assertEquals(CellType.NUMERIC, cv.getCellType());
- assertEquals(expectedResult, cv.getNumberValue(), 0.0);
- }
- }
-
- private static void addCell(Sheet sheet, int rowIx, int colIx,
- double value) {
- sheet.createRow(rowIx).createCell(colIx).setCellValue(value);
- }
-
- @Test
- void test55032() throws IOException {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("input");
-
- Row row = sheet.createRow(0);
- Cell cell = row.createCell(1);
-
- checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, ,0)", -59777.14585);
- checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, ,)", -59777.14585);
- checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, 500,)", -59878.6315455);
-
- checkFormulaValue(wb, cell, "FV(0.08/12, 20*12, 500, ,)", -294510.2078107270);
- checkFormulaValue(wb, cell, "PMT(0.08/12, 20*12, 500, ,)", -4.1822003450);
- checkFormulaValue(wb, cell, "NPER(0.08/12, 20*12, 500, ,)", -2.0758873434);
-
- wb.close();
- }
-
- // bug 52063: LOOKUP(2-arg) and LOOKUP(3-arg)
- // FIXME: This could be moved into LookupFunctionsTestCaseData.xls, which is tested by TestLookupFunctionsFromSpreadsheet.java
- @Test
- void testLookupFormula() throws Exception {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("52063");
-
- // Note: Values in arrays are in ascending order since LOOKUP expects that in order to work properly
- // column
- // A B C
- // +-------
- // row 1 | P Q R
- // row 2 | X Y Z
- Row row = sheet.createRow(0);
- row.createCell(0).setCellValue("P");
- row.createCell(1).setCellValue("Q");
- row.createCell(2).setCellValue("R");
- row = sheet.createRow(1);
- row.createCell(0).setCellValue("X");
- row.createCell(1).setCellValue("Y");
- row.createCell(2).setCellValue("Z");
-
- Cell evalcell = sheet.createRow(2).createCell(0);
-
- //// ROW VECTORS
- // lookup and result row are the same
- checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C1)", "Q");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C1)", "R");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C1, A1:C1)", "Q");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C1, A1:C1)", "R");
-
- // lookup and result row are different
- checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C2)", "Y");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C2)", "Z");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C1, A2:C2)", "Y");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C1, A2:C2)", "Z");
-
- //// COLUMN VECTORS
- // lookup and result column are different
- checkFormulaValue(wb, evalcell, "LOOKUP(\"P\", A1:B2)", "Q");
- checkFormulaValue(wb, evalcell, "LOOKUP(\"X\", A1:A2, C1:C2)", "Z");
+ /**
+ * Bug 42448 - Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69 <p>
+ */
+ @Test
+ void test42448() throws IOException {
+ try (Workbook wb = new HSSFWorkbook()) {
+ Sheet sheet1 = wb.createSheet("Sheet1");
+
+ Row row = sheet1.createRow(0);
+ Cell cell = row.createCell(0);
+
+ // it's important to create the referenced sheet first
+ Sheet sheet2 = wb.createSheet("A"); // note name 'A'
+ // TODO - POI crashes if the formula is added before this sheet
+ // RuntimeException("Zero length string is an invalid sheet name")
+ // Excel doesn't crash but the formula doesn't work until it is
+ // re-entered
+
+ String inputFormula = "SUMPRODUCT(A!C7:A!C67, B8:B68) / B69"; // as per bug report
+ try {
+ cell.setCellFormula(inputFormula);
+ } catch (StringIndexOutOfBoundsException e) {
+ fail("Identified bug 42448");
+ }
+
+ assertEquals("SUMPRODUCT(A!C7:A!C67,B8:B68)/B69", cell.getCellFormula());
+
+ // might as well evaluate the sucker...
+
+ addCell(sheet2, 5, 2, 3.0); // A!C6
+ addCell(sheet2, 6, 2, 4.0); // A!C7
+ addCell(sheet2, 66, 2, 5.0); // A!C67
+ addCell(sheet2, 67, 2, 6.0); // A!C68
+
+ addCell(sheet1, 6, 1, 7.0); // B7
+ addCell(sheet1, 7, 1, 8.0); // B8
+ addCell(sheet1, 67, 1, 9.0); // B68
+ addCell(sheet1, 68, 1, 10.0); // B69
+
+ double expectedResult = (4.0 * 8.0 + 5.0 * 9.0) / 10.0;
+
+ FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+ CellValue cv = fe.evaluate(cell);
+
+ assertEquals(CellType.NUMERIC, cv.getCellType());
+ assertEquals(expectedResult, cv.getNumberValue(), 0.0);
+ }
+ }
+
+ private static void addCell(Sheet sheet, int rowIx, int colIx,
+ double value) {
+ sheet.createRow(rowIx).createCell(colIx).setCellValue(value);
+ }
+
+ @Test
+ void test55032() throws IOException {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("input");
+
+ Row row = sheet.createRow(0);
+ Cell cell = row.createCell(1);
+
+ checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, ,0)", -59777.14585);
+ checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, ,)", -59777.14585);
+ checkFormulaValue(wb, cell, "PV(0.08/12, 20*12, 500, 500,)", -59878.6315455);
+
+ checkFormulaValue(wb, cell, "FV(0.08/12, 20*12, 500, ,)", -294510.2078107270);
+ checkFormulaValue(wb, cell, "PMT(0.08/12, 20*12, 500, ,)", -4.1822003450);
+ checkFormulaValue(wb, cell, "NPER(0.08/12, 20*12, 500, ,)", -2.0758873434);
+
+ wb.close();
+ }
+
+ // bug 52063: LOOKUP(2-arg) and LOOKUP(3-arg)
+ // FIXME: This could be moved into LookupFunctionsTestCaseData.xls, which is tested by TestLookupFunctionsFromSpreadsheet.java
+ @Test
+ void testLookupFormula() throws Exception {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet("52063");
+
+ // Note: Values in arrays are in ascending order since LOOKUP expects that in order to work properly
+ // column
+ // A B C
+ // +-------
+ // row 1 | P Q R
+ // row 2 | X Y Z
+ Row row = sheet.createRow(0);
+ row.createCell(0).setCellValue("P");
+ row.createCell(1).setCellValue("Q");
+ row.createCell(2).setCellValue("R");
+ row = sheet.createRow(1);
+ row.createCell(0).setCellValue("X");
+ row.createCell(1).setCellValue("Y");
+ row.createCell(2).setCellValue("Z");
+
+ Cell evalcell = sheet.createRow(2).createCell(0);
+
+ //// ROW VECTORS
+ // lookup and result row are the same
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C1)", "Q");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C1)", "R");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C1, A1:C1)", "Q");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C1, A1:C1)", "R");
+
+ // lookup and result row are different
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C2)", "Y");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C2)", "Z");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"Q\", A1:C1, A2:C2)", "Y");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"R\", A1:C1, A2:C2)", "Z");
+
+ //// COLUMN VECTORS
+ // lookup and result column are different
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"P\", A1:B2)", "Q");
+ checkFormulaValue(wb, evalcell, "LOOKUP(\"X\", A1:A2, C1:C2)", "Z");
- wb.close();
- }
+ wb.close();
+ }
- private CellValue evaluateFormulaInCell(Workbook wb, Cell cell, String formula) {
- cell.setCellFormula(formula);
+ private CellValue evaluateFormulaInCell(Workbook wb, Cell cell, String formula) {
+ cell.setCellFormula(formula);
- FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+ FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
return evaluator.evaluate(cell);
- }
+ }
- private void checkFormulaValue(Workbook wb, Cell cell, String formula, double expectedValue) {
- CellValue value = evaluateFormulaInCell(wb, cell, formula);
- assertEquals(expectedValue, value.getNumberValue(), 0.0001);
- }
-
- private void checkFormulaValue(Workbook wb, Cell cell, String formula, String expectedValue) {
- CellValue value = evaluateFormulaInCell(wb, cell, formula);
- assertEquals(expectedValue, value.getStringValue());
- }
+ private void checkFormulaValue(Workbook wb, Cell cell, String formula, double expectedValue) {
+ CellValue value = evaluateFormulaInCell(wb, cell, formula);
+ assertEquals(expectedValue, value.getNumberValue(), 0.0001);
+ }
+
+ private void checkFormulaValue(Workbook wb, Cell cell, String formula, String expectedValue) {
+ CellValue value = evaluateFormulaInCell(wb, cell, formula);
+ assertEquals(expectedValue, value.getStringValue());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org