View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.OTHER;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertTrue;
6   
7   import java.util.Locale;
8   
9   import org.djunits.locale.DefaultLocale;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
17   * <p>
18   * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
19   * initial version Jn 6, 2014 <br>
20   * @author <a href="http://tudelft.nl/pknoppers">Peter Knoppers</a>
21   */
22  public class VolumeUnitTest extends AbstractLinearUnitTest<VolumeUnit>
23  {
24      /**
25       * Set the locale to "en" so we know what texts should be retrieved from the resources.
26       */
27      @SuppressWarnings("static-method")
28      @Before
29      public final void setup()
30      {
31          DefaultLocale.setLocale(new Locale("en"));
32      }
33  
34      /**
35       * Verify the result of some get*Key methods.
36       */
37      @Test
38      public final void keys()
39      {
40          checkKeys(VolumeUnit.CUBIC_METER, "VolumeUnit.cubic_meter", "VolumeUnit.m^3");
41      }
42  
43      /**
44       * Verify conversion factors, English names and abbreviations.
45       */
46      @Test
47      public final void conversions()
48      {
49          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_METER, 1, 0.00000001, "cubic meter", "m^3");
50          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_DECIMETER, 0.001, 0.0000000001, "cubic decimeter", "dm^3");
51          checkUnitRatioNameAndAbbreviation(VolumeUnit.LITER, 0.001, 0.0000000001, "liter", "L");
52          // Check two conversions between non-standard units
53          assertEquals("one CUBIC MILE is about 5451776000 CUBIC YARD", 5451776000.,
54                  getMultiplicationFactorTo(VolumeUnit.CUBIC_MILE, VolumeUnit.CUBIC_YARD), 0.5);
55          assertEquals("one CUBIC YARD is 1.83426465e-10 CUBIC MILE", 1.83426465e-10,
56                  getMultiplicationFactorTo(VolumeUnit.CUBIC_YARD, VolumeUnit.CUBIC_MILE), 0.0000000001);
57          // Check conversion factor to standard unit for all remaining time units
58          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_CENTIMETER, 0.000001, 0.000000000001, "cubic centimeter", "cm^3");
59          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_KILOMETER, 1e9, 1, "cubic kilometer", "km^3");
60          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_MILE, 4.16818183e9, 1000, "cubic mile", "mi^3");
61          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_FOOT, 0.0283168, 0.0000001, "cubic foot", "ft^3");
62          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_INCH, 1.6387e-5, 1e-9, "cubic inch", "in^3");
63          checkUnitRatioNameAndAbbreviation(VolumeUnit.CUBIC_YARD, 0.764554858, 0.0000001, "cubic yard", "yd^3");
64          checkUnitRatioNameAndAbbreviation(VolumeUnit.GALLON_US_FLUID, 0.0037854, 0.0000001, "gallon (US)", "gal(US)");
65          checkUnitRatioNameAndAbbreviation(VolumeUnit.OUNCE_US_FLUID, 0.000029574, 0.000000001, "ounce (fluid US)", "US fl oz");
66          // TODO checkUnitRatioNameAndAbbreviation(VolumeUnit.OUNCE_IMP_FLUID, .00002841306, 0.00000000001,
67          // "horsepower (metric)", "hp(M)");
68          // TODO checkUnitRatioNameAndAbbreviation(VolumeUnit.PINT_US_FLUID, 0.000473176473, 0.0000000000001, "pt(US fl)",
69          // "hp(M)");
70          // TODO checkUnitRatioNameAndAbbreviation(VolumeUnit.PINT_IMP, 735.49875, 0.00001, "horsepower (metric)", "hp(M)");
71          // TODO checkUnitRatioNameAndAbbreviation(VolumeUnit.QUART_US_FLUID, 0.000946353, 0.0000000001, "qt(US fl)", "hp(M)");
72          // TODO checkUnitRatioNameAndAbbreviation(VolumeUnit.QUART_IMP, 0.00113652, 0.000005, "quart (imperial)", "qt (imp)");
73      }
74  
75      /**
76       * Verify that we can create our own power unit.
77       */
78      @Test
79      public final void createVolumeUnit()
80      {
81          VolumeUnit myVU = new VolumeUnit("Barrel", "brl", OTHER, VolumeUnit.LITER, 119.240471);
82          assertTrue("Can create a new VolumeUnit", null != myVU);
83          checkUnitRatioNameAndAbbreviation(myVU, 0.119240471, 0.000001, "Barrel", "brl");
84      }
85  
86  }