From cf76cf66ea2039cd3e3052c940784dd88a87e2bd Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Tue, 28 Jul 2020 10:51:19 -0500 Subject: Added some tests for UnitValue and LinearUnitValue. --- src/org/unitConverter/unit/UnitTest.java | 77 ++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 23 deletions(-) (limited to 'src/org/unitConverter/unit/UnitTest.java') diff --git a/src/org/unitConverter/unit/UnitTest.java b/src/org/unitConverter/unit/UnitTest.java index c078cfc..2cf3126 100644 --- a/src/org/unitConverter/unit/UnitTest.java +++ b/src/org/unitConverter/unit/UnitTest.java @@ -17,6 +17,8 @@ package org.unitConverter.unit; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -25,7 +27,8 @@ import org.junit.jupiter.api.Test; import org.unitConverter.math.DecimalComparison; /** - * Testing the various Unit classes. This is NOT part of this program's public API. + * Testing the various Unit classes. This is NOT part of this program's public + * API. * * @author Adrien Hopkins * @since 2018-12-22 @@ -34,69 +37,97 @@ import org.unitConverter.math.DecimalComparison; 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); - + final LinearUnit inch = SI.METRE.times(0.0254) + .withName(NameSymbol.of("inch", "in")); + final LinearUnit foot = SI.METRE.times(0.3048) + .withName(NameSymbol.of("foot", "ft")); + assertEquals(inch.plus(foot), SI.METRE.times(0.3302)); assertEquals(foot.minus(inch), SI.METRE.times(0.2794)); + + // test with LinearUnitValue + final LinearUnitValue value1 = LinearUnitValue.getExact(SI.METRE, 15); + final LinearUnitValue value2 = LinearUnitValue.getExact(foot, 120); + final LinearUnitValue value3 = LinearUnitValue.getExact(SI.METRE, 0.5); + final LinearUnitValue value4 = LinearUnitValue.getExact(SI.KILOGRAM, 60); + + // make sure addition is done correctly + assertEquals(51.576, value1.plus(value2).getValue(), 0.001); + assertEquals(15.5, value1.plus(value3).getValue()); + assertEquals(52.076, value1.plus(value2).plus(value3).getValue(), 0.001); + + // make sure addition uses the correct unit, and is still associative + // (ignoring floating-point rounding errors) + assertEquals(SI.METRE, value1.plus(value2).getUnit()); + assertEquals(SI.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)); } - + @Test public void testConversion() { final LinearUnit metre = SI.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 = rng.nextDouble() * 1000000; - final double testValue = rng.nextDouble() * 1000000; + final double conversionFactor = UnitTest.rng.nextDouble() * 1000000; + final double testValue = UnitTest.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); + + assertEquals(actual, expected, + expected * DecimalComparison.DOUBLE_EPSILON); } } - + @Test public void testEquals() { final LinearUnit metre = SI.METRE; final Unit meter = SI.BaseUnits.METRE.asLinearUnit(); - + 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 generatedJoule = SI.KILOGRAM + .times(SI.METRE.toExponent(2)).dividedBy(SI.SECOND.toExponent(2)); final LinearUnit actualJoule = SI.JOULE; - + 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.METRE.dividedBy(SI.SECOND).dividedBy(3.6); - + assertEquals(generatedKPH, actualKPH); } - + @Test public void testPrefixes() { final LinearUnit generatedKilometre = SI.METRE.withPrefix(SI.KILO); final LinearUnit actualKilometre = SI.METRE.times(1000); - + assertEquals(generatedKilometre, actualKilometre); } } -- cgit v1.2.3