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); }
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)); }
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)); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
public void GivenUnitWithInversePowerThenLabelShouldPrettyPrint() { var result = new CompositeUnitOfMeasure("ag1[m2]-1").Label; Assert.AreEqual("ag/m^2", result); }
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); }
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; }
public void GivenUnitWithMultipleCompoundComponentsThenLabelShouldPrettyPrint() { var result = new CompositeUnitOfMeasure("[[cm2]1am1]1[Em2]-1").Label; Assert.AreEqual("cm^2 am/Em^2", result); }