예제 #1
0
        public void GivenDomainIdAndComponentsWhenInjectedThenPropertiesMatch()
        {
            var result = new CompositeUnitOfMeasure("m2", new[] { new UnitOfMeasureComponent(InternalUnitSystemManager.Instance.UnitOfMeasures["m"], 2), });

            Assert.AreEqual(1, result.Components.Count);
            Assert.AreEqual("m", result.Components.First().DomainID);
        }
예제 #2
0
        public void GivenCompositeUnitsOfDifferentTypesWhenConvertThenException()
        {
            var sourceUom = new CompositeUnitOfMeasure("[cm3]1[m2]-1");
            var targetUom = new CompositeUnitOfMeasure("bale1[in2]-1");

            Assert.Throws <InvalidOperationException>(() => _unitOfMeasureConverter.Convert(sourceUom, targetUom, 3));
        }
예제 #3
0
 private NumericValue SimplifyComposite(CompositeUnitOfMeasure compositeUnitOfMeasure, double value)
 {
     if (compositeUnitOfMeasure.Components.Count <= 1)
     {
         return(new NumericValue(compositeUnitOfMeasure.ToModelUom(), value));
     }
     foreach (var firstComponent in compositeUnitOfMeasure.Components)
     {
         foreach (var secondComponent in compositeUnitOfMeasure.Components)
         {
             if (firstComponent.DomainID == secondComponent.DomainID)
             {
                 continue;
             }
             try
             {
                 var firstDomainId  = firstComponent.Power > 1 ? firstComponent.DomainID + firstComponent.Power : firstComponent.DomainID;
                 var secondDomainId = secondComponent.Power > 1 ? secondComponent.DomainID + secondComponent.Power : secondComponent.DomainID;
                 value = _converter.Convert(InternalUnitSystemManager.Instance.UnitOfMeasures[firstDomainId],
                                            InternalUnitSystemManager.Instance.UnitOfMeasures[secondDomainId], value);
                 var uomComponents = CombineCompositeComponents(compositeUnitOfMeasure, firstComponent, secondComponent);
                 return(Simplify(uomComponents, value));
             }
             catch (InvalidOperationException)
             {
             }
         }
     }
     return(new NumericValue(compositeUnitOfMeasure.ToModelUom(), value));
 }
예제 #4
0
        public void GivenSquareUnitsWhenParsedShouldReturnSingleComponentWithPowerOfTwo()
        {
            var result = new CompositeUnitOfMeasure("m2");

            Assert.AreEqual(1, result.Components.Count);
            Assert.AreEqual(2, result.Components.First().Power);
            Assert.AreEqual("m", result.Components.First().DomainID);
        }
예제 #5
0
        public void GivenUnitWithNegativePowerWhenParsedShouldReturnSingleComponentWithPowerOfNegOne()
        {
            var result = new CompositeUnitOfMeasure("sec-1");

            Assert.AreEqual(1, result.Components.Count);
            Assert.AreEqual(-1, result.Components.First().Power);
            Assert.AreEqual("sec", result.Components.First().DomainID);
        }
예제 #6
0
        public void GivenScalarAndCompositeWhenConvertShouldUseCompositeConversionFactor()
        {
            var sourceUom = InternalUnitSystemManager.Instance.UnitOfMeasures["ac"];
            var targetUom = new CompositeUnitOfMeasure("m2");

            var result = _unitOfMeasureConverter.Convert(sourceUom, targetUom, 1);

            Assert.AreEqual(4046.8564223999997, result, Epsilon);
        }
예제 #7
0
        public void GivenCompositeUnitsOfMeasureWithSameNegativePowersWhenConvertThenConverted()
        {
            var kilometerPerHour = new CompositeUnitOfMeasure("km1hr-1");
            var milesPerHour     = new CompositeUnitOfMeasure("mi1hr-1");

            var result = _unitOfMeasureConverter.Convert(kilometerPerHour, milesPerHour, 0.055);

            Assert.AreEqual(0.034175415573053369, result);
        }
예제 #8
0
        public void GivenCompositeUnitWhenConvertToScalarShouldUseCompositeConversionFactor()
        {
            var sourceUom = new CompositeUnitOfMeasure("m2");
            var targetUom = InternalUnitSystemManager.Instance.UnitOfMeasures["ac"];

            var result = _unitOfMeasureConverter.Convert(sourceUom, targetUom, 500000);

            Assert.AreEqual(123.553, result, Epsilon);
        }
