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.Mass;
037import javax.measure.quantity.Speed;
038import javax.measure.quantity.Time;
039import javax.measure.quantity.Volume;
040import javax.measure.test.quantity.DistanceQuantity;
041import javax.measure.test.quantity.TestQuantities;
042import javax.measure.test.quantity.VolumeQuantity;
043
044import org.junit.jupiter.api.Test;
045
046import static org.junit.jupiter.api.Assertions.assertEquals;
047import static org.junit.jupiter.api.Assertions.assertNull;
048
049import static javax.measure.MetricPrefix.*;
050import static javax.measure.test.unit.AreaUnit.*;
051import static javax.measure.test.unit.DistanceUnit.*;
052import static javax.measure.test.unit.MassUnit.*;
053import static javax.measure.test.unit.SpeedUnit.*;
054import static javax.measure.test.unit.TimeUnit.*;
055import static javax.measure.test.unit.VolumeUnit.*;
056
057/**
058 * JUnit tests for the 24 prefixes used in the metric system (decimal multiples and submultiples of units).
059 * 
060 * @author <a href="mailto:werner@units.tech">Werner Keil</a>
061 * @version 2.2, May 9, 2023
062 * @since 2.0
063 */
064public class MetricPrefixTest {
065    @Test
066    public void testAtto() {
067        assertEquals("a", ATTO.getSymbol());
068        assertEquals("ATTO", ATTO.getName());
069        Quantity<Time> t1 = TestQuantities.getQuantity(1.0, ATTO(min));
070        assertEquals("min * 0.000000000000000060", t1.getUnit().toString());
071    }
072
073    @Test
074    public void testCenti() {
075        assertEquals("c", CENTI.getSymbol());
076        assertEquals("CENTI", CENTI.getName());
077        Quantity<Length> l1 = TestQuantities.getQuantity(1.0, CENTI(m));
078        assertEquals("m * 0.01", l1.getUnit().toString());
079    }
080
081    @Test
082    public void testDeci() {
083        Quantity<Volume> m1 = new VolumeQuantity(1.0, litre);
084        assertEquals(1d, m1.getValue());
085        assertEquals("litre * 0.001", m1.getUnit().toString());
086
087        Quantity<Volume> m2 = m1.to(DECI(litre));
088        assertNull(m2); // TODO temporary workaround
089    }
090
091    @Test
092    public void testDeca() {
093        assertEquals("da", DECA.getSymbol());
094        Quantity<Volume> v1 = TestQuantities.getQuantity(1.0, DECA(litre));
095        assertEquals("0.01", v1.getUnit().toString());
096    }
097    
098    @Test
099    public void testDeka() {
100        assertEquals("da", DECA.getSymbol());
101        Quantity<Volume> v1 = TestQuantities.getQuantity(1.0, DEKA(litre));
102        assertEquals("0.01", v1.getUnit().toString());
103    }
104
105    @Test
106    public void testExa() {
107        assertEquals("E", EXA.getSymbol());
108        Quantity<Length> l1 = TestQuantities.getQuantity(1.0, EXA(m));
109        assertEquals("m * 1000000000000000000", l1.getUnit().toString());
110    }
111
112    @Test
113    public void testFemto() {
114        assertEquals("f", FEMTO.getSymbol());
115        Quantity<Time> t1 = TestQuantities.getQuantity(1.0, FEMTO(s));
116        assertEquals("s * 0.0000000000000010", t1.getUnit().toString());
117    }
118
119    @Test
120    public void testGiga() {
121        assertEquals("G", GIGA.getSymbol());
122        Quantity<Area> a1 = TestQuantities.getQuantity(1.0, GIGA(acre));
123        assertEquals("4047000000000", a1.getUnit().toString());
124    }
125
126    @Test
127    public void testHecto() {
128        assertEquals("h", HECTO.getSymbol());
129        Quantity<Volume> v1 = TestQuantities.getQuantity(1.0, HECTO(litre));
130        assertEquals("0.1", v1.getUnit().toString());
131    }
132
133    @Test
134    public void testKilo() {
135        final Quantity<Length> m1 = new DistanceQuantity(1, m);
136        final Unit<Length> km = KILO(m);
137        assertEquals("k", KILO.getSymbol());
138        assertEquals(1d, m1.getValue());
139        assertEquals("m * 1000.0", km.toString());
140        if (km instanceof TestUnit) {
141            @SuppressWarnings("rawtypes")
142            final TestUnit testKm = (TestUnit) km;
143            assertEquals(1000d, testKm.getMultFactor());
144        }
145    }
146
147    @Test
148    public void testMega() {
149        assertEquals("M", MEGA.getSymbol());
150        Quantity<Volume> v1 = TestQuantities.getQuantity(1.0, MEGA(litre));
151        assertEquals("1000.0", v1.getUnit().toString());
152    }
153
154    @Test
155    public void testMilli() {
156        Quantity<Volume> m1 = TestQuantities.getQuantity(10, MILLI(litre));
157        assertEquals(10d, m1.getValue());
158        assertEquals("0.0000010", m1.getUnit().toString());
159    }
160
161    @Test
162    public void testMicro() {
163        assertEquals("\\u00b5", toUnicode(MICRO.getSymbol().charAt(0)));
164        assertEquals("\u00b5", MICRO.getSymbol());
165        Quantity<Length> m1 = TestQuantities.getQuantity(1.0, m);
166        assertEquals(1d, m1.getValue());
167        assertEquals("m", m1.getUnit().getSymbol());
168
169        Quantity<Length> m2 = m1.to(MICRO(m));
170        assertNull(m2); // the unit model in the test implementation is only partial, hence null here
171    }
172
173    @Test
174    public void testNano() {
175        Quantity<Length> m1 = TestQuantities.getQuantity(1.0, m);
176        assertEquals(1d, m1.getValue());
177        assertEquals("m", m1.getUnit().getSymbol());
178
179        Quantity<Length> m2 = m1.to(NANO(m));
180        assertNull(m2); // the unit model in the test implementation is only partial, hence null here
181    }
182
183    @Test
184    public void testPeta() {
185        assertEquals("P", PETA.getSymbol());
186        Quantity<Speed> s1 = TestQuantities.getQuantity(10, PETA(kmh));
187        assertEquals(10d, s1.getValue());
188        assertEquals("1000000000000000", s1.getUnit().toString());
189    }
190
191    @Test
192    public void testPico() {
193        assertEquals("p", PICO.getSymbol());
194        Quantity<Volume> v1 = TestQuantities.getQuantity(10, PICO(litre));
195        assertEquals(10d, v1.getValue());
196        assertEquals("0.0000000000000010", v1.getUnit().toString());
197    }
198
199    @Test
200    public void testTera() {
201        assertEquals("T", TERA.getSymbol());
202        Quantity<Length> l1 = TestQuantities.getQuantity(10, TERA(m));
203        assertEquals(10d, l1.getValue());
204        assertEquals("m * 1000000000000", l1.getUnit().toString());
205    }
206
207    @Test
208    public void testYocto() {
209        assertEquals("y", YOCTO.getSymbol());
210        Quantity<Volume> v1 = TestQuantities.getQuantity(10, YOCTO(litre));
211        assertEquals(10d, v1.getValue());
212        assertEquals("0.000000000000000000000000001", v1.getUnit().toString());        
213    }
214
215    @Test
216    public void testYotta() {
217        assertEquals("Y", YOTTA.getSymbol());
218        Quantity<Area> a1 = TestQuantities.getQuantity(10, YOTTA(sqmetre));
219        assertEquals(10d, a1.getValue());
220        assertEquals("1000000000000000000000000", a1.getUnit().toString());
221    }
222
223    @Test
224    public void testZepto() {
225        assertEquals("z", ZEPTO.getSymbol());
226        Quantity<Time> t1 = TestQuantities.getQuantity(10, ZEPTO(s));
227        assertEquals(10d, t1.getValue());
228        assertEquals("s * 0.000000000000000000001", t1.getUnit().toString());
229    }
230
231    @Test
232    public void testZetta() {
233        assertEquals("Z", ZETTA.getSymbol());
234        Quantity<Length> l1 = TestQuantities.getQuantity(10, ZETTA(m));
235        assertEquals(10d, l1.getValue());
236        assertEquals("m * 1000000000000000000000", l1.getUnit().toString());
237    }
238
239    @Test
240    public void testRonna() {
241        assertEquals("R", RONNA.getSymbol());
242        Quantity<Length> l1 = TestQuantities.getQuantity(10, RONNA(m));
243        assertEquals(10d, l1.getValue());
244        assertEquals("m * 1000000000000000000000000000", l1.getUnit().toString());
245    }
246
247    @Test
248    public void testRonto() {
249        assertEquals("r", RONTO.getSymbol());
250        Quantity<Length> l1 = TestQuantities.getQuantity(1, RONTO(m));
251        assertEquals(1d, l1.getValue());
252        assertEquals("m * 0.000000000000000000000", l1.getUnit().toString());
253    }
254    
255    @Test
256    public void testQuetta() {
257        assertEquals("Q", QUETTA.getSymbol());
258        Quantity<Mass> m1 = TestQuantities.getQuantity(2, QUETTA(g));
259        assertEquals(2d, m1.getValue());
260        assertEquals("g * 1000000000000000000000000000", m1.getUnit().toString());
261    }
262
263    @Test
264    public void testQuecto() {
265        assertEquals("q", QUECTO.getSymbol());
266        Quantity<Length> l1 = TestQuantities.getQuantity(1, QUECTO(m));
267        assertEquals(1d, l1.getValue());
268        assertEquals("m * 0.000000000000000000000", l1.getUnit().toString());
269    }
270    
271    private static String toUnicode(char ch) {
272        return String.format("\\u%04x", (int) ch);
273    }
274}