diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-12-02 16:55:05 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-12-02 16:55:05 -0500 |
commit | cc79db65bc347c50267d0a719278ef1d90cf6b1a (patch) | |
tree | 65717e8d7e75247ced380a72842c50b94fc61a0e /src/test/java/sevenUnits/utils | |
parent | c4ae6887c32ee8d2e9e8853ffd12721d4db2fd3b (diff) | |
parent | 9c30c3ad4d4658964e2bb2bb5be6c2eebbfbe8af (diff) |
Merge branch 'release-0.3.2'
Diffstat (limited to 'src/test/java/sevenUnits/utils')
4 files changed, 139 insertions, 23 deletions
diff --git a/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java b/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java index 46afe77..d653848 100644 --- a/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java +++ b/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java @@ -31,7 +31,6 @@ import java.util.NoSuchElementException; import org.junit.jupiter.api.Test; -import sevenUnits.utils.ConditionalExistenceCollections; import sevenUnits.utils.ConditionalExistenceCollections.ConditionalExistenceIterator; /** diff --git a/src/test/java/sevenUnits/utils/ExpressionParserTest.java b/src/test/java/sevenUnits/utils/ExpressionParserTest.java index 29648ee..a954b12 100644 --- a/src/test/java/sevenUnits/utils/ExpressionParserTest.java +++ b/src/test/java/sevenUnits/utils/ExpressionParserTest.java @@ -18,37 +18,65 @@ package sevenUnits.utils; import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.stream.IntStream; +import java.util.stream.Stream; -import sevenUnits.utils.ExpressionParser; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +// TODO add tests for expression-to-RPN and RPN-to-result /** - * A test for the {@code ExpressionParser} class. This is NOT part of this program's public API. + * A test for the {@code ExpressionParser} class. This is NOT part of this + * program's public API. * * @author Adrien Hopkins * @since 2019-03-22 * @since v0.2.0 */ class ExpressionParserTest { - private static final ExpressionParser<Integer> numberParser = new ExpressionParser.Builder<>(Integer::parseInt) - .addBinaryOperator("+", (o1, o2) -> o1 + o2, 0).addBinaryOperator("-", (o1, o2) -> o1 - o2, 0) - .addBinaryOperator("*", (o1, o2) -> o1 * o2, 1).addBinaryOperator("/", (o1, o2) -> o1 / o2, 1) - .addBinaryOperator("^", (o1, o2) -> (int) Math.pow(o1, o2), 2).build(); - + private static final ExpressionParser<Integer> numberParser = new ExpressionParser.Builder<>( + Integer::parseInt).addBinaryOperator("+", (o1, o2) -> o1 + o2, 0) + .addBinaryOperator("-", (o1, o2) -> o1 - o2, 0) + .addBinaryOperator("*", (o1, o2) -> o1 * o2, 1) + .addBinaryOperator("/", (o1, o2) -> o1 / o2, 1) + .addBinaryOperator("^", (o1, o2) -> (int) Math.pow(o1, o2), 2) + .build(); + /** - * Test method for {@link sevenUnits.utils.ExpressionParser#parseExpression(java.lang.String)}. + * The expressions used in the expression parsing tests */ - @Test - public void testParseExpression() { - // test parsing of expressions - assertEquals((int) numberParser.parseExpression("1 + 2 ^ 5 * 3"), 97); - assertEquals((int) numberParser.parseExpression("(1 + 2) ^ 5 * 3"), 729); - - // ensure it normally goes left to right - assertEquals((int) numberParser.parseExpression("1 + 2 + 3 + 4"), 10); - assertEquals((int) numberParser.parseExpression("12 - 4 - 3"), 5); - assertEquals((int) numberParser.parseExpression("12 - (4 - 3)"), 11); - assertEquals((int) numberParser.parseExpression("1 / 2 + 3"), 3); + private static final List<String> TEST_EXPRESSIONS = List.of( + // test parsing of expressions + "1 + 2 ^ 5 * 3", "(1 + 2) ^ 5 * 3", + "12 * 5 + (3 ^ (2 * 3) - 72) / (3 + 3 * 2)", + + // ensure it normally goes from left to right + "1 + 2 + 3 + 4", "12 - 4 - 3", "12 - (4 - 3)", "1 / 2 + 3"); + + /** + * The expected results for evaluating these expressions + */ + private static final int[] RESULTS = { 97, 729, 133, 10, 5, 11, 3 }; + + /** + * @return A stream of objects, where each one is an expression and the + * expected result + * @since 2021-09-27 + */ + private static final Stream<Arguments> testParseExpressionData() { + return IntStream.range(0, TEST_EXPRESSIONS.size()) + .mapToObj(i -> Arguments.of(TEST_EXPRESSIONS.get(i), RESULTS[i])); + } + + /** + * Test method for + * {@link sevenUnits.utils.ExpressionParser#parseExpression(java.lang.String)}. + */ + @ParameterizedTest + @MethodSource("testParseExpressionData") + public void testParseExpression(String expression, int value) { + assertEquals(value, numberParser.parseExpression(expression)); } - } diff --git a/src/test/java/sevenUnits/utils/ObjectProductTest.java b/src/test/java/sevenUnits/utils/ObjectProductTest.java index 13fd7ec..15ff277 100644 --- a/src/test/java/sevenUnits/utils/ObjectProductTest.java +++ b/src/test/java/sevenUnits/utils/ObjectProductTest.java @@ -30,7 +30,6 @@ import static sevenUnits.unit.Metric.Dimensions.VOLUME; import org.junit.jupiter.api.Test; import sevenUnits.unit.Metric; -import sevenUnits.utils.ObjectProduct; /** * Tests for {@link ObjectProduct} using BaseDimension as a test object. This is NOT part of this program's public API. diff --git a/src/test/java/sevenUnits/utils/UncertainDoubleTest.java b/src/test/java/sevenUnits/utils/UncertainDoubleTest.java new file mode 100644 index 0000000..c891f20 --- /dev/null +++ b/src/test/java/sevenUnits/utils/UncertainDoubleTest.java @@ -0,0 +1,90 @@ +/** + * Copyright (C) 2021 Adrien Hopkins + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package sevenUnits.utils; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static sevenUnits.utils.UncertainDouble.fromString; +import static sevenUnits.utils.UncertainDouble.of; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +/** + * Tests for the UncertainDouble + * + * @author Adrien Hopkins + * @since 2021-11-29 + */ +class UncertainDoubleTest { + @Test + final void testCompareTo() { + assertTrue(of(2.0, 0.5).compareTo(of(2.0, 0.1)) == 0); + assertTrue(of(2.0, 0.5).compareTo(of(1.0, 0.1)) > 0); + assertTrue(of(2.0, 0.5).compareTo(of(3.0, 0.1)) < 0); + } + + @Test + final void testExactOperations() { + final UncertainDouble x = UncertainDouble.of(Math.PI, 0.1); + + // slightly different because roundoff errors + final UncertainDouble x1 = UncertainDouble.of(Math.PI + Math.E - Math.E, + 0.1); + final UncertainDouble x2 = UncertainDouble.of(Math.PI * Math.E / Math.E, + 0.1); + + // get results + final UncertainDouble result1 = x.plusExact(Math.E).minusExact(Math.E); + final UncertainDouble result2 = x.timesExact(Math.E) + .dividedByExact(Math.E); + + // test that these operations work & don't change uncertainty + assertEquals(x1, result1); + assertTrue(x.equivalent(result1)); + assertEquals(x2, result2); + assertTrue(x.equivalent(result2)); + + // exponents are different + assertEquals(Math.pow(Math.PI, Math.E), + x.toExponentExact(Math.E).value()); + } + + @Test + final void testFromString() { + // valid strings + assertEquals(of(2.0, 0.5), fromString("2.0 ± 0.5")); + assertEquals(of(2.0, 0.5), fromString("2.0 +- 0.5")); + assertEquals(of(2.0, 0.0), fromString("2.0")); + + // invalid strings + for (final String s : List.of("2.A", "A", "2.0 ± .", "± 3.5")) { + assertThrows(IllegalArgumentException.class, () -> fromString(s)); + } + + // back and forth + assertEquals("2.0 ± 0.5", of(2.0, 0.5).toString()); + assertEquals("2.0", of(2.0, 0).toString()); + } + + @Test + final void testHashCode() { + assertEquals(of(2.0, 0.5).hashCode(), fromString("2.0 ± 0.5").hashCode()); + } +} |