001/*
002 * Units of Measurement API
003 * Copyright (c) 2014-2023, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
004 *
005 * All rights reserved.
006 *
007 * Redistribution and use in source and binary forms, with or without modification,
008 * are permitted provided that the following conditions are met:
009 *
010 * 1. Redistributions of source code must retain the above copyright notice,
011 *    this list of conditions and the following disclaimer.
012 *
013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
014 *    and the following disclaimer in the documentation and/or other materials provided with the distribution.
015 *
016 * 3. Neither the name of JSR-385 nor the names of its contributors may be used to endorse or promote products
017 *    derived from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package javax.measure.test.quantity;
031
032import static org.junit.jupiter.api.Assertions.assertEquals;
033import static org.junit.jupiter.api.Assertions.assertNotNull;
034import static org.junit.jupiter.api.Assertions.assertTrue;
035
036import javax.measure.Quantity;
037import javax.measure.quantity.Area;
038import javax.measure.test.unit.AreaUnit;
039import javax.measure.test.unit.DistanceUnit;
040
041import org.junit.jupiter.api.BeforeEach;
042import org.junit.jupiter.api.Test;
043
044/**
045 * @author Werner Keil
046 */
047public class DistanceQuantityTest {
048
049    DistanceQuantity distance;
050    DistanceUnit m;
051
052    @BeforeEach
053    public void setUp() {
054        m = DistanceUnit.m;
055        distance = new DistanceQuantity(100, m);
056    }
057
058    @Test
059    public void testAreaQuantity() {
060        assertNotNull(distance);
061    }
062
063    @Test
064    public void testAdd() {
065        DistanceQuantity dist2 = new DistanceQuantity(50, m);
066        DistanceQuantity result = distance.add(dist2);
067        assertEquals(150d, result.scalar);
068    }
069
070    @Test
071    public void testSubtract() {
072        DistanceQuantity dist2 = new DistanceQuantity(50, m);
073        DistanceQuantity result = distance.subtract(dist2);
074        assertEquals(50d, result.scalar);
075    }
076
077    @Test
078    public void testEq() {
079        DistanceQuantity dist2 = new DistanceQuantity(100, m);
080        assertTrue(dist2.eq(distance));
081    }
082
083    @Test
084    public void testGt() {
085        DistanceQuantity dist2 = new DistanceQuantity(120, m);
086        assertTrue(dist2.gt(distance));
087    }
088
089    @Test
090    public void testLt() {
091        DistanceQuantity dist2 = new DistanceQuantity(20, m);
092        assertTrue(dist2.lt(distance));
093    }
094
095    @Test
096    public void testGe() {
097        DistanceQuantity dist2 = new DistanceQuantity(120, m);
098        assertTrue(dist2.ge(distance));
099        dist2 = new DistanceQuantity(100, m);
100        assertTrue(dist2.ge(distance));
101    }
102
103    @Test
104    public void testLe() {
105        DistanceQuantity dist2 = new DistanceQuantity(20, m);
106        assertTrue(dist2.le(distance));
107        dist2 = new DistanceQuantity(100, m);
108        assertTrue(dist2.le(distance));
109    }
110
111    @Test
112    public void testMultiplyDouble() {
113        DistanceQuantity result = distance.multiply(3d);
114        assertEquals(300d, result.scalar);
115    }
116
117    @Test
118    public void testDivideDouble() {
119        DistanceQuantity result = distance.divide(10d);
120        assertEquals(10d, result.scalar);
121    }
122
123    @Test
124    public void testMultiplyDistanceQuantity() {
125        DistanceQuantity dist = new DistanceQuantity(15, DistanceUnit.m);
126        AreaQuantity result = distance.multiply(dist);
127        assertEquals(AreaUnit.class, result.getUnit().getClass());
128        assertEquals(AreaQuantity.class, result.getClass());
129        assertEquals(Area.class, result.getType());
130        assertEquals(1500d, result.getValue());
131    }
132
133    @Test
134    public void testConvert() {
135        DistanceQuantity result = distance.convert(DistanceUnit.in);
136        assertEquals(100d, result.scalar);
137    }
138
139    @Test
140    public void testShowInUnits() {
141        String result = distance.showInUnits(DistanceUnit.mile, 2);
142        assertEquals("0.062150403977625855 mile", result);
143    }
144
145    @Test
146    public void testToSystemUnit() {
147        assertEquals(distance.toSystemUnit(), distance.to(distance.getUnit().getSystemUnit()));
148    }
149
150    @Test
151    public void testNegate() {
152        assertEquals(distance.negate().getValue(), -distance.getValue().doubleValue());
153    }
154
155    @Test
156    public void testAbsolute() {
157        assertEquals(Quantity.Scale.ABSOLUTE, distance.getScale());
158    }
159}