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.Volume;
038import javax.measure.test.unit.AreaUnit;
039import javax.measure.test.unit.DistanceUnit;
040import javax.measure.test.unit.VolumeUnit;
041
042import org.junit.jupiter.api.BeforeEach;
043import org.junit.jupiter.api.Test;
044
045/**
046 * @author Werner Keil
047 */
048public class AreaQuantityTest {
049
050    AreaQuantity area;
051    AreaUnit m2;
052
053    @BeforeEach
054    public void setUp() {
055        m2 = AreaUnit.sqmetre;
056        area = new AreaQuantity(100, m2);
057    }
058
059    @Test
060    public void testAreaQuantity() {
061        assertNotNull(area);
062    }
063
064    @Test
065    public void testAdd() {
066        AreaQuantity area2 = new AreaQuantity(50, m2);
067        AreaQuantity result = area.add(area2);
068        assertEquals(150d, result.scalar);
069    }
070
071    @Test
072    public void testSubtract() {
073        AreaQuantity area2 = new AreaQuantity(50, m2);
074        AreaQuantity result = area.subtract(area2);
075        assertEquals(50d, result.scalar);
076    }
077
078    @Test
079    public void testEq() {
080        AreaQuantity area2 = new AreaQuantity(100, m2);
081        assertTrue(area2.eq(area));
082    }
083
084    @Test
085    public void testGt() {
086        AreaQuantity area2 = new AreaQuantity(120, m2);
087        assertTrue(area2.gt(area));
088    }
089
090    @Test
091    public void testLt() {
092        AreaQuantity area2 = new AreaQuantity(20, m2);
093        assertTrue(area2.lt(area));
094    }
095
096    @Test
097    public void testGe() {
098        AreaQuantity area2 = new AreaQuantity(120, m2);
099        assertTrue(area2.ge(area));
100        area2 = new AreaQuantity(100, m2);
101        assertTrue(area2.ge(area));
102    }
103
104    @Test
105    public void testLe() {
106        AreaQuantity area2 = new AreaQuantity(20, m2);
107        assertTrue(area2.le(area));
108        area2 = new AreaQuantity(100, m2);
109        assertTrue(area2.le(area));
110    }
111
112    @Test
113    public void testMultiplyDouble() {
114        AreaQuantity result = area.multiply(3d);
115        assertEquals(300d, result.scalar);
116    }
117
118    @Test
119    public void testDivideDouble() {
120        AreaQuantity result = area.divide(10d);
121        assertEquals(10d, result.scalar);
122    }
123
124    @Test
125    public void testDivideDistanceQuantity() {
126        DistanceQuantity distance = new DistanceQuantity(10, DistanceUnit.m);
127        DistanceQuantity result = area.divide(distance);
128        assertEquals(10d, result.scalar);
129    }
130
131    @Test
132    public void testMultiplyDistanceQuantity() {
133        DistanceQuantity distance = new DistanceQuantity(15, DistanceUnit.m);
134        VolumeQuantity result = area.multiply(distance);
135        assertEquals(VolumeUnit.class, result.getUnit().getClass());
136        assertEquals(VolumeQuantity.class, result.getClass());
137        assertEquals(Volume.class, result.getType());
138        assertEquals(1500d, result.getValue());
139    }
140
141    @Test
142    public void testConvert() {
143        AreaQuantity result = area.convert(AreaUnit.acre);
144        assertEquals(100d, result.scalar);
145    }
146
147    @Test
148    public void testShowInUnits() {
149        String result = area.showInUnits(AreaUnit.hectare, 2);
150        assertEquals("0.01 hectare", result);
151    }
152
153    @Test
154    public void testToSystemUnit() {
155        assertEquals(area.toSystemUnit(), area.to(area.getUnit().getSystemUnit()));
156    }
157
158    @Test
159    public void testNegate() {
160        assertEquals(area.negate().getValue(), -area.getValue().doubleValue());
161    }
162
163    @Test
164    public void testScale() {
165        assertEquals(Quantity.Scale.ABSOLUTE, area.getScale());
166    }
167}