View Javadoc
1   package org.djunits.unit;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.util.Locale;
7   
8   import org.djunits.unit.unitsystem.UnitSystem;
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
16   * <p>
17   * @author <a href="https://tudelft.nl/pknoppers">Peter Knoppers</a>
18   */
19  public class VolumeUnitTest extends AbstractLinearUnitTest<VolumeUnit>
20  {
21      /**
22       * Set the locale to "en" so we know what texts should be retrieved from the resources.
23       */
24      @Before
25      public final void setup()
26      {
27          Locale.setDefault(new Locale("en"));
28      }
29  
30      /**
31       * Verify conversion factors, English names and abbreviations.
32       */
33      @Test
34      public final void conversions()
35      {
36          assertEquals("m3", VolumeUnit.SI.getQuantity().getSiDimensions().toString(true, false));
37          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_METER, 1, 0.00000001, "cubic meter", "m^3");
38          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_DECIMETER, 0.001, 0.0000000001, "cubic decimeter", "dm^3");
39          checkUnitRatioNameAndAbbreviation(VolumeUnit.LITER, 0.001, 0.0000000001, "liter", "L");
40          // Check two conversions between non-standard units
41          assertEquals("one CUBIC MILE is about 5451776000 CUBIC YARD", 5451776000.,
42                  getMultiplicationFactorTo(VolumeUnit.CUBIC_MILE, VolumeUnit.CUBIC_YARD), 0.5);
43          assertEquals("one CUBIC YARD is 1.83426465e-10 CUBIC MILE", 1.83426465e-10,
44                  getMultiplicationFactorTo(VolumeUnit.CUBIC_YARD, VolumeUnit.CUBIC_MILE), 0.0000000001);
45          // Check conversion factor to standard unit for all remaining time units
46          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_CENTIMETER, 0.000001, 0.000000000001, "cubic centimeter", "cm^3");
47          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_KILOMETER, 1e9, 1, "cubic kilometer", "km^3");
48          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_MILE, 4.16818183e9, 1000, "cubic mile", "mi^3");
49          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_FOOT, 0.0283168, 0.0000001, "cubic foot", "ft^3");
50          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_INCH, 1.6387e-5, 1e-9, "cubic inch", "in^3");
51          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_YARD, 0.764554858, 0.0000001, "cubic yard", "yd^3");
52          checkUnitRatioNameAndAbbreviation(VolumeUnit.GALLON_US, 0.0037854, 0.0000001, "gallon (US)", "gal(US)");
53          checkUnitRatioNameAndAbbreviation(VolumeUnit.FLUID_OUNCE_US, 0.000029574, 0.000000001, "fluid ounce (US)", "fl.oz(US)");
54          checkUnitRatioNameAndAbbreviation(VolumeUnit.FLUID_OUNCE_IMP, .00002841306, 0.00000000001, "fluid ounce (imp)",
55                  "fl.oz(imp)");
56          checkUnitRatioNameAndAbbreviation(VolumeUnit.PINT_US, 0.000473176473, 0.0000000000001, "pint (US)", "pt(US)");
57          checkUnitRatioNameAndAbbreviation(VolumeUnit.PINT_IMP, 0.5 * 0.00113652, 0.00001, "pint (imp)", "pt(imp)");
58          checkUnitRatioNameAndAbbreviation(VolumeUnit.QUART_US, 0.000946353, 0.0000000001, "quart (US)", "qt(US)");
59          checkUnitRatioNameAndAbbreviation(VolumeUnit.QUART_IMP, 0.00113652, 0.000005, "quart (imp)", "qt(imp)");
60      }
61  
62      /**
63       * Verify that we can create our own power unit.
64       */
65      @Test
66      public final void createVolumeUnit()
67      {
68          VolumeUnit myVU = VolumeUnit.LITER.deriveLinear(119.240471, "brl", "Barrel", UnitSystem.OTHER);
69          assertTrue("Can create a new VolumeUnit", null != myVU);
70          checkUnitRatioNameAndAbbreviation(myVU, 0.119240471, 0.000001, "Barrel", "brl");
71          VolumeUnit.BASE.unregister(myVU);
72      }
73  
74  }