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.Temperature; 038import javax.measure.test.unit.TemperatureUnit; 039 040import org.junit.jupiter.api.BeforeEach; 041import org.junit.jupiter.api.Test; 042 043/** 044 * @author Werner Keil 045 */ 046public class TemperatureQuantityTest { 047 048 private TemperatureQuantity temp; 049 private TemperatureUnit k; 050 051 @BeforeEach 052 public void setUp() { 053 k = TemperatureUnit.KELVIN; 054 temp = new TemperatureQuantity(100, k); 055 } 056 057 @Test 058 public void testQuantity() { 059 assertNotNull(temp); 060 } 061 062 @Test 063 public void testAdd() { 064 TemperatureQuantity temp2 = new TemperatureQuantity(50, k); 065 TemperatureQuantity result = temp.add(temp2); 066 assertEquals(150d, result.scalar); 067 } 068 069 @Test 070 public void testSubtract() { 071 TemperatureQuantity temp2 = new TemperatureQuantity(50, k); 072 TemperatureQuantity result = temp.subtract(temp2); 073 assertEquals(50d, result.scalar); 074 } 075 076 @Test 077 public void testEq() { 078 TemperatureQuantity temp2 = new TemperatureQuantity(100, k); 079 assertTrue(temp2.eq(temp)); 080 } 081 082 @Test 083 public void testGt() { 084 TemperatureQuantity temp2 = new TemperatureQuantity(120, k); 085 assertTrue(temp2.gt(temp)); 086 } 087 088 @Test 089 public void testLt() { 090 TemperatureQuantity temp2 = new TemperatureQuantity(20, k); 091 assertTrue(temp2.lt(temp)); 092 } 093 094 @Test 095 public void testGe() { 096 TemperatureQuantity temp2 = new TemperatureQuantity(120, k); 097 assertTrue(temp2.ge(temp)); 098 temp2 = new TemperatureQuantity(100, k); 099 assertTrue(temp2.ge(temp)); 100 } 101 102 @Test 103 public void testLe() { 104 TemperatureQuantity temp2 = new TemperatureQuantity(20, k); 105 assertTrue(temp2.le(temp)); 106 temp2 = new TemperatureQuantity(100, k); 107 assertTrue(temp2.le(temp)); 108 } 109 110 @Test 111 public void testMultiplyDouble() { 112 Quantity<Temperature> result = temp.multiply(3d); 113 assertEquals(300d, result.getValue()); 114 } 115 116 @Test 117 public void testDivideDouble() { 118 TemperatureQuantity result = temp.divide(10d); 119 assertEquals(10d, result.scalar); 120 } 121 122 @Test 123 public void testConvert() { 124 TemperatureQuantity result = temp.convert(TemperatureUnit.KELVIN); 125 assertEquals(100d, result.scalar); 126 } 127 128 @Test 129 public void testToSystemUnit() { 130 assertEquals(temp.toSystemUnit(), temp.to(temp.getUnit().getSystemUnit())); 131 } 132 133 @Test 134 public void testNegate() { 135 assertEquals(temp.negate().getValue(), -temp.getValue().doubleValue()); 136 } 137 138 @Test 139 public void testScale() { 140 assertEquals(Quantity.Scale.ABSOLUTE, temp.getScale()); 141 } 142 143 @Test 144 public void testLevelCelsius() { 145 TemperatureQuantity temp2 = new TemperatureQuantity(20, TemperatureUnit.CELSIUS); 146 assertEquals(Quantity.Scale.RELATIVE, temp2.getScale()); 147 } 148 149 @Test 150 public void testLevelFahrenheit() { 151 TemperatureQuantity temp2 = new TemperatureQuantity(60, TemperatureUnit.FAHRENHEIT); 152 assertEquals(Quantity.Scale.RELATIVE, temp2.getScale()); 153 } 154 155 @Test 156 public void testAbsolute() { 157 assertEquals(Quantity.Scale.ABSOLUTE, temp.getScale()); 158 } 159}