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; 031 032import javax.measure.Quantity; 033import javax.measure.Unit; 034import javax.measure.quantity.Area; 035import javax.measure.quantity.Length; 036import javax.measure.quantity.Speed; 037import javax.measure.quantity.Time; 038import javax.measure.quantity.Volume; 039import javax.measure.test.quantity.DistanceQuantity; 040import javax.measure.test.quantity.TestQuantities; 041import javax.measure.test.quantity.VolumeQuantity; 042 043import org.junit.jupiter.api.Test; 044 045import static org.junit.jupiter.api.Assertions.assertEquals; 046import static org.junit.jupiter.api.Assertions.assertNotNull; 047import static org.junit.jupiter.api.Assertions.assertNull; 048 049import static javax.measure.BinaryPrefix.*; 050import static javax.measure.test.unit.AreaUnit.*; 051import static javax.measure.test.unit.DistanceUnit.*; 052import static javax.measure.test.unit.SpeedUnit.*; 053import static javax.measure.test.unit.TimeUnit.*; 054import static javax.measure.test.unit.VolumeUnit.*; 055 056public class BinaryPrefixTest { 057 @Test 058 public void testKibi() { 059 final Quantity<Length> m1 = new DistanceQuantity(1, m); 060 final Unit<Length> km = KIBI(m); 061 assertEquals("Ki", KIBI.getSymbol()); 062 assertEquals("KIBI", KIBI.getName()); 063 assertEquals(1d, m1.getValue()); 064 assertEquals("m * 1024.0", km.toString()); 065 if (km instanceof TestUnit) { 066 @SuppressWarnings("rawtypes") 067 TestUnit testKm = (TestUnit) km; 068 assertEquals(1024d, testKm.getMultFactor()); 069 } 070 } 071 072 @Test 073 public void testMebi() { 074 assertEquals("Mi", MEBI.getSymbol()); 075 assertEquals("MEBI", MEBI.getName()); 076 Quantity<Time> t1 = TestQuantities.getQuantity(1.0, MEBI(s)); 077 assertNotNull(t1); 078 assertEquals("1.0 s * 1048576.0", t1.toString()); 079 } 080 081 @Test 082 public void testExbi() { 083 Quantity<Volume> v1 = new VolumeQuantity(1.0, litre); 084 assertEquals(1d, v1.getValue()); 085 assertEquals("litre * 0.001", v1.getUnit().toString()); 086 087 Quantity<Volume> v2 = v1.to(EXBI(litre)); 088 assertNull(v2); // TODO temporary workaround 089 } 090 091 @Test 092 public void testGibi() { 093 assertEquals("Gi", GIBI.getSymbol()); 094 Quantity<Speed> s1 = TestQuantities.getQuantity(1.0, GIBI(mph)); 095 assertNotNull(s1); 096 assertEquals("1.0 2779789807058944", s1.toString()); 097 } 098 099 @Test 100 public void testTebi() { 101 assertEquals("Ti", TEBI.getSymbol()); 102 Quantity<Volume> v1 = TestQuantities.getQuantity(1.0, TEBI(litre)); 103 assertNotNull(v1); 104 assertEquals("1.0 1099511627.776", v1.toString()); 105 } 106 107 @Test 108 public void testPebi() { 109 assertEquals("Pi", PEBI.getSymbol()); 110 Quantity<Area> a1 = TestQuantities.getQuantity(1.0, PEBI(acre)); 111 assertNotNull(a1); 112 assertEquals("1.0 4556516922992099300", a1.toString()); 113 } 114 115 @Test 116 public void testYobi() { 117 assertEquals("Yi", YOBI.getSymbol()); 118 Quantity<Area> a1 = TestQuantities.getQuantity(1.0, YOBI(acre)); 119 assertNotNull(a1); 120 assertEquals("1.0 4892522791980404000000000000", a1.toString()); 121 } 122 123 @Test 124 public void testZebi() { 125 assertEquals("Zi", ZEBI.getSymbol()); 126 Quantity<Area> a1 = TestQuantities.getQuantity(1.0, ZEBI(acre)); 127 assertNotNull(a1); 128 assertEquals("1.0 4777854289043363500000000", a1.toString()); 129 } 130}