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