This is an automated email from the ASF dual-hosted git repository. ddekany pushed a commit to branch 3 in repository https://gitbox.apache.org/repos/asf/freemarker.git
commit 4cc9e519a248145a13c880987439e00bc28db68e Author: ddekany <[email protected]> AuthorDate: Sun Jan 1 15:51:11 2023 +0100 Forward ported from 2.3-gae: (Added some tests for ArithmeticEngine.toNumber, and ?number) --- .../org/apache/freemarker/core/NumberBiTest.java | 48 ++++++++++++++++++++++ ...icEngineTest.java => ArithmeticEngineTest.java} | 40 +++++++++++++++--- .../impl/BigDecimalArithmeticEngine.java | 2 +- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/NumberBiTest.java b/freemarker-core-test/src/test/java/org/apache/freemarker/core/NumberBiTest.java new file mode 100644 index 00000000..1e0524db --- /dev/null +++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/NumberBiTest.java @@ -0,0 +1,48 @@ +/* + * 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.freemarker.core; + +import org.apache.freemarker.test.TemplateTest; +import org.junit.Test; + +import java.io.IOException; + +public class NumberBiTest extends TemplateTest { + + @Test + public void testSimple() throws TemplateException, IOException { + assertNumberBi("1", "1"); + assertNumberBi("-1", "-1"); + assertNumberBi("1.9000", "1.9"); + assertNumberBi("19E-1", "1.9"); + assertNumberBi("INF", "INF"); + assertNumberBi("-Infinity", "-INF"); + assertNumberBi("NaN", "NaN"); + } + + @Test + public void testPlusPrefix() throws TemplateException, IOException { + assertNumberBi("+1", "1"); + } + + private final void assertNumberBi(String input, String output) throws TemplateException, IOException { + assertOutput("${'" + input + "'?number?c}", output); + } +} diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngineTest.java b/freemarker-core-test/src/test/java/org/apache/freemarker/core/arithmetic/impl/ArithmeticEngineTest.java similarity index 69% rename from freemarker-core-test/src/test/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngineTest.java rename to freemarker-core-test/src/test/java/org/apache/freemarker/core/arithmetic/impl/ArithmeticEngineTest.java index 422ec16a..a1a4b793 100644 --- a/freemarker-core-test/src/test/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngineTest.java +++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/arithmetic/impl/ArithmeticEngineTest.java @@ -19,15 +19,17 @@ package org.apache.freemarker.core.arithmetic.impl; -import static org.apache.freemarker.core.arithmetic.impl.BigDecimalArithmeticEngine.*; -import static org.junit.Assert.*; +import org.apache.freemarker.core.arithmetic.ArithmeticEngine; +import org.hamcrest.Matchers; +import org.junit.Test; import java.math.BigDecimal; import java.math.BigInteger; -import org.junit.Test; +import static org.apache.freemarker.core.arithmetic.impl.BigDecimalArithmeticEngine.INSTANCE; +import static org.junit.Assert.*; -public class BigDecimalArithmeticEngineTest { +public class ArithmeticEngineTest { @Test public void compareNumbersZeroTest() { @@ -93,5 +95,33 @@ public class BigDecimalArithmeticEngineTest { } } } - + + @Test + public void toNumberTest() { + for (ArithmeticEngine arithmeticEngine : new ArithmeticEngine[]{BigDecimalArithmeticEngine.INSTANCE, ConservativeArithmeticEngine.INSTANCE}) { + assertEquals(Double.POSITIVE_INFINITY, arithmeticEngine.toNumber("INF")); + assertEquals(Double.NEGATIVE_INFINITY, arithmeticEngine.toNumber("-INF")); + assertEquals(Double.NEGATIVE_INFINITY, arithmeticEngine.toNumber("-Infinity")); + assertEquals(Double.POSITIVE_INFINITY, arithmeticEngine.toNumber("Infinity")); + Number nan = arithmeticEngine.toNumber("NaN"); + assertThat(nan, Matchers.instanceOf(Double.class)); + assertTrue(Double.isNaN((double) nan)); + } + + assertEquals(new BigDecimal("1234567"), BigDecimalArithmeticEngine.INSTANCE.toNumber("1234567")); + assertEquals(Integer.valueOf("1234567"), ConservativeArithmeticEngine.INSTANCE.toNumber("1234567")); + + assertEquals(new BigDecimal("12345678901234"), BigDecimalArithmeticEngine.INSTANCE.toNumber("12345678901234")); + assertEquals(12345678901234L, ConservativeArithmeticEngine.INSTANCE.toNumber("12345678901234")); + + assertEquals(new BigDecimal("12345678901234567890"), BigDecimalArithmeticEngine.INSTANCE.toNumber("12345678901234567890")); + assertEquals(new BigInteger("12345678901234567890"), ConservativeArithmeticEngine.INSTANCE.toNumber("12345678901234567890")); + + assertEquals(new BigDecimal("1.9"), BigDecimalArithmeticEngine.INSTANCE.toNumber("1.9")); + assertEquals(1.9, ConservativeArithmeticEngine.INSTANCE.toNumber("1.9")); + + assertEquals(new BigDecimal("0.9"), BigDecimalArithmeticEngine.INSTANCE.toNumber(".9")); + assertEquals(0.9, ConservativeArithmeticEngine.INSTANCE.toNumber(".9")); + } + } diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngine.java b/freemarker-core/src/main/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngine.java index 63512a07..14d69634 100644 --- a/freemarker-core/src/main/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngine.java +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/arithmetic/impl/BigDecimalArithmeticEngine.java @@ -38,7 +38,7 @@ public class BigDecimalArithmeticEngine extends ArithmeticEngine { @Override public int compareNumbers(Number first, Number second) { // We try to find the result based on the sign (+/-/0) first, because: - // - It's much faster than converting to BigDecial, and comparing to 0 is the most common comparison. + // - It's much faster than converting to BigDecimal, and comparing to 0 is the most common comparison. // - It doesn't require any type conversions, and thus things like "Infinity > 0" won't fail. int firstSignum = _NumberUtils.getSignum(first); int secondSignum = _NumberUtils.getSignum(second);
