diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-11-29 17:55:35 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-11-29 17:55:35 -0500 |
commit | 8ea77520ce58e948eeffc4c2e8c25c69e88ed00a (patch) | |
tree | 970052482622edd7e7fa950dfcee66b165818a4f /src | |
parent | 16dfc3d7c7813fa343f3f3502bf5a2fea4a252cc (diff) |
Added a full suite of tests for the UncertainDouble
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/sevenUnits/utils/UncertainDouble.java | 15 | ||||
-rw-r--r-- | src/test/java/sevenUnits/utils/UncertainDoubleTest.java | 90 |
2 files changed, 100 insertions, 5 deletions
diff --git a/src/main/java/sevenUnits/utils/UncertainDouble.java b/src/main/java/sevenUnits/utils/UncertainDouble.java index 8fe4b31..fe41104 100644 --- a/src/main/java/sevenUnits/utils/UncertainDouble.java +++ b/src/main/java/sevenUnits/utils/UncertainDouble.java @@ -36,13 +36,14 @@ public final class UncertainDouble implements Comparable<UncertainDouble> { */ public static final UncertainDouble ZERO = UncertainDouble.of(0, 0); + static final String NUMBER_REGEX = "(\\d+(?:[\\.,]\\d+))"; + /** * A regular expression that can recognize toString forms */ - private static final Pattern TO_STRING = Pattern - .compile("([a-zA-Z_0-9\\.\\,]+)" // a number - // optional "± [number]" - + "(?:\\s*(?:±|\\+-)\\s*([a-zA-Z_0-9\\.\\,]+))?"); + static final Pattern TO_STRING = Pattern.compile(NUMBER_REGEX + // optional "± [number]" + + "(?:\\s*(?:±|\\+-)\\s*" + NUMBER_REGEX + ")?"); /** * Parses a string in the form of {@link UncertainDouble#toString(boolean)} @@ -60,10 +61,14 @@ public final class UncertainDouble implements Comparable<UncertainDouble> { Objects.requireNonNull(s, "s may not be null"); final Matcher matcher = TO_STRING.matcher(s); + if (!matcher.matches()) + throw new IllegalArgumentException( + "Could not parse stirng \"" + s + "\"."); + double value, uncertainty; try { value = Double.parseDouble(matcher.group(1)); - } catch (IllegalStateException | NumberFormatException e) { + } catch (final NumberFormatException e) { throw new IllegalArgumentException( "String " + s + " not in correct format."); } 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()); + } +} |