예제 #9
0
        public void GivenCompositeUnitsOfMeasureWithZeroValuesWhenConvertThenZero()
        {
            var kilometersPerHour = new CompositeUnitOfMeasure("km1hr-1");
            var milesPerHour      = new CompositeUnitOfMeasure("mi1hr-1");

            var result = _unitOfMeasureConverter.Convert(kilometersPerHour, milesPerHour, 0);

            Assert.AreEqual(0, result);
        }
예제 #10
0
        public void GivenNestedCompoundUnitsInDifferentSystemsWhenConvertThenValueIsConverted()
        {
            var sourceUom = new CompositeUnitOfMeasure("[cm3]1[m2]-1");
            var targetUom = new CompositeUnitOfMeasure("[ft3]1[yd2]-1");

            var result = _unitOfMeasureConverter.Convert(sourceUom, targetUom, 100);

            Assert.AreEqual(0.00295275601, result, 0.000000001);
        }
예제 #11
0
        public void GivenTwoUnitsWithOneSquaredWhenParsedShouldReturnOneSimpleAndOneCompositeComponent()
        {
            var result = new CompositeUnitOfMeasure("bu1[m2]-1");

            Assert.AreEqual(2, result.Components.Count);
            Assert.AreEqual(1, result.Components.First().Power);
            Assert.AreEqual("bu", result.Components.First().DomainID);

            Assert.AreEqual("m2", result.Components.Last().DomainID);
            Assert.AreEqual(-1, result.Components.Last().Power);
        }
예제 #12
0
        public void GivenCubedUnitWithNegativePowerWhenParsedShouldReturnSingleCompositeComponent()
        {
            var result = new CompositeUnitOfMeasure("[dm3]-1");

            Assert.AreEqual(1, result.Components.Count);

            var compositeComponent = (CompositeUnitOfMeasure)result.Components.First().Unit;

            Assert.AreEqual(3, compositeComponent.Components.First().Power);
            Assert.AreEqual("dm", compositeComponent.Components.First().DomainID);
        }
예제 #13
0
        public static AgGateway.ADAPT.ApplicationDataModel.Common.UnitOfMeasure GetModelUom(string unit)
        {
            AgGateway.ADAPT.ApplicationDataModel.Common.UnitOfMeasure uom = UnitSystemManager.GetUnitOfMeasure(unit);

            if (uom == null)
            {
                UnitOfMeasure incompatibleUnit = new CompositeUnitOfMeasure(unit);
                uom = incompatibleUnit.ToModelUom();
            }

            return(uom);
        }
예제 #14
0
        public void GivenTwoUnitsWhenParsedShouldReturnTwoComponents()
        {
            var result = new CompositeUnitOfMeasure("bu1ac-1");

            Assert.AreEqual(2, result.Components.Count);

            Assert.AreEqual(1, result.Components.First().Power);
            Assert.AreEqual(-1, result.Components.Last().Power);

            Assert.AreEqual("bu", result.Components.First().DomainID);
            Assert.AreEqual("ac", result.Components.Last().DomainID);
        }
예제 #15
0
        public void GivenCompoundUnitsInDifferentSystemWhenConvertThenValueIsConverted()
        {
            var sourceUom = new CompositeUnitOfMeasure("bale1[in2]-1");
            var targetUom = new CompositeUnitOfMeasure("bale1[ft2]-1");

            var result = _unitOfMeasureConverter.Convert(sourceUom, targetUom, 1);

            Assert.AreEqual(144, result, 0.000000001);

            sourceUom = new CompositeUnitOfMeasure("bale1[mm2]-1");
            result    = _unitOfMeasureConverter.Convert(sourceUom, targetUom, 1);
            Assert.AreEqual(92903.04, result, 0.000000001);
        }
예제 #16
0
        public void GivenComplexCompositeUnitWithComplexDenominatorShouldProperlyBreakDownDomainId()
        {
            var result = new CompositeUnitOfMeasure("bale1[[ft2]1yd1]-1");

            Assert.AreEqual("bale", result.Components.First().DomainID);

            Assert.AreEqual("[ft2]1yd1", result.Components.Last().DomainID);

            var denominator = (CompositeUnitOfMeasure)result.Components.Last().Unit;

            Assert.AreEqual("ft2", denominator.Components.First().DomainID);
            Assert.AreEqual("yd", denominator.Components.Last().DomainID);
        }
