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.locale.DefaultLocale;
9   import org.djunits.unit.unitsystem.UnitSystem;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
17   * <p>
18   * @author <a href="https://tudelft.nl/pknoppers">Peter Knoppers</a>
19   */
20  public class TimeUnitTest extends AbstractOffsetUnitTest<TimeUnit>
21  {
22      /**
23       * Set the locale to "en" so we know what texts should be retrieved from the resources.
24       */
25      @Before
26      public final void setup()
27      {
28          DefaultLocale.setLocale(new Locale("en"));
29      }
30  
31      /**
32       * Verify conversion factors, English names and abbreviations.
33       */
34      @Test
35      public final void conversions()
36      {
37          assertEquals("s", TimeUnit.DEFAULT.getQuantity().getSiDimensions().toString(true, false));
38          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_SECOND, 1, 0.0, 0.00000001, "second", "s");
39          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_HOUR, 3600, 0.0, 0.0005, "hour", "h");
40          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_DAY, 86400, 0.0, 0.001, "day", "day");
41          // Check two conversions between non-standard units
42          assertEquals("one DAY is 24 HOUR", 24, getMultiplicationFactorTo(TimeUnit.BASE_DAY, TimeUnit.BASE_HOUR), 0.0001);
43          assertEquals("one HOUR is about 0.0417 DAY", 0.0417, getMultiplicationFactorTo(TimeUnit.BASE_HOUR, TimeUnit.BASE_DAY),
44                  0.0001);
45          // Check conversion factor to standard unit for all remaining time units
46          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_MILLISECOND, 0.001, 0.0, 0.00000001, "millisecond", "ms");
47          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_MINUTE, 60, 0.0, 0.000001, "minute", "min");
48          checkUnitRatioOffsetNameAndAbbreviation(TimeUnit.BASE_WEEK, 7 * 86400, 0.0, 0.1, "week", "wk");
49  
50          // TODO: shifted units
51      }
52  
53      /**
54       * Verify that we can create our own duration unit.
55       */
56      @Test
57      public final void createTimeUnit()
58      {
59          TimeUnit myTU = TimeUnit.BASE_SECOND.deriveLinearOffset(14.0 * 86400, 0.0, DurationUnit.SECOND, "fn", "Fortnight",
60                  UnitSystem.OTHER);
61          assertTrue("Can create a new TimeUnit", null != myTU);
62          checkUnitRatioOffsetNameAndAbbreviation(myTU, 14 * 86400, 0.0, 1, "Fortnight", "fn");
63          TimeUnit.BASE.unregister(myTU);
64      }
65  
66      /**
67       * Verify relative base unit.
68       */
69      @Test
70      public final void testRelative()
71      {
72          assertEquals(DurationUnit.BASE, TimeUnit.DEFAULT.getRelativeQuantity());
73      }
74  
75  }