2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
/* ====================================================================
|
|
|
|
* The Apache Software License, Version 1.1
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 The Apache Software Foundation. All rights
|
|
|
|
* reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by the
|
|
|
|
* Apache Software Foundation (http://www.apache.org/)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
*
|
|
|
|
* 4. The names "Apache" and "Apache Software Foundation" and
|
|
|
|
* "Apache POI" must not be used to endorse or promote products
|
|
|
|
* derived from this software without prior written permission. For
|
|
|
|
* written permission, please contact apache@apache.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "Apache",
|
|
|
|
* "Apache POI", nor may "Apache" appear in their name, without
|
|
|
|
* prior written permission of the Apache Software Foundation.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many
|
|
|
|
* individuals on behalf of the Apache Software Foundation. For more
|
|
|
|
* information on the Apache Software Foundation, please see
|
|
|
|
* <http://www.apache.org/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
|
import org.apache.poi.hssf.model.Sheet;
|
|
|
|
import org.apache.poi.hssf.record.Record;
|
|
|
|
import org.apache.poi.hssf.record.BOFRecord;
|
|
|
|
import org.apache.poi.hssf.record.EOFRecord;
|
2002-04-28 16:51:55 -04:00
|
|
|
import org.apache.poi.hssf.util.ReferenceUtil;
|
2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Andrew C. Oliver (acoliver at apache dot org)
|
2002-04-29 20:18:47 -04:00
|
|
|
* @author Avik Sengupta
|
2002-04-27 13:26:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class TestFormulas
|
2002-04-27 20:45:10 -04:00
|
|
|
extends TestCase {
|
|
|
|
public TestFormulas(String s) {
|
2002-04-27 13:26:38 -04:00
|
|
|
super(s);
|
|
|
|
}
|
2002-04-27 20:45:10 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add 1+1 -- WHoohoo!
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void testBasicAddIntegers()
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
short rownum = 0;
|
|
|
|
File file = File.createTempFile("testFormula",".xls");
|
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
//get our minimum values
|
|
|
|
r = s.createRow((short)1);
|
2002-04-28 11:42:42 -04:00
|
|
|
c = r.createCell((short)1);
|
2002-04-27 20:45:10 -04:00
|
|
|
c.setCellFormula(1 + "+" + 1);
|
|
|
|
|
|
|
|
wb.write(out);
|
2002-04-28 11:42:42 -04:00
|
|
|
out.close();
|
2002-04-27 20:45:10 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow((short)1);
|
|
|
|
c = r.getCell((short)1);
|
|
|
|
|
|
|
|
assertTrue("Formula is as expected",("1+1".equals(c.getCellFormula())));
|
|
|
|
in.close();
|
2002-04-27 20:45:10 -04:00
|
|
|
}
|
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
/**
|
|
|
|
* Add various integers
|
|
|
|
*/
|
2002-04-27 20:45:10 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
public void testAddIntegers()
|
2002-04-27 20:45:10 -04:00
|
|
|
throws Exception {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("+");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply various integers
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void testMultplyIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("*");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract various integers
|
2002-04-28 11:42:42 -04:00
|
|
|
*/
|
2002-04-27 22:03:53 -04:00
|
|
|
public void testSubtractIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("-");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract various integers
|
2002-04-28 11:42:42 -04:00
|
|
|
*/
|
2002-04-27 22:03:53 -04:00
|
|
|
public void testDivideIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponentialize various integers;
|
|
|
|
*/
|
2002-04-28 12:10:00 -04:00
|
|
|
public void testPowerIntegers()
|
2002-04-27 22:03:53 -04:00
|
|
|
throws Exception {
|
|
|
|
binomialOperator("^");
|
|
|
|
}
|
2002-04-28 12:10:00 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatinate two numbers 1&2 = 12
|
|
|
|
*/
|
|
|
|
public void testConcatIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("&");
|
|
|
|
}
|
2002-04-27 22:03:53 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests 1*2+3*4
|
|
|
|
*/
|
|
|
|
public void testOrderOfOperationsMultiply()
|
|
|
|
throws Exception {
|
|
|
|
orderTest("1*2+3*4");
|
|
|
|
}
|
2002-04-27 22:03:53 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests 1*2+3^4
|
|
|
|
*/
|
|
|
|
public void testOrderOfOperationsPower()
|
|
|
|
throws Exception {
|
|
|
|
orderTest("1*2+3^4");
|
|
|
|
}
|
2002-04-28 12:40:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that parenthesis are obeyed
|
|
|
|
*/
|
|
|
|
public void testParenthesis()
|
|
|
|
throws Exception {
|
|
|
|
orderTest("(1*3)+2+(1+2)*(3^4)^5");
|
|
|
|
}
|
2002-04-28 16:51:55 -04:00
|
|
|
|
|
|
|
public void testReferencesOpr()
|
|
|
|
throws Exception {
|
|
|
|
String[] operation = new String[] {
|
|
|
|
"+", "-", "*", "/", "^", "&"
|
|
|
|
};
|
|
|
|
for (int k = 0; k < operation.length; k++) {
|
|
|
|
operationRefTest(operation[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
/**
|
|
|
|
* Tests creating a file with floating point in a formula.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void testFloat()
|
2002-04-30 21:11:05 -04:00
|
|
|
throws Exception {
|
|
|
|
floatTest("*");
|
|
|
|
floatTest("/");
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
private void floatTest(String operator)
|
|
|
|
throws Exception {
|
|
|
|
short rownum = 0;
|
|
|
|
File file = File.createTempFile("testFormulaFloat",".xls");
|
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
//get our minimum values
|
|
|
|
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)1);
|
|
|
|
c.setCellFormula(""+Float.MIN_VALUE + operator + Float.MIN_VALUE);
|
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2) ) {
|
|
|
|
r = s.createRow((short) x);
|
|
|
|
|
|
|
|
for (short y = 1; y < 256 && y > 0; y= (short) (y +2)) {
|
|
|
|
|
|
|
|
c = r.createCell((short) y);
|
|
|
|
c.setCellFormula("" + x+"."+y + operator + y +"."+x);
|
|
|
|
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
}
|
2002-04-30 21:11:05 -04:00
|
|
|
}
|
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)0);
|
|
|
|
c.setCellFormula("" + Float.MAX_VALUE + operator + Float.MAX_VALUE);
|
|
|
|
}
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
out=null;wb=null; //otherwise we get out of memory error!
|
|
|
|
floatVerify(operator,file);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void floatVerify(String operator, File file)
|
|
|
|
throws Exception {
|
|
|
|
short rownum = 0;
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(in);
|
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
// dont know how to check correct result .. for the moment, we just verify that the file can be read.
|
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.getRow((short) x);
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
for (short y = 1; y < 256 && y > 0; y=(short)(y+2)) {
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
c = r.getCell((short) y);
|
|
|
|
assertTrue("got a formula",c.getCellFormula()!=null);
|
|
|
|
|
|
|
|
assertTrue("loop Formula is as expected "+x+"."+y+operator+y+"."+x+"!="+c.getCellFormula(),(
|
|
|
|
(""+x+"."+y+operator+y+"."+x).equals(c.getCellFormula()) ));
|
|
|
|
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
}
|
2002-04-30 21:11:05 -04:00
|
|
|
|
|
|
|
in.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
|
|
|
|
public void testAreaSum()
|
|
|
|
throws Exception {
|
|
|
|
areaFunctionTest("SUM");
|
|
|
|
}
|
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
public void testAreaAverage()
|
|
|
|
throws Exception {
|
|
|
|
areaFunctionTest("AVERAGE");
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:19:28 -04:00
|
|
|
|
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
private void operationRefTest(String operator)
|
|
|
|
throws Exception {
|
|
|
|
File file = File.createTempFile("testFormula",".xls");
|
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
//get our minimum values
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)1);
|
|
|
|
c.setCellFormula("A2" + operator + "A3");
|
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.createRow((short) x);
|
|
|
|
|
|
|
|
for (short y = 1; y < 256 && y > 0; y++) {
|
|
|
|
|
|
|
|
String ref=null;
|
|
|
|
String ref2=null;
|
|
|
|
short refx1=0;
|
|
|
|
short refy1=0;
|
|
|
|
short refx2=0;
|
|
|
|
short refy2=0;
|
|
|
|
if (x +50 < Short.MAX_VALUE) {
|
|
|
|
refx1=(short)(x+50);
|
|
|
|
refx2=(short)(x+46);
|
|
|
|
} else {
|
|
|
|
refx1=(short)(x-4);
|
|
|
|
refx2=(short)(x-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y+50 < 255) {
|
|
|
|
refy1=(short)(y+50);
|
|
|
|
refy2=(short)(y+49);
|
|
|
|
} else {
|
|
|
|
refy1=(short)(y-4);
|
|
|
|
refy2=(short)(y-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = ReferenceUtil.getReferenceFromXY(refx1,refy1);
|
|
|
|
ref2 = ReferenceUtil.getReferenceFromXY(refx2,refy2);
|
|
|
|
|
|
|
|
c = r.createCell((short) y);
|
|
|
|
c.setCellFormula("" + ref + operator + ref2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//make sure we do the maximum value of the Int operator
|
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)0);
|
|
|
|
c.setCellFormula("" + "B1" + operator + "IV255");
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
operationalRefVerify(operator,file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the sheet we wrote out by binomialOperator and makes sure the formulas
|
|
|
|
* all match what we expect (x operator y)
|
|
|
|
*/
|
|
|
|
private void operationalRefVerify(String operator, File file)
|
|
|
|
throws Exception {
|
|
|
|
short rownum = 0;
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(in);
|
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
//get our minimum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)1);
|
|
|
|
//get our minimum values
|
|
|
|
assertTrue("minval Formula is as expected A2"+operator+"A3 != "+c.getCellFormula(),
|
|
|
|
( ("A2"+operator+"A3").equals(c.getCellFormula())
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.getRow((short) x);
|
|
|
|
|
|
|
|
for (short y = 1; y < 256 && y > 0; y++) {
|
|
|
|
|
|
|
|
String ref=null;
|
|
|
|
String ref2=null;
|
|
|
|
short refx1=0;
|
|
|
|
short refy1=0;
|
|
|
|
short refx2=0;
|
|
|
|
short refy2=0;
|
|
|
|
if (x +50 < Short.MAX_VALUE) {
|
|
|
|
refx1=(short)(x+50);
|
|
|
|
refx2=(short)(x+46);
|
|
|
|
} else {
|
|
|
|
refx1=(short)(x-4);
|
|
|
|
refx2=(short)(x-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y+50 < 255) {
|
|
|
|
refy1=(short)(y+50);
|
|
|
|
refy2=(short)(y+49);
|
|
|
|
} else {
|
|
|
|
refy1=(short)(y-4);
|
|
|
|
refy2=(short)(y-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
c = r.getCell((short) y);
|
|
|
|
|
|
|
|
ref = ReferenceUtil.getReferenceFromXY(refx1,refy1);
|
|
|
|
ref2 = ReferenceUtil.getReferenceFromXY(refx2,refy2);
|
|
|
|
|
|
|
|
|
|
|
|
assertTrue("loop Formula is as expected "+ref+operator+ref2+"!="+c.getCellFormula(),(
|
|
|
|
(""+ref+operator+ref2).equals(c.getCellFormula())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//test our maximum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)0);
|
|
|
|
|
|
|
|
assertTrue("maxval Formula is as expected",(
|
|
|
|
("B1"+operator+"IV255").equals(c.getCellFormula())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests order wrting out == order writing in for a given formula
|
|
|
|
*/
|
|
|
|
private void orderTest(String formula)
|
|
|
|
throws Exception {
|
|
|
|
File file = File.createTempFile("testFormula",".xls");
|
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
//get our minimum values
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)1);
|
|
|
|
c.setCellFormula(formula);
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
|
|
|
|
//get our minimum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)1);
|
|
|
|
assertTrue("minval Formula is as expected",
|
|
|
|
formula.equals(c.getCellFormula())
|
|
|
|
);
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
/**
|
|
|
|
* All multi-binomial operator tests use this to create a worksheet with a
|
|
|
|
* huge set of x operator y formulas. Next we call binomialVerify and verify
|
|
|
|
* that they are all how we expect.
|
|
|
|
*/
|
|
|
|
private void binomialOperator(String operator)
|
2002-04-27 22:03:53 -04:00
|
|
|
throws Exception {
|
2002-04-27 13:26:38 -04:00
|
|
|
short rownum = 0;
|
|
|
|
File file = File.createTempFile("testFormula",".xls");
|
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
//get our minimum values
|
2002-04-27 22:03:53 -04:00
|
|
|
r = s.createRow((short)0);
|
2002-04-27 20:45:10 -04:00
|
|
|
c = r.createCell((short)1);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula(1 + operator + 1);
|
2002-04-27 20:45:10 -04:00
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
2002-04-27 13:26:38 -04:00
|
|
|
r = s.createRow((short) x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
for (short y = 1; y < 256 && y > 0; y++) {
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
c = r.createCell((short) y);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula("" + x + operator + y);
|
2002-04-27 20:45:10 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//make sure we do the maximum value of the Int operator
|
2002-04-27 20:45:10 -04:00
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)0);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula("" + Short.MAX_VALUE + operator + Short.MAX_VALUE);
|
2002-04-27 20:45:10 -04:00
|
|
|
}
|
2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
2002-04-27 22:03:53 -04:00
|
|
|
assertTrue("file exists",file.exists());
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialVerify(operator,file);
|
|
|
|
}
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
/**
|
|
|
|
* Opens the sheet we wrote out by binomialOperator and makes sure the formulas
|
|
|
|
* all match what we expect (x operator y)
|
|
|
|
*/
|
|
|
|
private void binomialVerify(String operator, File file)
|
2002-04-27 22:03:53 -04:00
|
|
|
throws Exception {
|
|
|
|
short rownum = 0;
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(in);
|
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
//get our minimum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)1);
|
2002-04-28 12:40:18 -04:00
|
|
|
assertTrue("minval Formula is as expected 1"+operator+"1 != "+c.getCellFormula(),
|
2002-04-28 11:42:42 -04:00
|
|
|
( ("1"+operator+"1").equals(c.getCellFormula())
|
|
|
|
));
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.getRow((short) x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
for (short y = 1; y < 256 && y > 0; y++) {
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
c = r.getCell((short) y);
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
assertTrue("loop Formula is as expected "+x+operator+y+"!="+c.getCellFormula(),(
|
|
|
|
(""+x+operator+y).equals(c.getCellFormula())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2002-04-28 11:42:42 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
//test our maximum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)0);
|
2002-04-28 11:42:42 -04:00
|
|
|
|
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
assertTrue("maxval Formula is as expected",(
|
2002-04-28 11:42:42 -04:00
|
|
|
(""+Short.MAX_VALUE+operator+Short.MAX_VALUE).equals(c.getCellFormula())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
in.close();
|
2002-04-28 11:42:42 -04:00
|
|
|
assertTrue("file exists",file.exists());
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|
2002-04-29 20:18:47 -04:00
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
|
|
|
|
|
2002-04-29 20:18:47 -04:00
|
|
|
/**
|
2002-04-29 22:12:25 -04:00
|
|
|
* Writes a function then tests to see if its correct
|
2002-04-29 20:18:47 -04:00
|
|
|
*
|
|
|
|
*/
|
2002-04-29 22:12:25 -04:00
|
|
|
public void areaFunctionTest(String function)
|
|
|
|
throws Exception {
|
|
|
|
|
2002-04-29 20:18:47 -04:00
|
|
|
short rownum = 0;
|
2002-04-29 22:12:25 -04:00
|
|
|
File file = File.createTempFile("testFormulaAreaFunction"+function,".xls");
|
2002-04-29 20:18:47 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
r = s.createRow((short) 0);
|
2002-04-29 20:18:47 -04:00
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
c = r.createCell((short) 0);
|
|
|
|
c.setCellFormula(function+"(A2:A3)");
|
2002-04-29 20:18:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
2002-04-29 22:12:25 -04:00
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell((short)0);
|
|
|
|
|
|
|
|
assertTrue("function ="+function+"(A2:A3)",
|
|
|
|
( (function+"(A2:A3)").equals((function+"(A2:A3)")) )
|
|
|
|
);
|
|
|
|
in.close();
|
|
|
|
}
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
|
|
|
|
public static void main(String [] args) {
|
2002-04-27 13:26:38 -04:00
|
|
|
System.out
|
2002-04-27 20:45:10 -04:00
|
|
|
.println("Testing org.apache.poi.hssf.usermodel.TestFormulas");
|
2002-04-27 13:26:38 -04:00
|
|
|
junit.textui.TestRunner.run(TestFormulas.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|