View Javadoc
1   package org.djunits.value.vfloat.vector;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertSame;
5   
6   import org.djunits.unit.AccelerationUnit;
7   import org.djunits.unit.AngleSolidUnit;
8   import org.djunits.unit.AngleUnit;
9   import org.djunits.unit.AreaUnit;
10  import org.djunits.unit.DimensionlessUnit;
11  import org.djunits.unit.DirectionUnit;
12  import org.djunits.unit.EnergyUnit;
13  import org.djunits.unit.LengthUnit;
14  import org.djunits.unit.MoneyPerAreaUnit;
15  import org.djunits.unit.MoneyPerEnergyUnit;
16  import org.djunits.unit.MoneyUnit;
17  import org.djunits.unit.Unit;
18  import org.djunits.value.StorageType;
19  import org.djunits.value.ValueException;
20  import org.junit.Test;
21  
22  /**
23   * Test the instantiation utility functions.
24   * <p>
25   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
27   * </p>
28   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
29   * initial version Aug 11, 2018 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
32   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
33   */
34  public class FloatVectorUtilTest
35  {
36      /**
37       * Test the instantiation utility function of classes.
38       * @throws ValueException on instantiation error
39       */
40      @Test
41      public final void instantiateTest() throws ValueException
42      {
43          float[] a12 = new float[] { 1.0f, 2.0f };
44          float[] a1020 = new float[] { 10.0f, 20.0f };
45          float[] a12h = new float[] { 100.0f, 200.0f };
46          float[] a12k = new float[] { 1000.0f, 2000.0f };
47          float[] a12k10 = new float[] { 10000.0f, 20000.0f };
48  
49          FloatDimensionlessVector dimensionless = FloatVectorUtil.instantiate(a1020, DimensionlessUnit.SI, StorageType.DENSE);
50          assertEquals("Dimensionless", 10.0f, dimensionless.getSI(0), 0.001d);
51          dimensionless = FloatVectorUtil.instantiateSI(a1020, DimensionlessUnit.SI, StorageType.DENSE);
52          assertEquals("Dimensionless", 10.0f, dimensionless.getSI(0), 0.001d);
53          assertSame("Dimensionless", DimensionlessUnit.SI, dimensionless.getUnit());
54  
55          FloatAccelerationVector acceleration = FloatVectorUtil.instantiate(a1020, AccelerationUnit.SI, StorageType.DENSE);
56          assertEquals("Acceleration", 10.0f, acceleration.getSI(0), 0.001d);
57          acceleration =
58                  FloatVectorUtil.instantiate(new float[] { 12960.0f, 10.0f }, AccelerationUnit.KM_PER_HOUR_2, StorageType.DENSE);
59          assertEquals("Acceleration", 1.0f, acceleration.getSI(0), 0.001d);
60          acceleration = FloatVectorUtil.instantiateSI(a1020, AccelerationUnit.KM_PER_HOUR_2, StorageType.DENSE);
61          assertEquals("Acceleration", 10.0f, acceleration.getSI(0), 0.001d);
62          assertSame("Acceleration", AccelerationUnit.KM_PER_HOUR_2, acceleration.getUnit());
63  
64          FloatAngleSolidVector angleSolid = FloatVectorUtil.instantiate(a1020, AngleSolidUnit.SI, StorageType.DENSE);
65          assertEquals("AngleSolid", 10.0f, angleSolid.getSI(0), 0.001d);
66          angleSolid = FloatVectorUtil.instantiate(a12, AngleSolidUnit.SQUARE_DEGREE, StorageType.DENSE);
67          assertEquals("AngleSolid", (Math.PI / 180.0) * (Math.PI / 180.0), angleSolid.getSI(0), 0.001d);
68          angleSolid = FloatVectorUtil.instantiateSI(a1020, AngleSolidUnit.SQUARE_DEGREE, StorageType.DENSE);
69          assertEquals("AngleSolid", 10.0f, angleSolid.getSI(0), 0.001d);
70          assertSame("AngleSolid", AngleSolidUnit.SQUARE_DEGREE, angleSolid.getUnit());
71  
72          FloatAngleVector angle = FloatVectorUtil.instantiate(a1020, AngleUnit.SI, StorageType.DENSE);
73          assertEquals("Angle", 10.0f, angle.getSI(0), 0.001d);
74          angle = FloatVectorUtil.instantiate(a12, AngleUnit.DEGREE, StorageType.DENSE);
75          assertEquals("Angle", Math.PI / 180.0f, angle.getSI(0), 0.001d);
76          angle = FloatVectorUtil.instantiateSI(a1020, AngleUnit.DEGREE, StorageType.DENSE);
77          assertEquals("Angle", 10.0f, angle.getSI(0), 0.001d);
78          assertSame("Angle", AngleUnit.DEGREE, angle.getUnit());
79  
80          FloatDirectionVector direction = FloatVectorUtil.instantiate(a1020, DirectionUnit.BASE, StorageType.DENSE);
81          assertEquals("Direction", 10.0f, direction.getSI(0), 0.001d);
82          direction = FloatVectorUtil.instantiate(a12, DirectionUnit.EAST_DEGREE, StorageType.DENSE);
83          assertEquals("Direction", Math.PI / 180.0f, direction.getSI(0), 0.001d);
84          direction = FloatVectorUtil.instantiateSI(a1020, DirectionUnit.EAST_DEGREE, StorageType.DENSE);
85          assertEquals("Direction", 10.0f, direction.getSI(0), 0.001d);
86          assertSame("Direction", DirectionUnit.EAST_DEGREE, direction.getUnit());
87  
88          FloatAreaVector area = FloatVectorUtil.instantiate(a1020, AreaUnit.SI, StorageType.DENSE);
89          assertEquals("Area", 10.0f, area.getSI(0), 0.001d);
90          area = FloatVectorUtil.instantiate(a12, AreaUnit.HECTARE, StorageType.DENSE);
91          assertEquals("Area", 10000.0f, area.getSI(0), 0.001d);
92          area = FloatVectorUtil.instantiateSI(a1020, AreaUnit.HECTARE, StorageType.DENSE);
93          assertEquals("Area", 10.0f, area.getSI(0), 0.001d);
94          assertSame("Area", AreaUnit.HECTARE, area.getUnit());
95  
96          // TODO: other base units
97  
98          FloatMoneyVector money = FloatVectorUtil.instantiate(a1020, MoneyUnit.USD, StorageType.DENSE);
99          assertEquals("Money", 10.0f, money.getSI(0), 0.001d);
100         money = FloatVectorUtil.instantiate(a12h, MoneyUnit.EUR, StorageType.DENSE);
101         assertEquals("Money", 100.0f, money.getSI(0), 0.001d);
102         money = FloatVectorUtil.instantiateSI(a1020, MoneyUnit.EUR, StorageType.DENSE);
103         assertEquals("Money", 10.0f, money.getSI(0), 0.001d);
104         assertSame("Money", MoneyUnit.EUR, money.getUnit());
105 
106         MoneyPerAreaUnit unitUSDm2 = new MoneyPerAreaUnit(MoneyUnit.USD, AreaUnit.SQUARE_METER, "USD/m2", "$/m2");
107         MoneyPerAreaUnit unitEURha = new MoneyPerAreaUnit(MoneyUnit.EUR, AreaUnit.HECTARE, "EUR/ha", "EUR/ha");
108         FloatMoneyPerAreaVector moneyPerArea = FloatVectorUtil.instantiate(a1020, unitUSDm2, StorageType.DENSE);
109         assertEquals("MoneyPerArea", 10.0f, moneyPerArea.getSI(0), 0.001d);
110         moneyPerArea = FloatVectorUtil.instantiate(a12k10, unitEURha, StorageType.DENSE);
111         assertEquals("MoneyPerArea", 1.0f, moneyPerArea.getSI(0), 0.001d);
112         moneyPerArea = FloatVectorUtil.instantiateSI(a1020, unitEURha, StorageType.DENSE);
113         assertEquals("MoneyPerArea", 10.0f, moneyPerArea.getSI(0), 0.001d);
114         assertSame("MoneyPerArea", unitEURha, moneyPerArea.getUnit());
115 
116         MoneyPerEnergyUnit unitUSDJ = new MoneyPerEnergyUnit(MoneyUnit.USD, EnergyUnit.JOULE, "USD/J", "$/J");
117         MoneyPerEnergyUnit unitEURKJ = new MoneyPerEnergyUnit(MoneyUnit.EUR, EnergyUnit.KILOJOULE, "EUR/MWh", "EUR/KJ");
118         FloatMoneyPerEnergyVector moneyPerEnergy = FloatVectorUtil.instantiate(a1020, unitUSDJ, StorageType.DENSE);
119         assertEquals("MoneyPerEnerg", 10.0f, moneyPerEnergy.getSI(0), 0.001d);
120         moneyPerEnergy = FloatVectorUtil.instantiate(a12k, unitEURKJ, StorageType.DENSE);
121         assertEquals("MoneyPerEnerg", 1.0f, moneyPerEnergy.getSI(0), 0.001d);
122         moneyPerEnergy = FloatVectorUtil.instantiateSI(a1020, unitEURKJ, StorageType.DENSE);
123         assertEquals("MoneyPerEnerg", 10.0f, moneyPerEnergy.getSI(0), 0.001d);
124         assertSame("MoneyPerEnerg", unitEURKJ, moneyPerEnergy.getUnit());
125 
126     }
127 
128     /**
129      * Test the instantiation utility function of classes for anonymous units, also for the compiler.
130      * @throws ValueException on instantiation error
131      */
132     @Test
133     public final void anonymousUnitTest() throws ValueException
134     {
135         float[] a12 = new float[] { 1.0f, 2.0f };
136         float[] a1020 = new float[] { 10.0f, 20.0f };
137         float[] a12h = new float[] { 100.0f, 200.0f };
138         float[] a12k = new float[] { 1000.0f, 2000.0f };
139         float[] a12k10 = new float[] { 10000.0f, 20000.0f };
140 
141         Unit<?> unitSI = LengthUnit.SI;
142         Unit<?> unitKM = LengthUnit.KILOMETER;
143         FloatLengthVector length = FloatVectorUtil.instantiateAnonymous(a1020, unitSI, StorageType.DENSE);
144         assertEquals("Length", 10.0f, length.getSI(0), 0.001d);
145         length = FloatVectorUtil.instantiateAnonymous(a12, unitKM, StorageType.DENSE);
146         assertEquals("Length", 1000.0f, length.getSI(0), 0.001d);
147         length = FloatVectorUtil.instantiateAnonymousSI(a1020, unitKM, StorageType.DENSE);
148         assertEquals("Length", 10.0f, length.getSI(0), 0.001d);
149         assertSame("Length", unitKM, length.getUnit());
150 
151         // TODO: other base units
152 
153         Unit<?> unitUSD = MoneyUnit.USD;
154         Unit<?> unitEUR = MoneyUnit.EUR;
155         FloatMoneyVector money = FloatVectorUtil.instantiateAnonymous(a1020, unitUSD, StorageType.DENSE);
156         assertEquals("Money", 10.0f, money.getSI(0), 0.001d);
157         money = FloatVectorUtil.instantiateAnonymous(a12h, unitEUR, StorageType.DENSE);
158         assertEquals("Money", 100.0f, money.getSI(0), 0.001d);
159         money = FloatVectorUtil.instantiateAnonymousSI(a1020, unitEUR, StorageType.DENSE);
160         assertEquals("Money", 10.0f, money.getSI(0), 0.001d);
161         assertSame("Money", unitEUR, money.getUnit());
162 
163         Unit<?> unitUSDm2 = new MoneyPerAreaUnit(MoneyUnit.USD, AreaUnit.SQUARE_METER, "USD/m2", "$/m2");
164         Unit<?> unitEURha = new MoneyPerAreaUnit(MoneyUnit.EUR, AreaUnit.HECTARE, "EUR/ha", "EUR/ha");
165         FloatMoneyPerAreaVector moneyPerArea = FloatVectorUtil.instantiateAnonymous(a1020, unitUSDm2, StorageType.DENSE);
166         assertEquals("MoneyPerArea", 10.0f, moneyPerArea.getSI(0), 0.001d);
167         moneyPerArea = FloatVectorUtil.instantiateAnonymous(a12k10, unitEURha, StorageType.DENSE);
168         assertEquals("MoneyPerArea", 1.0f, moneyPerArea.getSI(0), 0.001d);
169         moneyPerArea = FloatVectorUtil.instantiateAnonymousSI(a1020, unitEURha, StorageType.DENSE);
170         assertEquals("MoneyPerArea", 10.0f, moneyPerArea.getSI(0), 0.001d);
171         assertSame("MoneyPerArea", unitEURha, moneyPerArea.getUnit());
172 
173         Unit<?> unitUSDJ = new MoneyPerEnergyUnit(MoneyUnit.USD, EnergyUnit.JOULE, "USD/J", "$/J");
174         Unit<?> unitEURKJ = new MoneyPerEnergyUnit(MoneyUnit.EUR, EnergyUnit.KILOJOULE, "EUR/MWh", "EUR/KJ");
175         FloatMoneyPerEnergyVector moneyPerEnergy = FloatVectorUtil.instantiateAnonymous(a1020, unitUSDJ, StorageType.DENSE);
176         assertEquals("MoneyPerEnerg", 10.0f, moneyPerEnergy.getSI(0), 0.001d);
177         moneyPerEnergy = FloatVectorUtil.instantiateAnonymous(a12k, unitEURKJ, StorageType.DENSE);
178         assertEquals("MoneyPerEnerg", 1.0f, moneyPerEnergy.getSI(0), 0.001d);
179         moneyPerEnergy = FloatVectorUtil.instantiateAnonymousSI(a1020, unitEURKJ, StorageType.DENSE);
180         assertEquals("MoneyPerEnerg", 10.0f, moneyPerEnergy.getSI(0), 0.001d);
181         assertSame("MoneyPerEnerg", unitEURKJ, moneyPerEnergy.getUnit());
182 
183     }
184 }