You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2015/01/09 21:37:11 UTC
svn commit: r1650654 - in /poi/trunk/src:
java/org/apache/poi/ss/formula/functions/
ooxml/testcases/org/apache/poi/xssf/usermodel/
testcases/org/apache/poi/ss/formula/functions/
Author: centic
Date: Fri Jan 9 20:37:11 2015
New Revision: 1650654
URL: http://svn.apache.org/r1650654
Log:
Adjust Bin2Dec similar to what we did for Hex2Dec in Bug 57196 and fix some smaller problems in the implementation found while adding full unit tests
Added:
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestBin2Dec.java
- copied, changed from r1650598, poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Bin.java
Modified:
poi/trunk/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java
poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java?rev=1650654&r1=1650653&r2=1650654&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Bin2Dec.java Fri Jan 9 20:37:11 2015
@@ -20,6 +20,7 @@ import org.apache.poi.ss.formula.Operati
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.OperandResolver;
+import org.apache.poi.ss.formula.eval.RefEval;
import org.apache.poi.ss.formula.eval.ValueEval;
/**
@@ -44,7 +45,13 @@ public class Bin2Dec extends Fixed1ArgFu
public static final FreeRefFunction instance = new Bin2Dec();
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE) {
- String number = OperandResolver.coerceValueToString(numberVE);
+ final String number;
+ if (numberVE instanceof RefEval) {
+ RefEval re = (RefEval) numberVE;
+ number = OperandResolver.coerceValueToString(re.getInnerValueEval(re.getFirstSheetIndex()));
+ } else {
+ number = OperandResolver.coerceValueToString(numberVE);
+ }
if (number.length() > 10) {
return ErrorEval.NUM_ERROR;
}
@@ -62,22 +69,25 @@ public class Bin2Dec extends Fixed1ArgFu
}
String value;
- int sum;
- if (isPositive) {
- //bit9*2^8 + bit8*2^7 + bit7*2^6 + bit6*2^5 + bit5*2^4+ bit3*2^2+ bit2*2^1+ bit1*2^0
- sum = getDecimalValue(unsigned);
- value = String.valueOf(sum);
- } else {
- //The leftmost bit is 1 -- this is negative number
- //Inverse bits [1-9]
- String inverted = toggleBits(unsigned);
- // Calculate decimal number
- sum = getDecimalValue(inverted);
-
- //Add 1 to obtained number
- sum++;
-
- value = "-" + String.valueOf(sum);
+ try {
+ if (isPositive) {
+ //bit9*2^8 + bit8*2^7 + bit7*2^6 + bit6*2^5 + bit5*2^4+ bit3*2^2+ bit2*2^1+ bit1*2^0
+ int sum = getDecimalValue(unsigned);
+ value = String.valueOf(sum);
+ } else {
+ //The leftmost bit is 1 -- this is negative number
+ //Inverse bits [1-9]
+ String inverted = toggleBits(unsigned);
+ // Calculate decimal number
+ int sum = getDecimalValue(inverted);
+
+ //Add 1 to obtained number
+ sum++;
+
+ value = "-" + String.valueOf(sum);
+ }
+ } catch (NumberFormatException e) {
+ return ErrorEval.NUM_ERROR;
}
return new NumberEval(Long.parseLong(value));
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java?rev=1650654&r1=1650653&r2=1650654&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Dec2Bin.java Fri Jan 9 20:37:11 2015
@@ -52,8 +52,8 @@ public class Dec2Bin extends Var1or2ArgF
public static final FreeRefFunction instance = new Dec2Bin();
- private final static long MIN_VALUE = Long.parseLong("-512");
- private final static long MAX_VALUE = Long.parseLong("512");
+ private final static long MIN_VALUE = -512;
+ private final static long MAX_VALUE = 511;
private final static int DEFAULT_PLACES_VALUE = 10;
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval placesVE) {
Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1650654&r1=1650653&r2=1650654&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Fri Jan 9 20:37:11 2015
@@ -1974,6 +1974,11 @@ public final class TestXSSFBugs extends
XSSFSheet sheet = wb.createSheet("Sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
+ cell.setCellValue("0");
+ cell = row.createCell(1);
+ cell.setCellValue(0);
+ cell = row.createCell(2);
+ cell.setCellValue(0);
// simple formula worked
cell.setCellFormula("DEC2HEX(O2+D2)");
@@ -2004,6 +2009,30 @@ public final class TestXSSFBugs extends
workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
+
+ // what other similar functions
+ cell.setCellFormula("DEC2BIN(O8)-O2+D2");
+ workbookEvaluator.clearAllCachedResultValues();
+
+ workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
+ workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
+ workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
+
+ // what other similar functions
+ cell.setCellFormula("DEC2BIN(A1)");
+ workbookEvaluator.clearAllCachedResultValues();
+
+ workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
+ workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
+ workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
+
+ // what other similar functions
+ cell.setCellFormula("BIN2DEC(B1)");
+ workbookEvaluator.clearAllCachedResultValues();
+
+ workbookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(wb), null, null);
+ workbookEvaluator.setDebugEvaluationOutputForNextEval(true);
+ workbookEvaluator.evaluate(new XSSFEvaluationCell(cell));
} finally {
System.clearProperty("org.apache.poi.util.POILogger");
System.clearProperty("poi.log.level");
Copied: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestBin2Dec.java (from r1650598, poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestBin2Dec.java?p2=poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestBin2Dec.java&p1=poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java&r1=1650598&r2=1650654&rev=1650654&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestBin2Dec.java Fri Jan 9 20:37:11 2015
@@ -30,20 +30,20 @@ import org.apache.poi.ss.formula.eval.St
import org.apache.poi.ss.formula.eval.ValueEval;
/**
- * Tests for {@link Hex2Dec}
+ * Tests for {@link Bin2Dec}
*
* @author cedric dot walter @ gmail dot com
*/
-public final class TestHex2Dec extends TestCase {
+public final class TestBin2Dec extends TestCase {
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[] { new StringEval(number1) };
- return new Hex2Dec().evaluate(args, -1, -1);
+ return new Bin2Dec().evaluate(args, -1, -1);
}
private static void confirmValue(String msg, String number1, String expected) {
ValueEval result = invokeValue(number1);
- assertEquals(NumberEval.class, result.getClass());
+ assertEquals("Had: " + result.toString(), NumberEval.class, result.getClass());
assertEquals(msg, expected, ((NumberEval) result).getStringValue());
}
@@ -54,21 +54,23 @@ public final class TestHex2Dec extends T
}
public void testBasic() {
- confirmValue("Converts octal 'A5' to decimal (165)", "A5", "165");
- confirmValue("Converts octal FFFFFFFF5B to decimal (-165)", "FFFFFFFF5B", "-165");
- confirmValue("Converts octal 3DA408B9 to decimal (-165)", "3DA408B9", "1034160313");
+ confirmValue("Converts binary '00101' to decimal (5)", "00101", "5");
+ confirmValue("Converts binary '1111111111' to decimal (-1)", "1111111111", "-1");
+ confirmValue("Converts binary '1111111110' to decimal (-2)", "1111111110", "-2");
+ confirmValue("Converts binary '0111111111' to decimal (511)", "0111111111", "511");
}
public void testErrors() {
- confirmValueError("not a valid octal number","GGGGGGG", ErrorEval.NUM_ERROR);
- confirmValueError("not a valid octal number","3.14159", ErrorEval.NUM_ERROR);
+ confirmValueError("does not support more than 10 digits","01010101010", ErrorEval.NUM_ERROR);
+ confirmValueError("not a valid binary number","GGGGGGG", ErrorEval.NUM_ERROR);
+ confirmValueError("not a valid binary number","3.14159", ErrorEval.NUM_ERROR);
}
public void testEvalOperationEvaluationContext() {
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
- ValueEval result = new Hex2Dec().evaluate(args, ctx);
+ ValueEval result = new Bin2Dec().evaluate(args, ctx);
assertEquals(NumberEval.class, result.getClass());
assertEquals("0", ((NumberEval) result).getStringValue());
@@ -78,7 +80,7 @@ public final class TestHex2Dec extends T
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 0) };
- ValueEval result = new Hex2Dec().evaluate(args, ctx);
+ ValueEval result = new Bin2Dec().evaluate(args, ctx);
assertEquals(ErrorEval.class, result.getClass());
assertEquals(ErrorEval.VALUE_INVALID, result);
@@ -103,7 +105,7 @@ public final class TestHex2Dec extends T
OperationEvaluationContext ctx = createContext();
ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
- ValueEval result = new Hex2Dec().evaluate(args, -1, -1);
+ ValueEval result = new Bin2Dec().evaluate(args, -1, -1);
assertEquals(NumberEval.class, result.getClass());
assertEquals("0", ((NumberEval) result).getStringValue());
Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Bin.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Bin.java?rev=1650654&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Bin.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Bin.java Fri Jan 9 20:37:11 2015
@@ -0,0 +1,240 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.formula.IStabilityClassifier;
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.WorkbookEvaluator;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+
+/**
+ * Tests for {@link Dec2Bin}
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public final class TestDec2Bin extends TestCase {
+
+ private static ValueEval invokeValue(String number1) {
+ ValueEval[] args = new ValueEval[] { new StringEval(number1) };
+ return new Dec2Bin().evaluate(args, -1, -1);
+ }
+
+ private static ValueEval invokeBack(String number1) {
+ ValueEval[] args = new ValueEval[] { new StringEval(number1) };
+ return new Bin2Dec().evaluate(args, -1, -1);
+ }
+
+ private static void confirmValue(String msg, String number1, String expected) {
+ ValueEval result = invokeValue(number1);
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ assertEquals(msg, expected, ((StringEval) result).getStringValue());
+ }
+
+ private static void confirmValueError(String msg, String number1, ErrorEval numError) {
+ ValueEval result = invokeValue(number1);
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(msg, numError, result);
+ }
+
+ public void testBasic() {
+ confirmValue("Converts binary '00101' from binary (5)", "5", "101");
+ confirmValue("Converts binary '1111111111' from binary (-1)", "-1", "1111111111");
+ confirmValue("Converts binary '1111111110' from binary (-2)", "-2", "1111111110");
+ confirmValue("Converts binary '0111111111' from binary (511)", "511", "111111111");
+ confirmValue("Converts binary '1000000000' from binary (511)", "-512", "1000000000");
+ }
+
+ public void testErrors() {
+ confirmValueError("fails for >= 512 or < -512","512", ErrorEval.NUM_ERROR);
+ confirmValueError("fails for >= 512 or < -512","-513", ErrorEval.NUM_ERROR);
+ confirmValueError("not a valid decimal number","GGGGGGG", ErrorEval.VALUE_INVALID);
+ confirmValueError("not a valid decimal number","3.14159a", ErrorEval.VALUE_INVALID);
+ }
+
+ public void testEvalOperationEvaluationContext() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
+ ValueEval result = new Dec2Bin().evaluate(args, ctx);
+
+ assertEquals(StringEval.class, result.getClass());
+ assertEquals("1101", ((StringEval) result).getStringValue());
+ }
+
+ public void testEvalOperationEvaluationContextFails() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ErrorEval.VALUE_INVALID };
+ ValueEval result = new Dec2Bin().evaluate(args, ctx);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ private OperationEvaluationContext createContext() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+ Row row = sheet.createRow(0);
+ Cell cell = row.createCell(0);
+ cell.setCellValue("13.43");
+ cell = row.createCell(1);
+ cell.setCellValue("8");
+ cell = row.createCell(2);
+ cell.setCellValue("-8");
+ cell = row.createCell(3);
+ cell.setCellValue("1");
+
+ HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
+ WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
+
+ public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
+ return true;
+ }
+ }, null);
+ OperationEvaluationContext ctx = new OperationEvaluationContext(workbookEvaluator,
+ workbook, 0, 0, 0, null);
+ return ctx;
+ }
+
+ public void testRefs() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ assertEquals("1101", ((StringEval) result).getStringValue());
+ }
+
+ public void testWithPlacesIntInt() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ // TODO: documentation and behavior do not match here!
+ assertEquals("1101", ((StringEval) result).getStringValue());
+ }
+
+ public void testWithPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Bin().evaluate(args, ctx);
+
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ // TODO: documentation and behavior do not match here!
+ assertEquals("1101", ((StringEval) result).getStringValue());
+ }
+
+ public void testWithToShortPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 3) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.NUM_ERROR, result);
+ }
+
+ public void testWithTooManyParamsIntInt() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ public void testWithTooManyParams() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Bin().evaluate(args, ctx);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ public void testWithErrorPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ErrorEval.NULL_INTERSECTION };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.NULL_INTERSECTION, result);
+ }
+
+ public void testWithNegativePlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 2) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.NUM_ERROR, result);
+ }
+
+ public void testWithZeroPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), new NumberEval(0.0) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.NUM_ERROR, result);
+ }
+
+ public void testWithEmptyPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(1, 0) };
+ ValueEval result = new Dec2Bin().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ public void testBackAndForth() {
+ for (int i = -512; i < 512; i++) {
+ ValueEval result = invokeValue(Integer.toString(i));
+ assertEquals("Had: " + result.toString(), StringEval.class,
+ result.getClass());
+
+ ValueEval back = invokeBack(((StringEval) result).getStringValue());
+ assertEquals("Had: " + back.toString(), NumberEval.class,
+ back.getClass());
+
+ assertEquals(Integer.toString(i),
+ ((NumberEval) back).getStringValue());
+ }
+ }
+}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java?rev=1650654&r1=1650653&r2=1650654&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java Fri Jan 9 20:37:11 2015
@@ -18,9 +18,19 @@
package org.apache.poi.ss.formula.functions;
import junit.framework.TestCase;
+
+import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.formula.IStabilityClassifier;
+import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
/**
* Tests for {@link Dec2Hex}
@@ -34,6 +44,11 @@ public final class TestDec2Hex extends T
return new Dec2Hex().evaluate(args, -1, -1);
}
+ private static ValueEval invokeBack(String number1) {
+ ValueEval[] args = new ValueEval[] { new StringEval(number1) };
+ return new Hex2Dec().evaluate(args, -1, -1);
+ }
+
private static ValueEval invokeValue(String number1) {
ValueEval[] args = new ValueEval[] { new StringEval(number1), };
return new Dec2Hex().evaluate(args, -1, -1);
@@ -75,4 +90,132 @@ public final class TestDec2Hex extends T
confirmValueError("negative places not allowed","549755813888","-10", ErrorEval.NUM_ERROR);
confirmValueError("non number places not allowed","ABCDEF","0", ErrorEval.VALUE_INVALID);
}
+
+ public void testEvalOperationEvaluationContextFails() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ErrorEval.VALUE_INVALID };
+ ValueEval result = new Dec2Hex().evaluate(args, ctx);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ private OperationEvaluationContext createContext() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+ Row row = sheet.createRow(0);
+ Cell cell = row.createCell(0);
+ cell.setCellValue("123.43");
+ cell = row.createCell(1);
+ cell.setCellValue("8");
+ cell = row.createCell(2);
+ cell.setCellValue("-8");
+
+ HSSFEvaluationWorkbook workbook = HSSFEvaluationWorkbook.create(wb);
+ WorkbookEvaluator workbookEvaluator = new WorkbookEvaluator(workbook, new IStabilityClassifier() {
+
+ public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) {
+ return true;
+ }
+ }, null);
+ OperationEvaluationContext ctx = new OperationEvaluationContext(workbookEvaluator,
+ workbook, 0, 0, 0, null);
+ return ctx;
+ }
+
+ public void testRefs() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0) };
+ ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
+
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ assertEquals("7B", ((StringEval) result).getStringValue());
+ }
+
+ public void testWithPlacesIntInt() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
+
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ assertEquals("0000007B", ((StringEval) result).getStringValue());
+ }
+
+ public void testWithPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Hex().evaluate(args, ctx);
+
+ assertEquals("Had: " + result.toString(), StringEval.class, result.getClass());
+ assertEquals("0000007B", ((StringEval) result).getStringValue());
+ }
+
+ public void testWithTooManyParamsIntInt() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ public void testWithTooManyParams() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 1), ctx.getRefEval(0, 1) };
+ ValueEval result = new Dec2Hex().evaluate(args, ctx);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ public void testWithErrorPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ErrorEval.NULL_INTERSECTION };
+ ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.NULL_INTERSECTION, result);
+ }
+
+ public void testWithNegativePlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(0, 2) };
+ ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.NUM_ERROR, result);
+ }
+
+ public void testWithEmptyPlaces() {
+ OperationEvaluationContext ctx = createContext();
+
+ ValueEval[] args = new ValueEval[] { ctx.getRefEval(0, 0), ctx.getRefEval(1, 0) };
+ ValueEval result = new Dec2Hex().evaluate(args, -1, -1);
+
+ assertEquals(ErrorEval.class, result.getClass());
+ assertEquals(ErrorEval.VALUE_INVALID, result);
+ }
+
+ public void testBackAndForth() {
+ for (int i = -512; i < 512; i++) {
+ ValueEval result = invokeValue(Integer.toString(i));
+ assertEquals("Had: " + result.toString(), StringEval.class,
+ result.getClass());
+
+ ValueEval back = invokeBack(((StringEval) result).getStringValue());
+ assertEquals("Had: " + back.toString(), NumberEval.class,
+ back.getClass());
+
+ assertEquals(Integer.toString(i),
+ ((NumberEval) back).getStringValue());
+ }
+ }
}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java?rev=1650654&r1=1650653&r2=1650654&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestHex2Dec.java Fri Jan 9 20:37:11 2015
@@ -54,14 +54,14 @@ public final class TestHex2Dec extends T
}
public void testBasic() {
- confirmValue("Converts octal 'A5' to decimal (165)", "A5", "165");
- confirmValue("Converts octal FFFFFFFF5B to decimal (-165)", "FFFFFFFF5B", "-165");
- confirmValue("Converts octal 3DA408B9 to decimal (-165)", "3DA408B9", "1034160313");
+ confirmValue("Converts hex 'A5' to decimal (165)", "A5", "165");
+ confirmValue("Converts hex FFFFFFFF5B to decimal (-165)", "FFFFFFFF5B", "-165");
+ confirmValue("Converts hex 3DA408B9 to decimal (-165)", "3DA408B9", "1034160313");
}
public void testErrors() {
- confirmValueError("not a valid octal number","GGGGGGG", ErrorEval.NUM_ERROR);
- confirmValueError("not a valid octal number","3.14159", ErrorEval.NUM_ERROR);
+ confirmValueError("not a valid hex number","GGGGGGG", ErrorEval.NUM_ERROR);
+ confirmValueError("not a valid hex number","3.14159", ErrorEval.NUM_ERROR);
}
public void testEvalOperationEvaluationContext() {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org