From 26291e672b0e683edc9d57710a9a9d96ca199c45 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 24 Mar 2024 13:14:11 -0500 Subject: Format source code & set explicit UTF-8 --- src/test/java/sevenUnits/unit/MultiUnitTest.java | 26 +-- .../java/sevenUnits/unit/UnitDatabaseTest.java | 196 ++++++++++----------- src/test/java/sevenUnits/unit/UnitTest.java | 72 ++++---- .../utils/ConditionalExistenceCollectionsTest.java | 26 +-- .../sevenUnits/utils/ExpressionParserTest.java | 19 +- .../java/sevenUnits/utils/ObjectProductTest.java | 3 +- .../java/sevenUnits/utils/SemanticVersionTest.java | 56 +++--- .../java/sevenUnits/utils/UncertainDoubleTest.java | 28 +-- .../java/sevenUnitsGUI/PrefixRepetitionTest.java | 10 +- src/test/java/sevenUnitsGUI/PrefixSearchTest.java | 22 +-- src/test/java/sevenUnitsGUI/PresenterTest.java | 94 +++++----- src/test/java/sevenUnitsGUI/RoundingTest.java | 36 ++-- src/test/java/sevenUnitsGUI/TabbedViewTest.java | 22 +-- 13 files changed, 305 insertions(+), 305 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/sevenUnits/unit/MultiUnitTest.java b/src/test/java/sevenUnits/unit/MultiUnitTest.java index 30f2941..949a1f1 100644 --- a/src/test/java/sevenUnits/unit/MultiUnitTest.java +++ b/src/test/java/sevenUnits/unit/MultiUnitTest.java @@ -31,7 +31,7 @@ import org.junit.jupiter.api.Test; * @since 2020-10-03 */ class MultiUnitTest { - + /** * Ensures that the {@code MultiUnit} can convert properly. */ @@ -40,24 +40,24 @@ class MultiUnitTest { final Random rng = ThreadLocalRandom.current(); final MultiUnit footInch = MultiUnit.of(BritishImperial.Length.FOOT, BritishImperial.Length.INCH); - + assertEquals(1702.0, footInch.convertTo(Metric.METRE.withPrefix(Metric.MILLI), Arrays.asList(5.0, 7.0)), 1.0); - + for (int i = 0; i < 1000; i++) { final double feet = rng.nextInt(1000); final double inches = rng.nextDouble() * 12; final double millimetres = feet * 304.8 + inches * 25.4; - + final List feetAndInches = Metric.METRE .withPrefix(Metric.MILLI).convertTo(footInch, millimetres); assertEquals(feet, feetAndInches.get(0), 1e-10); assertEquals(inches, feetAndInches.get(1), 1e-10); } } - + /** * Test method for {@link sevenUnits.unit.MultiUnit#convertFromBase(double)}. */ @@ -66,24 +66,24 @@ class MultiUnitTest { final Random rng = ThreadLocalRandom.current(); final MultiUnit footInch = MultiUnit.of(BritishImperial.Length.FOOT, BritishImperial.Length.INCH); - + // 1.7 m =~ 5' + 7" final List values = footInch.convertFromBase(1.7018); - + assertEquals(5, values.get(0)); assertEquals(7, values.get(1), 1e-12); - + for (int i = 0; i < 1000; i++) { final double feet = rng.nextInt(1000); final double inches = rng.nextDouble() * 12; final double metres = feet * 0.3048 + inches * 0.0254; - + final List feetAndInches = footInch.convertFromBase(metres); assertEquals(feet, feetAndInches.get(0), 1e-10); assertEquals(inches, feetAndInches.get(1), 1e-10); } } - + /** * Test method for * {@link sevenUnits.unit.MultiUnit#convertToBase(java.util.List)}. @@ -93,16 +93,16 @@ class MultiUnitTest { final Random rng = ThreadLocalRandom.current(); final MultiUnit footInch = MultiUnit.of(BritishImperial.Length.FOOT, BritishImperial.Length.INCH); - + // 1.7 m =~ 5' + 7" assertEquals(1.7018, footInch.convertToBase(Arrays.asList(5.0, 7.0)), 1e-12); - + for (int i = 0; i < 1000; i++) { final double feet = rng.nextInt(1000); final double inches = rng.nextDouble() * 12; final double metres = feet * 0.3048 + inches * 0.0254; - + assertEquals(metres, footInch.convertToBase(Arrays.asList(feet, inches)), 1e-12); } diff --git a/src/test/java/sevenUnits/unit/UnitDatabaseTest.java b/src/test/java/sevenUnits/unit/UnitDatabaseTest.java index 4be33dd..9d650f0 100644 --- a/src/test/java/sevenUnits/unit/UnitDatabaseTest.java +++ b/src/test/java/sevenUnits/unit/UnitDatabaseTest.java @@ -53,9 +53,9 @@ import sevenUnits.utils.UncertainDouble; class UnitDatabaseTest { private static final class SimpleEntry implements Map.Entry { private final K key; - + private V value; - + /** * * @since 2021-10-07 @@ -64,7 +64,7 @@ class UnitDatabaseTest { this.key = key; this.value = value; } - + @Override public boolean equals(Object obj) { if (this == obj) @@ -75,23 +75,23 @@ class UnitDatabaseTest { 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; @@ -99,20 +99,20 @@ class UnitDatabaseTest { 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; // 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 @@ -123,9 +123,9 @@ 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. * @@ -139,7 +139,7 @@ class UnitDatabaseTest { private static Map.Entry entry(K key, V value) { return new SimpleEntry<>(key, value); } - + /** * Loads the dimensionfile at src/test/resources/[path] to the database * {@code loadTo}. @@ -156,7 +156,7 @@ class UnitDatabaseTest { fail(e.getClass() + " occurred upon loading file \"" + path + "\"."); } } - + /** * Loads the unitfile at src/test/resources/[path] to the database * {@code loadTo}. @@ -173,7 +173,7 @@ class UnitDatabaseTest { fail(e.getClass() + " occurred upon loading file \"" + path + "\"."); } } - + /** * A test for the {@link UnitDatabase#evaluateUnitExpression(String)} * function. Simple because the expression parser has its own test. @@ -183,26 +183,26 @@ class UnitDatabaseTest { @Test public void testEvaluateExpression() { final UnitDatabase database = new UnitDatabase(); - + database.addUnit("J", J); database.addUnit("K", K); - + database.addPrefix("A", A); database.addPrefix("B", B); database.addPrefix("C", C); - + final LinearUnitValue expected = LinearUnitValue.of(J, UncertainDouble.of(12, Math.sqrt(14.625))); // note: units are exact, each number has an uncertainty of 1 final LinearUnitValue actual = database .evaluateUnitExpression("J + (2 * 3) J + (20 / 4) J"); assertEquals(expected, actual); - + // check that negation works properly assertEquals(2, database.evaluateUnitExpression("J - -1 * J").getValueExact()); } - + /** * Test for {@link UnitDatabase#getUnit}, {@link UnitDatabase#getLinearUnit} * and {@link UnitDatabase#getLinearUnitValue}. @@ -212,14 +212,14 @@ class UnitDatabaseTest { @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")); @@ -228,14 +228,14 @@ class UnitDatabaseTest { 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}. @@ -247,21 +247,21 @@ class UnitDatabaseTest { public void testInfiniteSetExceptions() { // load units final UnitDatabase infiniteDatabase = new UnitDatabase(); - + infiniteDatabase.addUnit("J", J); infiniteDatabase.addUnit("K", K); - + infiniteDatabase.addPrefix("A", A); infiniteDatabase.addPrefix("B", B); infiniteDatabase.addPrefix("C", C); - + final Set> entrySet = infiniteDatabase.unitMap() .entrySet(); final Set keySet = infiniteDatabase.unitMap().keySet(); assertThrows(IllegalStateException.class, () -> entrySet.toArray()); assertThrows(IllegalStateException.class, () -> keySet.toArray()); } - + /** * A bunch of tests for invalid dimension files * @@ -282,7 +282,7 @@ class UnitDatabaseTest { assertTrue(e instanceof IllegalArgumentException || e instanceof NoSuchElementException); } - + /** * A bunch of tests for invalid unit files * @@ -300,7 +300,7 @@ class UnitDatabaseTest { assertTrue(e instanceof IllegalArgumentException || e instanceof NoSuchElementException); } - + /** * Tests loading a valid dimension-file with some derived dimensions. * @@ -312,13 +312,13 @@ class UnitDatabaseTest { database.addDimension("LENGTH", Metric.Dimensions.LENGTH); database.addDimension("MASS", Metric.Dimensions.MASS); database.addDimension("TIME", Metric.Dimensions.TIME); - + loadDimensionFile(database, "/test-dimensionfile-valid1.txt"); assertEquals(Metric.Dimensions.ENERGY, database.getDimension("ENERGY")); assertEquals(Metric.Dimensions.POWER, database.getDimension("POWER")); - + } - + /** * Tests loading a valid unitfile with some prefixes and no units. * @@ -327,13 +327,13 @@ class UnitDatabaseTest { @Test public void testLoadingValidPrefixes() { final UnitDatabase database = new UnitDatabase(); - + loadUnitsFile(database, "/test-unitsfile-valid2.txt"); assertEquals(7, database.getPrefix("A").getMultiplier()); assertEquals(11, database.getPrefix("B").getMultiplier()); assertEquals(13, database.getPrefix("C").getMultiplier()); } - + /** * Tests loading a valid unitfile with some units and preloaded prefixes * @@ -342,43 +342,43 @@ class UnitDatabaseTest { @Test public void testLoadingValidUnits() { final UnitDatabase database = new UnitDatabase(); - + 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); - + loadUnitsFile(database, "/test-unitsfile-valid1.txt"); - + final Unit expected1 = ((LinearUnit) U).withPrefix(A).withPrefix(B) .withPrefix(C); final Unit actual1 = database.getUnit("test1"); assertEquals(expected1, actual1); - + final Unit expected2 = ((LinearUnit) W).withPrefix(B) .times(((LinearUnit) V).withPrefix(C)); final Unit actual2 = database.getUnit("test2"); assertEquals(expected2, actual2); - + final Unit expected3 = ((LinearUnit) U) .times(A.getMultiplier() + C.getMultiplier() - B.getMultiplier()); final Unit actual3 = database.getUnit("test3"); assertEquals(expected3, actual3); - + final UnitValue expected4 = UnitValue.of(U, 1); final UnitValue actual4 = database .evaluateUnitExpression("-5 * U + -3 * U + 12 * U - 3 * U") .asUnitValue(); assertEquals(expected4, actual4); - + assertTrue(System.err.toString().length() > 0); } - + /** * Tests the iterator of the prefixless unit map. These tests are simple, as * the unit map iterator is simple. @@ -388,16 +388,16 @@ class UnitDatabaseTest { @Test public void testPrefixedUnitMapIterator() { final UnitDatabase database1 = new UnitDatabase(); - + database1.addUnit("U", U); database1.addUnit("V", V); database1.addUnit("W", W); - + final Map map1 = database1.unitMap(); final Iterator keyIterator1 = map1.keySet().iterator(); final Iterator> entryIterator1 = map1.entrySet() .iterator(); - + final Set expectedKeys = Set.of("U", "V", "W"); final Set actualKeys = new HashSet<>(); while (keyIterator1.hasNext()) { @@ -405,7 +405,7 @@ class UnitDatabaseTest { } assertEquals(expectedKeys, actualKeys); assertEquals(expectedKeys, map1.keySet()); - + final Set> expectedEntries = Set.of(entry("U", U), entry("V", V), entry("W", W)); final Set> actualEntries = new HashSet<>(); @@ -415,7 +415,7 @@ class UnitDatabaseTest { assertEquals(expectedEntries, actualEntries); assertEquals(expectedEntries, map1.entrySet()); } - + /** * Test that prefixes correctly apply to units. * @@ -425,28 +425,28 @@ class UnitDatabaseTest { @Test public void testPrefixes() { final UnitDatabase database = new UnitDatabase(); - + database.addUnit("U", U); database.addUnit("V", V); database.addUnit("W", W); - + database.addPrefix("A", A); database.addPrefix("B", B); database.addPrefix("C", C); - + // test the getPrefixesFromName method final List expected = Arrays.asList(C, B, A); assertEquals(expected, database.getPrefixesFromName("ABCU")); - + // 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. * @@ -462,19 +462,19 @@ class UnitDatabaseTest { final UnitDatabase database = new UnitDatabase(); final Map prefixlessUnits = database .unitMapPrefixless(true); - + 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. @@ -485,18 +485,18 @@ class UnitDatabaseTest { @Test public void testPrefixlessUnits() { final UnitDatabase database = new UnitDatabase(); - + 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")); assertThrows(NoSuchElementException.class, () -> database.getUnit("Z")); } - + @Test public void testRemovableDuplicates() { final Map unitMap = new HashMap<>(); @@ -504,7 +504,7 @@ class UnitDatabaseTest { unitMap.put("metre", Metric.METRE); unitMap.put("m", Metric.METRE); unitMap.put("second", Metric.SECOND); - + assertTrue(UnitDatabase.isRemovableDuplicate(unitMap, entry("m", Metric.METRE))); assertTrue(UnitDatabase.isRemovableDuplicate(unitMap, @@ -514,28 +514,28 @@ class UnitDatabaseTest { assertFalse(UnitDatabase.isRemovableDuplicate(unitMap, entry("second", Metric.SECOND))); } - + @Test public void testToString() { final UnitDatabase database = new UnitDatabase(); - + database.addUnit("J", J); database.addUnit("K", J); - + database.addPrefix("A", A); database.addPrefix("B", B); database.addPrefix("C", C); - + if ("Unit Database with 1 units, 3 unit prefixes and 0 dimensions" .equals(database.toString())) { fail("Database counts by number of units, not number of unit names."); } - + assertEquals( "Unit Database with 2 units, 3 unit prefixes and 0 dimensions", database.toString()); } - + /** * Test that unit expressions return the correct value. * @@ -546,37 +546,37 @@ class UnitDatabaseTest { public void testUnitExpressions() { // load units final UnitDatabase database = new UnitDatabase(); - + 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); - + // test incorrect expressions assertThrows(IllegalArgumentException.class, () -> database.getUnitFromExpression("U + V")); assertThrows(IllegalArgumentException.class, () -> database.getUnitFromExpression("U - V")); } - + /** * Tests both the unit name iterator and the name-unit entry iterator * @@ -587,25 +587,25 @@ class UnitDatabaseTest { public void testUnitIterator() { // load units final UnitDatabase database = new UnitDatabase(); - + 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(true).size(); final int NUM_PREFIXES = database.prefixMap(true).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 @@ -614,31 +614,31 @@ class UnitDatabaseTest { 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. @@ -654,28 +654,28 @@ class UnitDatabaseTest { public void testUnitPrefixCombinations() { // load units final UnitDatabase database = new UnitDatabase(); - + 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", UnitPrefix.valueOf(17)); - + final Unit expected2 = J.times(17); final Unit actual2 = database.getUnit("ABCJ"); - + assertEquals(expected2, actual2); } } diff --git a/src/test/java/sevenUnits/unit/UnitTest.java b/src/test/java/sevenUnits/unit/UnitTest.java index d3699ca..c93043b 100644 --- a/src/test/java/sevenUnits/unit/UnitTest.java +++ b/src/test/java/sevenUnits/unit/UnitTest.java @@ -42,17 +42,17 @@ import sevenUnits.utils.UncertainDouble; class UnitTest { /** A random number generator */ private static final Random rng = ThreadLocalRandom.current(); - + @Test public void testAdditionAndSubtraction() { final LinearUnit inch = Metric.METRE.times(0.0254) .withName(NameSymbol.of("inch", "in")); final LinearUnit foot = Metric.METRE.times(0.3048) .withName(NameSymbol.of("foot", "ft")); - + assertEquals(inch.plus(foot), Metric.METRE.times(0.3302)); assertEquals(foot.minus(inch), Metric.METRE.times(0.2794)); - + // test with LinearUnitValue final LinearUnitValue value1 = LinearUnitValue.getExact(Metric.METRE, 15); final LinearUnitValue value2 = LinearUnitValue.getExact(foot, 120); @@ -60,70 +60,70 @@ class UnitTest { 0.5); final LinearUnitValue value4 = LinearUnitValue.getExact(Metric.KILOGRAM, 60); - + // make sure addition is done correctly assertEquals(51.576, value1.plus(value2).getValueExact(), 0.001); assertEquals(15.5, value1.plus(value3).getValueExact()); assertEquals(52.076, value1.plus(value2).plus(value3).getValueExact(), 0.001); - + // make sure addition uses the correct unit, and is still associative // (ignoring floating-point rounding errors) assertEquals(Metric.METRE, value1.plus(value2).getUnit()); assertEquals(Metric.METRE, value1.plus(value2).plus(value3).getUnit()); assertEquals(foot, value2.plus(value1).getUnit()); assertTrue(value1.plus(value2).equals(value2.plus(value1), true)); - + // make sure errors happen when they should assertThrows(IllegalArgumentException.class, () -> value1.plus(value4)); assertThrows(IllegalArgumentException.class, () -> value1.minus(value4)); } - + @Test public void testConversion() { final LinearUnit metre = Metric.METRE; final Unit inch = metre.times(0.0254); - + final UnitValue value = UnitValue.of(inch, 75); - + assertEquals(1.9, inch.convertTo(metre, 75), 0.01); assertEquals(1.9, value.convertTo(metre).getValue(), 0.01); - + // try random stuff for (int i = 0; i < 1000; i++) { // initiate random values final double conversionFactor = UnitTest.rng.nextDouble() * 1000000; final double testValue = UnitTest.rng.nextDouble() * 1000000; final double expected = testValue * conversionFactor; - + // test final Unit unit = Metric.METRE.times(conversionFactor); final double actual = unit.convertToBase(testValue); - + assertEquals(actual, expected, expected * DecimalComparison.DOUBLE_EPSILON); } } - + @Test public void testEquals() { final LinearUnit metre = Metric.METRE; final Unit meter = Metric.BaseUnits.METRE.asLinearUnit(); - + assertEquals(metre, meter); } - + @Test public void testIsMetric() { final Unit metre = Metric.METRE; final Unit megasecond = Metric.SECOND.withPrefix(Metric.MEGA); final Unit hour = Metric.HOUR; - + assertTrue(metre.isMetric()); assertTrue(megasecond.isMetric()); assertFalse(hour.isMetric()); } - + @Test public void testMultiplicationAndDivision() { // test unit-times-unit multiplication @@ -131,29 +131,29 @@ class UnitTest { .times(Metric.METRE.toExponent(2)) .dividedBy(Metric.SECOND.toExponent(2)); final LinearUnit actualJoule = Metric.JOULE; - + assertEquals(generatedJoule, actualJoule); - + // test multiplication by conversion factors final LinearUnit kilometre = Metric.METRE.times(1000); final LinearUnit hour = Metric.SECOND.times(3600); final LinearUnit generatedKPH = kilometre.dividedBy(hour); - + final LinearUnit actualKPH = Metric.METRE.dividedBy(Metric.SECOND) .dividedBy(3.6); - + assertEquals(generatedKPH, actualKPH); } - + @Test public void testPrefixes() { final LinearUnit generatedKilometre = Metric.METRE .withPrefix(Metric.KILO); final LinearUnit actualKilometre = Metric.METRE.times(1000); - + assertEquals(generatedKilometre, actualKilometre); } - + /** * Tests converting an uncertain LinearUnitValue to a string. * @@ -163,13 +163,13 @@ class UnitTest { public void testValueToString1() { final LinearUnitValue value = LinearUnitValue.of(Metric.METRE, UncertainDouble.of(10, 0.24)); - - assertEquals("(10.0 ± 0.2) m", value.toString()); - assertEquals("(10.0 ± 0.2) m", + + assertEquals("(10.0 � 0.2) m", value.toString()); + assertEquals("(10.0 � 0.2) m", value.toString(true, RoundingMode.HALF_EVEN)); assertEquals("10.0 m", value.toString(false, RoundingMode.HALF_EVEN)); } - + /** * Tests converting a certain LinearUnitValue to a string. * @@ -179,13 +179,13 @@ class UnitTest { public void testValueToString2() { final LinearUnitValue value = LinearUnitValue.of(Metric.METRE, UncertainDouble.of(10, 0)); - + assertEquals("10.0 m", value.toString()); - assertEquals("(10.0 ± 0.0) m", + assertEquals("(10.0 � 0.0) m", value.toString(true, RoundingMode.HALF_EVEN)); assertEquals("10.0 m", value.toString(false, RoundingMode.HALF_EVEN)); } - + /** * Tests converting an unnamed LinearUnitValue to a string. * @@ -196,11 +196,11 @@ class UnitTest { final LinearUnitValue value = LinearUnitValue.of( Metric.METRE.withName(NameSymbol.EMPTY), UncertainDouble.of(10, 0.24)); - + assertEquals("10.0 unnamed unit (= 10.0 m)", value.toString(false, RoundingMode.HALF_EVEN)); } - + /** * Tests converting a named UnitValue to a string. * @@ -209,10 +209,10 @@ class UnitTest { @Test public void testValueToString4() { final UnitValue value = UnitValue.of(BritishImperial.FAHRENHEIT, 80); - + assertEquals("80.0 \u00B0F", value.toString()); } - + /** * Tests converting an unnamed UnitValue to a string. * @@ -222,7 +222,7 @@ class UnitTest { public void testValueToString5() { final UnitValue value = UnitValue .of(USCustomary.FAHRENHEIT.withName(NameSymbol.EMPTY), 50); - + assertEquals("50.0 unnamed unit (= 283.15 K)", value.toString()); } } diff --git a/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java b/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java index 6b5f9cf..868385b 100644 --- a/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java +++ b/src/test/java/sevenUnits/utils/ConditionalExistenceCollectionsTest.java @@ -43,7 +43,7 @@ import sevenUnits.utils.ConditionalExistenceCollections.ConditionalExistenceIter * @since 2019-10-16 */ class ConditionalExistenceCollectionsTest { - + /** * The returned iterator ignores elements that don't start with "a". * @@ -57,7 +57,7 @@ class ConditionalExistenceCollectionsTest { .conditionalExistenceIterator(it, s -> s.startsWith("a")); return cit; } - + /** * The returned map ignores mappings where the value is zero. * @@ -75,7 +75,7 @@ class ConditionalExistenceCollectionsTest { e -> !Integer.valueOf(0).equals(e.getValue())); return conditionalMap; } - + /** * Test method for the ConditionalExistenceMap's containsKey method. */ @@ -87,7 +87,7 @@ class ConditionalExistenceCollectionsTest { assertFalse(map.containsKey("five")); assertFalse(map.containsKey("zero")); } - + /** * Test method for the ConditionalExistenceMap's containsValue method. */ @@ -99,7 +99,7 @@ class ConditionalExistenceCollectionsTest { assertFalse(map.containsValue(5)); assertFalse(map.containsValue(0)); } - + /** * Test method for the ConditionalExistenceMap's entrySet method. */ @@ -110,7 +110,7 @@ class ConditionalExistenceCollectionsTest { assertTrue(e.getValue() != 0); } } - + /** * Test method for the ConditionalExistenceMap's get method. */ @@ -122,7 +122,7 @@ class ConditionalExistenceCollectionsTest { assertEquals(null, map.get("five")); assertEquals(null, map.get("zero")); } - + /** * Test method for the ConditionalExistenceCollection's iterator. */ @@ -130,23 +130,23 @@ class ConditionalExistenceCollectionsTest { void testIterator() { final ConditionalExistenceIterator testIterator = this .getTestIterator(); - + assertTrue(testIterator.hasNext); assertTrue(testIterator.hasNext()); assertEquals("aa", testIterator.nextElement); assertEquals("aa", testIterator.next()); - + assertTrue(testIterator.hasNext); assertTrue(testIterator.hasNext()); assertEquals("ab", testIterator.nextElement); assertEquals("ab", testIterator.next()); - + assertFalse(testIterator.hasNext); assertFalse(testIterator.hasNext()); assertEquals(null, testIterator.nextElement); assertThrows(NoSuchElementException.class, testIterator::next); } - + /** * Test method for the ConditionalExistenceMap's keySet operation. */ @@ -155,7 +155,7 @@ class ConditionalExistenceCollectionsTest { final Map map = this.getTestMap(); assertFalse(map.keySet().contains("zero")); } - + /** * Test method for the ConditionalExistenceMap's values operation. */ @@ -164,5 +164,5 @@ class ConditionalExistenceCollectionsTest { final Map map = this.getTestMap(); assertFalse(map.values().contains(0)); } - + } diff --git a/src/test/java/sevenUnits/utils/ExpressionParserTest.java b/src/test/java/sevenUnits/utils/ExpressionParserTest.java index a954b12..3a95285 100644 --- a/src/test/java/sevenUnits/utils/ExpressionParserTest.java +++ b/src/test/java/sevenUnits/utils/ExpressionParserTest.java @@ -38,12 +38,11 @@ import org.junit.jupiter.params.provider.MethodSource; 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(); - + .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(); + /** * The expressions used in the expression parsing tests */ @@ -51,15 +50,15 @@ class ExpressionParserTest { // 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 @@ -69,7 +68,7 @@ class ExpressionParserTest { 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)}. diff --git a/src/test/java/sevenUnits/utils/ObjectProductTest.java b/src/test/java/sevenUnits/utils/ObjectProductTest.java index 15ff277..8c6b353 100644 --- a/src/test/java/sevenUnits/utils/ObjectProductTest.java +++ b/src/test/java/sevenUnits/utils/ObjectProductTest.java @@ -32,7 +32,8 @@ import org.junit.jupiter.api.Test; import sevenUnits.unit.Metric; /** - * Tests for {@link ObjectProduct} using BaseDimension as a test object. This is NOT part of this program's public API. + * Tests for {@link ObjectProduct} using BaseDimension as a test object. This is + * NOT part of this program's public API. * * @author Adrien Hopkins * @since 2018-12-12 diff --git a/src/test/java/sevenUnits/utils/SemanticVersionTest.java b/src/test/java/sevenUnits/utils/SemanticVersionTest.java index 877b258..1e59ae3 100644 --- a/src/test/java/sevenUnits/utils/SemanticVersionTest.java +++ b/src/test/java/sevenUnits/utils/SemanticVersionTest.java @@ -48,20 +48,20 @@ public final class SemanticVersionTest { "1.0.0 not compatible with 1.0.5"); assertTrue(stableVersion(1, 3, 1).compatibleWith(stableVersion(1, 4, 0)), "1.3.1 not compatible with 1.4.0"); - + // 0.y.z should not be compatible with any other version assertFalse(stableVersion(0, 4, 0).compatibleWith(stableVersion(0, 4, 1)), "0.4.0 compatible with 0.4.1 (0.y.z versions should be treated as unstable/incompatbile)"); - + // upgrading major version should = incompatible assertFalse(stableVersion(1, 0, 0).compatibleWith(stableVersion(2, 0, 0)), "1.0.0 compatible with 2.0.0"); - + // dowgrade should = incompatible assertFalse(stableVersion(1, 1, 0).compatibleWith(stableVersion(1, 0, 0)), "1.1.0 compatible with 1.0.0"); } - + /** * Tests {@link SemanticVersionNumber#toString} for complex version numbers * @@ -79,7 +79,7 @@ public final class SemanticVersionTest { .preRelease("x-y-z", "--").build(); assertEquals("1.0.0-x-y-z.--", v3.toString()); } - + /** * Tests that complex version can be created and their parts read * @@ -94,7 +94,7 @@ public final class SemanticVersionTest { assertEquals(3, v1.patchVersion()); assertEquals(List.of("1", "2", "3"), v1.preReleaseIdentifiers()); assertEquals(List.of(), v1.buildMetadata()); - + final SemanticVersionNumber v2 = builder(4, 5, 6).preRelease("abc", 123) .buildMetadata("2022-02-19").build(); assertEquals(4, v2.majorVersion()); @@ -102,7 +102,7 @@ public final class SemanticVersionTest { assertEquals(6, v2.patchVersion()); assertEquals(List.of("abc", "123"), v2.preReleaseIdentifiers()); assertEquals(List.of("2022-02-19"), v2.buildMetadata()); - + final SemanticVersionNumber v3 = builder(1, 0, 0) .preRelease("x-y-z", "--").build(); assertEquals(1, v3.majorVersion()); @@ -111,7 +111,7 @@ public final class SemanticVersionTest { assertEquals(List.of("x-y-z", "--"), v3.preReleaseIdentifiers()); assertEquals(List.of(), v3.buildMetadata()); } - + /** * Test that semantic version strings can be parsed correctly * @@ -132,7 +132,7 @@ public final class SemanticVersionTest { "1.0.0+abc is treated as invalid"); assertTrue(isValidVersionString("1.0.0-abc+def"), "1.0.0-abc+def is treated as invalid"); - + // test that invalid versions don't match assertFalse(isValidVersionString("1.0"), "1.0 is treated as valid (patch should be required)"); @@ -142,7 +142,7 @@ public final class SemanticVersionTest { "1.0.0- is treated as valid (pre-release must not be empty)"); assertFalse(isValidVersionString("1.0.0+"), "1.0.0+ is treated as valid (build metadata must not be empty)"); - + // test that versions can be parsed assertEquals(stableVersion(1, 0, 0), fromString("1.0.0"), "Could not parse 1.0.0"); @@ -152,7 +152,7 @@ public final class SemanticVersionTest { fromString("1.2.3-abc.56.def+2022abc99"), "Could not parse 1.2.3-abc.56.def+2022abc99"); } - + /** * Ensures it is impossible to create invalid version numbers */ @@ -168,7 +168,7 @@ public final class SemanticVersionTest { assertThrows(IllegalArgumentException.class, () -> stableVersion(-3, 0, 7), "Negative major version number tolerated by stableVersion"); - + // preRelease() assertThrows(IllegalArgumentException.class, () -> preRelease(1, 0, -1, "test", 2), @@ -190,7 +190,7 @@ public final class SemanticVersionTest { assertThrows(IllegalArgumentException.class, () -> preRelease(1, 0, 0, "abc+cde", 1), "Invalid string tolerated by preRelease"); - + // builder() assertThrows(IllegalArgumentException.class, () -> builder(1, 0, -1), "Negative patch tolerated by builder"); @@ -198,7 +198,7 @@ public final class SemanticVersionTest { "Negative minor version number tolerated by builder"); assertThrows(IllegalArgumentException.class, () -> builder(-3, 0, 7), "Negative major version number tolerated by builder"); - + final SemanticVersionNumber.Builder testBuilder = builder(1, 2, 3); // note: builder.buildMetadata(null) doesn't even compile lol // builder.buildMetadata @@ -220,7 +220,7 @@ public final class SemanticVersionTest { assertThrows(IllegalArgumentException.class, () -> testBuilder.buildMetadata(List.of("")), "Invalid string tolerated by builder.buildMetadata(List)"); - + // builder.preRelease assertThrows(NullPointerException.class, () -> testBuilder.preRelease(null, "abc"), @@ -240,7 +240,7 @@ public final class SemanticVersionTest { assertThrows(IllegalArgumentException.class, () -> testBuilder.preRelease(List.of("")), "Invalid string tolerated by builder.preRelease(List)"); - + // the overloadings that accept numeric arguments assertThrows(IllegalArgumentException.class, () -> testBuilder.preRelease(-1), @@ -257,12 +257,12 @@ public final class SemanticVersionTest { assertThrows(IllegalArgumentException.class, () -> testBuilder.preRelease("#$#c", 1), "Invalid string tolerated by builder.preRelease(String, int)"); - + // ensure all these attempts didn't change the builder assertEquals(builder(1, 2, 3), testBuilder, "Attempts at making invalid version number succeeded despite throwing errors"); } - + /** * Test for {@link SemanticVersionNumber#isStable} * @@ -282,7 +282,7 @@ public final class SemanticVersionTest { .isStable(), "9.9.99+lots-of-metadata.abc123.2022 should be stable but is not"); } - + /** * Tests that the versions are ordered by * {@link SemanticVersionNumber#compareTo} according to official rules. Tests @@ -311,7 +311,7 @@ public final class SemanticVersionTest { final SemanticVersionNumber v210 = stableVersion(2, 1, 0); final SemanticVersionNumber v211 = stableVersion(2, 1, 1); final SemanticVersionNumber v300 = stableVersion(3, 0, 0); - + // test order of version numbers assertTrue(v100a.compareTo(v100a1) < 0, "1.0.0-alpha >= 1.0.0-alpha.1"); assertTrue(v100a1.compareTo(v100ab) < 0, @@ -327,25 +327,25 @@ public final class SemanticVersionTest { assertTrue(v201.compareTo(v210) < 0, "2.0.1 >= 2.1.0"); assertTrue(v210.compareTo(v211) < 0, "2.1.0 >= 2.1.1"); assertTrue(v211.compareTo(v300) < 0, "2.1.1 >= 3.0.0"); - + // test symmetry - assume previous tests passed assertTrue(v100a1.compareTo(v100a) > 0, "1.0.0-alpha.1 <= 1.0.0-alpha"); assertTrue(v100.compareTo(v100rc1) > 0, "1.0.0 <= 1.0.0-rc.1"); assertTrue(v300.compareTo(v211) > 0, "3.0.0 <= 2.1.1"); - + // test transitivity assertTrue(v100a.compareTo(v100b11) < 0, "1.0.0-alpha >= 1.0.0-beta.11"); assertTrue(v100b.compareTo(v200) < 0, "1.0.0-beta >= 2.0.0"); assertTrue(v100.compareTo(v300) < 0, "1.0.0 >= 3.0.0"); assertTrue(v100a.compareTo(v300) < 0, "1.0.0-alpha >= 3.0.0"); - + // test metadata is ignored assertEquals(0, v100.compareTo(v100plus), "Build metadata not ignored"); // test metadata is NOT ignored by alternative comparator assertTrue(BUILD_METADATA_COMPARATOR.compare(v100, v100plus) > 0, "Build metadata ignored by BUILD_METADATA_COMPARATOR"); } - + /** * Tests that simple stable versions can be created and their parts read * @@ -357,13 +357,13 @@ public final class SemanticVersionTest { assertEquals(1, v100.majorVersion()); assertEquals(0, v100.minorVersion()); assertEquals(0, v100.patchVersion()); - + final SemanticVersionNumber v925 = stableVersion(9, 2, 5); assertEquals(9, v925.majorVersion()); assertEquals(2, v925.minorVersion()); assertEquals(5, v925.patchVersion()); } - + /** * Tests that {@link SemanticVersionNumber#toString} works for simple version * numbers @@ -374,11 +374,11 @@ public final class SemanticVersionTest { public void testSimpleToString() { final SemanticVersionNumber v100 = stableVersion(1, 0, 0); assertEquals("1.0.0", v100.toString()); - + final SemanticVersionNumber v845a1 = preRelease(8, 4, 5, "alpha", 1); assertEquals("8.4.5-alpha.1", v845a1.toString()); } - + /** * Tests that simple unstable versions can be created and their parts read * diff --git a/src/test/java/sevenUnits/utils/UncertainDoubleTest.java b/src/test/java/sevenUnits/utils/UncertainDoubleTest.java index 5ccef28..36b373b 100644 --- a/src/test/java/sevenUnits/utils/UncertainDoubleTest.java +++ b/src/test/java/sevenUnits/utils/UncertainDoubleTest.java @@ -43,36 +43,36 @@ class UncertainDoubleTest { 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); } - + /** * Tests the ___exact operations */ @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 for {@link UncertainDouble#fromRoundedString} * @@ -82,29 +82,29 @@ class UncertainDoubleTest { final void testFromRoundedString() { assertEquals(of(12345.678, 0.001), fromRoundedString("12345.678")); } - + /** * Test for {@link UncertainDouble#fromString} */ @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.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")) { + 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 � 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()); + assertEquals(of(2.0, 0.5).hashCode(), fromString("2.0 � 0.5").hashCode()); } } diff --git a/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java b/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java index 8ea3fd0..476e407 100644 --- a/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java +++ b/src/test/java/sevenUnitsGUI/PrefixRepetitionTest.java @@ -54,7 +54,7 @@ class PrefixRepetitionTest { assertFalse(COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.MILLI)), "Complex repetition does not factor direction of prefixes"); } - + /** * Tests the {@code NO_REPETITION} rule. * @@ -68,7 +68,7 @@ class PrefixRepetitionTest { assertTrue(NO_REPETITION.test(List.of(Metric.MILLI))); assertTrue(NO_REPETITION.test(List.of()), "Empty list yields false"); } - + /** * Tests the {@code NO_RESTRICTION} rule. * @@ -82,7 +82,7 @@ class PrefixRepetitionTest { assertTrue(NO_RESTRICTION.test(List.of(Metric.MILLI))); assertTrue(NO_RESTRICTION.test(List.of())); } - + /** * Ensures that the complex repetition rule allows valid prefix lists. * @@ -95,7 +95,7 @@ class PrefixRepetitionTest { assertTrue(COMPLEX_REPETITION.test(List.of(Metric.KILO)), "Single prefix not allowed"); assertTrue(COMPLEX_REPETITION.test(List.of()), "No prefixes not allowed"); - + // valid complex repetition assertTrue(COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.KILO)), "Valid complex repetition (kiloyotta) not allowed"); @@ -109,7 +109,7 @@ class PrefixRepetitionTest { COMPLEX_REPETITION.test(List.of(Metric.YOTTA, Metric.YOTTA, Metric.KILO, Metric.DEKA)), "Valid complex repetition (dekakiloyottayotta) not allowed"); - + // valid with negative prefixes assertTrue(COMPLEX_REPETITION.test(List.of(Metric.YOCTO, Metric.MILLI)), "Valid complex repetition (milliyocto) not allowed"); diff --git a/src/test/java/sevenUnitsGUI/PrefixSearchTest.java b/src/test/java/sevenUnitsGUI/PrefixSearchTest.java index ca238fe..305d0d7 100644 --- a/src/test/java/sevenUnitsGUI/PrefixSearchTest.java +++ b/src/test/java/sevenUnitsGUI/PrefixSearchTest.java @@ -45,7 +45,7 @@ class PrefixSearchTest { private static final PrefixSearchRule getCommonRuleCopy() { return getCoherentOnlyRule(Set.of(Metric.KILO, Metric.MILLI)); } - + /** * Test method for * {@link sevenUnitsGUI.PrefixSearchRule#apply(java.util.Map.Entry)}, for a @@ -60,11 +60,11 @@ class PrefixSearchTest { Metric.KILOMETRE, "millimetre", Metric.MILLIMETRE); final var actual = COMMON_PREFIXES .apply(Map.entry("metre", Metric.METRE)); - + assertEquals(expected, actual, "Prefixes not correctly applied to coherent unit."); } - + /** * Test method for * {@link sevenUnitsGUI.PrefixSearchRule#equals(java.lang.Object)}. @@ -76,11 +76,11 @@ class PrefixSearchTest { final void testEquals() { assertEquals(getCommonRuleCopy(), getCommonRuleCopy(), "equals considers something other than prefixes/rule"); - + assertNotEquals(getCoherentOnlyRule(Set.of()), getUniversalRule(Set.of()), "equals ignores rule"); } - + /** * Test method for {@link sevenUnitsGUI.PrefixSearchRule#getPrefixes()}. * @@ -93,7 +93,7 @@ class PrefixSearchTest { assertEquals(Metric.ALL_PREFIXES, PrefixSearchRule.ALL_METRIC_PREFIXES.getPrefixes()); } - + /** * Test method for {@link sevenUnitsGUI.PrefixSearchRule#hashCode()}. * @@ -105,7 +105,7 @@ class PrefixSearchTest { assertEquals(getCommonRuleCopy().hashCode(), getCommonRuleCopy().hashCode()); } - + /** * Tests prefix searching for a non-coherent unit and * {@link PrefixSearchRule#COMMON_PREFIXES}. @@ -118,10 +118,10 @@ class PrefixSearchTest { final var input = Map.entry("inch", BritishImperial.Length.INCH); final var expected = Map.ofEntries(input); final var actual = COMMON_PREFIXES.apply(input); - + assertEquals(expected, actual, "Prefixes applied to non-coherent unit."); } - + /** * Tests that {@link PrefixSearchRule#NO_PREFIXES} returns the original unit. * @@ -141,7 +141,7 @@ class PrefixSearchTest { } } } - + /** * Test method for {@link sevenUnitsGUI.PrefixSearchRule#toString()}. * @@ -154,5 +154,5 @@ class PrefixSearchTest { "Apply the following prefixes: [kilo (\u00D7 1000.0), milli (\u00D7 0.001)]", COMMON_PREFIXES.toString()); } - + } diff --git a/src/test/java/sevenUnitsGUI/PresenterTest.java b/src/test/java/sevenUnitsGUI/PresenterTest.java index 5755701..9e25a08 100644 --- a/src/test/java/sevenUnitsGUI/PresenterTest.java +++ b/src/test/java/sevenUnitsGUI/PresenterTest.java @@ -64,14 +64,14 @@ public final class PresenterTest { "test-settings.txt"); static final Set testUnits = Set.of(Metric.METRE, Metric.KILOMETRE, Metric.METRE_PER_SECOND, Metric.KILOMETRE_PER_HOUR); - + static final Set> testDimensions = Set .of(Metric.Dimensions.LENGTH, Metric.Dimensions.VELOCITY); - + private static final Stream, Map>> SEARCH_RULES = Stream .of(PrefixSearchRule.NO_PREFIXES, PrefixSearchRule.COMMON_PREFIXES, PrefixSearchRule.ALL_METRIC_PREFIXES); - + /** * @return rounding rules used by {@link #testRoundingRules} * @since v0.4.0 @@ -81,18 +81,18 @@ public final class PresenterTest { final var SCIENTIFIC_ROUNDING = StandardDisplayRules.uncertaintyBased(); final var INTEGER_ROUNDING = StandardDisplayRules.fixedDecimals(0); final var SIG_FIG_ROUNDING = StandardDisplayRules.fixedPrecision(4); - + return Stream.of(SCIENTIFIC_ROUNDING, INTEGER_ROUNDING, SIG_FIG_ROUNDING); } - + private static final Stream, Map>> getSearchRules() { return SEARCH_RULES; } - + private static final Set names(Set units) { return units.stream().map(Nameable::getName).collect(Collectors.toSet()); } - + /** * Test method for {@link Presenter#convertExpressions} * @@ -104,20 +104,20 @@ public final class PresenterTest { // setup final ViewBot viewBot = new ViewBot(); final Presenter presenter = new Presenter(viewBot); - + viewBot.setFromExpression("10000.0 m"); viewBot.setToExpression("km"); - + // convert expression presenter.convertExpressions(); - + // test result final List outputs = viewBot .expressionConversionList(); assertEquals("10000.0 m = 10.00000 km", outputs.get(outputs.size() - 1).toString()); } - + /** * Tests that unit-conversion Views can correctly convert units * @@ -129,16 +129,16 @@ public final class PresenterTest { // setup final ViewBot viewBot = new ViewBot(); final Presenter presenter = new Presenter(viewBot); - + viewBot.setFromUnitNames(names(testUnits)); viewBot.setToUnitNames(names(testUnits)); viewBot.setFromSelection("metre"); viewBot.setToSelection("kilometre"); viewBot.setInputValue("10000.0"); - + // convert units presenter.convertUnits(); - + /* * use result from system as expected - I'm not testing unit conversion * here (that's for the backend tests), I'm just testing that it correctly @@ -154,7 +154,7 @@ public final class PresenterTest { expectedOutput.getValue().toString(false, RoundingMode.HALF_EVEN)); assertEquals(List.of(expectedUC), viewBot.unitConversionList()); } - + /** * Tests that duplicate units are successfully removed, if that is asked for * @@ -165,7 +165,7 @@ public final class PresenterTest { void testDuplicateUnits() { final var metre = Metric.METRE; final var meter = Metric.METRE.withName(NameSymbol.of("meter", "m")); - + // load 2 duplicate units final var viewBot = new ViewBot(); final var presenter = new Presenter(viewBot); @@ -174,20 +174,20 @@ public final class PresenterTest { presenter.database.addUnit("meter", meter); presenter.setOneWayConversionEnabled(false); presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES); - + // test that only one of them is included if duplicate units disabled presenter.setShowDuplicates(false); presenter.updateView(); assertEquals(1, viewBot.getFromUnitNames().size()); assertEquals(1, viewBot.getToUnitNames().size()); - + // test that both of them is included if duplicate units enabled presenter.setShowDuplicates(true); presenter.updateView(); assertEquals(2, viewBot.getFromUnitNames().size()); assertEquals(2, viewBot.getToUnitNames().size()); } - + /** * Tests that one-way conversion correctly filters From and To units * @@ -200,7 +200,7 @@ public final class PresenterTest { final var allNames = Set.of("metre", "inch", "tempC"); final var metricNames = Set.of("metre", "tempC"); final var nonMetricNames = Set.of("inch", "tempC"); - + // load view with one metric and one non-metric unit final var viewBot = new ViewBot(); final var presenter = new Presenter(viewBot); @@ -209,19 +209,19 @@ public final class PresenterTest { presenter.database.addUnit("inch", BritishImperial.Length.INCH); presenter.database.addUnit("tempC", Metric.CELSIUS); presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES); - + // test that units are removed from each side when one-way conversion is // enabled presenter.setOneWayConversionEnabled(true); assertEquals(nonMetricNames, viewBot.getFromUnitNames()); assertEquals(metricNames, viewBot.getToUnitNames()); - + // test that units are kept when one-way conversion is disabled presenter.setOneWayConversionEnabled(false); assertEquals(allNames, viewBot.getFromUnitNames()); assertEquals(allNames, viewBot.getToUnitNames()); } - + /** * Tests the prefix-viewing functionality. * @@ -235,23 +235,23 @@ public final class PresenterTest { final var presenter = new Presenter(viewBot); viewBot.setViewablePrefixNames(Set.of("kilo", "milli")); presenter.setNumberDisplayRule(UncertainDouble::toString); - + // view prefix viewBot.setViewedPrefixName("kilo"); presenter.prefixSelected(); // just in case - + // get correct values final var expectedNameSymbol = presenter.database.getPrefix("kilo") .getNameSymbol(); final var expectedMultiplierString = String .valueOf(Metric.KILO.getMultiplier()); - + // test that presenter's values are correct final var prefixRecord = viewBot.prefixViewList().get(0); assertEquals(expectedNameSymbol, prefixRecord.getNameSymbol()); assertEquals(expectedMultiplierString, prefixRecord.multiplierString()); } - + /** * Tests that rounding rules are used correctly. * @@ -265,13 +265,13 @@ public final class PresenterTest { final var viewBot = new ViewBot(); final var presenter = new Presenter(viewBot); presenter.setNumberDisplayRule(roundingRule); - + // convert and round viewBot.setInputValue("12345.6789"); viewBot.setFromSelection("metre"); viewBot.setToSelection("kilometre"); presenter.convertUnits(); - + // test the result of the rounding final String expectedOutputString = roundingRule .apply(UncertainDouble.fromRoundedString("12.3456789")); @@ -279,7 +279,7 @@ public final class PresenterTest { .outputValueString(); assertEquals(expectedOutputString, actualOutputString); } - + /** * Tests that the Presenter correctly applies search rules. * @@ -294,15 +294,15 @@ public final class PresenterTest { // setup final var viewBot = new ViewBot(); final var presenter = new Presenter(viewBot); - + presenter.setSearchRule(searchRule); presenter.setOneWayConversionEnabled(false); - + presenter.database.clear(); presenter.database.addUnit("metre", Metric.METRE); presenter.database.addUnit("inch", BritishImperial.Length.INCH); presenter.updateView(); - + // create expected output based on rule final Set expectedOutput = new HashSet<>(); expectedOutput.addAll(searchRule @@ -310,11 +310,11 @@ public final class PresenterTest { expectedOutput.addAll( searchRule.apply(Map.entry("metre", Metric.METRE)).keySet()); final Set actualOutput = viewBot.getFromUnitNames(); - + // test output assertEquals(expectedOutput, actualOutput); } - + /** * Tests that settings can be saved to and loaded from a file. * @@ -326,7 +326,7 @@ public final class PresenterTest { // setup final var viewBot = new ViewBot(); final var presenter = new Presenter(viewBot); - + // set and save custom settings presenter.setOneWayConversionEnabled(true); presenter.setShowDuplicates(true); @@ -335,12 +335,12 @@ public final class PresenterTest { DefaultPrefixRepetitionRule.COMPLEX_REPETITION); assumeTrue(presenter.writeSettings(TEST_SETTINGS), "Could not write to settings file."); - + // overwrite custom settings presenter.setOneWayConversionEnabled(false); presenter.setShowDuplicates(false); presenter.setNumberDisplayRule(StandardDisplayRules.uncertaintyBased()); - + // load settings & test that they're the same presenter.loadSettings(TEST_SETTINGS); assertTrue(presenter.oneWayConversionEnabled()); @@ -348,7 +348,7 @@ public final class PresenterTest { assertEquals(StandardDisplayRules.fixedPrecision(11), presenter.getNumberDisplayRule()); } - + /** * Ensures the Presenter generates the correct data upon a unit-viewing. * @@ -361,12 +361,12 @@ public final class PresenterTest { final var viewBot = new ViewBot(); final var presenter = new Presenter(viewBot); viewBot.setViewableUnitNames(names(testUnits)); - + // view unit viewBot.setViewedUnitName("metre"); presenter.unitNameSelected(); // just in case this isn't triggered // automatically - + // get correct values final var expectedNameSymbol = presenter.database.getUnit("metre") .getNameSymbol(); @@ -374,7 +374,7 @@ public final class PresenterTest { final var expectedDimensionName = presenter .getDimensionName(Metric.METRE.getDimension()); final var expectedUnitType = UnitType.METRIC; - + // test for correctness final var viewRecord = viewBot.unitViewList().get(0); assertEquals(expectedNameSymbol, viewRecord.getNameSymbol()); @@ -382,7 +382,7 @@ public final class PresenterTest { assertEquals(expectedDimensionName, viewRecord.dimensionName()); assertEquals(expectedUnitType, viewRecord.unitType()); } - + /** * Test for {@link Presenter#updateView()} * @@ -396,7 +396,7 @@ public final class PresenterTest { final Presenter presenter = new Presenter(viewBot); presenter.setOneWayConversionEnabled(false); presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES); - + // override default database units presenter.database.clear(); for (final Unit unit : testUnits) { @@ -406,18 +406,18 @@ public final class PresenterTest { presenter.database.addDimension( dimension.getPrimaryName().orElseThrow(), dimension); } - + // set from and to units viewBot.setFromUnitNames(names(testUnits)); viewBot.setToUnitNames(names(testUnits)); viewBot.setDimensionNames(names(testDimensions)); viewBot.setSelectedDimensionName(Metric.Dimensions.LENGTH.getName()); - + // filter to length units only, then get the filtered sets of units presenter.updateView(); final Set fromUnits = viewBot.getFromUnitNames(); final Set toUnits = viewBot.getToUnitNames(); - + // test that fromUnits/toUnits is [METRE, KILOMETRE] assertEquals(Set.of("metre", "kilometre"), fromUnits); assertEquals(Set.of("metre", "kilometre"), toUnits); diff --git a/src/test/java/sevenUnitsGUI/RoundingTest.java b/src/test/java/sevenUnitsGUI/RoundingTest.java index ca1a272..f749f85 100644 --- a/src/test/java/sevenUnitsGUI/RoundingTest.java +++ b/src/test/java/sevenUnitsGUI/RoundingTest.java @@ -49,13 +49,13 @@ class RoundingTest { private static final FixedDecimals ZERO_DECIMALS = fixedDecimals(0); private static final FixedDecimals TWO_DECIMALS = fixedDecimals(2); private static final FixedDecimals SIX_DECIMALS = fixedDecimals(6); - + private static final FixedPrecision ONE_SIG_FIG = fixedPrecision(1); private static final FixedPrecision THREE_SIG_FIGS = fixedPrecision(3); private static final FixedPrecision TWELVE_SIG_FIGS = fixedPrecision(12); - + private static final UncertaintyBased UNCERTAINTY_BASED = uncertaintyBased(); - + // numbers to test rounding with private static final UncertainDouble INPUT1 = UncertainDouble.of(12.3456789, 0.0); @@ -65,7 +65,7 @@ class RoundingTest { 0.0); private static final UncertainDouble INPUT4 = UncertainDouble.of(0.00001234, 0.000001); - + /** * @return arguments for * {@link #testFixedDecimalRounding(UncertainDouble, String, String, String)} @@ -79,7 +79,7 @@ class RoundingTest { Arguments.of(INPUT3, "12345432", "12345432.10", "12345432.100000"), Arguments.of(INPUT4, "0", "0.00", "0.000012")); } - + /** * @return arguments for * {@link #testFixedPrecisionRounding(UncertainDouble, String, String, String)} @@ -93,7 +93,7 @@ class RoundingTest { Arguments.of(INPUT3, "1E+7", "1.23E+7", "12345432.1000"), Arguments.of(INPUT4, "0.00001", "0.0000123", "0.0000123400000000")); } - + /** * @return arguments for * {@link #testUncertaintyRounding(UncertainDouble, String)} @@ -107,7 +107,7 @@ class RoundingTest { Arguments.of(INPUT3, "1.23454321E7"), Arguments.of(INPUT4, "0.0000123")); } - + /** * Test for {@link FixedDecimals#decimalPlaces()} and * {@link FixedPrecision#significantFigures()}. @@ -124,7 +124,7 @@ class RoundingTest { "TWO_DECIMALS has " + TWO_DECIMALS.decimalPlaces() + " decimals"); assertEquals(6, SIX_DECIMALS.decimalPlaces(), "SIX_DECIMALS has " + SIX_DECIMALS.decimalPlaces() + " decimals"); - + // ensure # of sig figs can be accessed assertEquals(1, ONE_SIG_FIG.significantFigures(), "ONE_SIG_FIG has " + ONE_SIG_FIG.significantFigures() + " significant figures"); @@ -134,7 +134,7 @@ class RoundingTest { "TWELVE_SIG_FIGS has " + TWELVE_SIG_FIGS.significantFigures() + " significant figures"); } - + /** * Tests that the rounding methods' equals() methods work. * @@ -150,14 +150,14 @@ class RoundingTest { "TWO_DECIMALS == SIX_DECIMALS"); assertTrue(Objects.equals(fixedDecimals(0), fixedDecimals(0)), "FixedDecimals.equals() depends on something other than decimal places."); - + assertTrue(ONE_SIG_FIG.equals(ONE_SIG_FIG), "ONE_SIG_FIG does not equal itself"); assertFalse(THREE_SIG_FIGS.equals(TWELVE_SIG_FIGS), "THREE_SIG_FIGS == TWELVE_SIG_FIGS"); assertTrue(Objects.equals(fixedPrecision(1), fixedPrecision(1)), "FixedPrecision.equals() depends on something other than significant figures."); - + // test that FixedDecimals is never equal to FixedPrecision // this unlikely argument is the test - the equals should return false! @SuppressWarnings("unlikely-arg-type") @@ -165,7 +165,7 @@ class RoundingTest { fixedPrecision(4)); assertFalse(differentRulesEqual, "fixedDecimals(4) == fixedPrecision(4)"); } - + /** * Ensures that fixed decimal rounding works as expected * @@ -192,7 +192,7 @@ class RoundingTest { "TWO_DECIMALS rounded " + input + " as " + SIX_DECIMALS.apply(input) + " (should be " + sixDecimalString + ")"); } - + /** * Ensures that fixed precision rounding works as expected * @@ -221,7 +221,7 @@ class RoundingTest { + TWELVE_SIG_FIGS.apply(input) + " (should be " + twelveSigFigString + ")"); } - + /** * Tests that {@link StandardDisplayRules#getStandardRule} gets rounding * rules as intended. @@ -236,11 +236,11 @@ class RoundingTest { getStandardRule("Round to 3 significant figures")); assertEquals(UNCERTAINTY_BASED, getStandardRule("Uncertainty-Based Rounding")); - + assertThrows(IllegalArgumentException.class, () -> getStandardRule("Not a rounding rule")); } - + /** * Tests that the rounding methods' equals() methods work. * @@ -253,7 +253,7 @@ class RoundingTest { assertEquals(ONE_SIG_FIG.hashCode(), ONE_SIG_FIG.hashCode()); assertEquals(UNCERTAINTY_BASED.hashCode(), UNCERTAINTY_BASED.hashCode()); } - + /** * Tests that the {@code toString()} methods of the three rounding rule * classes work correctly. @@ -267,7 +267,7 @@ class RoundingTest { assertEquals("Round to 3 significant figures", THREE_SIG_FIGS.toString()); assertEquals("Uncertainty-Based Rounding", UNCERTAINTY_BASED.toString()); } - + /** * Tests that Uncertainty Rounding works as expected * diff --git a/src/test/java/sevenUnitsGUI/TabbedViewTest.java b/src/test/java/sevenUnitsGUI/TabbedViewTest.java index 00092a4..165718f 100644 --- a/src/test/java/sevenUnitsGUI/TabbedViewTest.java +++ b/src/test/java/sevenUnitsGUI/TabbedViewTest.java @@ -36,17 +36,17 @@ class TabbedViewTest { private static final TabbedView setupView() { final var view = new TabbedView(); final var presenter = view.getPresenter(); - + presenter.setNumberDisplayRule(StandardDisplayRules.uncertaintyBased()); presenter.setPrefixRepetitionRule( DefaultPrefixRepetitionRule.NO_RESTRICTION); presenter.setSearchRule(PrefixSearchRule.COMMON_PREFIXES); presenter.setOneWayConversionEnabled(false); presenter.setShowDuplicates(true); - + return view; } - + /** * Simulates an expression conversion operation, and ensures it works * properly. @@ -57,18 +57,18 @@ class TabbedViewTest { @Test void testExpressionConversion() { final var view = setupView(); - + // prepare for unit conversion view.masterPane.setSelectedIndex(1); view.fromEntry.setText("250.0 inch"); view.toEntry.setText("metre"); - + view.convertExpressionButton.doClick(); - + // check result of conversion assertEquals("250.0 inch = 6.350 metre", view.expressionOutput.getText()); } - + /** * Simulates a unit conversion operation, and ensures it works properly. * @@ -78,18 +78,18 @@ class TabbedViewTest { @Test void testUnitConversion() { final var view = setupView(); - + // prepare for unit conversion view.masterPane.setSelectedIndex(0); view.dimensionSelector.setSelectedItem("Length"); view.fromSearch.getSearchList().setSelectedValue("inch", true); view.toSearch.getSearchList().setSelectedValue("metre", true); view.valueInput.setText("250.0"); - + view.convertUnitButton.doClick(); - + // check result of conversion assertEquals("250.0 inch = 6.350 metre", view.unitOutput.getText()); } - + } -- cgit v1.2.3