Search in sources :

Example 31 with StringEval

use of org.apache.poi.ss.formula.eval.StringEval in project poi by apache.

the class TestProper method testMicroBenchmark.

@Test
public void testMicroBenchmark() {
    ValueEval strArg = new StringEval("some longer text that needs a number of replacements to check for runtime of different implementations");
    long start = System.currentTimeMillis();
    for (int i = 0; i < 300000; i++) {
        final ValueEval ret = TextFunction.PROPER.evaluate(new ValueEval[] { strArg }, 0, 0);
        assertEquals("Some Longer Text That Needs A Number Of Replacements To Check For Runtime Of Different Implementations", ((StringEval) ret).getStringValue());
    }
    // Took aprox. 600ms on a decent Laptop in July 2016
    System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms");
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) Test(org.junit.Test)

Example 32 with StringEval

use of org.apache.poi.ss.formula.eval.StringEval in project poi by apache.

the class TestMatch method testHeterogeneous.

public void testHeterogeneous() {
    ValueEval[] values = { new NumberEval(4), BoolEval.FALSE, new NumberEval(5), new StringEval("Albert"), BoolEval.FALSE, BoolEval.TRUE, new NumberEval(10), new StringEval("Charles"), new StringEval("Ed"), new NumberEval(10), new NumberEval(25), BoolEval.TRUE, new StringEval("Ed") };
    AreaEval ae = EvalFactory.createAreaEval("A1:A13", values);
    assertEquals(ErrorEval.NA, invokeMatch(new StringEval("Aaron"), ae, MATCH_LARGEST_LTE));
    confirmInt(5, invokeMatch(BoolEval.FALSE, ae, MATCH_LARGEST_LTE));
    confirmInt(2, invokeMatch(BoolEval.FALSE, ae, MATCH_EXACT));
    confirmInt(3, invokeMatch(new NumberEval(5), ae, MATCH_LARGEST_LTE));
    confirmInt(3, invokeMatch(new NumberEval(5), ae, MATCH_EXACT));
    confirmInt(8, invokeMatch(new StringEval("CHARLES"), ae, MATCH_EXACT));
    //wildcard values
    confirmInt(8, invokeMatch(new StringEval("CHAR*"), ae, MATCH_EXACT));
    confirmInt(8, invokeMatch(new StringEval("*CHARLES"), ae, MATCH_EXACT));
    confirmInt(4, invokeMatch(new StringEval("Ben"), ae, MATCH_LARGEST_LTE));
    confirmInt(13, invokeMatch(new StringEval("ED"), ae, MATCH_LARGEST_LTE));
    confirmInt(13, invokeMatch(new StringEval("ED*"), ae, MATCH_LARGEST_LTE));
    confirmInt(13, invokeMatch(new StringEval("*ED"), ae, MATCH_LARGEST_LTE));
    confirmInt(9, invokeMatch(new StringEval("ED"), ae, MATCH_EXACT));
    confirmInt(9, invokeMatch(new StringEval("ED*"), ae, MATCH_EXACT));
    confirmInt(13, invokeMatch(new StringEval("Hugh"), ae, MATCH_LARGEST_LTE));
    assertEquals(ErrorEval.NA, invokeMatch(new StringEval("Hugh"), ae, MATCH_EXACT));
    confirmInt(11, invokeMatch(new NumberEval(30), ae, MATCH_LARGEST_LTE));
    confirmInt(12, invokeMatch(BoolEval.TRUE, ae, MATCH_LARGEST_LTE));
}
Also used : StringEval(org.apache.poi.ss.formula.eval.StringEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 33 with StringEval

use of org.apache.poi.ss.formula.eval.StringEval in project poi by apache.

the class TestMatch method testSimpleString.

public void testSimpleString() {
    // Arrange
    ValueEval[] values = { new StringEval("Albert"), new StringEval("Charles"), new StringEval("Ed"), new StringEval("Greg"), new StringEval("Ian") };
    AreaEval ae = EvalFactory.createAreaEval("A1:A5", values);
    // Note String comparisons are case insensitive
    confirmInt(3, invokeMatch(new StringEval("Ed"), ae, MATCH_LARGEST_LTE));
    confirmInt(3, invokeMatch(new StringEval("eD"), ae, MATCH_LARGEST_LTE));
    confirmInt(3, invokeMatch(new StringEval("Ed"), ae, MATCH_EXACT));
    confirmInt(3, invokeMatch(new StringEval("ed"), ae, MATCH_EXACT));
    assertEquals(ErrorEval.NA, invokeMatch(new StringEval("Hugh"), ae, MATCH_EXACT));
}
Also used : StringEval(org.apache.poi.ss.formula.eval.StringEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Example 34 with StringEval

use of org.apache.poi.ss.formula.eval.StringEval in project poi by apache.

the class TestText method testTextWithDateFormatSecondArg.

@Test
public void testTextWithDateFormatSecondArg() {
    TimeZone userTZ = LocaleUtil.getUserTimeZone();
    LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
    try {
        // Test with Java style M=Month
        ValueEval numArg = new NumberEval(321.321);
        ValueEval formatArg = new StringEval("dd:MM:yyyy hh:mm:ss");
        ValueEval[] args = { numArg, formatArg };
        ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        ValueEval testResult = new StringEval("16:11:1900 07:42:14");
        assertEquals(testResult.toString(), result.toString());
        // Excel also supports "m before h is month"
        formatArg = new StringEval("dd:mm:yyyy hh:mm:ss");
        args[1] = formatArg;
        result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        testResult = new StringEval("16:11:1900 07:42:14");
        assertEquals(testResult.toString(), result.toString());
        // this line is intended to compute how "November" would look like in the current locale
        // update: now the locale will be (if not set otherwise) always Locale.getDefault() (see LocaleUtil)
        DateFormatSymbols dfs = DateFormatSymbols.getInstance(LocaleUtil.getUserLocale());
        SimpleDateFormat sdf = new SimpleDateFormat("MMMM", dfs);
        sdf.setTimeZone(LocaleUtil.getUserTimeZone());
        String november = sdf.format(LocaleUtil.getLocaleCalendar(2015, 10, 1).getTime());
        // Again with Java style
        formatArg = new StringEval("MMMM dd, yyyy");
        args[1] = formatArg;
        result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        testResult = new StringEval(november + " 16, 1900");
        assertEquals(testResult.toString(), result.toString());
        // And Excel style
        formatArg = new StringEval("mmmm dd, yyyy");
        args[1] = formatArg;
        result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        testResult = new StringEval(november + " 16, 1900");
        assertEquals(testResult.toString(), result.toString());
    } finally {
        LocaleUtil.setUserTimeZone(userTZ);
    }
}
Also used : TimeZone(java.util.TimeZone) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) DateFormatSymbols(java.text.DateFormatSymbols) SimpleDateFormat(java.text.SimpleDateFormat) NumberEval(org.apache.poi.ss.formula.eval.NumberEval) Test(org.junit.Test)

Example 35 with StringEval

use of org.apache.poi.ss.formula.eval.StringEval in project poi by apache.

the class TestText method testTextWithFractionFormatSecondArg.

@Test
public void testTextWithFractionFormatSecondArg() {
    ValueEval numArg = new NumberEval(321.321);
    ValueEval formatArg = new StringEval("# #/#");
    ValueEval[] args = { numArg, formatArg };
    ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    ValueEval testResult = new StringEval("321 1/3");
    assertEquals(testResult.toString(), result.toString());
    formatArg = new StringEval("# #/##");
    args[1] = formatArg;
    result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    testResult = new StringEval("321 26/81");
    assertEquals(testResult.toString(), result.toString());
    formatArg = new StringEval("#/##");
    args[1] = formatArg;
    result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
    testResult = new StringEval("26027/81");
    assertEquals(testResult.toString(), result.toString());
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval) Test(org.junit.Test)

Aggregations

StringEval (org.apache.poi.ss.formula.eval.StringEval)58 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)48 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)29 Test (org.junit.Test)18 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)13 Calendar (java.util.Calendar)10 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)10 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)9 Date (java.util.Date)8 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)7 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)5 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3 RefEval (org.apache.poi.ss.formula.eval.RefEval)3 BigDecimal (java.math.BigDecimal)1 DateFormatSymbols (java.text.DateFormatSymbols)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 NumberFormat (java.text.NumberFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1