/** * Tests {@link Scalar#multiply(Quantity)}, {@link Scalar#divide(Quantity)} and {@link Quantity#inverse()}. * Those tests depend on proper working of {@link Quantities#create(double, Unit)}, which depends in turn on * proper declarations of {@link ScalarFactory} in {@link Units} initialization. */ @Test public void testMultiplyDivideQuantity() { final Quantity<Length> q1 = new Scalar.Length(24, Units.METRE); final Quantity<Time> q2 = new Scalar.Time ( 4, Units.SECOND); final Quantity<Speed> q3 = q1.divide(q2).asType(Speed.class); assertSame ("unit", Units.METRES_PER_SECOND, q3.getUnit()); assertEquals("value", 6, q3.getValue().doubleValue(), STRICT); assertInstanceOf("Length/Time", Scalar.Speed.class, q3); final Quantity<Area> q4 = q1.multiply(q1).asType(Area.class); assertSame ("unit", Units.SQUARE_METRE, q4.getUnit()); assertEquals("value", 576, q4.getValue().doubleValue(), STRICT); assertInstanceOf("Length⋅Length", Scalar.Area.class, q4); final Quantity<Frequency> q5 = q2.inverse().asType(Frequency.class); assertSame ("unit", Units.HERTZ, q5.getUnit()); assertEquals("value", 0.25, q5.getValue().doubleValue(), STRICT); assertInstanceOf("1/Time", Scalar.Frequency.class, q5); }
/** * Tests {@link Scalar#add(Quantity)} and {@link Scalar#subtract(Quantity)}. * Those tests depend on proper working of {@link Scalar#create(double, Unit)}. */ @Test public void testAddSubtract() { final Quantity<Length> q0 = new Scalar.Length(0, Units.MILLIMETRE); final Quantity<Length> q1 = new Scalar.Length(24, Units.METRE); final Quantity<Length> q2 = new Scalar.Length(3, Units.KILOMETRE); assertSame(q1, q1.add(q0)); assertSame(q2, q2.subtract(q0)); final Quantity<Length> q3 = q1.add(q2); assertSame ("unit", Units.METRE, q3.getUnit()); assertEquals("value", 3024, q3.getValue().doubleValue(), STRICT); final Quantity<Length> q4 = q2.subtract(q1); assertSame ("unit", Units.KILOMETRE, q4.getUnit()); assertEquals("value", 2.976, q4.getValue().doubleValue(), STRICT); }
/** * Tests {@link Scalar#equals(Object)} and {@link Scalar#hashCode()}. */ @Test public void testEqualsAndHashCode() { final Quantity<Length> q1 = new Scalar.Length(24, Units.METRE); Quantity<Length> q2 = new Scalar.Length(24, Units.METRE); assertEquals("hashCode()", q1.hashCode(), q2.hashCode()); assertEquals("equals(…)", q1, q2); q2 = new Scalar.Length(12, Units.METRE); assertNotEquals("hashCode()", q1.hashCode(), q2.hashCode()); assertNotEquals("equals(…)", q1, q2); q2 = new Scalar.Length(24, Units.CENTIMETRE); assertNotEquals("hashCode()", q1.hashCode(), q2.hashCode()); assertNotEquals("equals(…)", q1, q2); }
/** * Tests {@link Scalar#multiply(Number)} and {@link Scalar#divide(Number)}. * Those tests depend on proper working of {@link Scalar#create(double, Unit)}. */ @Test public void testMultiplyDivideNumber() { final Quantity<Length> q1 = new Scalar.Length(24, Units.KILOMETRE); assertSame(q1, q1.multiply(1)); assertSame(q1, q1.divide (1)); final Quantity<Length> q2 = q1.multiply(2); assertSame ("unit", Units.KILOMETRE, q2.getUnit()); assertEquals("value", 48, q2.getValue().doubleValue(), STRICT); final Quantity<Length> q3 = q1.divide(3); assertSame ("unit", Units.KILOMETRE, q3.getUnit()); assertEquals("value", 8, q3.getValue().doubleValue(), STRICT); }
final SystemUnit<Length> m = add(Length.class, new ScalarFactory<Length> () {@Override public Length create(double value, Unit<Length> unit) {return new Scalar.Length (value, unit);}}, length, "m", (byte) (SI | PREFIXABLE), Constants.EPSG_METRE); final SystemUnit<Area> m2 = add(Area.class, new ScalarFactory<Area> () {@Override public Area create(double value, Unit<Area> unit) {return new Scalar.Area (value, unit);}}, area, "m²", (byte) (SI | PREFIXABLE), (short) 0);
/** * Tests {@link Scalar} serialization. */ @Test public void testSerialization() { final Quantity<Length> q1 = new Scalar.Length(24, Units.KILOMETRE); assertNotSame(q1, assertSerializedEquals(q1)); }
@Override Quantity<javax.measure.quantity.Length> create(double value, Unit<javax.measure.quantity.Length> unit) { return new Length(value, unit); } }
/** * Tests {@link Scalar#toString()}. */ @Test public void testToString() { assertEquals("toString()", "24 km", new Scalar.Length (24.00, Units.KILOMETRE).toString()); assertEquals("toString()", "10.25 h", new Scalar.Time (10.25, Units.HOUR) .toString()); assertEquals("toString()", "0.25", new Scalar.Dimensionless( 0.25, Units.UNITY) .toString()); }
@Override Quantity<javax.measure.quantity.Length> create(double value, Unit<javax.measure.quantity.Length> unit) { return new Length(value, unit); } }