From 987fd8406d65505aedecd17e51216eb0ce393fbb Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 22 May 2019 17:32:40 -0400 Subject: Added new default methods to the Unit interface. --- src/test/java/UnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/test/java/UnitTest.java') diff --git a/src/test/java/UnitTest.java b/src/test/java/UnitTest.java index 00fcf3c..7a3f29d 100755 --- a/src/test/java/UnitTest.java +++ b/src/test/java/UnitTest.java @@ -63,7 +63,7 @@ public class UnitTest { final BaseUnit metre = SI.METRE; final Unit inch = metre.times(0.0254); - assertEquals(1.9, inch.convertToBase(75), 0.01); + assertEquals(1.9, inch.convertTo(metre, 75), 0.01); // try random stuff for (int i = 0; i < 1000; i++) { -- cgit v1.2.3 From 740c5a4c13e98bd04b385eee9b881e53eb88d3b8 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 26 Aug 2019 16:52:11 -0400 Subject: The unit file parser now uses regular expressions. --- .settings/org.eclipse.jdt.core.prefs | 2 ++ CHANGELOG.org | 20 +++++++++++------ src/org/unitConverter/UnitsDatabase.java | 26 +++++++++++++++++----- .../converterGUI/DelegateListModel.java | 0 .../converterGUI/FilterComparator.java | 0 .../unitConverter/converterGUI/GridBagBuilder.java | 0 .../converterGUI/UnitConverterGUI.java | 0 src/org/unitConverter/dimension/BaseDimension.java | 0 .../dimension/OtherBaseDimension.java | 0 .../unitConverter/dimension/SIBaseDimension.java | 0 .../dimension/StandardDimensions.java | 0 src/org/unitConverter/dimension/UnitDimension.java | 0 src/org/unitConverter/dimension/package-info.java | 0 src/org/unitConverter/unit/BaseUnit.java | 0 src/org/unitConverter/unit/DefaultUnitPrefix.java | 0 src/org/unitConverter/unit/NonlinearUnits.java | 0 src/org/unitConverter/unit/SIPrefix.java | 0 src/org/unitConverter/unit/Unit.java | 0 src/org/unitConverter/unit/UnitPrefix.java | 0 src/org/unitConverter/unit/UnitSystem.java | 0 src/test/java/UnitDimensionTest.java | 0 src/test/java/UnitTest.java | 0 unitsfile.txt | 0 23 files changed, 36 insertions(+), 12 deletions(-) mode change 100755 => 100644 src/org/unitConverter/UnitsDatabase.java mode change 100755 => 100644 src/org/unitConverter/converterGUI/DelegateListModel.java mode change 100755 => 100644 src/org/unitConverter/converterGUI/FilterComparator.java mode change 100755 => 100644 src/org/unitConverter/converterGUI/GridBagBuilder.java mode change 100755 => 100644 src/org/unitConverter/converterGUI/UnitConverterGUI.java mode change 100755 => 100644 src/org/unitConverter/dimension/BaseDimension.java mode change 100755 => 100644 src/org/unitConverter/dimension/OtherBaseDimension.java mode change 100755 => 100644 src/org/unitConverter/dimension/SIBaseDimension.java mode change 100755 => 100644 src/org/unitConverter/dimension/StandardDimensions.java mode change 100755 => 100644 src/org/unitConverter/dimension/UnitDimension.java mode change 100755 => 100644 src/org/unitConverter/dimension/package-info.java mode change 100755 => 100644 src/org/unitConverter/unit/BaseUnit.java mode change 100755 => 100644 src/org/unitConverter/unit/DefaultUnitPrefix.java mode change 100755 => 100644 src/org/unitConverter/unit/NonlinearUnits.java mode change 100755 => 100644 src/org/unitConverter/unit/SIPrefix.java mode change 100755 => 100644 src/org/unitConverter/unit/Unit.java mode change 100755 => 100644 src/org/unitConverter/unit/UnitPrefix.java mode change 100755 => 100644 src/org/unitConverter/unit/UnitSystem.java mode change 100755 => 100644 src/test/java/UnitDimensionTest.java mode change 100755 => 100644 src/test/java/UnitTest.java mode change 100755 => 100644 unitsfile.txt (limited to 'src/test/java/UnitTest.java') diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 8445b6b..db24ee7 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -7,7 +7,9 @@ org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=1.8 diff --git a/CHANGELOG.org b/CHANGELOG.org index 77e7593..1a49709 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -1,7 +1,20 @@ * Changelog All notable changes in this project will be shown in this file. +** Unreleased +*** Added + - Added a simple unit conversion method to the Unit interface + - Added a static factory to create nonlinear units to the Unit interface +*** Changed + - You can now use tabs or spaces to separate unit names and their definitions in unit files. ** v0.2.0 - [2019-04-14] +*** Added + - A selection-based unit converter which allows you to select two units, input a value, and convert. + - The UnitDatabase now stores dimensions. + - A system to parse mathematical expressions, used to parse unit expressions. + - You can now add and subtract in unit expressions! + - Instructions for obtaining unit instances are provided in the relevant classes + - The UnitPrefix interface now provides default times, dividedBy and toExponent methods. *** Changed - When searching for units, units with no prefixes are searched for before prefixed units - Smaller prefixes are searched for before larger prefixes @@ -10,13 +23,6 @@ All notable changes in this project will be shown in this file. - BaseUnit is now a subclass of LinearUnit - In unit files, Comments can now start in the middle of lines - UnitsDatabase.addAllFromFile() has been renamed to loadUnitsFile() -*** Added - - A selection-based unit converter which allows you to select two units, input a value, and convert. - - The UnitDatabase now stores dimensions. - - A system to parse mathematical expressions, used to parse unit expressions. - - You can now add and subtract in unit expressions! - - Instructions for obtaining unit instances are provided in the relevant classes - - The UnitPrefix interface now provides default times, dividedBy and toExponent methods. ** v0.1.0 - [2019-02-01] NOTE: At this stage, the API is subject to significant change. *** Added diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java old mode 100755 new mode 100644 index 37d53d4..dcc98df --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -36,6 +36,8 @@ import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Predicate; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.unitConverter.dimension.UnitDimension; import org.unitConverter.math.DecimalComparison; @@ -971,6 +973,11 @@ public final class UnitsDatabase { } } + /** + * A regular expression that separates names and expressions in unit files. + */ + private static final Pattern NAME_EXPRESSION = Pattern.compile("(\\S+)\\s+(\\S.*)"); + /** * The exponent operator * @@ -1200,13 +1207,22 @@ public final class UnitsDatabase { } // divide line into name and expression - final String[] parts = line.split("\t"); - if (parts.length < 2) + final Matcher lineMatcher = NAME_EXPRESSION.matcher(line); + if (!lineMatcher.matches()) throw new IllegalArgumentException(String.format( - "Lines must consist of a unit name and its definition, separated by tab(s) (line %d).", + "Error at line %d: Lines of a unit file must consist of a unit name, then spaces or tabs, then a unit expression.", lineCounter)); - final String name = parts[0]; - final String expression = parts[parts.length - 1]; + final String name = lineMatcher.group(1); + final String expression = lineMatcher.group(2); + + // divide line into name and expression + // final String[] parts = line.split("\t"); + // if (parts.length < 2) + // throw new IllegalArgumentException(String.format( + // "Lines must consist of a unit name and its definition, separated by tab(s) (line %d).", + // lineCounter)); + // final String name = parts[0]; + // final String expression = parts[parts.length - 1]; if (name.endsWith(" ")) { System.err.printf("Warning - line %d's unit name ends in a space", lineCounter); diff --git a/src/org/unitConverter/converterGUI/DelegateListModel.java b/src/org/unitConverter/converterGUI/DelegateListModel.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/converterGUI/FilterComparator.java b/src/org/unitConverter/converterGUI/FilterComparator.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/converterGUI/GridBagBuilder.java b/src/org/unitConverter/converterGUI/GridBagBuilder.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/dimension/BaseDimension.java b/src/org/unitConverter/dimension/BaseDimension.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/dimension/OtherBaseDimension.java b/src/org/unitConverter/dimension/OtherBaseDimension.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/dimension/SIBaseDimension.java b/src/org/unitConverter/dimension/SIBaseDimension.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/dimension/StandardDimensions.java b/src/org/unitConverter/dimension/StandardDimensions.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/dimension/UnitDimension.java b/src/org/unitConverter/dimension/UnitDimension.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/dimension/package-info.java b/src/org/unitConverter/dimension/package-info.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/DefaultUnitPrefix.java b/src/org/unitConverter/unit/DefaultUnitPrefix.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/NonlinearUnits.java b/src/org/unitConverter/unit/NonlinearUnits.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/SIPrefix.java b/src/org/unitConverter/unit/SIPrefix.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/Unit.java b/src/org/unitConverter/unit/Unit.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java old mode 100755 new mode 100644 diff --git a/src/org/unitConverter/unit/UnitSystem.java b/src/org/unitConverter/unit/UnitSystem.java old mode 100755 new mode 100644 diff --git a/src/test/java/UnitDimensionTest.java b/src/test/java/UnitDimensionTest.java old mode 100755 new mode 100644 diff --git a/src/test/java/UnitTest.java b/src/test/java/UnitTest.java old mode 100755 new mode 100644 diff --git a/unitsfile.txt b/unitsfile.txt old mode 100755 new mode 100644 -- cgit v1.2.3 From efb8729719257c4a766058c5c7122648d94dd8b3 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Tue, 15 Oct 2019 17:53:40 -0400 Subject: Upgraded to JUnit 5 and moved tests to their proper directories --- .classpath | 6 +- .project | 6 - src/org/unitConverter/UnitsDatabaseTest.java | 312 ++++++++++++++++++++ .../unitConverter/dimension/UnitDimensionTest.java | 77 +++++ .../unitConverter/math/ExpressionParserTest.java | 52 ++++ src/org/unitConverter/unit/UnitTest.java | 111 ++++++++ src/test/java/ExpressionParserTest.java | 53 ---- src/test/java/UnitDimensionTest.java | 79 ------ src/test/java/UnitTest.java | 116 -------- src/test/java/UnitsDatabaseTest.java | 313 --------------------- src/test/java/package-info.java | 24 -- 11 files changed, 553 insertions(+), 596 deletions(-) create mode 100644 src/org/unitConverter/UnitsDatabaseTest.java create mode 100644 src/org/unitConverter/dimension/UnitDimensionTest.java create mode 100644 src/org/unitConverter/math/ExpressionParserTest.java create mode 100644 src/org/unitConverter/unit/UnitTest.java delete mode 100644 src/test/java/ExpressionParserTest.java delete mode 100644 src/test/java/UnitDimensionTest.java delete mode 100644 src/test/java/UnitTest.java delete mode 100644 src/test/java/UnitsDatabaseTest.java delete mode 100644 src/test/java/package-info.java (limited to 'src/test/java/UnitTest.java') diff --git a/.classpath b/.classpath index ef141e6..8fc330a 100644 --- a/.classpath +++ b/.classpath @@ -5,16 +5,12 @@ + - - - - - diff --git a/.project b/.project index 56f686e..656186b 100644 --- a/.project +++ b/.project @@ -10,14 +10,8 @@ - - org.eclipse.m2e.core.maven2Builder - - - - org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature diff --git a/src/org/unitConverter/UnitsDatabaseTest.java b/src/org/unitConverter/UnitsDatabaseTest.java new file mode 100644 index 0000000..c46d598 --- /dev/null +++ b/src/org/unitConverter/UnitsDatabaseTest.java @@ -0,0 +1,312 @@ +/** + * Copyright (C) 2019 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 . + */ +package org.unitConverter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.junit.jupiter.api.Test; +import org.unitConverter.unit.DefaultUnitPrefix; +import org.unitConverter.unit.LinearUnit; +import org.unitConverter.unit.SI; +import org.unitConverter.unit.Unit; +import org.unitConverter.unit.UnitPrefix; + +/** + * A test for the {@link UnitsDatabase} class. This is NOT part of this program's public API. + * + * @author Adrien Hopkins + * @since 2019-04-14 + * @since v0.2.0 + */ +class UnitsDatabaseTest { + // some linear units and one nonlinear + private static final Unit U = SI.METRE; + private static final Unit V = SI.KILOGRAM; + private static final Unit W = SI.SECOND; + + // used for testing expressions + // J = U^2 * V / W^2 + private static final LinearUnit J = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); + private static final LinearUnit K = SI.KELVIN; + + private static final Unit NONLINEAR = Unit.fromConversionFunctions(SI.METRE, o -> o + 1, o -> o - 1); + + // make the prefix values prime so I can tell which multiplications were made + private static final UnitPrefix A = new DefaultUnitPrefix(2); + private static final UnitPrefix B = new DefaultUnitPrefix(3); + private static final UnitPrefix C = new DefaultUnitPrefix(5); + private static final UnitPrefix AB = new DefaultUnitPrefix(7); + private static final UnitPrefix BC = new DefaultUnitPrefix(11); + + /** + * Confirms that operations that shouldn't function for infinite databases throw an {@code IllegalStateException}. + * + * @since 2019-05-03 + */ + @Test + public void testInfiniteSetExceptions() { + // load units + final UnitsDatabase infiniteDatabase = new UnitsDatabase(); + + infiniteDatabase.addUnit("J", J); + infiniteDatabase.addUnit("K", K); + + infiniteDatabase.addPrefix("A", A); + infiniteDatabase.addPrefix("B", B); + infiniteDatabase.addPrefix("C", C); + + { + boolean exceptionThrown = false; + try { + infiniteDatabase.unitMap().entrySet().toArray(); + } catch (final IllegalStateException e) { + exceptionThrown = true; + // pass! + } finally { + if (!exceptionThrown) { + fail("No IllegalStateException thrown"); + } + } + } + + { + boolean exceptionThrown = false; + try { + infiniteDatabase.unitMap().keySet().toArray(); + } catch (final IllegalStateException e) { + exceptionThrown = true; + // pass! + } finally { + if (!exceptionThrown) { + fail("No IllegalStateException thrown"); + } + } + } + } + + /** + * Test that prefixes correctly apply to units. + * + * @since 2019-04-14 + * @since v0.2.0 + */ + @Test + public void testPrefixes() { + final UnitsDatabase database = new UnitsDatabase(); + + database.addUnit("U", U); + database.addUnit("V", V); + database.addUnit("W", W); + + database.addPrefix("A", A); + database.addPrefix("B", B); + database.addPrefix("C", C); + + // get the product + final Unit abcuNonlinear = database.getUnit("ABCU"); + assert abcuNonlinear instanceof LinearUnit; + + final LinearUnit abcu = (LinearUnit) abcuNonlinear; + assertEquals(A.getMultiplier() * B.getMultiplier() * C.getMultiplier(), abcu.getConversionFactor(), 1e-15); + } + + /** + * Tests the functionnalites of the prefixless unit map. + * + *

