bug 33160, original patch and testcase supplied by Amol Deshmukh.. thanks. This version slightly more generic

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353641 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Avik Sengupta 2005-04-21 13:01:29 +00:00
parent c27f7ab5e5
commit f931d7cf7e
2 changed files with 43 additions and 2 deletions

View File

@ -484,12 +484,28 @@ public class FormulaParser {
if (IsDigit(look)) number = number +"."+ GetNum(); //this also takes care of someone entering "1234."
tokens.add(new NumberPtg(number));
} else {
tokens.add(new IntPtg(number)); //TODO:what if the number is too big to be a short? ..add factory to return Int or Number!
tokens.add(getNumberPtgFromString(number)); //TODO:what if the number is too big to be a short? ..add factory to return Int or Number!
}
}
}
private void StringLiteral()
/** Get a PTG for an integer from its string representation.
* return Int or Number Ptg based on size of input
* @param number
* @return
*/
private Ptg getNumberPtgFromString(String number) {
try {
return new IntPtg(number);
} catch (NumberFormatException e) {
System.out.println("Caught NFE, returning Number Ptg");
return new NumberPtg(number);
}
}
private void StringLiteral()
{
// Can't use match here 'cuz it consumes whitespace
// which we need to preserve inside the string.

View File

@ -23,6 +23,7 @@ import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
import org.apache.poi.hssf.record.formula.AddPtg;
import org.apache.poi.hssf.record.formula.AttrPtg;
import org.apache.poi.hssf.record.formula.BoolPtg;
import org.apache.poi.hssf.record.formula.DividePtg;
import org.apache.poi.hssf.record.formula.EqualPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
import org.apache.poi.hssf.record.formula.IntPtg;
@ -30,6 +31,7 @@ import org.apache.poi.hssf.record.formula.LessEqualPtg;
import org.apache.poi.hssf.record.formula.LessThanPtg;
import org.apache.poi.hssf.record.formula.NamePtg;
import org.apache.poi.hssf.record.formula.NotEqualPtg;
import org.apache.poi.hssf.record.formula.NumberPtg;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.record.formula.ReferencePtg;
import org.apache.poi.hssf.record.formula.StringPtg;
@ -353,6 +355,29 @@ public class TestFormulaParser extends TestCase {
assertTrue("ptg0 has Value class", ptg[0].getPtgClass() == Ptg.CLASS_VALUE);
}
/** bug 33160*/
public void testLargeInt() {
FormulaParser fp = new FormulaParser("40", null);
fp.parse();
Ptg[] ptg=fp.getRPNPtg();
assertTrue("ptg is Int, is "+ptg[0].getClass(),ptg[0] instanceof IntPtg);
fp = new FormulaParser("40000", null);
fp.parse();
ptg=fp.getRPNPtg();
assertTrue("ptg should be Number, is "+ptg[0].getClass(), ptg[0] instanceof NumberPtg);
}
/** bug 33160, testcase by Amol Deshmukh*/
public void testSimpleLongFormula() {
FormulaParser fp = new FormulaParser("40000/2", null);
fp.parse();
Ptg[] ptgs = fp.getRPNPtg();
assertTrue("three tokens expected, got "+ptgs.length,ptgs.length == 3);
assertTrue("NumberPtg",(ptgs[0] instanceof NumberPtg));
assertTrue("IntPtg",(ptgs[1] instanceof IntPtg));
assertTrue("DividePtg",(ptgs[2] instanceof DividePtg));
}
public static void main(String [] args) {
System.out.println("Testing org.apache.poi.hssf.record.formula.FormulaParser");
junit.textui.TestRunner.run(TestFormulaParser.class);