diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java index d8c7f84d4a7..794107ebc4d 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/QuantityTypeTest.java @@ -14,6 +14,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.number.IsCloseTo.closeTo; import static org.junit.jupiter.api.Assertions.*; import static org.openhab.core.library.unit.MetricPrefix.CENTI; @@ -340,6 +341,15 @@ public void testAdd(Locale locale) { QuantityType result = new QuantityType<>("20 m").add(new QuantityType<>("20cm")); assertThat(result, is(new QuantityType<>("20.20 m"))); + + assertThat(new QuantityType<>("65 °F").add(new QuantityType<>("1 °F")), is(new QuantityType<>("66 °F"))); + assertThat(new QuantityType<>("65 °F").add(new QuantityType<>("2 °F")), is(new QuantityType<>("67 °F"))); + assertThat(new QuantityType<>("1 °F").add(new QuantityType<>("65 °F")), is(new QuantityType<>("66 °F"))); + assertThat(new QuantityType<>("2 °F").add(new QuantityType<>("65 °F")), is(new QuantityType<>("67 °F"))); + + assertThat(new QuantityType<>("65 kWh").add(new QuantityType<>("1 kWh")), is(new QuantityType<>("66 kWh"))); + assertThat(new QuantityType<>("65 kJ").add(new QuantityType<>("1 kJ")), is(new QuantityType<>("66 kJ"))); + assertThat(new QuantityType<>("65 kWh").add(new QuantityType<>("1 kJ")), is(new QuantityType<>("234001 kJ"))); } @Test @@ -354,16 +364,67 @@ public void testSubtract(Locale locale) { QuantityType result = new QuantityType<>("20 m").subtract(new QuantityType<>("20cm")); assertThat(result, is(new QuantityType<>("19.80 m"))); + + assertThat(new QuantityType<>("65 °F").subtract(new QuantityType<>("1 °F")), is(new QuantityType<>("64 °F"))); + assertThat(new QuantityType<>("65 °F").subtract(new QuantityType<>("2 °F")), is(new QuantityType<>("63 °F"))); + assertThat(new QuantityType<>("1 °F").subtract(new QuantityType<>("65 °F")), is(new QuantityType<>("-64 °F"))); + assertThat(new QuantityType<>("2 °F").subtract(new QuantityType<>("65 °F")), is(new QuantityType<>("-63 °F"))); + + assertThat(new QuantityType<>("65 kWh").subtract(new QuantityType<>("1 kWh")), + is(new QuantityType<>("64 kWh"))); + assertThat(new QuantityType<>("65 kJ").subtract(new QuantityType<>("1 kJ")), is(new QuantityType<>("64 kJ"))); + assertThat(new QuantityType<>("65 kWh").subtract(new QuantityType<>("1 kJ")), + is(new QuantityType<>("233999 kJ"))); } @Test public void testMultiplyNumber() { assertThat(new QuantityType<>("2 m").multiply(BigDecimal.valueOf(2)), is(new QuantityType<>("4 m"))); + + assertThat(new QuantityType<>("65 °F").multiply(BigDecimal.valueOf(1)).toUnit("°F").doubleValue(), + is(closeTo(65d, 0.0000000000000001d))); + assertThat(new QuantityType<>("65 °F").multiply(BigDecimal.valueOf(2)).toUnit("°F").doubleValue(), + is(closeTo(589.67d, 0.0000000000000001d))); } @Test public void testMultiplyQuantityType() { + QuantityType result; + assertThat(new QuantityType<>("2 m").multiply(new QuantityType<>("4 cm")), is(new QuantityType<>("8 m·cm"))); + + assertThat(new QuantityType<>("65 °F").multiply(QuantityType.valueOf(1, Units.ONE)).toUnit("°F").doubleValue(), + is(closeTo(65d, 0.0000000000000001d))); + assertThat(new QuantityType<>("65 °F").multiply(QuantityType.valueOf(2, Units.ONE)).toUnit("°F").doubleValue(), + is(closeTo(589.67d, 0.0000000000000001d))); + assertThat(QuantityType.valueOf(1, Units.ONE).multiply(new QuantityType<>("65 °F")).toUnit("°F").doubleValue(), + is(closeTo(65d, 0.0000000000000001d))); + assertThat(QuantityType.valueOf(2, Units.ONE).multiply(new QuantityType<>("65 °F")).toUnit("°F").doubleValue(), + is(closeTo(589.67d, 0.0000000000000001d))); + + result = new QuantityType<>("65 °F").multiply(new QuantityType<>("1 °F")); + assertThat(result.doubleValue(), is(closeTo(74598.68175925925925925925925925927d, 0.0000000000000001d))); + assertEquals(Units.KELVIN.multiply(Units.KELVIN), result.getUnit()); + result = new QuantityType<>("65 °F").multiply(new QuantityType<>("2 °F")); + assertThat(result.doubleValue(), is(closeTo(74760.6169444444444444444444444444d, 0.0000000000000001d))); + assertEquals(Units.KELVIN.multiply(Units.KELVIN), result.getUnit()); + result = new QuantityType<>("1 °F").multiply(new QuantityType<>("65 °F")); + assertThat(result.doubleValue(), is(closeTo(74598.68175925925925925925925925927d, 0.0000000000000001d))); + assertEquals(Units.KELVIN.multiply(Units.KELVIN), result.getUnit()); + result = new QuantityType<>("2 °F").multiply(new QuantityType<>("65 °F")); + assertThat(result.doubleValue(), is(closeTo(74760.6169444444444444444444444444d, 0.0000000000000001d))); + assertEquals(Units.KELVIN.multiply(Units.KELVIN), result.getUnit()); + + assertThat(new QuantityType<>("65 kWh").multiply(QuantityType.valueOf(1, Units.ONE)), + is(new QuantityType<>("65 kWh"))); + assertThat(new QuantityType<>("65 kJ").multiply(QuantityType.valueOf(1, Units.ONE)), + is(new QuantityType<>("65 kJ"))); + assertThat(new QuantityType<>("65 kWh").multiply(new QuantityType<>("1 kWh")), + is(new QuantityType<>(65, Units.KILOWATT_HOUR.multiply(Units.KILOWATT_HOUR)))); + assertThat(new QuantityType<>("65 kJ").multiply(new QuantityType<>("1 kJ")), + is(new QuantityType<>(65, MetricPrefix.KILO(Units.JOULE).multiply(MetricPrefix.KILO(Units.JOULE))))); + assertThat(new QuantityType<>("65 kWh").multiply(new QuantityType<>("1 kJ")), + is(new QuantityType<>(65, Units.KILOWATT_HOUR.multiply(MetricPrefix.KILO(Units.JOULE))))); } @ParameterizedTest @@ -372,6 +433,11 @@ public void testDivideNumber(Locale locale) { Locale.setDefault(locale); assertThat(new QuantityType<>("4 m").divide(BigDecimal.valueOf(2)), is(new QuantityType<>("2 m"))); + + assertThat(new QuantityType<>("65 °F").divide(BigDecimal.valueOf(1)).toUnit("°F").doubleValue(), + is(closeTo(65d, 0.0000000000000001d))); + assertThat(new QuantityType<>("65 °F").divide(BigDecimal.valueOf(2)).toUnit("°F").doubleValue(), + is(closeTo(-197.335d, 0.0000000000000001d))); } @ParameterizedTest @@ -379,7 +445,52 @@ public void testDivideNumber(Locale locale) { public void testDivideQuantityType(Locale locale) { Locale.setDefault(locale); + QuantityType result; + assertThat(new QuantityType<>("4 m").divide(new QuantityType<>("2 cm")), is(new QuantityType<>("2 m/cm"))); + + assertThat(new QuantityType<>("65 °F").divide(QuantityType.valueOf(1, Units.ONE)).toUnit("°F").doubleValue(), + is(closeTo(65d, 0.0000000000000001d))); + assertThat(new QuantityType<>("65 °F").divide(QuantityType.valueOf(2, Units.ONE)).toUnit("°F").doubleValue(), + is(closeTo(-197.335d, 0.0000000000000001d))); + result = QuantityType.valueOf(1, Units.ONE).divide(new QuantityType<>("65 °F")); + assertThat(result.doubleValue(), is(closeTo(0.003430727886099834181485505174681228d, 0.0000000000000001d))); + assertEquals(Units.KELVIN.inverse(), result.getUnit()); + result = QuantityType.valueOf(2, Units.ONE).divide(new QuantityType<>("65 °F")); + assertThat(result.doubleValue(), is(closeTo(0.006861455772199668362971010349362456d, 0.0000000000000001d))); + assertEquals(Units.KELVIN.inverse(), result.getUnit()); + + result = new QuantityType<>("65 °F").divide(new QuantityType<>("1 °F")); + assertThat(result.doubleValue(), + is(closeTo(1.138928083009529598193934920876115122114246640762367855514793670089202480d, + 0.0000000000000001d))); + assertEquals(Units.ONE, result.getUnit()); + result = new QuantityType<>("65 °F").divide(new QuantityType<>("2 °F")); + assertThat(result.doubleValue(), + is(closeTo(1.136461108584053544739749171486126553533555353390950245846600385556783676d, + 0.0000000000000001d))); + assertEquals(Units.ONE, result.getUnit()); + result = new QuantityType<>("1 °F").divide(new QuantityType<>("65 °F")); + assertThat(result.doubleValue(), + is(closeTo(0.878018564049783673547182038233556349552596235093804994885674169795613456d, + 0.0000000000000001d))); + assertEquals(Units.ONE, result.getUnit()); + result = new QuantityType<>("2 °F").divide(new QuantityType<>("65 °F")); + assertThat(result.doubleValue(), + is(closeTo(0.879924523986505803648007318886157031927295252253797625173918844225890256d, + 0.0000000000000001d))); + assertEquals(Units.ONE, result.getUnit()); + + assertThat(new QuantityType<>("65 kWh").divide(QuantityType.valueOf(1, Units.ONE)), + is(new QuantityType<>("65 kWh"))); + assertThat(new QuantityType<>("65 kJ").divide(QuantityType.valueOf(1, Units.ONE)), + is(new QuantityType<>("65 kJ"))); + assertThat(new QuantityType<>("65 kWh").divide(new QuantityType<>("1 kWh")), + is(new QuantityType<>(65, Units.ONE))); + assertThat(new QuantityType<>("65 kJ").divide(new QuantityType<>("1 kJ")), + is(new QuantityType<>(65, Units.ONE))); + assertThat(new QuantityType<>("65 kWh").divide(new QuantityType<>("1 kJ")).toUnit(Units.ONE), + is(new QuantityType<>(234000, Units.ONE))); } @ParameterizedTest