예제 #17
0
        private static List <UnitOfMeasureComponent> CombineCompositeComponents(CompositeUnitOfMeasure compositeUnitOfMeasure,
                                                                                UnitOfMeasureComponent firstComponent, UnitOfMeasureComponent secondComponent)
        {
            var power         = CalculateCompositePower(firstComponent.Power, secondComponent.Power);
            var uomComponents = compositeUnitOfMeasure.Components.ToList();

            uomComponents.Remove(firstComponent);
            uomComponents.Remove(secondComponent);
            if (power != 0)
            {
                uomComponents.Add(new UnitOfMeasureComponent(InternalUnitSystemManager.Instance.UnitOfMeasures[secondComponent.DomainID], secondComponent.Power * 2));
            }
            return(uomComponents);
        }
예제 #18
0
        public void GivenComplexCompositeUnitWhenParsedShouldProperlyBreakDownDomainId()
        {
            var result = new CompositeUnitOfMeasure("[[am2]1am1]1[am2]-1");

            Assert.AreEqual("[[am2]1am1]1[am2]-1", result.DomainID);
            Assert.AreEqual("[am2]1am1", result.Components.First().DomainID);
            Assert.AreEqual("am2", result.Components.Last().DomainID);

            var numerator = (CompositeUnitOfMeasure)result.Components.First().Unit;

            Assert.AreEqual("am2", numerator.Components.First().DomainID);
            Assert.AreEqual("am", numerator.Components.Last().DomainID);

            var denominator = (CompositeUnitOfMeasure)result.Components.Last().Unit;

            Assert.AreEqual("am", denominator.Components.First().DomainID);
        }
예제 #19
0
        public void GivenUnitWithInversePowerThenLabelShouldPrettyPrint()
        {
            var result = new CompositeUnitOfMeasure("ag1[m2]-1").Label;

            Assert.AreEqual("ag/m^2", result);
        }
예제 #20
0
        public void GivenUnitWithMultipleComponentsOfFirstPowerShouldPrettyPrint()
        {
            var result = new CompositeUnitOfMeasure("ac1ft1").Label;

            Assert.AreEqual("ac ft", result);
        }
 private NumericValue SimplifyComposite(CompositeUnitOfMeasure compositeUnitOfMeasure, double value)
 {
     if (compositeUnitOfMeasure.Components.Count <= 1)
         return new NumericValue(compositeUnitOfMeasure.ToModelUom(), value);
     foreach (var firstComponent in compositeUnitOfMeasure.Components)
     {
         foreach (var secondComponent in compositeUnitOfMeasure.Components)
         {
             if(firstComponent.DomainID == secondComponent.DomainID)
                 continue;
             try
             {
                 var firstDomainId = firstComponent.Power > 1 ? firstComponent.DomainID + firstComponent.Power : firstComponent.DomainID;
                 var secondDomainId = secondComponent.Power > 1 ? secondComponent.DomainID + secondComponent.Power : secondComponent.DomainID;
                 value = _converter.Convert(InternalUnitSystemManager.Instance.UnitOfMeasures[firstDomainId],
                                                                 InternalUnitSystemManager.Instance.UnitOfMeasures[secondDomainId], value);
                 var uomComponents = CombineCompositeComponents(compositeUnitOfMeasure, firstComponent, secondComponent);
                 return Simplify(uomComponents, value);
             }
             catch (InvalidOperationException)
             {
             }
         }
     }
     return new NumericValue(compositeUnitOfMeasure.ToModelUom(), value);
 }
예제 #22
0
        public void GivenSquaredCompositeUnitThenLabelShouldPrettyPrint()
        {
            var result = new CompositeUnitOfMeasure("m2").Label;

            Assert.AreEqual("m^2", result);
        }
 private static List<UnitOfMeasureComponent> CombineCompositeComponents(CompositeUnitOfMeasure compositeUnitOfMeasure,
     UnitOfMeasureComponent firstComponent, UnitOfMeasureComponent secondComponent)
 {
     var power = CalculateCompositePower(firstComponent.Power, secondComponent.Power);
     var uomComponents = compositeUnitOfMeasure.Components.ToList();
     uomComponents.Remove(firstComponent);
     uomComponents.Remove(secondComponent);
     if (power != 0)
     {
         uomComponents.Add(new UnitOfMeasureComponent(InternalUnitSystemManager.Instance.UnitOfMeasures[secondComponent.DomainID], secondComponent.Power * 2));
     }
     return uomComponents;
 }
예제 #24
0
        public void GivenUnitWithMultipleCompoundComponentsThenLabelShouldPrettyPrint()
        {
            var result = new CompositeUnitOfMeasure("[[cm2]1am1]1[Em2]-1").Label;

            Assert.AreEqual("cm^2 am/Em^2", result);
        }