View Javadoc
1   package org.djunits.unit.quantity;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNotEquals;
5   import static org.junit.jupiter.api.Assertions.assertNotNull;
6   import static org.junit.jupiter.api.Assertions.assertNull;
7   import static org.junit.jupiter.api.Assertions.fail;
8   
9   import org.djunits.quantity.Quantities;
10  import org.djunits.quantity.Quantity;
11  import org.djunits.unit.Unit;
12  import org.djunits.unit.scale.IdentityScale;
13  import org.djunits.unit.scale.LinearScale;
14  import org.djunits.unit.si.SIDimensions;
15  import org.djunits.unit.si.SIPrefixes;
16  import org.djunits.unit.unitsystem.UnitSystem;
17  import org.djunits.unit.util.UnitException;
18  import org.djunits.unit.util.UnitRuntimeException;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * QuantityTest.java.
23   * <p>
24   * Copyright (c) 2019-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>
26   * </p>
27   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
28   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
29   */
30  public class QuantityTest
31  {
32      /**
33       * Test the Quantity functions.
34       * @throws UnitException on error
35       */
36      @SuppressWarnings("rawtypes")
37      @Test
38      public void testQuantity() throws UnitException
39      {
40  
41          assertEquals(QUnit.BASE, QUnit.BASE);
42          assertNotEquals(QUnit.BASE, null);
43          assertNotEquals(QUnit.BASE, new Object());
44          Quantity<QUnit> quantity1 = new Quantity<>("kgm4/s5A3", SIDimensions.of("kgm4/s5A3"));
45          Quantity<QUnit> quantity2 = new Quantity<>("Test2", new SIDimensions(0, 0, 1, 4, -5, -3, 0, 0, 0));
46          Quantity<QUnit> quantity3 = new Quantity<>("Test3", new byte[] {0, 0, 1, 4, -5, -3, 0, 0, 0});
47          assertEquals(quantity1, quantity2);
48          assertEquals(quantity1, quantity3);
49          assertNotEquals(quantity1, QUnit.BASE); // QUnit has a standard base; quantity1 not (yet)
50          assertNotEquals(quantity1.hashCode(), QUnit.BASE.hashCode()); // QUnit has a standard base; quantity1 not (yet)
51          assertEquals(new SIDimensions(0, 0, 1, 4, -5, -3, 0, 0, 0), quantity1.getSiDimensions());
52          Quantity<QUnit> quantity4 = new Quantity<>("kg2m4/s5A3", SIDimensions.of("kg2m4/s5A3"));
53          assertNotEquals(quantity1, quantity4);
54  
55          try
56          {
57              SIDimensions si = null;
58              Quantity<QUnit> quantity5 = new Quantity<>("Test4", si);
59              fail("Should not have been able to create " + quantity5 + " with argument null");
60          }
61          catch (NullPointerException | UnitRuntimeException e)
62          {
63              // ok
64          }
65  
66          try
67          {
68              Quantity<QUnit> quantity5 = new Quantity<>("", SIDimensions.of("kg2m4/s5A3"));
69              fail("Should not have been able to create " + quantity5 + " with empty name");
70          }
71          catch (NullPointerException | UnitRuntimeException e)
72          {
73              // ok
74          }
75  
76          try
77          {
78              Quantity<QUnit> quantity5 = new Quantity<>(null, SIDimensions.of("kg2m4/s5A3"));
79              fail("Should not have been able to create " + quantity5 + " with null name");
80          }
81          catch (NullPointerException | UnitRuntimeException e)
82          {
83              // ok
84          }
85  
86          assertEquals(QUnit.SI, QUnit.BASE.of("Q"));
87          assertEquals(QUnit.SI, QUnit.BASE.getStandardUnit());
88          assertEquals(QUnit.SI, QUnit.BASE.getUnitByAbbreviation("Q"));
89          assertEquals(QUnit.SI, QUnit.BASE.getUnitById("Q"));
90  
91          assertEquals(QUnit.QQQ, QUnit.BASE.of("QQQ"));
92          assertNotEquals(QUnit.QQQ, QUnit.BASE.getStandardUnit());
93          assertEquals(QUnit.QQQ, QUnit.BASE.getUnitByAbbreviation("QQQ"));
94          assertEquals(QUnit.QQQ, QUnit.BASE.getUnitById("QQQ"));
95  
96          assertEquals(QUnit.KILOQUEEZ, QUnit.BASE.of("kQ"));
97          assertNotNull(QUnit.BASE.of("yQ"));
98          assertNotNull(QUnit.BASE.of("YQ"));
99          assertNotNull(QUnit.BASE.of("muQ"));
100         assertNotNull(QUnit.BASE.of("MQ"));
101 
102         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("kgm4/s5A3"));
103         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("kg.m4/s5.A3"));
104         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("kg.m^4/s^5.A^3"));
105         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("kgm4s-5A-3"));
106         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("kg.m4.s-5.A-3"));
107         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("kg.m^4.s^-5.A^-3"));
108         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("m4kg/A3s5"));
109         assertEquals(QUnit.QUEEZ, QUnit.BASE.of("s^-5.A^-3.kg.m^4"));
110         assertNull(QUnit.BASE.of("kgm4/s5A4"));
111         assertNull(QUnit.BASE.of("abcdef"));
112         assertNull(QUnit.BASE.of("s^-5.A^-3.kg^-1.m^4"));
113 
114         assertEquals(QUnit.BASE.of("GQ"), QUnit.BASE.getUnitById("GQ"));
115         assertEquals(QUnit.BASE.of("GQ"), QUnit.BASE.getUnitsById().get("GQ"));
116         assertEquals(QUnit.BASE.of("GQ"), QUnit.BASE.getUnitByAbbreviation("GQ"));
117         assertEquals(QUnit.BASE.of("GQ"), QUnit.BASE.getUnitsByAbbreviation().get("GQ"));
118 
119         try
120         {
121             SIDimensions.of("m/m/m/m");
122             fail("Quantity.of string with multiple slashes should have thrown a UnitRuntimeException");
123         }
124         catch (UnitException ut)
125         {
126             // Ignore expected exception
127         }
128         try
129         {
130             new Quantity("m/m/m/m", "m/m/m/m");
131             fail("constructing Quantity from string with multiple slashes should have thrown a UnitRuntimeException");
132         }
133         catch (UnitRuntimeException urt)
134         {
135             // Ignore expected exception
136         }
137 
138         QUnit.BASE.unregister(QUnit.QQQ);
139         assertNull(QUnit.BASE.of("QQQ"));
140         Quantities.INSTANCE.unregister(quantity1); // probably not registered
141         Quantities.INSTANCE.unregister(quantity2); // probably not registered
142         Quantities.INSTANCE.unregister(quantity3); // probably not registered
143         Quantities.INSTANCE.unregister(quantity4); // probably not registered
144         Quantities.INSTANCE.unregister(QUnit.BASE); // should unregister
145     }
146 
147     /** */
148     protected static class QUnit extends Unit<QUnit>
149     {
150         /** */
151         private static final long serialVersionUID = 1L;
152 
153         /** */
154         public static final Quantity<QUnit> BASE = new Quantity<>("kgm4/s5A3", "kgm4/s5A3");
155 
156         /** */
157         public static final QUnit SI =
158                 new QUnit().build(new Unit.Builder<QUnit>().setId("Q").setName("queez").setScale(IdentityScale.SCALE)
159                         .setSiPrefixes(SIPrefixes.UNIT, 1.0).setQuantity(BASE).setUnitSystem(UnitSystem.OTHER));
160 
161         /** */
162         public static final QUnit QUEEZ = SI;
163 
164         /** */
165         public static final QUnit KILOQUEEZ =
166                 new QUnit().build(new Unit.Builder<QUnit>().setId("kQ").setName("kiloqueez").setScale(new LinearScale(1000.0))
167                         .setSiPrefixes(SIPrefixes.NONE, 1.0).setQuantity(BASE).setUnitSystem(UnitSystem.OTHER));
168 
169         /** */
170         public static final QUnit MEGAQUEEZ = new QUnit()
171                 .build(new Unit.Builder<QUnit>().setId("MQ").setName("megaqueez").setScale(new LinearScale(1_000_000.0))
172                         .setSiPrefixes(SIPrefixes.NONE, 1.0).setQuantity(BASE).setUnitSystem(UnitSystem.OTHER));
173 
174         /** */
175         public static final QUnit QQQ =
176                 new QUnit().build(new Unit.Builder<QUnit>().setId("QQQ").setName("qqqeezz").setScale(new LinearScale(86400.0))
177                         .setSiPrefixes(SIPrefixes.NONE, 1.0).setQuantity(BASE).setUnitSystem(UnitSystem.OTHER));
178     }
179 
180 }