+ * The map should be an auto-updating view of the units in the database. + *

+ * + * @since 2019-04-14 + * @since v0.2.0 + */ + @Test + public void testPrefixlessUnitMap() { + final UnitsDatabase database = new UnitsDatabase(); + final Map prefixlessUnits = database.unitMapPrefixless(); + + database.addUnit("U", U); + database.addUnit("V", V); + database.addUnit("W", W); + + // this should work because the map should be an auto-updating view + assertTrue(prefixlessUnits.containsKey("U")); + assertFalse(prefixlessUnits.containsKey("Z")); + + assertTrue(prefixlessUnits.containsValue(U)); + assertFalse(prefixlessUnits.containsValue(NONLINEAR)); + } + + /** + * Tests that the database correctly stores and retrieves units, ignoring prefixes. + * + * @since 2019-04-14 + * @since v0.2.0 + */ + @Test + public void testPrefixlessUnits() { + final UnitsDatabase database = new UnitsDatabase(); + + database.addUnit("U", U); + database.addUnit("V", V); + database.addUnit("W", W); + + assertTrue(database.containsUnitName("U")); + assertFalse(database.containsUnitName("Z")); + + assertEquals(U, database.getUnit("U")); + assertEquals(null, database.getUnit("Z")); + } + + /** + * Test that unit expressions return the correct value. + * + * @since 2019-04-14 + * @since v0.2.0 + */ + @Test + public void testUnitExpressions() { + // load units + final UnitsDatabase database = new UnitsDatabase(); + + database.addUnit("U", U); + database.addUnit("V", V); + database.addUnit("W", W); + database.addUnit("fj", J.times(5)); + database.addUnit("ej", J.times(8)); + + database.addPrefix("A", A); + database.addPrefix("B", B); + database.addPrefix("C", C); + + // first test - test prefixes and operations + final Unit expected1 = J.withPrefix(A).withPrefix(B).withPrefix(C).withPrefix(C); + final Unit actual1 = database.getUnitFromExpression("ABV * CU^2 / W / W"); + + assertEquals(expected1, actual1); + + // second test - test addition and subtraction + final Unit expected2 = J.times(58); + final Unit actual2 = database.getUnitFromExpression("2 fj + 6 ej"); + + assertEquals(expected2, actual2); + } + + /** + * Tests both the unit name iterator and the name-unit entry iterator + * + * @since 2019-04-14 + * @since v0.2.0 + */ + @Test + public void testUnitIterator() { + // load units + final UnitsDatabase database = new UnitsDatabase(); + + database.addUnit("J", J); + database.addUnit("K", K); + + database.addPrefix("A", A); + database.addPrefix("B", B); + database.addPrefix("C", C); + + final int NUM_UNITS = database.unitMapPrefixless().size(); + final int NUM_PREFIXES = database.prefixMap().size(); + + final Iterator nameIterator = database.unitMap().keySet().iterator(); + final Iterator> entryIterator = database.unitMap().entrySet().iterator(); + + int expectedLength = 1; + int unitsWithThisLengthSoFar = 0; + + // loop 1000 times + for (int i = 0; i < 1000; i++) { + // expected length of next + if (unitsWithThisLengthSoFar >= NUM_UNITS * (int) Math.pow(NUM_PREFIXES, expectedLength - 1)) { + expectedLength++; + unitsWithThisLengthSoFar = 0; + } + + // test that stuff is valid + final String nextName = nameIterator.next(); + final Unit nextUnit = database.getUnit(nextName); + final Entry nextEntry = entryIterator.next(); + + assertEquals(expectedLength, nextName.length()); + assertEquals(nextName, nextEntry.getKey()); + assertEquals(nextUnit, nextEntry.getValue()); + + unitsWithThisLengthSoFar++; + } + + // test toString for consistency + final String entryIteratorString = entryIterator.toString(); + for (int i = 0; i < 3; i++) { + assertEquals(entryIteratorString, entryIterator.toString()); + } + + final String nameIteratorString = nameIterator.toString(); + for (int i = 0; i < 3; i++) { + assertEquals(nameIteratorString, nameIterator.toString()); + } + } + + /** + * Determine, given a unit name that could mean multiple things, which meaning is chosen. + *

+ * For example, "ABCU" could mean "A-B-C-U", "AB-C-U", or "A-BC-U". In this case, "AB-C-U" is the correct choice. + *

+ * + * @since 2019-04-14 + * @since v0.2.0 + */ + @Test + public void testUnitPrefixCombinations() { + // load units + final UnitsDatabase database = new UnitsDatabase(); + + database.addUnit("J", J); + + database.addPrefix("A", A); + database.addPrefix("B", B); + database.addPrefix("C", C); + database.addPrefix("AB", AB); + database.addPrefix("BC", BC); + + // test 1 - AB-C-J vs A-BC-J vs A-B-C-J + final Unit expected1 = J.withPrefix(AB).withPrefix(C); + final Unit actual1 = database.getUnit("ABCJ"); + + assertEquals(expected1, actual1); + + // test 2 - ABC-J vs AB-CJ vs AB-C-J + database.addUnit("CJ", J.times(13)); + database.addPrefix("ABC", new DefaultUnitPrefix(17)); + + final Unit expected2 = J.times(17); + final Unit actual2 = database.getUnit("ABCJ"); + + assertEquals(expected2, actual2); + } +} diff --git a/src/org/unitConverter/dimension/UnitDimensionTest.java b/src/org/unitConverter/dimension/UnitDimensionTest.java new file mode 100644 index 0000000..017e3d2 --- /dev/null +++ b/src/org/unitConverter/dimension/UnitDimensionTest.java @@ -0,0 +1,77 @@ +/** + * Copyright (C) 2018 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 . + */ +package org.unitConverter.dimension; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.unitConverter.dimension.StandardDimensions.AREA; +import static org.unitConverter.dimension.StandardDimensions.ENERGY; +import static org.unitConverter.dimension.StandardDimensions.LENGTH; +import static org.unitConverter.dimension.StandardDimensions.MASS; +import static org.unitConverter.dimension.StandardDimensions.MASS_DENSITY; +import static org.unitConverter.dimension.StandardDimensions.QUANTITY; +import static org.unitConverter.dimension.StandardDimensions.TIME; +import static org.unitConverter.dimension.StandardDimensions.VOLUME; + +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link UnitDimension}. This is NOT part of this program's public API. + * + * @author Adrien Hopkins + * @since 2018-12-12 + * @since v0.1.0 + */ +class UnitDimensionTest { + /** + * Tests {@link UnitDimension#equals} + * + * @since 2018-12-12 + * @since v0.1.0 + */ + @Test + public void testEquals() { + assertEquals(LENGTH, LENGTH); + assertFalse(LENGTH.equals(QUANTITY)); + } + + /** + * Tests {@code UnitDimension}'s exponentiation + * + * @since 2019-01-15 + * @since v0.1.0 + */ + @Test + public void testExponents() { + assertEquals(1, LENGTH.getExponent(SIBaseDimension.LENGTH)); + assertEquals(3, VOLUME.getExponent(SIBaseDimension.LENGTH)); + } + + /** + * Tests {@code UnitDimension}'s multiplication and division. + * + * @since 2018-12-12 + * @since v0.1.0 + */ + @Test + public void testMultiplicationAndDivision() { + assertEquals(AREA, LENGTH.times(LENGTH)); + assertEquals(MASS_DENSITY, MASS.dividedBy(VOLUME)); + assertEquals(ENERGY, AREA.times(MASS).dividedBy(TIME).dividedBy(TIME)); + assertEquals(LENGTH, LENGTH.times(TIME).dividedBy(TIME)); + } +} diff --git a/src/org/unitConverter/math/ExpressionParserTest.java b/src/org/unitConverter/math/ExpressionParserTest.java new file mode 100644 index 0000000..f3180c1 --- /dev/null +++ b/src/org/unitConverter/math/ExpressionParserTest.java @@ -0,0 +1,52 @@ +/** + * Copyright (C) 2019 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 . + */ +package org.unitConverter.math; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * 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 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 org.unitConverter.math.ExpressionParser#parseExpression(java.lang.String)}. + */ + @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); + } + +} diff --git a/src/org/unitConverter/unit/UnitTest.java b/src/org/unitConverter/unit/UnitTest.java new file mode 100644 index 0000000..7ae5fbf --- /dev/null +++ b/src/org/unitConverter/unit/UnitTest.java @@ -0,0 +1,111 @@ +/** + * Copyright (C) 2018 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 . + */ +package org.unitConverter.unit; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + +import org.junit.jupiter.api.Test; +import org.unitConverter.dimension.StandardDimensions; +import org.unitConverter.math.DecimalComparison; + +/** + * Testing the various Unit classes. This is NOT part of this program's public API. + * + * @author Adrien Hopkins + * @since 2018-12-22 + * @since v0.1.0 + */ +class UnitTest { + /** A random number generator */ + private static final Random rng = ThreadLocalRandom.current(); + + @Test + public void testAdditionAndSubtraction() { + final LinearUnit inch = SI.METRE.times(0.0254); + final LinearUnit foot = SI.METRE.times(0.3048); + + assertEquals(inch.plus(foot), SI.METRE.times(0.3302)); + assertEquals(foot.minus(inch), SI.METRE.times(0.2794)); + } + + @Test + public void testBaseUnitExclusives() { + // this test should have a compile error if I am doing something wrong + final BaseUnit metrePerSecondSquared = SI.METRE.dividedBy(SI.SECOND.toExponent(2)); + + assertEquals(metrePerSecondSquared, SI.SI.getBaseUnit(StandardDimensions.ACCELERATION)); + } + + @Test + public void testConversion() { + final BaseUnit metre = SI.METRE; + final Unit inch = metre.times(0.0254); + + assertEquals(1.9, inch.convertTo(metre, 75), 0.01); + + // try random stuff + for (int i = 0; i < 1000; i++) { + // initiate random values + final double conversionFactor = rng.nextDouble() * 1000000; + final double testValue = rng.nextDouble() * 1000000; + final double expected = testValue * conversionFactor; + + // test + final Unit unit = SI.METRE.times(conversionFactor); + final double actual = unit.convertToBase(testValue); + + assertEquals(actual, expected, expected * DecimalComparison.DOUBLE_EPSILON); + } + } + + @Test + public void testEquals() { + final BaseUnit metre = SI.METRE; + final Unit meter = SI.SI.getBaseUnit(StandardDimensions.LENGTH); + + assertEquals(metre, meter); + } + + @Test + public void testMultiplicationAndDivision() { + // test unit-times-unit multiplication + final LinearUnit generatedJoule = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); + final LinearUnit actualJoule = SI.SI.getBaseUnit(StandardDimensions.ENERGY); + + assertEquals(generatedJoule, actualJoule); + + // test multiplication by conversion factors + final LinearUnit kilometre = SI.METRE.times(1000); + final LinearUnit hour = SI.SECOND.times(3600); + final LinearUnit generatedKPH = kilometre.dividedBy(hour); + + final LinearUnit actualKPH = SI.SI.getBaseUnit(StandardDimensions.VELOCITY).dividedBy(3.6); + + assertEquals(generatedKPH, actualKPH); + } + + @Test + public void testPrefixes() { + final LinearUnit generatedKilometre = SI.METRE.withPrefix(SIPrefix.KILO); + final LinearUnit actualKilometre = SI.METRE.times(1000); + + assertEquals(generatedKilometre, actualKilometre); + } +} diff --git a/src/test/java/ExpressionParserTest.java b/src/test/java/ExpressionParserTest.java deleted file mode 100644 index 40c91ac..0000000 --- a/src/test/java/ExpressionParserTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (C) 2019 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 . - */ -package test.java; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.unitConverter.math.ExpressionParser; - -/** - * 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 - */ -public class ExpressionParserTest { - private static final ExpressionParser 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 org.unitConverter.math.ExpressionParser#parseExpression(java.lang.String)}. - */ - @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); - } - -} diff --git a/src/test/java/UnitDimensionTest.java b/src/test/java/UnitDimensionTest.java deleted file mode 100644 index 587cf4c..0000000 --- a/src/test/java/UnitDimensionTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright (C) 2018 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 . - */ -package test.java; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.unitConverter.dimension.StandardDimensions.AREA; -import static org.unitConverter.dimension.StandardDimensions.ENERGY; -import static org.unitConverter.dimension.StandardDimensions.LENGTH; -import static org.unitConverter.dimension.StandardDimensions.MASS; -import static org.unitConverter.dimension.StandardDimensions.MASS_DENSITY; -import static org.unitConverter.dimension.StandardDimensions.QUANTITY; -import static org.unitConverter.dimension.StandardDimensions.TIME; -import static org.unitConverter.dimension.StandardDimensions.VOLUME; - -import org.junit.Test; -import org.unitConverter.dimension.SIBaseDimension; -import org.unitConverter.dimension.UnitDimension; - -/** - * Tests for {@link UnitDimension}. This is NOT part of this program's public API. - * - * @author Adrien Hopkins - * @since 2018-12-12 - * @since v0.1.0 - */ -public class UnitDimensionTest { - /** - * Tests {@link UnitDimension#equals} - * - * @since 2018-12-12 - * @since v0.1.0 - */ - @Test - public void testEquals() { - assertEquals(LENGTH, LENGTH); - assertFalse(LENGTH.equals(QUANTITY)); - } - - /** - * Tests {@code UnitDimension}'s exponentiation - * - * @since 2019-01-15 - * @since v0.1.0 - */ - @Test - public void testExponents() { - assertEquals(1, LENGTH.getExponent(SIBaseDimension.LENGTH)); - assertEquals(3, VOLUME.getExponent(SIBaseDimension.LENGTH)); - } - - /** - * Tests {@code UnitDimension}'s multiplication and division. - * - * @since 2018-12-12 - * @since v0.1.0 - */ - @Test - public void testMultiplicationAndDivision() { - assertEquals(AREA, LENGTH.times(LENGTH)); - assertEquals(MASS_DENSITY, MASS.dividedBy(VOLUME)); - assertEquals(ENERGY, AREA.times(MASS).dividedBy(TIME).dividedBy(TIME)); - assertEquals(LENGTH, LENGTH.times(TIME).dividedBy(TIME)); - } -} diff --git a/src/test/java/UnitTest.java b/src/test/java/UnitTest.java deleted file mode 100644 index 7a3f29d..0000000 --- a/src/test/java/UnitTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright (C) 2018 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 . - */ -package test.java; - -import static org.junit.Assert.assertEquals; - -import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; - -import org.junit.Test; -import org.unitConverter.dimension.StandardDimensions; -import org.unitConverter.math.DecimalComparison; -import org.unitConverter.unit.BaseUnit; -import org.unitConverter.unit.LinearUnit; -import org.unitConverter.unit.SI; -import org.unitConverter.unit.SIPrefix; -import org.unitConverter.unit.Unit; - -/** - * Testing the various Unit classes. This is NOT part of this program's public API. - * - * @author Adrien Hopkins - * @since 2018-12-22 - * @since v0.1.0 - */ -public class UnitTest { - /** A random number generator */ - private static final Random rng = ThreadLocalRandom.current(); - - @Test - public void testAdditionAndSubtraction() { - final LinearUnit inch = SI.METRE.times(0.0254); - final LinearUnit foot = SI.METRE.times(0.3048); - - assertEquals(inch.plus(foot), SI.METRE.times(0.3302)); - assertEquals(foot.minus(inch), SI.METRE.times(0.2794)); - } - - @Test - public void testBaseUnitExclusives() { - // this test should have a compile error if I am doing something wrong - final BaseUnit metrePerSecondSquared = SI.METRE.dividedBy(SI.SECOND.toExponent(2)); - - assertEquals(metrePerSecondSquared, SI.SI.getBaseUnit(StandardDimensions.ACCELERATION)); - } - - @Test - public void testConversion() { - final BaseUnit metre = SI.METRE; - final Unit inch = metre.times(0.0254); - - assertEquals(1.9, inch.convertTo(metre, 75), 0.01); - - // try random stuff - for (int i = 0; i < 1000; i++) { - // initiate random values - final double conversionFactor = rng.nextDouble() * 1000000; - final double testValue = rng.nextDouble() * 1000000; - final double expected = testValue * conversionFactor; - - // test - final Unit unit = SI.METRE.times(conversionFactor); - final double actual = unit.convertToBase(testValue); - - assertEquals(actual, expected, expected * DecimalComparison.DOUBLE_EPSILON); - } - } - - @Test - public void testEquals() { - final BaseUnit metre = SI.METRE; - final Unit meter = SI.SI.getBaseUnit(StandardDimensions.LENGTH); - - assertEquals(metre, meter); - } - - @Test - public void testMultiplicationAndDivision() { - // test unit-times-unit multiplication - final LinearUnit generatedJoule = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); - final LinearUnit actualJoule = SI.SI.getBaseUnit(StandardDimensions.ENERGY); - - assertEquals(generatedJoule, actualJoule); - - // test multiplication by conversion factors - final LinearUnit kilometre = SI.METRE.times(1000); - final LinearUnit hour = SI.SECOND.times(3600); - final LinearUnit generatedKPH = kilometre.dividedBy(hour); - - final LinearUnit actualKPH = SI.SI.getBaseUnit(StandardDimensions.VELOCITY).dividedBy(3.6); - - assertEquals(generatedKPH, actualKPH); - } - - @Test - public void testPrefixes() { - final LinearUnit generatedKilometre = SI.METRE.withPrefix(SIPrefix.KILO); - final LinearUnit actualKilometre = SI.METRE.times(1000); - - assertEquals(generatedKilometre, actualKilometre); - } -} diff --git a/src/test/java/UnitsDatabaseTest.java b/src/test/java/UnitsDatabaseTest.java deleted file mode 100644 index 0d67c20..0000000 --- a/src/test/java/UnitsDatabaseTest.java +++ /dev/null @@ -1,313 +0,0 @@ -/** - * Copyright (C) 2019 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 . - */ -package test.java; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; - -import org.junit.Test; -import org.unitConverter.UnitsDatabase; -import org.unitConverter.unit.DefaultUnitPrefix; -import org.unitConverter.unit.LinearUnit; -import org.unitConverter.unit.SI; -import org.unitConverter.unit.Unit; -import org.unitConverter.unit.UnitPrefix; - -/** - * A test for the {@link UnitsDatabase} class. This is NOT part of this program's public API. - * - * @author Adrien Hopkins - * @since 2019-04-14 - * @since v0.2.0 - */ -public class UnitsDatabaseTest { - // some linear units and one nonlinear - private static final Unit U = SI.METRE; - private static final Unit V = SI.KILOGRAM; - private static final Unit W = SI.SECOND; - - // used for testing expressions - // J = U^2 * V / W^2 - private static final LinearUnit J = SI.KILOGRAM.times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); - private static final LinearUnit K = SI.KELVIN; - - private static final Unit NONLINEAR = Unit.fromConversionFunctions(SI.METRE, o -> o + 1, o -> o - 1); - - // make the prefix values prime so I can tell which multiplications were made - private static final UnitPrefix A = new DefaultUnitPrefix(2); - private static final UnitPrefix B = new DefaultUnitPrefix(3); - private static final UnitPrefix C = new DefaultUnitPrefix(5); - private static final UnitPrefix AB = new DefaultUnitPrefix(7); - private static final UnitPrefix BC = new DefaultUnitPrefix(11); - - /** - * Confirms that operations that shouldn't function for infinite databases throw an {@code IllegalStateException}. - * - * @since 2019-05-03 - */ - @Test - public void testInfiniteSetExceptions() { - // load units - final UnitsDatabase infiniteDatabase = new UnitsDatabase(); - - infiniteDatabase.addUnit("J", J); - infiniteDatabase.addUnit("K", K); - - infiniteDatabase.addPrefix("A", A); - infiniteDatabase.addPrefix("B", B); - infiniteDatabase.addPrefix("C", C); - - { - boolean exceptionThrown = false; - try { - infiniteDatabase.unitMap().entrySet().toArray(); - } catch (final IllegalStateException e) { - exceptionThrown = true; - // pass! - } finally { - if (!exceptionThrown) { - fail("No IllegalStateException thrown"); - } - } - } - - { - boolean exceptionThrown = false; - try { - infiniteDatabase.unitMap().keySet().toArray(); - } catch (final IllegalStateException e) { - exceptionThrown = true; - // pass! - } finally { - if (!exceptionThrown) { - fail("No IllegalStateException thrown"); - } - } - } - } - - /** - * Test that prefixes correctly apply to units. - * - * @since 2019-04-14 - * @since v0.2.0 - */ - @Test - public void testPrefixes() { - final UnitsDatabase database = new UnitsDatabase(); - - database.addUnit("U", U); - database.addUnit("V", V); - database.addUnit("W", W); - - database.addPrefix("A", A); - database.addPrefix("B", B); - database.addPrefix("C", C); - - // get the product - final Unit abcuNonlinear = database.getUnit("ABCU"); - assert abcuNonlinear instanceof LinearUnit; - - final LinearUnit abcu = (LinearUnit) abcuNonlinear; - assertEquals(A.getMultiplier() * B.getMultiplier() * C.getMultiplier(), abcu.getConversionFactor(), 1e-15); - } - - /** - * Tests the functionnalites of the prefixless unit map. - * - *

- * The map should be an auto-updating view of the units in the database. - *

- * - * @since 2019-04-14 - * @since v0.2.0 - */ - @Test - public void testPrefixlessUnitMap() { - final UnitsDatabase database = new UnitsDatabase(); - final Map prefixlessUnits = database.unitMapPrefixless(); - - database.addUnit("U", U); - database.addUnit("V", V); - database.addUnit("W", W); - - // this should work because the map should be an auto-updating view - assertTrue(prefixlessUnits.containsKey("U")); - assertFalse(prefixlessUnits.containsKey("Z")); - - assertTrue(prefixlessUnits.containsValue(U)); - assertFalse(prefixlessUnits.containsValue(NONLINEAR)); - } - - /** - * Tests that the database correctly stores and retrieves units, ignoring prefixes. - * - * @since 2019-04-14 - * @since v0.2.0 - */ - @Test - public void testPrefixlessUnits() { - final UnitsDatabase database = new UnitsDatabase(); - - database.addUnit("U", U); - database.addUnit("V", V); - database.addUnit("W", W); - - assertTrue(database.containsUnitName("U")); - assertFalse(database.containsUnitName("Z")); - - assertEquals(U, database.getUnit("U")); - assertEquals(null, database.getUnit("Z")); - } - - /** - * Test that unit expressions return the correct value. - * - * @since 2019-04-14 - * @since v0.2.0 - */ - @Test - public void testUnitExpressions() { - // load units - final UnitsDatabase database = new UnitsDatabase(); - - database.addUnit("U", U); - database.addUnit("V", V); - database.addUnit("W", W); - database.addUnit("fj", J.times(5)); - database.addUnit("ej", J.times(8)); - - database.addPrefix("A", A); - database.addPrefix("B", B); - database.addPrefix("C", C); - - // first test - test prefixes and operations - final Unit expected1 = J.withPrefix(A).withPrefix(B).withPrefix(C).withPrefix(C); - final Unit actual1 = database.getUnitFromExpression("ABV * CU^2 / W / W"); - - assertEquals(expected1, actual1); - - // second test - test addition and subtraction - final Unit expected2 = J.times(58); - final Unit actual2 = database.getUnitFromExpression("2 fj + 6 ej"); - - assertEquals(expected2, actual2); - } - - /** - * Tests both the unit name iterator and the name-unit entry iterator - * - * @since 2019-04-14 - * @since v0.2.0 - */ - @Test - public void testUnitIterator() { - // load units - final UnitsDatabase database = new UnitsDatabase(); - - database.addUnit("J", J); - database.addUnit("K", K); - - database.addPrefix("A", A); - database.addPrefix("B", B); - database.addPrefix("C", C); - - final int NUM_UNITS = database.unitMapPrefixless().size(); - final int NUM_PREFIXES = database.prefixMap().size(); - - final Iterator nameIterator = database.unitMap().keySet().iterator(); - final Iterator> entryIterator = database.unitMap().entrySet().iterator(); - - int expectedLength = 1; - int unitsWithThisLengthSoFar = 0; - - // loop 1000 times - for (int i = 0; i < 1000; i++) { - // expected length of next - if (unitsWithThisLengthSoFar >= NUM_UNITS * (int) Math.pow(NUM_PREFIXES, expectedLength - 1)) { - expectedLength++; - unitsWithThisLengthSoFar = 0; - } - - // test that stuff is valid - final String nextName = nameIterator.next(); - final Unit nextUnit = database.getUnit(nextName); - final Entry nextEntry = entryIterator.next(); - - assertEquals(expectedLength, nextName.length()); - assertEquals(nextName, nextEntry.getKey()); - assertEquals(nextUnit, nextEntry.getValue()); - - unitsWithThisLengthSoFar++; - } - - // test toString for consistency - final String entryIteratorString = entryIterator.toString(); - for (int i = 0; i < 3; i++) { - assertEquals(entryIteratorString, entryIterator.toString()); - } - - final String nameIteratorString = nameIterator.toString(); - for (int i = 0; i < 3; i++) { - assertEquals(nameIteratorString, nameIterator.toString()); - } - } - - /** - * Determine, given a unit name that could mean multiple things, which meaning is chosen. - *

- * For example, "ABCU" could mean "A-B-C-U", "AB-C-U", or "A-BC-U". In this case, "AB-C-U" is the correct choice. - *

- * - * @since 2019-04-14 - * @since v0.2.0 - */ - @Test - public void testUnitPrefixCombinations() { - // load units - final UnitsDatabase database = new UnitsDatabase(); - - database.addUnit("J", J); - - database.addPrefix("A", A); - database.addPrefix("B", B); - database.addPrefix("C", C); - database.addPrefix("AB", AB); - database.addPrefix("BC", BC); - - // test 1 - AB-C-J vs A-BC-J vs A-B-C-J - final Unit expected1 = J.withPrefix(AB).withPrefix(C); - final Unit actual1 = database.getUnit("ABCJ"); - - assertEquals(expected1, actual1); - - // test 2 - ABC-J vs AB-CJ vs AB-C-J - database.addUnit("CJ", J.times(13)); - database.addPrefix("ABC", new DefaultUnitPrefix(17)); - - final Unit expected2 = J.times(17); - final Unit actual2 = database.getUnit("ABCJ"); - - assertEquals(expected2, actual2); - } -} diff --git a/src/test/java/package-info.java b/src/test/java/package-info.java deleted file mode 100644 index 3da7fcb..0000000 --- a/src/test/java/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (C) 2019 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 . - */ -/** - * All of the Unit Converter tests. Everything in this package is NOT part of Unit Converter's public API. - * - * @author Adrien Hopkins - * @since 2019-03-16 - * @since v0.2.0 - */ -package test.java; \ No newline at end of file -- cgit v1.2.3