diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-10-07 16:21:00 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-10-07 16:21:00 -0500 |
commit | b4fd2b39e85e2a086e65555ad7c45244bec8ae37 (patch) | |
tree | a49542fe896f56b02d73a42b9c458d4df5d3f90b /src | |
parent | b59082c6b558705d4bd5effce2ae4b98c8a3ebe5 (diff) |
Added tests for getUnit and the prefixed unit map
Also fixed a bug where a prefixed unit map with units but no prefixes would
appear empty
Diffstat (limited to 'src')
3 files changed, 146 insertions, 8 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitDatabase.java b/src/main/java/sevenUnits/unit/UnitDatabase.java index 7c72570..d3c65b2 100644 --- a/src/main/java/sevenUnits/unit/UnitDatabase.java +++ b/src/main/java/sevenUnits/unit/UnitDatabase.java @@ -243,7 +243,8 @@ public final class UnitDatabase { return false; else { if (this.prefixNames.isEmpty()) - return this.unitNamePosition >= this.unitNames.size() - 1; + return this.prefixCoordinates.isEmpty() + && this.unitNamePosition < this.unitNames.size(); else return true; } @@ -557,7 +558,8 @@ public final class UnitDatabase { return false; else { if (this.prefixNames.isEmpty()) - return this.unitNamePosition >= this.unitNames.size() - 1; + return this.prefixCoordinates.isEmpty() + && this.unitNamePosition < this.unitNames.size(); else return true; } @@ -1038,7 +1040,7 @@ public final class UnitDatabase { @Override public String toString() { if (this.units.isEmpty() || this.prefixes.isEmpty()) - return super.toString(); + return new HashMap<>(this).toString(); else return String.format( "Infinite map of name-unit entries created from units %s and prefixes %s", @@ -1645,7 +1647,7 @@ public final class UnitDatabase { * @since 2019-03-22 * @since v0.2.0 */ - private LinearUnit getLinearUnit(final String name) { + LinearUnit getLinearUnit(final String name) { // see if I am using a function-unit like tempC(100) Objects.requireNonNull(name, "name may not be null"); if (name.contains("(") && name.contains(")")) { @@ -1680,7 +1682,7 @@ public final class UnitDatabase { * @return {@code LinearUnitValue} instance * @since 2020-08-04 */ - private LinearUnitValue getLinearUnitValue(final String name) { + LinearUnitValue getLinearUnitValue(final String name) { try { // try to parse it as a number - otherwise it is not a number! final BigDecimal number = new BigDecimal(name); diff --git a/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java b/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java index 2adb579..bee4dd1 100644 --- a/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java +++ b/src/main/java/sevenUnits/utils/ConditionalExistenceCollections.java @@ -283,7 +283,7 @@ public final class ConditionalExistenceCollections { @Override public Set<K> keySet() { - return conditionalExistenceSet(super.keySet(), + return conditionalExistenceSet(this.map.keySet(), k -> this.entryExistenceCondition.test(this.getEntry(k))); } diff --git a/src/test/java/sevenUnits/unit/UnitDatabaseTest.java b/src/test/java/sevenUnits/unit/UnitDatabaseTest.java index ed3b6b5..90c18e6 100644 --- a/src/test/java/sevenUnits/unit/UnitDatabaseTest.java +++ b/src/test/java/sevenUnits/unit/UnitDatabaseTest.java @@ -25,11 +25,13 @@ import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Set; import org.junit.jupiter.api.Test; @@ -47,21 +49,70 @@ import sevenUnits.utils.UncertainDouble; * @since v0.2.0 */ class UnitDatabaseTest { + private static final class SimpleEntry<K, V> implements Map.Entry<K, V> { + private final K key; + + private V value; + + /** + * + * @since 2021-10-07 + */ + public SimpleEntry(K key, V value) { + this.key = key; + this.value = value; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof Map.Entry<?, ?>)) + return false; + final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj; + return Objects.equals(this.key, other.getKey()) + && Objects.equals(this.value, other.getValue()); + } + + @Override + public K getKey() { + return this.key; + } + + @Override + public V getValue() { + return this.value; + } + + @Override + public int hashCode() { + return (this.key == null ? 0 : this.key.hashCode()) + ^ (this.value == null ? 0 : this.value.hashCode()); + } + + @Override + public V setValue(V value) { + final V oldValue = this.value; + this.value = value; + return oldValue; + } + } + // some linear units and one nonlinear private static final Unit U = Metric.METRE; private static final Unit V = Metric.KILOGRAM; - private static final Unit W = Metric.SECOND; + private static final Unit W = Metric.SECOND; // used for testing expressions // J = U^2 * V / W^2 private static final LinearUnit J = Metric.KILOGRAM .times(Metric.METRE.toExponent(2)) .dividedBy(Metric.SECOND.toExponent(2)); + private static final LinearUnit K = Metric.KELVIN; private static final Unit NONLINEAR = Unit.fromConversionFunctions( Metric.METRE.getBase(), o -> o + 1, o -> o - 1); - // make the prefix values prime so I can tell which multiplications were made private static final UnitPrefix A = UnitPrefix.valueOf(2) .withName(NameSymbol.ofName("A")); @@ -70,9 +121,24 @@ class UnitDatabaseTest { private static final UnitPrefix C = UnitPrefix.valueOf(5) .withName(NameSymbol.ofName("C")); private static final UnitPrefix AB = UnitPrefix.valueOf(7); + private static final UnitPrefix BC = UnitPrefix.valueOf(11); /** + * Gets a map entry. + * + * @param <K> type of key + * @param <V> type of value + * @param key key in entry + * @param value value in entry + * @return entry + * @since 2021-10-07 + */ + private static <K, V> Map.Entry<K, V> entry(K key, V value) { + return new SimpleEntry<>(key, value); + } + + /** * Loads the dimensionfile at src/test/resources/[path] to the database * {@code loadTo}. * @@ -132,6 +198,39 @@ class UnitDatabaseTest { } /** + * Test for {@link UnitDatabase#getUnit}, {@link UnitDatabase#getLinearUnit} + * and {@link UnitDatabase#getLinearUnitValue}. + * + * @since 2021-10-07 + */ + @Test + public void testGetUnit() { + final UnitDatabase database = new UnitDatabase(); + + database.addUnit("m", Metric.METRE); + database.addUnit("meter", Metric.METRE); + database.addUnit("metre", Metric.METRE); + database.addUnit("badname", Metric.METRE); + database.addUnit("K", Metric.KELVIN); + database.addUnit("degC", Metric.CELSIUS); + + // ensure getUnit returns units, regardless of whether the name is one of + // the unit's names + assertEquals(Metric.METRE, database.getUnit("m")); + assertEquals(Metric.METRE, database.getUnit("metre")); + assertEquals(Metric.METRE, database.getUnit("meter")); + assertEquals(Metric.METRE, database.getUnit("badname")); + assertThrows(NoSuchElementException.class, + () -> database.getUnit("blabla")); + + assertEquals(Metric.KELVIN, database.getLinearUnit("K")); + assertThrows(IllegalArgumentException.class, + () -> database.getLinearUnit("degC")); + assertEquals(Metric.KELVIN.times(373.15), + database.getLinearUnit("degC(100)")); + } + + /** * Confirms that operations that shouldn't function for infinite databases * throw an {@code IllegalStateException}. * @@ -275,6 +374,43 @@ class UnitDatabaseTest { } /** + * Tests the iterator of the prefixless unit map. These tests are simple, as + * the unit map iterator is simple. + * + * @since 2021-10-07 + */ + @Test + public void testPrefixedUnitMapIterator() { + final UnitDatabase database1 = new UnitDatabase(); + + database1.addUnit("U", U); + database1.addUnit("V", V); + database1.addUnit("W", W); + + final Map<String, Unit> map1 = database1.unitMap(); + final Iterator<String> keyIterator1 = map1.keySet().iterator(); + final Iterator<Map.Entry<String, Unit>> entryIterator1 = map1.entrySet() + .iterator(); + + final Set<String> expectedKeys = Set.of("U", "V", "W"); + final Set<String> actualKeys = new HashSet<>(); + while (keyIterator1.hasNext()) { + actualKeys.add(keyIterator1.next()); + } + assertEquals(expectedKeys, actualKeys); + assertEquals(expectedKeys, map1.keySet()); + + final Set<Map.Entry<String, Unit>> expectedEntries = Set.of(entry("U", U), + entry("V", V), entry("W", W)); + final Set<Map.Entry<String, Unit>> actualEntries = new HashSet<>(); + while (entryIterator1.hasNext()) { + actualEntries.add(entryIterator1.next()); + } + assertEquals(expectedEntries, actualEntries); + assertEquals(expectedEntries, map1.entrySet()); + } + + /** * Test that prefixes correctly apply to units. * * @since 2019-04-14 |