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.

TestFixed.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.io.IOException;
  17. import junit.framework.TestCase;
  18. import org.apache.poi.hssf.usermodel.HSSFCell;
  19. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  20. import org.apache.poi.hssf.usermodel.HSSFSheet;
  21. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  22. import org.apache.poi.ss.formula.eval.BoolEval;
  23. import org.apache.poi.ss.formula.eval.ErrorEval;
  24. import org.apache.poi.ss.formula.eval.NumberEval;
  25. import org.apache.poi.ss.formula.eval.StringEval;
  26. import org.apache.poi.ss.formula.eval.ValueEval;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.CellValue;
  29. import org.apache.poi.ss.usermodel.ErrorConstants;
  30. public final class TestFixed extends TestCase {
  31. private HSSFCell cell11;
  32. private HSSFFormulaEvaluator evaluator;
  33. @Override
  34. public void setUp() throws IOException {
  35. HSSFWorkbook wb = new HSSFWorkbook();
  36. try {
  37. HSSFSheet sheet = wb.createSheet("new sheet");
  38. cell11 = sheet.createRow(0).createCell(0);
  39. cell11.setCellType(HSSFCell.CELL_TYPE_FORMULA);
  40. evaluator = new HSSFFormulaEvaluator(wb);
  41. } finally {
  42. wb.close();
  43. }
  44. }
  45. public void testValid() {
  46. // thousands separator
  47. confirm("FIXED(1234.56789,2,TRUE)", "1234.57");
  48. confirm("FIXED(1234.56789,2,FALSE)", "1,234.57");
  49. // rounding
  50. confirm("FIXED(1.8,0,TRUE)", "2");
  51. confirm("FIXED(1.2,0,TRUE)", "1");
  52. confirm("FIXED(1.5,0,TRUE)", "2");
  53. confirm("FIXED(1,0,TRUE)", "1");
  54. // fractional digits
  55. confirm("FIXED(1234.56789,7,TRUE)", "1234.5678900");
  56. confirm("FIXED(1234.56789,0,TRUE)", "1235");
  57. confirm("FIXED(1234.56789,-1,TRUE)", "1230");
  58. // less than three arguments
  59. confirm("FIXED(1234.56789)", "1,234.57");
  60. confirm("FIXED(1234.56789,3)", "1,234.568");
  61. // invalid arguments
  62. confirmValueError("FIXED(\"invalid\")");
  63. confirmValueError("FIXED(1,\"invalid\")");
  64. confirmValueError("FIXED(1,2,\"invalid\")");
  65. // strange arguments
  66. confirm("FIXED(1000,2,8)", "1000.00");
  67. confirm("FIXED(1000,2,0)", "1,000.00");
  68. // corner cases
  69. confirm("FIXED(1.23456789012345,15,TRUE)", "1.234567890123450");
  70. // Seems POI accepts longer numbers than Excel does, excel trims the
  71. // number to 15 digits and removes the "9" in the formula itself.
  72. // Not the fault of FIXED though.
  73. // confirm("FIXED(1.234567890123459,15,TRUE)", "1.234567890123450");
  74. confirm("FIXED(60,-2,TRUE)", "100");
  75. confirm("FIXED(10,-2,TRUE)", "0");
  76. // rounding propagation
  77. confirm("FIXED(99.9,0,TRUE)", "100");
  78. }
  79. public void testOptionalParams() {
  80. Fixed fixed = new Fixed();
  81. ValueEval evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789));
  82. assertTrue(evaluate instanceof StringEval);
  83. assertEquals("1,234.57", ((StringEval)evaluate).getStringValue());
  84. evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789), new NumberEval(1));
  85. assertTrue(evaluate instanceof StringEval);
  86. assertEquals("1,234.6", ((StringEval)evaluate).getStringValue());
  87. evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789), new NumberEval(1), BoolEval.TRUE);
  88. assertTrue(evaluate instanceof StringEval);
  89. assertEquals("1234.6", ((StringEval)evaluate).getStringValue());
  90. evaluate = fixed.evaluate(new ValueEval[] {}, 1, 1);
  91. assertTrue(evaluate instanceof ErrorEval);
  92. evaluate = fixed.evaluate(new ValueEval[] { new NumberEval(1), new NumberEval(1), new NumberEval(1), new NumberEval(1) }, 1, 1);
  93. assertTrue(evaluate instanceof ErrorEval);
  94. }
  95. private void confirm(String formulaText, String expectedResult) {
  96. cell11.setCellFormula(formulaText);
  97. evaluator.clearAllCachedResultValues();
  98. CellValue cv = evaluator.evaluate(cell11);
  99. assertEquals("Wrong result type: " + cv.formatAsString(), Cell.CELL_TYPE_STRING, cv.getCellType());
  100. String actualValue = cv.getStringValue();
  101. assertEquals(expectedResult, actualValue);
  102. }
  103. private void confirmValueError(String formulaText) {
  104. cell11.setCellFormula(formulaText);
  105. evaluator.clearAllCachedResultValues();
  106. CellValue cv = evaluator.evaluate(cell11);
  107. assertTrue("Wrong result type: " + cv.formatAsString(),
  108. cv.getCellType() == Cell.CELL_TYPE_ERROR
  109. && cv.getErrorValue() == ErrorConstants.ERROR_VALUE);
  110. }
  111. }