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.

TestHex2Dec.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 junit.framework.TestCase;
  17. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.ss.formula.IStabilityClassifier;
  20. import org.apache.poi.ss.formula.OperationEvaluationContext;
  21. import org.apache.poi.ss.formula.WorkbookEvaluator;
  22. import org.apache.poi.ss.formula.eval.ErrorEval;
  23. import org.apache.poi.ss.formula.eval.NumberEval;
  24. import org.apache.poi.ss.formula.eval.StringEval;
  25. import org.apache.poi.ss.formula.eval.ValueEval;
  26. /**
  27. * Tests for {@link Hex2Dec}
  28. *
  29. * @author cedric dot walter @ gmail dot com
  30. */
  31. public final class TestHex2Dec extends TestCase {
  32. private static ValueEval invokeValue(String number1) {
  33. ValueEval[] args = new ValueEval[] { new StringEval(number1) };
  34. return new Hex2Dec().evaluate(args, -1, -1);
  35. }
  36. private static void confirmValue(String msg, String number1, String expected) {
  37. ValueEval result = invokeValue(number1);
  38. assertEquals(NumberEval.class, result.getClass());
  39. assertEquals(msg, expected, ((NumberEval) result).getStringValue());
  40. }
  41. private static void confirmValueError(String msg, String number1, ErrorEval numError) {
  42. ValueEval result = invokeValue(number1);
  43. assertEquals(ErrorEval.class, result.getClass());
  44. assertEquals(msg, numError, result);
  45. }
  46. public void testBasic() {
  47. confirmValue("Converts octal 'A5' to decimal (165)", "A5", "165");
  48. confirmValue("Converts octal FFFFFFFF5B to decimal (-165)", "FFFFFFFF5B", "-165");
  49. confirmValue("Converts octal 3DA408B9 to decimal (-165)", "3DA408B9", "1034160313");
  50. }
  51. public void testErrors() {
  52. confirmValueError("not a valid octal number","GGGGGGG", ErrorEval.NUM_ERROR);
  53. confirmValueError("not a valid octal number","3.14159", ErrorEval.NUM_ERROR);
  54. }
  55. public void testEvalOperationEvaluationContext() {
  56. OperationEvaluationContext ctx = createContext();
  57. ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
  58. ValueEval result = new Hex2Dec().evaluate(args, ctx);
  59. assertEquals(NumberEval.class, result.getClass());
  60. assertEquals("0", ((NumberEval) result).getStringValue());
  61. }
  62. public void testEvalOperationEvaluationContextFails() {
  63. OperationEvaluationContext ctx = createContext();
  64. ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 0) };
  65. ValueEval result = new Hex2Dec().evaluate(args, ctx);
  66. assertEquals(ErrorEval.class, result.getClass());
  67. assertEquals(ErrorEval.VALUE_INVALID, result);
  68. }
  69. private OperationEvaluationContext createContext() {
  70. HSSFWorkbook wb = new HSSFWorkbook();
  71. wb.createSheet();
  72. HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
  73. WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
  74. public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
  75. return true;
  76. }
  77. }, null);
  78. OperationEvaluationContext ctx = new OperationEvaluationContext(workbookEvaluator,
  79. workbook, 0, 0, 0, null);
  80. return ctx;
  81. }
  82. public void testRefs() {
  83. OperationEvaluationContext ctx = createContext();
  84. ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
  85. ValueEval result = new Hex2Dec().evaluate(args, -1, -1);
  86. assertEquals(NumberEval.class, result.getClass());
  87. assertEquals("0", ((NumberEval) result).getStringValue());
  88. }
  89. }