diff --git a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
index 8694e214b9..d42f27cf6c 100644
--- a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
+++ b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
@@ -164,6 +164,8 @@ private void GenerateStaticConstructor()
Writer.WL($@"
}},
BaseUnit, Zero, BaseDimensions, QuantityType.{_quantity.Name});
+
+ RegisterDefaultConversions(DefaultConversionFunctions);
}}
");
}
@@ -213,9 +215,9 @@ private void GenerateInstanceConstructors()
_value = Guard.EnsureValidNumber(value, nameof(value));"
: @"
_value = value;");
- Writer.WL(@"
+ Writer.WL($@"
_unit = firstUnitInfo?.Value ?? throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
- }
+ }}
");
}
@@ -224,6 +226,11 @@ private void GenerateStaticProperties()
Writer.WL($@"
#region Static Properties
+ ///
+ /// The containing the default generated conversion functions for instances.
+ ///
+ public static UnitConverter DefaultConversionFunctions {{ get; }} = new UnitConverter();
+
///
public static QuantityInfo<{_unitEnumName}> Info {{ get; }}
@@ -360,27 +367,29 @@ internal static void RegisterDefaultConversions(UnitConverter unitConverter)
if(unit.SingularName == _quantity.BaseUnit)
continue;
- Writer.WL( $@"
- unitConverter.SetConversionFunction<{_quantity.Name}>({_unitEnumName}.{_quantity.BaseUnit}, {_quantity.Name}Unit.{unit.SingularName}, quantity => quantity.ToUnit({_quantity.Name}Unit.{unit.SingularName}));");
+ var func = unit.FromBaseToUnitFunc.Replace("{x}", "quantity.Value");
+ Writer.WL($@"
+ unitConverter.SetConversionFunction<{_quantity.Name}>({_unitEnumName}.{_quantity.BaseUnit}, {_quantity.Name}Unit.{unit.SingularName}, quantity => new {_quantity.Name}({func}, {_quantity.Name}Unit.{unit.SingularName}));");
}
- Writer.WL( $@"
+ Writer.WL($@"
// Register in unit converter: BaseUnit <-> BaseUnit
unitConverter.SetConversionFunction<{_quantity.Name}>({_unitEnumName}.{_quantity.BaseUnit}, {_unitEnumName}.{_quantity.BaseUnit}, quantity => quantity);
- // Register in unit converter: {_quantity.Name}Unit -> BaseUnit" );
+ // Register in unit converter: {_quantity.Name}Unit -> BaseUnit");
foreach(var unit in _quantity.Units)
{
if(unit.SingularName == _quantity.BaseUnit)
continue;
- Writer.WL($@"
- unitConverter.SetConversionFunction<{_quantity.Name}>({_quantity.Name}Unit.{unit.SingularName}, {_unitEnumName}.{_quantity.BaseUnit}, quantity => quantity.ToBaseUnit());" );
+ var func = unit.FromUnitToBaseFunc.Replace("{x}", "quantity.Value");
+ Writer.WL($@"
+ unitConverter.SetConversionFunction<{_quantity.Name}>({_quantity.Name}Unit.{unit.SingularName}, {_unitEnumName}.{_quantity.BaseUnit}, quantity => new {_quantity.Name}({func}, {_unitEnumName}.{_quantity.BaseUnit}));");
}
- Writer.WL( $@"
+ Writer.WL($@"
}}
///
@@ -903,11 +912,42 @@ double IQuantity.As(Enum unit)
///
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation .
///
+ /// The unit to convert to.
/// A {_quantity.Name} with the specified unit.
public {_quantity.Name} ToUnit({_unitEnumName} unit)
{{
- var convertedValue = GetValueAs(unit);
- return new {_quantity.Name}(convertedValue, unit);
+ return ToUnit(unit, DefaultConversionFunctions);
+ }}
+
+ ///
+ /// Converts this {_quantity.Name} to another {_quantity.Name} using the given with the unit representation .
+ ///
+ /// The unit to convert to.
+ /// The to use for the conversion.
+ /// A {_quantity.Name} with the specified unit.
+ public {_quantity.Name} ToUnit({_unitEnumName} unit, UnitConverter unitConverter)
+ {{
+ if(Unit == unit)
+ {{
+ // Already in requested units.
+ return this;
+ }}
+ else if(unitConverter.TryGetConversionFunction((typeof({_quantity.Name}), Unit, typeof({_quantity.Name}), unit), out var conversionFunction))
+ {{
+ // Direct conversion to requested unit found. Return the converted quantity.
+ var converted = conversionFunction(this);
+ return ({_quantity.Name})converted;
+ }}
+ else if(Unit != BaseUnit)
+ {{
+ // Direct conversion to requested unit NOT found. Convert to BaseUnit, and then from BaseUnit to requested unit.
+ var inBaseUnits = ToUnit(BaseUnit);
+ return inBaseUnits.ToUnit(unit);
+ }}
+ else
+ {{
+ throw new NotImplementedException($""Can not convert {{Unit}} to {{unit}}."");
+ }}
}}
///
@@ -916,7 +956,16 @@ IQuantity IQuantity.ToUnit(Enum unit)
if(!(unit is {_unitEnumName} unitAs{_unitEnumName}))
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
- return ToUnit(unitAs{_unitEnumName});
+ return ToUnit(unitAs{_unitEnumName}, DefaultConversionFunctions);
+ }}
+
+ ///
+ IQuantity IQuantity.ToUnit(Enum unit, UnitConverter unitConverter)
+ {{
+ if(!(unit is {_unitEnumName} unitAs{_unitEnumName}))
+ throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
+
+ return ToUnit(unitAs{_unitEnumName}, unitConverter);
}}
///
@@ -941,62 +990,16 @@ IQuantity IQuantity.ToUnit(Enum unit)
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit) => ToUnit(unit);
///
- IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
+ IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit, UnitConverter unitConverter) => ToUnit(unit, unitConverter);
- ///
- /// Converts the current value + unit to the base unit.
- /// This is typically the first step in converting from one unit to another.
- ///
- /// The value in the base unit representation.
- private {_valueType} GetValueInBaseUnit()
- {{
- switch(Unit)
- {{");
- foreach (var unit in _quantity.Units)
- {
- var func = unit.FromUnitToBaseFunc.Replace("{x}", "_value");
- Writer.WL($@"
- case {_unitEnumName}.{unit.SingularName}: return {func};");
- }
-
- Writer.WL($@"
- default:
- throw new NotImplementedException($""Can not convert {{Unit}} to base units."");
- }}
- }}
-
- ///
- /// Converts the current value + unit to the base unit.
- /// This is typically the first step in converting from one unit to another.
- ///
- /// The value in the base unit representation.
- internal {_quantity.Name} ToBaseUnit()
- {{
- var baseUnitValue = GetValueInBaseUnit();
- return new {_quantity.Name}(baseUnitValue, BaseUnit);
- }}
+ ///
+ IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
private {_valueType} GetValueAs({_unitEnumName} unit)
{{
- if(Unit == unit)
- return _value;
-
- var baseUnitValue = GetValueInBaseUnit();
-
- switch(unit)
- {{");
- foreach (var unit in _quantity.Units)
- {
- var func = unit.FromBaseToUnitFunc.Replace("{x}", "baseUnitValue");
- Writer.WL($@"
- case {_unitEnumName}.{unit.SingularName}: return {func};");
- }
-
- Writer.WL(@"
- default:
- throw new NotImplementedException($""Can not convert {Unit} to {unit}."");
- }
- }
+ var converted = ToUnit(unit);
+ return ({_valueType})converted.Value;
+ }}
#endregion
");
@@ -1084,7 +1087,7 @@ public string ToString(string format, IFormatProvider? provider)
}}
#endregion
-" );
+");
}
private void GenerateIConvertibleMethods()
diff --git a/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs b/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
index b0c7b9a5d6..40610f5685 100644
--- a/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
+++ b/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
@@ -76,6 +76,7 @@ public override string Generate()
Writer.WL(GeneratedFileHeader);
Writer.WL($@"
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -95,16 +96,37 @@ namespace UnitsNet.Tests
// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class {_quantity.Name}TestsBase : QuantityTestsBase
{{");
- foreach (var unit in _quantity.Units) Writer.WL($@"
+ foreach(var unit in _quantity.Units) Writer.WL($@"
protected abstract double {unit.PluralName}InOne{_baseUnit.SingularName} {{ get; }}");
Writer.WL("");
Writer.WL($@"
// ReSharper disable VirtualMemberNeverOverriden.Global");
- foreach (var unit in _quantity.Units) Writer.WL($@"
+ foreach(var unit in _quantity.Units) Writer.WL($@"
protected virtual double {unit.PluralName}Tolerance {{ get {{ return 1e-5; }} }}"); Writer.WL($@"
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor({_unitEnumName} unit)
+ {{
+ return unit switch
+ {{");
+ foreach(var unit in _quantity.Units) Writer.WL($@"
+ {GetUnitFullName(unit)} => ({unit.PluralName}InOne{_baseUnit.SingularName}, {unit.PluralName}Tolerance),");
+ Writer.WL($@"
+ _ => throw new NotSupportedException()
+ }};
+ }}
+
+ public static IEnumerable UnitTypes = new List
+ {{");
+ foreach(var unit in _quantity.Units)
+ {
+ Writer.WL($@"
+ new object[] {{ {GetUnitFullName(unit)} }},");
+ }
+ Writer.WL($@"
+ }};
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{{
@@ -116,14 +138,14 @@ public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit()
{{
var quantity = new {_quantity.Name}();
Assert.Equal(0, quantity.Value);");
- if (_quantity.BaseType == "decimal") Writer.WL($@"
+ if(_quantity.BaseType == "decimal") Writer.WL($@"
Assert.Equal(0m, ((IDecimalQuantity)quantity).Value);");
Writer.WL($@"
Assert.Equal({_baseUnitFullName}, quantity.Unit);
}}
");
- if (_quantity.BaseType == "double") Writer.WL($@"
+ if(_quantity.BaseType == "double") Writer.WL($@"
[Fact]
public void Ctor_WithInfinityValue_ThrowsArgumentException()
{{
@@ -183,7 +205,7 @@ public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
{_quantity.Name} {baseUnitVariableName} = {_quantity.Name}.From{_baseUnit.PluralName}(1);");
- foreach (var unit in _quantity.Units) Writer.WL($@"
+ foreach(var unit in _quantity.Units) Writer.WL($@"
AssertEx.EqualTolerance({unit.PluralName}InOne{_baseUnit.SingularName}, {baseUnitVariableName}.{unit.PluralName}, {unit.PluralName}Tolerance);");
Writer.WL($@"
}}
@@ -192,7 +214,7 @@ public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit()
{{");
int i = 0;
- foreach (var unit in _quantity.Units)
+ foreach(var unit in _quantity.Units)
{
var quantityVariable = $"quantity{i++:D2}";
Writer.WL($@"
@@ -205,7 +227,7 @@ public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit()
Writer.WL($@"
}}
");
- if (_quantity.BaseType == "double") Writer.WL($@"
+ if(_quantity.BaseType == "double") Writer.WL($@"
[Fact]
public void From{_baseUnit.PluralName}_WithInfinityValue_ThrowsArgumentException()
{{
@@ -224,7 +246,7 @@ public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit()
public void As()
{{
var {baseUnitVariableName} = {_quantity.Name}.From{_baseUnit.PluralName}(1);");
- foreach (var unit in _quantity.Units) Writer.WL($@"
+ foreach(var unit in _quantity.Units) Writer.WL($@"
AssertEx.EqualTolerance({unit.PluralName}InOne{_baseUnit.SingularName}, {baseUnitVariableName}.As({GetUnitFullName(unit)}), {unit.PluralName}Tolerance);");
Writer.WL($@"
}}
@@ -246,29 +268,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}}
}}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit({_unitEnumName} unit)
{{
- var {baseUnitVariableName} = {_quantity.Name}.From{_baseUnit.PluralName}(1);");
- foreach (var unit in _quantity.Units)
- {
- var asQuantityVariableName = $"{unit.SingularName.ToLowerInvariant()}Quantity";
+ var inBaseUnits = {_quantity.Name}.From(1.0, {_quantity.Name}.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- Writer.WL("");
- Writer.WL($@"
- var {asQuantityVariableName} = {baseUnitVariableName}.ToUnit({GetUnitFullName(unit)});
- AssertEx.EqualTolerance({unit.PluralName}InOne{_baseUnit.SingularName}, (double){asQuantityVariableName}.Value, {unit.PluralName}Tolerance);
- Assert.Equal({GetUnitFullName(unit)}, {asQuantityVariableName}.Unit);");
- }
- Writer.WL($@"
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual({_unitEnumName} unit)
{{
- var quantityInBaseUnit = {_quantity.Name}.From{_baseUnit.PluralName}(1).ToBaseUnit();
- Assert.Equal({_quantity.Name}.BaseUnit, quantityInBaseUnit.Unit);");
- Writer.WL($@"
+ var quantity = {_quantity.Name}.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }}
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit({_unitEnumName} unit)
+ {{
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = {_quantity.Name}.Units.FirstOrDefault(u => u != {_quantity.Name}.BaseUnit && u != {_unitEnumName}.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == {_unitEnumName}.Undefined)
+ fromUnit = {_quantity.Name}.BaseUnit;
+
+ var quantity = {_quantity.Name}.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}}
[Fact]
@@ -701,9 +735,9 @@ public void GetHashCode_Equals()
}}
");
- if( _quantity.GenerateArithmetic )
+ if(_quantity.GenerateArithmetic)
{
- Writer.WL( $@"
+ Writer.WL($@"
[Theory]
[InlineData(1.0)]
[InlineData(-1.0)]
@@ -716,7 +750,7 @@ public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value)
Writer.WL($@"
}}
-}}" );
+}}");
return Writer.ToString();
}
}
diff --git a/UnitsNet.Tests/CustomQuantities/HowMuch.cs b/UnitsNet.Tests/CustomQuantities/HowMuch.cs
index d41f0dcfab..ff41feb577 100644
--- a/UnitsNet.Tests/CustomQuantities/HowMuch.cs
+++ b/UnitsNet.Tests/CustomQuantities/HowMuch.cs
@@ -49,6 +49,8 @@ public IQuantity ToUnit(Enum unit)
throw new ArgumentException("Must be of type HowMuchUnit.", nameof(unit));
}
+ public IQuantity ToUnit(Enum unit, UnitConverter unitConverter) => throw new NotImplementedException();
+
public IQuantity ToUnit(UnitSystem unitSystem) => throw new NotImplementedException();
public override string ToString() => $"{Value} {Unit}";
diff --git a/UnitsNet.Tests/DummyIQuantity.cs b/UnitsNet.Tests/DummyIQuantity.cs
index 04f97a7ea2..6bcb848c2d 100644
--- a/UnitsNet.Tests/DummyIQuantity.cs
+++ b/UnitsNet.Tests/DummyIQuantity.cs
@@ -1,6 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Text;
using JetBrains.Annotations;
namespace UnitsNet.Tests
@@ -17,44 +15,22 @@ internal class DummyIQuantity : IQuantity
public double Value => throw new NotImplementedException();
- public double As(Enum unit)
- {
- throw new NotImplementedException();
- }
-
- public double As(UnitSystem unitSystem)
- {
- throw new NotImplementedException();
- }
-
- public string ToString([CanBeNull] IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
-
- public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
- {
- throw new NotImplementedException();
- }
-
- public string ToString([CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args)
- {
- throw new NotImplementedException();
- }
-
- public string ToString(string format, IFormatProvider formatProvider)
- {
- throw new NotImplementedException();
- }
-
- public IQuantity ToUnit(Enum unit)
- {
- throw new NotImplementedException();
- }
-
- public IQuantity ToUnit(UnitSystem unitSystem)
- {
- throw new NotImplementedException();
- }
+ public double As(Enum unit ) => throw new NotImplementedException();
+
+ public double As(UnitSystem unitSystem ) => throw new NotImplementedException();
+
+ public string ToString([CanBeNull] IFormatProvider provider ) => throw new NotImplementedException();
+
+ public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix ) => throw new NotImplementedException();
+
+ public string ToString([CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args ) => throw new NotImplementedException();
+
+ public string ToString(string format, IFormatProvider formatProvider ) => throw new NotImplementedException();
+
+ public IQuantity ToUnit(Enum unit ) => throw new NotImplementedException();
+
+ public IQuantity ToUnit(Enum unit, UnitConverter unitConverter) => throw new NotImplementedException();
+
+ public IQuantity ToUnit(UnitSystem unitSystem ) => throw new NotImplementedException();
}
}
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs
index 8fdff341d5..13abeeccbc 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class AccelerationTestsBase : QuantityTestsBase
protected virtual double StandardGravityTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AccelerationUnit unit)
+ {
+ return unit switch
+ {
+ AccelerationUnit.CentimeterPerSecondSquared => (CentimetersPerSecondSquaredInOneMeterPerSecondSquared, CentimetersPerSecondSquaredTolerance),
+ AccelerationUnit.DecimeterPerSecondSquared => (DecimetersPerSecondSquaredInOneMeterPerSecondSquared, DecimetersPerSecondSquaredTolerance),
+ AccelerationUnit.FootPerSecondSquared => (FeetPerSecondSquaredInOneMeterPerSecondSquared, FeetPerSecondSquaredTolerance),
+ AccelerationUnit.InchPerSecondSquared => (InchesPerSecondSquaredInOneMeterPerSecondSquared, InchesPerSecondSquaredTolerance),
+ AccelerationUnit.KilometerPerSecondSquared => (KilometersPerSecondSquaredInOneMeterPerSecondSquared, KilometersPerSecondSquaredTolerance),
+ AccelerationUnit.KnotPerHour => (KnotsPerHourInOneMeterPerSecondSquared, KnotsPerHourTolerance),
+ AccelerationUnit.KnotPerMinute => (KnotsPerMinuteInOneMeterPerSecondSquared, KnotsPerMinuteTolerance),
+ AccelerationUnit.KnotPerSecond => (KnotsPerSecondInOneMeterPerSecondSquared, KnotsPerSecondTolerance),
+ AccelerationUnit.MeterPerSecondSquared => (MetersPerSecondSquaredInOneMeterPerSecondSquared, MetersPerSecondSquaredTolerance),
+ AccelerationUnit.MicrometerPerSecondSquared => (MicrometersPerSecondSquaredInOneMeterPerSecondSquared, MicrometersPerSecondSquaredTolerance),
+ AccelerationUnit.MillimeterPerSecondSquared => (MillimetersPerSecondSquaredInOneMeterPerSecondSquared, MillimetersPerSecondSquaredTolerance),
+ AccelerationUnit.MillistandardGravity => (MillistandardGravityInOneMeterPerSecondSquared, MillistandardGravityTolerance),
+ AccelerationUnit.NanometerPerSecondSquared => (NanometersPerSecondSquaredInOneMeterPerSecondSquared, NanometersPerSecondSquaredTolerance),
+ AccelerationUnit.StandardGravity => (StandardGravityInOneMeterPerSecondSquared, StandardGravityTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AccelerationUnit.CentimeterPerSecondSquared },
+ new object[] { AccelerationUnit.DecimeterPerSecondSquared },
+ new object[] { AccelerationUnit.FootPerSecondSquared },
+ new object[] { AccelerationUnit.InchPerSecondSquared },
+ new object[] { AccelerationUnit.KilometerPerSecondSquared },
+ new object[] { AccelerationUnit.KnotPerHour },
+ new object[] { AccelerationUnit.KnotPerMinute },
+ new object[] { AccelerationUnit.KnotPerSecond },
+ new object[] { AccelerationUnit.MeterPerSecondSquared },
+ new object[] { AccelerationUnit.MicrometerPerSecondSquared },
+ new object[] { AccelerationUnit.MillimeterPerSecondSquared },
+ new object[] { AccelerationUnit.MillistandardGravity },
+ new object[] { AccelerationUnit.NanometerPerSecondSquared },
+ new object[] { AccelerationUnit.StandardGravity },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AccelerationUnit unit)
{
- var meterpersecondsquared = Acceleration.FromMetersPerSecondSquared(1);
-
- var centimeterpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.CentimeterPerSecondSquared);
- AssertEx.EqualTolerance(CentimetersPerSecondSquaredInOneMeterPerSecondSquared, (double)centimeterpersecondsquaredQuantity.Value, CentimetersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.CentimeterPerSecondSquared, centimeterpersecondsquaredQuantity.Unit);
-
- var decimeterpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.DecimeterPerSecondSquared);
- AssertEx.EqualTolerance(DecimetersPerSecondSquaredInOneMeterPerSecondSquared, (double)decimeterpersecondsquaredQuantity.Value, DecimetersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.DecimeterPerSecondSquared, decimeterpersecondsquaredQuantity.Unit);
-
- var footpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.FootPerSecondSquared);
- AssertEx.EqualTolerance(FeetPerSecondSquaredInOneMeterPerSecondSquared, (double)footpersecondsquaredQuantity.Value, FeetPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.FootPerSecondSquared, footpersecondsquaredQuantity.Unit);
-
- var inchpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.InchPerSecondSquared);
- AssertEx.EqualTolerance(InchesPerSecondSquaredInOneMeterPerSecondSquared, (double)inchpersecondsquaredQuantity.Value, InchesPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.InchPerSecondSquared, inchpersecondsquaredQuantity.Unit);
-
- var kilometerpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.KilometerPerSecondSquared);
- AssertEx.EqualTolerance(KilometersPerSecondSquaredInOneMeterPerSecondSquared, (double)kilometerpersecondsquaredQuantity.Value, KilometersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.KilometerPerSecondSquared, kilometerpersecondsquaredQuantity.Unit);
-
- var knotperhourQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.KnotPerHour);
- AssertEx.EqualTolerance(KnotsPerHourInOneMeterPerSecondSquared, (double)knotperhourQuantity.Value, KnotsPerHourTolerance);
- Assert.Equal(AccelerationUnit.KnotPerHour, knotperhourQuantity.Unit);
-
- var knotperminuteQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.KnotPerMinute);
- AssertEx.EqualTolerance(KnotsPerMinuteInOneMeterPerSecondSquared, (double)knotperminuteQuantity.Value, KnotsPerMinuteTolerance);
- Assert.Equal(AccelerationUnit.KnotPerMinute, knotperminuteQuantity.Unit);
+ var inBaseUnits = Acceleration.From(1.0, Acceleration.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var knotpersecondQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.KnotPerSecond);
- AssertEx.EqualTolerance(KnotsPerSecondInOneMeterPerSecondSquared, (double)knotpersecondQuantity.Value, KnotsPerSecondTolerance);
- Assert.Equal(AccelerationUnit.KnotPerSecond, knotpersecondQuantity.Unit);
-
- var meterpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.MeterPerSecondSquared);
- AssertEx.EqualTolerance(MetersPerSecondSquaredInOneMeterPerSecondSquared, (double)meterpersecondsquaredQuantity.Value, MetersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.MeterPerSecondSquared, meterpersecondsquaredQuantity.Unit);
-
- var micrometerpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.MicrometerPerSecondSquared);
- AssertEx.EqualTolerance(MicrometersPerSecondSquaredInOneMeterPerSecondSquared, (double)micrometerpersecondsquaredQuantity.Value, MicrometersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.MicrometerPerSecondSquared, micrometerpersecondsquaredQuantity.Unit);
-
- var millimeterpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.MillimeterPerSecondSquared);
- AssertEx.EqualTolerance(MillimetersPerSecondSquaredInOneMeterPerSecondSquared, (double)millimeterpersecondsquaredQuantity.Value, MillimetersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.MillimeterPerSecondSquared, millimeterpersecondsquaredQuantity.Unit);
-
- var millistandardgravityQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.MillistandardGravity);
- AssertEx.EqualTolerance(MillistandardGravityInOneMeterPerSecondSquared, (double)millistandardgravityQuantity.Value, MillistandardGravityTolerance);
- Assert.Equal(AccelerationUnit.MillistandardGravity, millistandardgravityQuantity.Unit);
-
- var nanometerpersecondsquaredQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.NanometerPerSecondSquared);
- AssertEx.EqualTolerance(NanometersPerSecondSquaredInOneMeterPerSecondSquared, (double)nanometerpersecondsquaredQuantity.Value, NanometersPerSecondSquaredTolerance);
- Assert.Equal(AccelerationUnit.NanometerPerSecondSquared, nanometerpersecondsquaredQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var standardgravityQuantity = meterpersecondsquared.ToUnit(AccelerationUnit.StandardGravity);
- AssertEx.EqualTolerance(StandardGravityInOneMeterPerSecondSquared, (double)standardgravityQuantity.Value, StandardGravityTolerance);
- Assert.Equal(AccelerationUnit.StandardGravity, standardgravityQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AccelerationUnit unit)
+ {
+ var quantity = Acceleration.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AccelerationUnit unit)
{
- var quantityInBaseUnit = Acceleration.FromMetersPerSecondSquared(1).ToBaseUnit();
- Assert.Equal(Acceleration.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Acceleration.Units.FirstOrDefault(u => u != Acceleration.BaseUnit && u != AccelerationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AccelerationUnit.Undefined)
+ fromUnit = Acceleration.BaseUnit;
+
+ var quantity = Acceleration.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs
index 9495e04c8f..01cd7be916 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -71,6 +72,48 @@ public abstract partial class AmountOfSubstanceTestsBase : QuantityTestsBase
protected virtual double PoundMolesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AmountOfSubstanceUnit unit)
+ {
+ return unit switch
+ {
+ AmountOfSubstanceUnit.Centimole => (CentimolesInOneMole, CentimolesTolerance),
+ AmountOfSubstanceUnit.CentipoundMole => (CentipoundMolesInOneMole, CentipoundMolesTolerance),
+ AmountOfSubstanceUnit.Decimole => (DecimolesInOneMole, DecimolesTolerance),
+ AmountOfSubstanceUnit.DecipoundMole => (DecipoundMolesInOneMole, DecipoundMolesTolerance),
+ AmountOfSubstanceUnit.Kilomole => (KilomolesInOneMole, KilomolesTolerance),
+ AmountOfSubstanceUnit.KilopoundMole => (KilopoundMolesInOneMole, KilopoundMolesTolerance),
+ AmountOfSubstanceUnit.Megamole => (MegamolesInOneMole, MegamolesTolerance),
+ AmountOfSubstanceUnit.Micromole => (MicromolesInOneMole, MicromolesTolerance),
+ AmountOfSubstanceUnit.MicropoundMole => (MicropoundMolesInOneMole, MicropoundMolesTolerance),
+ AmountOfSubstanceUnit.Millimole => (MillimolesInOneMole, MillimolesTolerance),
+ AmountOfSubstanceUnit.MillipoundMole => (MillipoundMolesInOneMole, MillipoundMolesTolerance),
+ AmountOfSubstanceUnit.Mole => (MolesInOneMole, MolesTolerance),
+ AmountOfSubstanceUnit.Nanomole => (NanomolesInOneMole, NanomolesTolerance),
+ AmountOfSubstanceUnit.NanopoundMole => (NanopoundMolesInOneMole, NanopoundMolesTolerance),
+ AmountOfSubstanceUnit.PoundMole => (PoundMolesInOneMole, PoundMolesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AmountOfSubstanceUnit.Centimole },
+ new object[] { AmountOfSubstanceUnit.CentipoundMole },
+ new object[] { AmountOfSubstanceUnit.Decimole },
+ new object[] { AmountOfSubstanceUnit.DecipoundMole },
+ new object[] { AmountOfSubstanceUnit.Kilomole },
+ new object[] { AmountOfSubstanceUnit.KilopoundMole },
+ new object[] { AmountOfSubstanceUnit.Megamole },
+ new object[] { AmountOfSubstanceUnit.Micromole },
+ new object[] { AmountOfSubstanceUnit.MicropoundMole },
+ new object[] { AmountOfSubstanceUnit.Millimole },
+ new object[] { AmountOfSubstanceUnit.MillipoundMole },
+ new object[] { AmountOfSubstanceUnit.Mole },
+ new object[] { AmountOfSubstanceUnit.Nanomole },
+ new object[] { AmountOfSubstanceUnit.NanopoundMole },
+ new object[] { AmountOfSubstanceUnit.PoundMole },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -276,77 +319,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AmountOfSubstanceUnit unit)
{
- var mole = AmountOfSubstance.FromMoles(1);
-
- var centimoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Centimole);
- AssertEx.EqualTolerance(CentimolesInOneMole, (double)centimoleQuantity.Value, CentimolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Centimole, centimoleQuantity.Unit);
-
- var centipoundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.CentipoundMole);
- AssertEx.EqualTolerance(CentipoundMolesInOneMole, (double)centipoundmoleQuantity.Value, CentipoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.CentipoundMole, centipoundmoleQuantity.Unit);
-
- var decimoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Decimole);
- AssertEx.EqualTolerance(DecimolesInOneMole, (double)decimoleQuantity.Value, DecimolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Decimole, decimoleQuantity.Unit);
-
- var decipoundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.DecipoundMole);
- AssertEx.EqualTolerance(DecipoundMolesInOneMole, (double)decipoundmoleQuantity.Value, DecipoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.DecipoundMole, decipoundmoleQuantity.Unit);
-
- var kilomoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Kilomole);
- AssertEx.EqualTolerance(KilomolesInOneMole, (double)kilomoleQuantity.Value, KilomolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Kilomole, kilomoleQuantity.Unit);
-
- var kilopoundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.KilopoundMole);
- AssertEx.EqualTolerance(KilopoundMolesInOneMole, (double)kilopoundmoleQuantity.Value, KilopoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.KilopoundMole, kilopoundmoleQuantity.Unit);
-
- var megamoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Megamole);
- AssertEx.EqualTolerance(MegamolesInOneMole, (double)megamoleQuantity.Value, MegamolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Megamole, megamoleQuantity.Unit);
+ var inBaseUnits = AmountOfSubstance.From(1.0, AmountOfSubstance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var micromoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Micromole);
- AssertEx.EqualTolerance(MicromolesInOneMole, (double)micromoleQuantity.Value, MicromolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Micromole, micromoleQuantity.Unit);
-
- var micropoundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.MicropoundMole);
- AssertEx.EqualTolerance(MicropoundMolesInOneMole, (double)micropoundmoleQuantity.Value, MicropoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.MicropoundMole, micropoundmoleQuantity.Unit);
-
- var millimoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Millimole);
- AssertEx.EqualTolerance(MillimolesInOneMole, (double)millimoleQuantity.Value, MillimolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Millimole, millimoleQuantity.Unit);
-
- var millipoundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.MillipoundMole);
- AssertEx.EqualTolerance(MillipoundMolesInOneMole, (double)millipoundmoleQuantity.Value, MillipoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.MillipoundMole, millipoundmoleQuantity.Unit);
-
- var moleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Mole);
- AssertEx.EqualTolerance(MolesInOneMole, (double)moleQuantity.Value, MolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Mole, moleQuantity.Unit);
-
- var nanomoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.Nanomole);
- AssertEx.EqualTolerance(NanomolesInOneMole, (double)nanomoleQuantity.Value, NanomolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.Nanomole, nanomoleQuantity.Unit);
-
- var nanopoundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.NanopoundMole);
- AssertEx.EqualTolerance(NanopoundMolesInOneMole, (double)nanopoundmoleQuantity.Value, NanopoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.NanopoundMole, nanopoundmoleQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundmoleQuantity = mole.ToUnit(AmountOfSubstanceUnit.PoundMole);
- AssertEx.EqualTolerance(PoundMolesInOneMole, (double)poundmoleQuantity.Value, PoundMolesTolerance);
- Assert.Equal(AmountOfSubstanceUnit.PoundMole, poundmoleQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AmountOfSubstanceUnit unit)
+ {
+ var quantity = AmountOfSubstance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AmountOfSubstanceUnit unit)
{
- var quantityInBaseUnit = AmountOfSubstance.FromMoles(1).ToBaseUnit();
- Assert.Equal(AmountOfSubstance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = AmountOfSubstance.Units.FirstOrDefault(u => u != AmountOfSubstance.BaseUnit && u != AmountOfSubstanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AmountOfSubstanceUnit.Undefined)
+ fromUnit = AmountOfSubstance.BaseUnit;
+
+ var quantity = AmountOfSubstance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs
index 24063f7fe9..eb9a8dc24f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class AmplitudeRatioTestsBase : QuantityTestsBase
protected virtual double DecibelVoltsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AmplitudeRatioUnit unit)
+ {
+ return unit switch
+ {
+ AmplitudeRatioUnit.DecibelMicrovolt => (DecibelMicrovoltsInOneDecibelVolt, DecibelMicrovoltsTolerance),
+ AmplitudeRatioUnit.DecibelMillivolt => (DecibelMillivoltsInOneDecibelVolt, DecibelMillivoltsTolerance),
+ AmplitudeRatioUnit.DecibelUnloaded => (DecibelsUnloadedInOneDecibelVolt, DecibelsUnloadedTolerance),
+ AmplitudeRatioUnit.DecibelVolt => (DecibelVoltsInOneDecibelVolt, DecibelVoltsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AmplitudeRatioUnit.DecibelMicrovolt },
+ new object[] { AmplitudeRatioUnit.DecibelMillivolt },
+ new object[] { AmplitudeRatioUnit.DecibelUnloaded },
+ new object[] { AmplitudeRatioUnit.DecibelVolt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AmplitudeRatioUnit unit)
{
- var decibelvolt = AmplitudeRatio.FromDecibelVolts(1);
+ var inBaseUnits = AmplitudeRatio.From(1.0, AmplitudeRatio.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var decibelmicrovoltQuantity = decibelvolt.ToUnit(AmplitudeRatioUnit.DecibelMicrovolt);
- AssertEx.EqualTolerance(DecibelMicrovoltsInOneDecibelVolt, (double)decibelmicrovoltQuantity.Value, DecibelMicrovoltsTolerance);
- Assert.Equal(AmplitudeRatioUnit.DecibelMicrovolt, decibelmicrovoltQuantity.Unit);
-
- var decibelmillivoltQuantity = decibelvolt.ToUnit(AmplitudeRatioUnit.DecibelMillivolt);
- AssertEx.EqualTolerance(DecibelMillivoltsInOneDecibelVolt, (double)decibelmillivoltQuantity.Value, DecibelMillivoltsTolerance);
- Assert.Equal(AmplitudeRatioUnit.DecibelMillivolt, decibelmillivoltQuantity.Unit);
-
- var decibelunloadedQuantity = decibelvolt.ToUnit(AmplitudeRatioUnit.DecibelUnloaded);
- AssertEx.EqualTolerance(DecibelsUnloadedInOneDecibelVolt, (double)decibelunloadedQuantity.Value, DecibelsUnloadedTolerance);
- Assert.Equal(AmplitudeRatioUnit.DecibelUnloaded, decibelunloadedQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var decibelvoltQuantity = decibelvolt.ToUnit(AmplitudeRatioUnit.DecibelVolt);
- AssertEx.EqualTolerance(DecibelVoltsInOneDecibelVolt, (double)decibelvoltQuantity.Value, DecibelVoltsTolerance);
- Assert.Equal(AmplitudeRatioUnit.DecibelVolt, decibelvoltQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AmplitudeRatioUnit unit)
+ {
+ var quantity = AmplitudeRatio.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AmplitudeRatioUnit unit)
{
- var quantityInBaseUnit = AmplitudeRatio.FromDecibelVolts(1).ToBaseUnit();
- Assert.Equal(AmplitudeRatio.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = AmplitudeRatio.Units.FirstOrDefault(u => u != AmplitudeRatio.BaseUnit && u != AmplitudeRatioUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AmplitudeRatioUnit.Undefined)
+ fromUnit = AmplitudeRatio.BaseUnit;
+
+ var quantity = AmplitudeRatio.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs
index b495603ee4..bfecc91bbc 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -73,6 +74,50 @@ public abstract partial class AngleTestsBase : QuantityTestsBase
protected virtual double TiltTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AngleUnit unit)
+ {
+ return unit switch
+ {
+ AngleUnit.Arcminute => (ArcminutesInOneDegree, ArcminutesTolerance),
+ AngleUnit.Arcsecond => (ArcsecondsInOneDegree, ArcsecondsTolerance),
+ AngleUnit.Centiradian => (CentiradiansInOneDegree, CentiradiansTolerance),
+ AngleUnit.Deciradian => (DeciradiansInOneDegree, DeciradiansTolerance),
+ AngleUnit.Degree => (DegreesInOneDegree, DegreesTolerance),
+ AngleUnit.Gradian => (GradiansInOneDegree, GradiansTolerance),
+ AngleUnit.Microdegree => (MicrodegreesInOneDegree, MicrodegreesTolerance),
+ AngleUnit.Microradian => (MicroradiansInOneDegree, MicroradiansTolerance),
+ AngleUnit.Millidegree => (MillidegreesInOneDegree, MillidegreesTolerance),
+ AngleUnit.Milliradian => (MilliradiansInOneDegree, MilliradiansTolerance),
+ AngleUnit.Nanodegree => (NanodegreesInOneDegree, NanodegreesTolerance),
+ AngleUnit.Nanoradian => (NanoradiansInOneDegree, NanoradiansTolerance),
+ AngleUnit.NatoMil => (NatoMilsInOneDegree, NatoMilsTolerance),
+ AngleUnit.Radian => (RadiansInOneDegree, RadiansTolerance),
+ AngleUnit.Revolution => (RevolutionsInOneDegree, RevolutionsTolerance),
+ AngleUnit.Tilt => (TiltInOneDegree, TiltTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AngleUnit.Arcminute },
+ new object[] { AngleUnit.Arcsecond },
+ new object[] { AngleUnit.Centiradian },
+ new object[] { AngleUnit.Deciradian },
+ new object[] { AngleUnit.Degree },
+ new object[] { AngleUnit.Gradian },
+ new object[] { AngleUnit.Microdegree },
+ new object[] { AngleUnit.Microradian },
+ new object[] { AngleUnit.Millidegree },
+ new object[] { AngleUnit.Milliradian },
+ new object[] { AngleUnit.Nanodegree },
+ new object[] { AngleUnit.Nanoradian },
+ new object[] { AngleUnit.NatoMil },
+ new object[] { AngleUnit.Radian },
+ new object[] { AngleUnit.Revolution },
+ new object[] { AngleUnit.Tilt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -284,81 +329,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AngleUnit unit)
{
- var degree = Angle.FromDegrees(1);
-
- var arcminuteQuantity = degree.ToUnit(AngleUnit.Arcminute);
- AssertEx.EqualTolerance(ArcminutesInOneDegree, (double)arcminuteQuantity.Value, ArcminutesTolerance);
- Assert.Equal(AngleUnit.Arcminute, arcminuteQuantity.Unit);
-
- var arcsecondQuantity = degree.ToUnit(AngleUnit.Arcsecond);
- AssertEx.EqualTolerance(ArcsecondsInOneDegree, (double)arcsecondQuantity.Value, ArcsecondsTolerance);
- Assert.Equal(AngleUnit.Arcsecond, arcsecondQuantity.Unit);
-
- var centiradianQuantity = degree.ToUnit(AngleUnit.Centiradian);
- AssertEx.EqualTolerance(CentiradiansInOneDegree, (double)centiradianQuantity.Value, CentiradiansTolerance);
- Assert.Equal(AngleUnit.Centiradian, centiradianQuantity.Unit);
-
- var deciradianQuantity = degree.ToUnit(AngleUnit.Deciradian);
- AssertEx.EqualTolerance(DeciradiansInOneDegree, (double)deciradianQuantity.Value, DeciradiansTolerance);
- Assert.Equal(AngleUnit.Deciradian, deciradianQuantity.Unit);
-
- var degreeQuantity = degree.ToUnit(AngleUnit.Degree);
- AssertEx.EqualTolerance(DegreesInOneDegree, (double)degreeQuantity.Value, DegreesTolerance);
- Assert.Equal(AngleUnit.Degree, degreeQuantity.Unit);
-
- var gradianQuantity = degree.ToUnit(AngleUnit.Gradian);
- AssertEx.EqualTolerance(GradiansInOneDegree, (double)gradianQuantity.Value, GradiansTolerance);
- Assert.Equal(AngleUnit.Gradian, gradianQuantity.Unit);
-
- var microdegreeQuantity = degree.ToUnit(AngleUnit.Microdegree);
- AssertEx.EqualTolerance(MicrodegreesInOneDegree, (double)microdegreeQuantity.Value, MicrodegreesTolerance);
- Assert.Equal(AngleUnit.Microdegree, microdegreeQuantity.Unit);
-
- var microradianQuantity = degree.ToUnit(AngleUnit.Microradian);
- AssertEx.EqualTolerance(MicroradiansInOneDegree, (double)microradianQuantity.Value, MicroradiansTolerance);
- Assert.Equal(AngleUnit.Microradian, microradianQuantity.Unit);
+ var inBaseUnits = Angle.From(1.0, Angle.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var millidegreeQuantity = degree.ToUnit(AngleUnit.Millidegree);
- AssertEx.EqualTolerance(MillidegreesInOneDegree, (double)millidegreeQuantity.Value, MillidegreesTolerance);
- Assert.Equal(AngleUnit.Millidegree, millidegreeQuantity.Unit);
-
- var milliradianQuantity = degree.ToUnit(AngleUnit.Milliradian);
- AssertEx.EqualTolerance(MilliradiansInOneDegree, (double)milliradianQuantity.Value, MilliradiansTolerance);
- Assert.Equal(AngleUnit.Milliradian, milliradianQuantity.Unit);
-
- var nanodegreeQuantity = degree.ToUnit(AngleUnit.Nanodegree);
- AssertEx.EqualTolerance(NanodegreesInOneDegree, (double)nanodegreeQuantity.Value, NanodegreesTolerance);
- Assert.Equal(AngleUnit.Nanodegree, nanodegreeQuantity.Unit);
-
- var nanoradianQuantity = degree.ToUnit(AngleUnit.Nanoradian);
- AssertEx.EqualTolerance(NanoradiansInOneDegree, (double)nanoradianQuantity.Value, NanoradiansTolerance);
- Assert.Equal(AngleUnit.Nanoradian, nanoradianQuantity.Unit);
-
- var natomilQuantity = degree.ToUnit(AngleUnit.NatoMil);
- AssertEx.EqualTolerance(NatoMilsInOneDegree, (double)natomilQuantity.Value, NatoMilsTolerance);
- Assert.Equal(AngleUnit.NatoMil, natomilQuantity.Unit);
-
- var radianQuantity = degree.ToUnit(AngleUnit.Radian);
- AssertEx.EqualTolerance(RadiansInOneDegree, (double)radianQuantity.Value, RadiansTolerance);
- Assert.Equal(AngleUnit.Radian, radianQuantity.Unit);
-
- var revolutionQuantity = degree.ToUnit(AngleUnit.Revolution);
- AssertEx.EqualTolerance(RevolutionsInOneDegree, (double)revolutionQuantity.Value, RevolutionsTolerance);
- Assert.Equal(AngleUnit.Revolution, revolutionQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tiltQuantity = degree.ToUnit(AngleUnit.Tilt);
- AssertEx.EqualTolerance(TiltInOneDegree, (double)tiltQuantity.Value, TiltTolerance);
- Assert.Equal(AngleUnit.Tilt, tiltQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AngleUnit unit)
+ {
+ var quantity = Angle.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AngleUnit unit)
{
- var quantityInBaseUnit = Angle.FromDegrees(1).ToBaseUnit();
- Assert.Equal(Angle.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Angle.Units.FirstOrDefault(u => u != Angle.BaseUnit && u != AngleUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AngleUnit.Undefined)
+ fromUnit = Angle.BaseUnit;
+
+ var quantity = Angle.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentEnergyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentEnergyTestsBase.g.cs
index 6e94f37657..3731afa216 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentEnergyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentEnergyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class ApparentEnergyTestsBase : QuantityTestsBase
protected virtual double VoltampereHoursTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ApparentEnergyUnit unit)
+ {
+ return unit switch
+ {
+ ApparentEnergyUnit.KilovoltampereHour => (KilovoltampereHoursInOneVoltampereHour, KilovoltampereHoursTolerance),
+ ApparentEnergyUnit.MegavoltampereHour => (MegavoltampereHoursInOneVoltampereHour, MegavoltampereHoursTolerance),
+ ApparentEnergyUnit.VoltampereHour => (VoltampereHoursInOneVoltampereHour, VoltampereHoursTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ApparentEnergyUnit.KilovoltampereHour },
+ new object[] { ApparentEnergyUnit.MegavoltampereHour },
+ new object[] { ApparentEnergyUnit.VoltampereHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ApparentEnergyUnit unit)
{
- var voltamperehour = ApparentEnergy.FromVoltampereHours(1);
+ var inBaseUnits = ApparentEnergy.From(1.0, ApparentEnergy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilovoltamperehourQuantity = voltamperehour.ToUnit(ApparentEnergyUnit.KilovoltampereHour);
- AssertEx.EqualTolerance(KilovoltampereHoursInOneVoltampereHour, (double)kilovoltamperehourQuantity.Value, KilovoltampereHoursTolerance);
- Assert.Equal(ApparentEnergyUnit.KilovoltampereHour, kilovoltamperehourQuantity.Unit);
-
- var megavoltamperehourQuantity = voltamperehour.ToUnit(ApparentEnergyUnit.MegavoltampereHour);
- AssertEx.EqualTolerance(MegavoltampereHoursInOneVoltampereHour, (double)megavoltamperehourQuantity.Value, MegavoltampereHoursTolerance);
- Assert.Equal(ApparentEnergyUnit.MegavoltampereHour, megavoltamperehourQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltamperehourQuantity = voltamperehour.ToUnit(ApparentEnergyUnit.VoltampereHour);
- AssertEx.EqualTolerance(VoltampereHoursInOneVoltampereHour, (double)voltamperehourQuantity.Value, VoltampereHoursTolerance);
- Assert.Equal(ApparentEnergyUnit.VoltampereHour, voltamperehourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ApparentEnergyUnit unit)
+ {
+ var quantity = ApparentEnergy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ApparentEnergyUnit unit)
{
- var quantityInBaseUnit = ApparentEnergy.FromVoltampereHours(1).ToBaseUnit();
- Assert.Equal(ApparentEnergy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ApparentEnergy.Units.FirstOrDefault(u => u != ApparentEnergy.BaseUnit && u != ApparentEnergyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ApparentEnergyUnit.Undefined)
+ fromUnit = ApparentEnergy.BaseUnit;
+
+ var quantity = ApparentEnergy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentPowerTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentPowerTestsBase.g.cs
index 595427df3e..aa5184f730 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentPowerTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ApparentPowerTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class ApparentPowerTestsBase : QuantityTestsBase
protected virtual double VoltamperesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ApparentPowerUnit unit)
+ {
+ return unit switch
+ {
+ ApparentPowerUnit.Gigavoltampere => (GigavoltamperesInOneVoltampere, GigavoltamperesTolerance),
+ ApparentPowerUnit.Kilovoltampere => (KilovoltamperesInOneVoltampere, KilovoltamperesTolerance),
+ ApparentPowerUnit.Megavoltampere => (MegavoltamperesInOneVoltampere, MegavoltamperesTolerance),
+ ApparentPowerUnit.Voltampere => (VoltamperesInOneVoltampere, VoltamperesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ApparentPowerUnit.Gigavoltampere },
+ new object[] { ApparentPowerUnit.Kilovoltampere },
+ new object[] { ApparentPowerUnit.Megavoltampere },
+ new object[] { ApparentPowerUnit.Voltampere },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ApparentPowerUnit unit)
{
- var voltampere = ApparentPower.FromVoltamperes(1);
+ var inBaseUnits = ApparentPower.From(1.0, ApparentPower.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var gigavoltampereQuantity = voltampere.ToUnit(ApparentPowerUnit.Gigavoltampere);
- AssertEx.EqualTolerance(GigavoltamperesInOneVoltampere, (double)gigavoltampereQuantity.Value, GigavoltamperesTolerance);
- Assert.Equal(ApparentPowerUnit.Gigavoltampere, gigavoltampereQuantity.Unit);
-
- var kilovoltampereQuantity = voltampere.ToUnit(ApparentPowerUnit.Kilovoltampere);
- AssertEx.EqualTolerance(KilovoltamperesInOneVoltampere, (double)kilovoltampereQuantity.Value, KilovoltamperesTolerance);
- Assert.Equal(ApparentPowerUnit.Kilovoltampere, kilovoltampereQuantity.Unit);
-
- var megavoltampereQuantity = voltampere.ToUnit(ApparentPowerUnit.Megavoltampere);
- AssertEx.EqualTolerance(MegavoltamperesInOneVoltampere, (double)megavoltampereQuantity.Value, MegavoltamperesTolerance);
- Assert.Equal(ApparentPowerUnit.Megavoltampere, megavoltampereQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltampereQuantity = voltampere.ToUnit(ApparentPowerUnit.Voltampere);
- AssertEx.EqualTolerance(VoltamperesInOneVoltampere, (double)voltampereQuantity.Value, VoltamperesTolerance);
- Assert.Equal(ApparentPowerUnit.Voltampere, voltampereQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ApparentPowerUnit unit)
+ {
+ var quantity = ApparentPower.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ApparentPowerUnit unit)
{
- var quantityInBaseUnit = ApparentPower.FromVoltamperes(1).ToBaseUnit();
- Assert.Equal(ApparentPower.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ApparentPower.Units.FirstOrDefault(u => u != ApparentPower.BaseUnit && u != ApparentPowerUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ApparentPowerUnit.Undefined)
+ fromUnit = ApparentPower.BaseUnit;
+
+ var quantity = ApparentPower.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs
index f109ab006d..1d8d35a570 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class AreaDensityTestsBase : QuantityTestsBase
protected virtual double KilogramsPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AreaDensityUnit unit)
+ {
+ return unit switch
+ {
+ AreaDensityUnit.KilogramPerSquareMeter => (KilogramsPerSquareMeterInOneKilogramPerSquareMeter, KilogramsPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AreaDensityUnit.KilogramPerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AreaDensityUnit unit)
{
- var kilogrampersquaremeter = AreaDensity.FromKilogramsPerSquareMeter(1);
+ var inBaseUnits = AreaDensity.From(1.0, AreaDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilogrampersquaremeterQuantity = kilogrampersquaremeter.ToUnit(AreaDensityUnit.KilogramPerSquareMeter);
- AssertEx.EqualTolerance(KilogramsPerSquareMeterInOneKilogramPerSquareMeter, (double)kilogrampersquaremeterQuantity.Value, KilogramsPerSquareMeterTolerance);
- Assert.Equal(AreaDensityUnit.KilogramPerSquareMeter, kilogrampersquaremeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AreaDensityUnit unit)
+ {
+ var quantity = AreaDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AreaDensityUnit unit)
{
- var quantityInBaseUnit = AreaDensity.FromKilogramsPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(AreaDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = AreaDensity.Units.FirstOrDefault(u => u != AreaDensity.BaseUnit && u != AreaDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AreaDensityUnit.Undefined)
+ fromUnit = AreaDensity.BaseUnit;
+
+ var quantity = AreaDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs
index c885ab32f6..835911e9a6 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -53,6 +54,30 @@ public abstract partial class AreaMomentOfInertiaTestsBase : QuantityTestsBase
protected virtual double MillimetersToTheFourthTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AreaMomentOfInertiaUnit unit)
+ {
+ return unit switch
+ {
+ AreaMomentOfInertiaUnit.CentimeterToTheFourth => (CentimetersToTheFourthInOneMeterToTheFourth, CentimetersToTheFourthTolerance),
+ AreaMomentOfInertiaUnit.DecimeterToTheFourth => (DecimetersToTheFourthInOneMeterToTheFourth, DecimetersToTheFourthTolerance),
+ AreaMomentOfInertiaUnit.FootToTheFourth => (FeetToTheFourthInOneMeterToTheFourth, FeetToTheFourthTolerance),
+ AreaMomentOfInertiaUnit.InchToTheFourth => (InchesToTheFourthInOneMeterToTheFourth, InchesToTheFourthTolerance),
+ AreaMomentOfInertiaUnit.MeterToTheFourth => (MetersToTheFourthInOneMeterToTheFourth, MetersToTheFourthTolerance),
+ AreaMomentOfInertiaUnit.MillimeterToTheFourth => (MillimetersToTheFourthInOneMeterToTheFourth, MillimetersToTheFourthTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AreaMomentOfInertiaUnit.CentimeterToTheFourth },
+ new object[] { AreaMomentOfInertiaUnit.DecimeterToTheFourth },
+ new object[] { AreaMomentOfInertiaUnit.FootToTheFourth },
+ new object[] { AreaMomentOfInertiaUnit.InchToTheFourth },
+ new object[] { AreaMomentOfInertiaUnit.MeterToTheFourth },
+ new object[] { AreaMomentOfInertiaUnit.MillimeterToTheFourth },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -204,41 +229,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AreaMomentOfInertiaUnit unit)
{
- var metertothefourth = AreaMomentOfInertia.FromMetersToTheFourth(1);
-
- var centimetertothefourthQuantity = metertothefourth.ToUnit(AreaMomentOfInertiaUnit.CentimeterToTheFourth);
- AssertEx.EqualTolerance(CentimetersToTheFourthInOneMeterToTheFourth, (double)centimetertothefourthQuantity.Value, CentimetersToTheFourthTolerance);
- Assert.Equal(AreaMomentOfInertiaUnit.CentimeterToTheFourth, centimetertothefourthQuantity.Unit);
+ var inBaseUnits = AreaMomentOfInertia.From(1.0, AreaMomentOfInertia.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var decimetertothefourthQuantity = metertothefourth.ToUnit(AreaMomentOfInertiaUnit.DecimeterToTheFourth);
- AssertEx.EqualTolerance(DecimetersToTheFourthInOneMeterToTheFourth, (double)decimetertothefourthQuantity.Value, DecimetersToTheFourthTolerance);
- Assert.Equal(AreaMomentOfInertiaUnit.DecimeterToTheFourth, decimetertothefourthQuantity.Unit);
-
- var foottothefourthQuantity = metertothefourth.ToUnit(AreaMomentOfInertiaUnit.FootToTheFourth);
- AssertEx.EqualTolerance(FeetToTheFourthInOneMeterToTheFourth, (double)foottothefourthQuantity.Value, FeetToTheFourthTolerance);
- Assert.Equal(AreaMomentOfInertiaUnit.FootToTheFourth, foottothefourthQuantity.Unit);
-
- var inchtothefourthQuantity = metertothefourth.ToUnit(AreaMomentOfInertiaUnit.InchToTheFourth);
- AssertEx.EqualTolerance(InchesToTheFourthInOneMeterToTheFourth, (double)inchtothefourthQuantity.Value, InchesToTheFourthTolerance);
- Assert.Equal(AreaMomentOfInertiaUnit.InchToTheFourth, inchtothefourthQuantity.Unit);
-
- var metertothefourthQuantity = metertothefourth.ToUnit(AreaMomentOfInertiaUnit.MeterToTheFourth);
- AssertEx.EqualTolerance(MetersToTheFourthInOneMeterToTheFourth, (double)metertothefourthQuantity.Value, MetersToTheFourthTolerance);
- Assert.Equal(AreaMomentOfInertiaUnit.MeterToTheFourth, metertothefourthQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var millimetertothefourthQuantity = metertothefourth.ToUnit(AreaMomentOfInertiaUnit.MillimeterToTheFourth);
- AssertEx.EqualTolerance(MillimetersToTheFourthInOneMeterToTheFourth, (double)millimetertothefourthQuantity.Value, MillimetersToTheFourthTolerance);
- Assert.Equal(AreaMomentOfInertiaUnit.MillimeterToTheFourth, millimetertothefourthQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AreaMomentOfInertiaUnit unit)
+ {
+ var quantity = AreaMomentOfInertia.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AreaMomentOfInertiaUnit unit)
{
- var quantityInBaseUnit = AreaMomentOfInertia.FromMetersToTheFourth(1).ToBaseUnit();
- Assert.Equal(AreaMomentOfInertia.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = AreaMomentOfInertia.Units.FirstOrDefault(u => u != AreaMomentOfInertia.BaseUnit && u != AreaMomentOfInertiaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AreaMomentOfInertiaUnit.Undefined)
+ fromUnit = AreaMomentOfInertia.BaseUnit;
+
+ var quantity = AreaMomentOfInertia.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs
index 0eeb5c8c8c..d77e8fbd30 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class AreaTestsBase : QuantityTestsBase
protected virtual double UsSurveySquareFeetTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AreaUnit unit)
+ {
+ return unit switch
+ {
+ AreaUnit.Acre => (AcresInOneSquareMeter, AcresTolerance),
+ AreaUnit.Hectare => (HectaresInOneSquareMeter, HectaresTolerance),
+ AreaUnit.SquareCentimeter => (SquareCentimetersInOneSquareMeter, SquareCentimetersTolerance),
+ AreaUnit.SquareDecimeter => (SquareDecimetersInOneSquareMeter, SquareDecimetersTolerance),
+ AreaUnit.SquareFoot => (SquareFeetInOneSquareMeter, SquareFeetTolerance),
+ AreaUnit.SquareInch => (SquareInchesInOneSquareMeter, SquareInchesTolerance),
+ AreaUnit.SquareKilometer => (SquareKilometersInOneSquareMeter, SquareKilometersTolerance),
+ AreaUnit.SquareMeter => (SquareMetersInOneSquareMeter, SquareMetersTolerance),
+ AreaUnit.SquareMicrometer => (SquareMicrometersInOneSquareMeter, SquareMicrometersTolerance),
+ AreaUnit.SquareMile => (SquareMilesInOneSquareMeter, SquareMilesTolerance),
+ AreaUnit.SquareMillimeter => (SquareMillimetersInOneSquareMeter, SquareMillimetersTolerance),
+ AreaUnit.SquareNauticalMile => (SquareNauticalMilesInOneSquareMeter, SquareNauticalMilesTolerance),
+ AreaUnit.SquareYard => (SquareYardsInOneSquareMeter, SquareYardsTolerance),
+ AreaUnit.UsSurveySquareFoot => (UsSurveySquareFeetInOneSquareMeter, UsSurveySquareFeetTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { AreaUnit.Acre },
+ new object[] { AreaUnit.Hectare },
+ new object[] { AreaUnit.SquareCentimeter },
+ new object[] { AreaUnit.SquareDecimeter },
+ new object[] { AreaUnit.SquareFoot },
+ new object[] { AreaUnit.SquareInch },
+ new object[] { AreaUnit.SquareKilometer },
+ new object[] { AreaUnit.SquareMeter },
+ new object[] { AreaUnit.SquareMicrometer },
+ new object[] { AreaUnit.SquareMile },
+ new object[] { AreaUnit.SquareMillimeter },
+ new object[] { AreaUnit.SquareNauticalMile },
+ new object[] { AreaUnit.SquareYard },
+ new object[] { AreaUnit.UsSurveySquareFoot },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(AreaUnit unit)
{
- var squaremeter = Area.FromSquareMeters(1);
-
- var acreQuantity = squaremeter.ToUnit(AreaUnit.Acre);
- AssertEx.EqualTolerance(AcresInOneSquareMeter, (double)acreQuantity.Value, AcresTolerance);
- Assert.Equal(AreaUnit.Acre, acreQuantity.Unit);
-
- var hectareQuantity = squaremeter.ToUnit(AreaUnit.Hectare);
- AssertEx.EqualTolerance(HectaresInOneSquareMeter, (double)hectareQuantity.Value, HectaresTolerance);
- Assert.Equal(AreaUnit.Hectare, hectareQuantity.Unit);
-
- var squarecentimeterQuantity = squaremeter.ToUnit(AreaUnit.SquareCentimeter);
- AssertEx.EqualTolerance(SquareCentimetersInOneSquareMeter, (double)squarecentimeterQuantity.Value, SquareCentimetersTolerance);
- Assert.Equal(AreaUnit.SquareCentimeter, squarecentimeterQuantity.Unit);
-
- var squaredecimeterQuantity = squaremeter.ToUnit(AreaUnit.SquareDecimeter);
- AssertEx.EqualTolerance(SquareDecimetersInOneSquareMeter, (double)squaredecimeterQuantity.Value, SquareDecimetersTolerance);
- Assert.Equal(AreaUnit.SquareDecimeter, squaredecimeterQuantity.Unit);
-
- var squarefootQuantity = squaremeter.ToUnit(AreaUnit.SquareFoot);
- AssertEx.EqualTolerance(SquareFeetInOneSquareMeter, (double)squarefootQuantity.Value, SquareFeetTolerance);
- Assert.Equal(AreaUnit.SquareFoot, squarefootQuantity.Unit);
-
- var squareinchQuantity = squaremeter.ToUnit(AreaUnit.SquareInch);
- AssertEx.EqualTolerance(SquareInchesInOneSquareMeter, (double)squareinchQuantity.Value, SquareInchesTolerance);
- Assert.Equal(AreaUnit.SquareInch, squareinchQuantity.Unit);
-
- var squarekilometerQuantity = squaremeter.ToUnit(AreaUnit.SquareKilometer);
- AssertEx.EqualTolerance(SquareKilometersInOneSquareMeter, (double)squarekilometerQuantity.Value, SquareKilometersTolerance);
- Assert.Equal(AreaUnit.SquareKilometer, squarekilometerQuantity.Unit);
+ var inBaseUnits = Area.From(1.0, Area.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var squaremeterQuantity = squaremeter.ToUnit(AreaUnit.SquareMeter);
- AssertEx.EqualTolerance(SquareMetersInOneSquareMeter, (double)squaremeterQuantity.Value, SquareMetersTolerance);
- Assert.Equal(AreaUnit.SquareMeter, squaremeterQuantity.Unit);
-
- var squaremicrometerQuantity = squaremeter.ToUnit(AreaUnit.SquareMicrometer);
- AssertEx.EqualTolerance(SquareMicrometersInOneSquareMeter, (double)squaremicrometerQuantity.Value, SquareMicrometersTolerance);
- Assert.Equal(AreaUnit.SquareMicrometer, squaremicrometerQuantity.Unit);
-
- var squaremileQuantity = squaremeter.ToUnit(AreaUnit.SquareMile);
- AssertEx.EqualTolerance(SquareMilesInOneSquareMeter, (double)squaremileQuantity.Value, SquareMilesTolerance);
- Assert.Equal(AreaUnit.SquareMile, squaremileQuantity.Unit);
-
- var squaremillimeterQuantity = squaremeter.ToUnit(AreaUnit.SquareMillimeter);
- AssertEx.EqualTolerance(SquareMillimetersInOneSquareMeter, (double)squaremillimeterQuantity.Value, SquareMillimetersTolerance);
- Assert.Equal(AreaUnit.SquareMillimeter, squaremillimeterQuantity.Unit);
-
- var squarenauticalmileQuantity = squaremeter.ToUnit(AreaUnit.SquareNauticalMile);
- AssertEx.EqualTolerance(SquareNauticalMilesInOneSquareMeter, (double)squarenauticalmileQuantity.Value, SquareNauticalMilesTolerance);
- Assert.Equal(AreaUnit.SquareNauticalMile, squarenauticalmileQuantity.Unit);
-
- var squareyardQuantity = squaremeter.ToUnit(AreaUnit.SquareYard);
- AssertEx.EqualTolerance(SquareYardsInOneSquareMeter, (double)squareyardQuantity.Value, SquareYardsTolerance);
- Assert.Equal(AreaUnit.SquareYard, squareyardQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var ussurveysquarefootQuantity = squaremeter.ToUnit(AreaUnit.UsSurveySquareFoot);
- AssertEx.EqualTolerance(UsSurveySquareFeetInOneSquareMeter, (double)ussurveysquarefootQuantity.Value, UsSurveySquareFeetTolerance);
- Assert.Equal(AreaUnit.UsSurveySquareFoot, ussurveysquarefootQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(AreaUnit unit)
+ {
+ var quantity = Area.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AreaUnit unit)
{
- var quantityInBaseUnit = Area.FromSquareMeters(1).ToBaseUnit();
- Assert.Equal(Area.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Area.Units.FirstOrDefault(u => u != Area.BaseUnit && u != AreaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == AreaUnit.Undefined)
+ fromUnit = Area.BaseUnit;
+
+ var quantity = Area.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs
index dc91874b00..7b0d1ce275 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -93,6 +94,70 @@ public abstract partial class BitRateTestsBase : QuantityTestsBase
protected virtual double TerabytesPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(BitRateUnit unit)
+ {
+ return unit switch
+ {
+ BitRateUnit.BitPerSecond => (BitsPerSecondInOneBitPerSecond, BitsPerSecondTolerance),
+ BitRateUnit.BytePerSecond => (BytesPerSecondInOneBitPerSecond, BytesPerSecondTolerance),
+ BitRateUnit.ExabitPerSecond => (ExabitsPerSecondInOneBitPerSecond, ExabitsPerSecondTolerance),
+ BitRateUnit.ExabytePerSecond => (ExabytesPerSecondInOneBitPerSecond, ExabytesPerSecondTolerance),
+ BitRateUnit.ExbibitPerSecond => (ExbibitsPerSecondInOneBitPerSecond, ExbibitsPerSecondTolerance),
+ BitRateUnit.ExbibytePerSecond => (ExbibytesPerSecondInOneBitPerSecond, ExbibytesPerSecondTolerance),
+ BitRateUnit.GibibitPerSecond => (GibibitsPerSecondInOneBitPerSecond, GibibitsPerSecondTolerance),
+ BitRateUnit.GibibytePerSecond => (GibibytesPerSecondInOneBitPerSecond, GibibytesPerSecondTolerance),
+ BitRateUnit.GigabitPerSecond => (GigabitsPerSecondInOneBitPerSecond, GigabitsPerSecondTolerance),
+ BitRateUnit.GigabytePerSecond => (GigabytesPerSecondInOneBitPerSecond, GigabytesPerSecondTolerance),
+ BitRateUnit.KibibitPerSecond => (KibibitsPerSecondInOneBitPerSecond, KibibitsPerSecondTolerance),
+ BitRateUnit.KibibytePerSecond => (KibibytesPerSecondInOneBitPerSecond, KibibytesPerSecondTolerance),
+ BitRateUnit.KilobitPerSecond => (KilobitsPerSecondInOneBitPerSecond, KilobitsPerSecondTolerance),
+ BitRateUnit.KilobytePerSecond => (KilobytesPerSecondInOneBitPerSecond, KilobytesPerSecondTolerance),
+ BitRateUnit.MebibitPerSecond => (MebibitsPerSecondInOneBitPerSecond, MebibitsPerSecondTolerance),
+ BitRateUnit.MebibytePerSecond => (MebibytesPerSecondInOneBitPerSecond, MebibytesPerSecondTolerance),
+ BitRateUnit.MegabitPerSecond => (MegabitsPerSecondInOneBitPerSecond, MegabitsPerSecondTolerance),
+ BitRateUnit.MegabytePerSecond => (MegabytesPerSecondInOneBitPerSecond, MegabytesPerSecondTolerance),
+ BitRateUnit.PebibitPerSecond => (PebibitsPerSecondInOneBitPerSecond, PebibitsPerSecondTolerance),
+ BitRateUnit.PebibytePerSecond => (PebibytesPerSecondInOneBitPerSecond, PebibytesPerSecondTolerance),
+ BitRateUnit.PetabitPerSecond => (PetabitsPerSecondInOneBitPerSecond, PetabitsPerSecondTolerance),
+ BitRateUnit.PetabytePerSecond => (PetabytesPerSecondInOneBitPerSecond, PetabytesPerSecondTolerance),
+ BitRateUnit.TebibitPerSecond => (TebibitsPerSecondInOneBitPerSecond, TebibitsPerSecondTolerance),
+ BitRateUnit.TebibytePerSecond => (TebibytesPerSecondInOneBitPerSecond, TebibytesPerSecondTolerance),
+ BitRateUnit.TerabitPerSecond => (TerabitsPerSecondInOneBitPerSecond, TerabitsPerSecondTolerance),
+ BitRateUnit.TerabytePerSecond => (TerabytesPerSecondInOneBitPerSecond, TerabytesPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { BitRateUnit.BitPerSecond },
+ new object[] { BitRateUnit.BytePerSecond },
+ new object[] { BitRateUnit.ExabitPerSecond },
+ new object[] { BitRateUnit.ExabytePerSecond },
+ new object[] { BitRateUnit.ExbibitPerSecond },
+ new object[] { BitRateUnit.ExbibytePerSecond },
+ new object[] { BitRateUnit.GibibitPerSecond },
+ new object[] { BitRateUnit.GibibytePerSecond },
+ new object[] { BitRateUnit.GigabitPerSecond },
+ new object[] { BitRateUnit.GigabytePerSecond },
+ new object[] { BitRateUnit.KibibitPerSecond },
+ new object[] { BitRateUnit.KibibytePerSecond },
+ new object[] { BitRateUnit.KilobitPerSecond },
+ new object[] { BitRateUnit.KilobytePerSecond },
+ new object[] { BitRateUnit.MebibitPerSecond },
+ new object[] { BitRateUnit.MebibytePerSecond },
+ new object[] { BitRateUnit.MegabitPerSecond },
+ new object[] { BitRateUnit.MegabytePerSecond },
+ new object[] { BitRateUnit.PebibitPerSecond },
+ new object[] { BitRateUnit.PebibytePerSecond },
+ new object[] { BitRateUnit.PetabitPerSecond },
+ new object[] { BitRateUnit.PetabytePerSecond },
+ new object[] { BitRateUnit.TebibitPerSecond },
+ new object[] { BitRateUnit.TebibytePerSecond },
+ new object[] { BitRateUnit.TerabitPerSecond },
+ new object[] { BitRateUnit.TerabytePerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -339,121 +404,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(BitRateUnit unit)
{
- var bitpersecond = BitRate.FromBitsPerSecond(1);
-
- var bitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.BitPerSecond);
- AssertEx.EqualTolerance(BitsPerSecondInOneBitPerSecond, (double)bitpersecondQuantity.Value, BitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.BitPerSecond, bitpersecondQuantity.Unit);
-
- var bytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.BytePerSecond);
- AssertEx.EqualTolerance(BytesPerSecondInOneBitPerSecond, (double)bytepersecondQuantity.Value, BytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.BytePerSecond, bytepersecondQuantity.Unit);
-
- var exabitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.ExabitPerSecond);
- AssertEx.EqualTolerance(ExabitsPerSecondInOneBitPerSecond, (double)exabitpersecondQuantity.Value, ExabitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.ExabitPerSecond, exabitpersecondQuantity.Unit);
-
- var exabytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.ExabytePerSecond);
- AssertEx.EqualTolerance(ExabytesPerSecondInOneBitPerSecond, (double)exabytepersecondQuantity.Value, ExabytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.ExabytePerSecond, exabytepersecondQuantity.Unit);
-
- var exbibitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.ExbibitPerSecond);
- AssertEx.EqualTolerance(ExbibitsPerSecondInOneBitPerSecond, (double)exbibitpersecondQuantity.Value, ExbibitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.ExbibitPerSecond, exbibitpersecondQuantity.Unit);
-
- var exbibytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.ExbibytePerSecond);
- AssertEx.EqualTolerance(ExbibytesPerSecondInOneBitPerSecond, (double)exbibytepersecondQuantity.Value, ExbibytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.ExbibytePerSecond, exbibytepersecondQuantity.Unit);
-
- var gibibitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.GibibitPerSecond);
- AssertEx.EqualTolerance(GibibitsPerSecondInOneBitPerSecond, (double)gibibitpersecondQuantity.Value, GibibitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.GibibitPerSecond, gibibitpersecondQuantity.Unit);
-
- var gibibytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.GibibytePerSecond);
- AssertEx.EqualTolerance(GibibytesPerSecondInOneBitPerSecond, (double)gibibytepersecondQuantity.Value, GibibytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.GibibytePerSecond, gibibytepersecondQuantity.Unit);
-
- var gigabitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.GigabitPerSecond);
- AssertEx.EqualTolerance(GigabitsPerSecondInOneBitPerSecond, (double)gigabitpersecondQuantity.Value, GigabitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.GigabitPerSecond, gigabitpersecondQuantity.Unit);
-
- var gigabytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.GigabytePerSecond);
- AssertEx.EqualTolerance(GigabytesPerSecondInOneBitPerSecond, (double)gigabytepersecondQuantity.Value, GigabytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.GigabytePerSecond, gigabytepersecondQuantity.Unit);
-
- var kibibitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.KibibitPerSecond);
- AssertEx.EqualTolerance(KibibitsPerSecondInOneBitPerSecond, (double)kibibitpersecondQuantity.Value, KibibitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.KibibitPerSecond, kibibitpersecondQuantity.Unit);
-
- var kibibytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.KibibytePerSecond);
- AssertEx.EqualTolerance(KibibytesPerSecondInOneBitPerSecond, (double)kibibytepersecondQuantity.Value, KibibytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.KibibytePerSecond, kibibytepersecondQuantity.Unit);
-
- var kilobitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.KilobitPerSecond);
- AssertEx.EqualTolerance(KilobitsPerSecondInOneBitPerSecond, (double)kilobitpersecondQuantity.Value, KilobitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.KilobitPerSecond, kilobitpersecondQuantity.Unit);
+ var inBaseUnits = BitRate.From(1.0, BitRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilobytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.KilobytePerSecond);
- AssertEx.EqualTolerance(KilobytesPerSecondInOneBitPerSecond, (double)kilobytepersecondQuantity.Value, KilobytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.KilobytePerSecond, kilobytepersecondQuantity.Unit);
-
- var mebibitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.MebibitPerSecond);
- AssertEx.EqualTolerance(MebibitsPerSecondInOneBitPerSecond, (double)mebibitpersecondQuantity.Value, MebibitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.MebibitPerSecond, mebibitpersecondQuantity.Unit);
-
- var mebibytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.MebibytePerSecond);
- AssertEx.EqualTolerance(MebibytesPerSecondInOneBitPerSecond, (double)mebibytepersecondQuantity.Value, MebibytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.MebibytePerSecond, mebibytepersecondQuantity.Unit);
-
- var megabitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.MegabitPerSecond);
- AssertEx.EqualTolerance(MegabitsPerSecondInOneBitPerSecond, (double)megabitpersecondQuantity.Value, MegabitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.MegabitPerSecond, megabitpersecondQuantity.Unit);
-
- var megabytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.MegabytePerSecond);
- AssertEx.EqualTolerance(MegabytesPerSecondInOneBitPerSecond, (double)megabytepersecondQuantity.Value, MegabytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.MegabytePerSecond, megabytepersecondQuantity.Unit);
-
- var pebibitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.PebibitPerSecond);
- AssertEx.EqualTolerance(PebibitsPerSecondInOneBitPerSecond, (double)pebibitpersecondQuantity.Value, PebibitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.PebibitPerSecond, pebibitpersecondQuantity.Unit);
-
- var pebibytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.PebibytePerSecond);
- AssertEx.EqualTolerance(PebibytesPerSecondInOneBitPerSecond, (double)pebibytepersecondQuantity.Value, PebibytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.PebibytePerSecond, pebibytepersecondQuantity.Unit);
-
- var petabitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.PetabitPerSecond);
- AssertEx.EqualTolerance(PetabitsPerSecondInOneBitPerSecond, (double)petabitpersecondQuantity.Value, PetabitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.PetabitPerSecond, petabitpersecondQuantity.Unit);
-
- var petabytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.PetabytePerSecond);
- AssertEx.EqualTolerance(PetabytesPerSecondInOneBitPerSecond, (double)petabytepersecondQuantity.Value, PetabytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.PetabytePerSecond, petabytepersecondQuantity.Unit);
-
- var tebibitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.TebibitPerSecond);
- AssertEx.EqualTolerance(TebibitsPerSecondInOneBitPerSecond, (double)tebibitpersecondQuantity.Value, TebibitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.TebibitPerSecond, tebibitpersecondQuantity.Unit);
-
- var tebibytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.TebibytePerSecond);
- AssertEx.EqualTolerance(TebibytesPerSecondInOneBitPerSecond, (double)tebibytepersecondQuantity.Value, TebibytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.TebibytePerSecond, tebibytepersecondQuantity.Unit);
-
- var terabitpersecondQuantity = bitpersecond.ToUnit(BitRateUnit.TerabitPerSecond);
- AssertEx.EqualTolerance(TerabitsPerSecondInOneBitPerSecond, (double)terabitpersecondQuantity.Value, TerabitsPerSecondTolerance);
- Assert.Equal(BitRateUnit.TerabitPerSecond, terabitpersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var terabytepersecondQuantity = bitpersecond.ToUnit(BitRateUnit.TerabytePerSecond);
- AssertEx.EqualTolerance(TerabytesPerSecondInOneBitPerSecond, (double)terabytepersecondQuantity.Value, TerabytesPerSecondTolerance);
- Assert.Equal(BitRateUnit.TerabytePerSecond, terabytepersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(BitRateUnit unit)
+ {
+ var quantity = BitRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(BitRateUnit unit)
{
- var quantityInBaseUnit = BitRate.FromBitsPerSecond(1).ToBaseUnit();
- Assert.Equal(BitRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = BitRate.Units.FirstOrDefault(u => u != BitRate.BaseUnit && u != BitRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == BitRateUnit.Undefined)
+ fromUnit = BitRate.BaseUnit;
+
+ var quantity = BitRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs
index e7f822ff7f..cdbd7f043f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class BrakeSpecificFuelConsumptionTestsBase : QuantityTe
protected virtual double PoundsPerMechanicalHorsepowerHourTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(BrakeSpecificFuelConsumptionUnit unit)
+ {
+ return unit switch
+ {
+ BrakeSpecificFuelConsumptionUnit.GramPerKiloWattHour => (GramsPerKiloWattHourInOneKilogramPerJoule, GramsPerKiloWattHourTolerance),
+ BrakeSpecificFuelConsumptionUnit.KilogramPerJoule => (KilogramsPerJouleInOneKilogramPerJoule, KilogramsPerJouleTolerance),
+ BrakeSpecificFuelConsumptionUnit.PoundPerMechanicalHorsepowerHour => (PoundsPerMechanicalHorsepowerHourInOneKilogramPerJoule, PoundsPerMechanicalHorsepowerHourTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { BrakeSpecificFuelConsumptionUnit.GramPerKiloWattHour },
+ new object[] { BrakeSpecificFuelConsumptionUnit.KilogramPerJoule },
+ new object[] { BrakeSpecificFuelConsumptionUnit.PoundPerMechanicalHorsepowerHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(BrakeSpecificFuelConsumptionUnit unit)
{
- var kilogramperjoule = BrakeSpecificFuelConsumption.FromKilogramsPerJoule(1);
+ var inBaseUnits = BrakeSpecificFuelConsumption.From(1.0, BrakeSpecificFuelConsumption.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var gramperkilowatthourQuantity = kilogramperjoule.ToUnit(BrakeSpecificFuelConsumptionUnit.GramPerKiloWattHour);
- AssertEx.EqualTolerance(GramsPerKiloWattHourInOneKilogramPerJoule, (double)gramperkilowatthourQuantity.Value, GramsPerKiloWattHourTolerance);
- Assert.Equal(BrakeSpecificFuelConsumptionUnit.GramPerKiloWattHour, gramperkilowatthourQuantity.Unit);
-
- var kilogramperjouleQuantity = kilogramperjoule.ToUnit(BrakeSpecificFuelConsumptionUnit.KilogramPerJoule);
- AssertEx.EqualTolerance(KilogramsPerJouleInOneKilogramPerJoule, (double)kilogramperjouleQuantity.Value, KilogramsPerJouleTolerance);
- Assert.Equal(BrakeSpecificFuelConsumptionUnit.KilogramPerJoule, kilogramperjouleQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundpermechanicalhorsepowerhourQuantity = kilogramperjoule.ToUnit(BrakeSpecificFuelConsumptionUnit.PoundPerMechanicalHorsepowerHour);
- AssertEx.EqualTolerance(PoundsPerMechanicalHorsepowerHourInOneKilogramPerJoule, (double)poundpermechanicalhorsepowerhourQuantity.Value, PoundsPerMechanicalHorsepowerHourTolerance);
- Assert.Equal(BrakeSpecificFuelConsumptionUnit.PoundPerMechanicalHorsepowerHour, poundpermechanicalhorsepowerhourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(BrakeSpecificFuelConsumptionUnit unit)
+ {
+ var quantity = BrakeSpecificFuelConsumption.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(BrakeSpecificFuelConsumptionUnit unit)
{
- var quantityInBaseUnit = BrakeSpecificFuelConsumption.FromKilogramsPerJoule(1).ToBaseUnit();
- Assert.Equal(BrakeSpecificFuelConsumption.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = BrakeSpecificFuelConsumption.Units.FirstOrDefault(u => u != BrakeSpecificFuelConsumption.BaseUnit && u != BrakeSpecificFuelConsumptionUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == BrakeSpecificFuelConsumptionUnit.Undefined)
+ fromUnit = BrakeSpecificFuelConsumption.BaseUnit;
+
+ var quantity = BrakeSpecificFuelConsumption.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/CapacitanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/CapacitanceTestsBase.g.cs
index 644c0edda3..d6dc3c5031 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/CapacitanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/CapacitanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -55,6 +56,32 @@ public abstract partial class CapacitanceTestsBase : QuantityTestsBase
protected virtual double PicofaradsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(CapacitanceUnit unit)
+ {
+ return unit switch
+ {
+ CapacitanceUnit.Farad => (FaradsInOneFarad, FaradsTolerance),
+ CapacitanceUnit.Kilofarad => (KilofaradsInOneFarad, KilofaradsTolerance),
+ CapacitanceUnit.Megafarad => (MegafaradsInOneFarad, MegafaradsTolerance),
+ CapacitanceUnit.Microfarad => (MicrofaradsInOneFarad, MicrofaradsTolerance),
+ CapacitanceUnit.Millifarad => (MillifaradsInOneFarad, MillifaradsTolerance),
+ CapacitanceUnit.Nanofarad => (NanofaradsInOneFarad, NanofaradsTolerance),
+ CapacitanceUnit.Picofarad => (PicofaradsInOneFarad, PicofaradsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { CapacitanceUnit.Farad },
+ new object[] { CapacitanceUnit.Kilofarad },
+ new object[] { CapacitanceUnit.Megafarad },
+ new object[] { CapacitanceUnit.Microfarad },
+ new object[] { CapacitanceUnit.Millifarad },
+ new object[] { CapacitanceUnit.Nanofarad },
+ new object[] { CapacitanceUnit.Picofarad },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -212,45 +239,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(CapacitanceUnit unit)
{
- var farad = Capacitance.FromFarads(1);
-
- var faradQuantity = farad.ToUnit(CapacitanceUnit.Farad);
- AssertEx.EqualTolerance(FaradsInOneFarad, (double)faradQuantity.Value, FaradsTolerance);
- Assert.Equal(CapacitanceUnit.Farad, faradQuantity.Unit);
-
- var kilofaradQuantity = farad.ToUnit(CapacitanceUnit.Kilofarad);
- AssertEx.EqualTolerance(KilofaradsInOneFarad, (double)kilofaradQuantity.Value, KilofaradsTolerance);
- Assert.Equal(CapacitanceUnit.Kilofarad, kilofaradQuantity.Unit);
+ var inBaseUnits = Capacitance.From(1.0, Capacitance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megafaradQuantity = farad.ToUnit(CapacitanceUnit.Megafarad);
- AssertEx.EqualTolerance(MegafaradsInOneFarad, (double)megafaradQuantity.Value, MegafaradsTolerance);
- Assert.Equal(CapacitanceUnit.Megafarad, megafaradQuantity.Unit);
-
- var microfaradQuantity = farad.ToUnit(CapacitanceUnit.Microfarad);
- AssertEx.EqualTolerance(MicrofaradsInOneFarad, (double)microfaradQuantity.Value, MicrofaradsTolerance);
- Assert.Equal(CapacitanceUnit.Microfarad, microfaradQuantity.Unit);
-
- var millifaradQuantity = farad.ToUnit(CapacitanceUnit.Millifarad);
- AssertEx.EqualTolerance(MillifaradsInOneFarad, (double)millifaradQuantity.Value, MillifaradsTolerance);
- Assert.Equal(CapacitanceUnit.Millifarad, millifaradQuantity.Unit);
-
- var nanofaradQuantity = farad.ToUnit(CapacitanceUnit.Nanofarad);
- AssertEx.EqualTolerance(NanofaradsInOneFarad, (double)nanofaradQuantity.Value, NanofaradsTolerance);
- Assert.Equal(CapacitanceUnit.Nanofarad, nanofaradQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var picofaradQuantity = farad.ToUnit(CapacitanceUnit.Picofarad);
- AssertEx.EqualTolerance(PicofaradsInOneFarad, (double)picofaradQuantity.Value, PicofaradsTolerance);
- Assert.Equal(CapacitanceUnit.Picofarad, picofaradQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(CapacitanceUnit unit)
+ {
+ var quantity = Capacitance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(CapacitanceUnit unit)
{
- var quantityInBaseUnit = Capacitance.FromFarads(1).ToBaseUnit();
- Assert.Equal(Capacitance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Capacitance.Units.FirstOrDefault(u => u != Capacitance.BaseUnit && u != CapacitanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == CapacitanceUnit.Undefined)
+ fromUnit = Capacitance.BaseUnit;
+
+ var quantity = Capacitance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs
index a760f56fc7..b0ac487406 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class CoefficientOfThermalExpansionTestsBase : QuantityT
protected virtual double InverseKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(CoefficientOfThermalExpansionUnit unit)
+ {
+ return unit switch
+ {
+ CoefficientOfThermalExpansionUnit.InverseDegreeCelsius => (InverseDegreeCelsiusInOneInverseKelvin, InverseDegreeCelsiusTolerance),
+ CoefficientOfThermalExpansionUnit.InverseDegreeFahrenheit => (InverseDegreeFahrenheitInOneInverseKelvin, InverseDegreeFahrenheitTolerance),
+ CoefficientOfThermalExpansionUnit.InverseKelvin => (InverseKelvinInOneInverseKelvin, InverseKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { CoefficientOfThermalExpansionUnit.InverseDegreeCelsius },
+ new object[] { CoefficientOfThermalExpansionUnit.InverseDegreeFahrenheit },
+ new object[] { CoefficientOfThermalExpansionUnit.InverseKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(CoefficientOfThermalExpansionUnit unit)
{
- var inversekelvin = CoefficientOfThermalExpansion.FromInverseKelvin(1);
+ var inBaseUnits = CoefficientOfThermalExpansion.From(1.0, CoefficientOfThermalExpansion.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var inversedegreecelsiusQuantity = inversekelvin.ToUnit(CoefficientOfThermalExpansionUnit.InverseDegreeCelsius);
- AssertEx.EqualTolerance(InverseDegreeCelsiusInOneInverseKelvin, (double)inversedegreecelsiusQuantity.Value, InverseDegreeCelsiusTolerance);
- Assert.Equal(CoefficientOfThermalExpansionUnit.InverseDegreeCelsius, inversedegreecelsiusQuantity.Unit);
-
- var inversedegreefahrenheitQuantity = inversekelvin.ToUnit(CoefficientOfThermalExpansionUnit.InverseDegreeFahrenheit);
- AssertEx.EqualTolerance(InverseDegreeFahrenheitInOneInverseKelvin, (double)inversedegreefahrenheitQuantity.Value, InverseDegreeFahrenheitTolerance);
- Assert.Equal(CoefficientOfThermalExpansionUnit.InverseDegreeFahrenheit, inversedegreefahrenheitQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var inversekelvinQuantity = inversekelvin.ToUnit(CoefficientOfThermalExpansionUnit.InverseKelvin);
- AssertEx.EqualTolerance(InverseKelvinInOneInverseKelvin, (double)inversekelvinQuantity.Value, InverseKelvinTolerance);
- Assert.Equal(CoefficientOfThermalExpansionUnit.InverseKelvin, inversekelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(CoefficientOfThermalExpansionUnit unit)
+ {
+ var quantity = CoefficientOfThermalExpansion.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(CoefficientOfThermalExpansionUnit unit)
{
- var quantityInBaseUnit = CoefficientOfThermalExpansion.FromInverseKelvin(1).ToBaseUnit();
- Assert.Equal(CoefficientOfThermalExpansion.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = CoefficientOfThermalExpansion.Units.FirstOrDefault(u => u != CoefficientOfThermalExpansion.BaseUnit && u != CoefficientOfThermalExpansionUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == CoefficientOfThermalExpansionUnit.Undefined)
+ fromUnit = CoefficientOfThermalExpansion.BaseUnit;
+
+ var quantity = CoefficientOfThermalExpansion.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs
index b7bc23a8f1..c7643f3540 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -143,6 +144,120 @@ public abstract partial class DensityTestsBase : QuantityTestsBase
protected virtual double TonnesPerCubicMillimeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(DensityUnit unit)
+ {
+ return unit switch
+ {
+ DensityUnit.CentigramPerDeciliter => (CentigramsPerDeciLiterInOneKilogramPerCubicMeter, CentigramsPerDeciLiterTolerance),
+ DensityUnit.CentigramPerLiter => (CentigramsPerLiterInOneKilogramPerCubicMeter, CentigramsPerLiterTolerance),
+ DensityUnit.CentigramPerMilliliter => (CentigramsPerMilliliterInOneKilogramPerCubicMeter, CentigramsPerMilliliterTolerance),
+ DensityUnit.DecigramPerDeciliter => (DecigramsPerDeciLiterInOneKilogramPerCubicMeter, DecigramsPerDeciLiterTolerance),
+ DensityUnit.DecigramPerLiter => (DecigramsPerLiterInOneKilogramPerCubicMeter, DecigramsPerLiterTolerance),
+ DensityUnit.DecigramPerMilliliter => (DecigramsPerMilliliterInOneKilogramPerCubicMeter, DecigramsPerMilliliterTolerance),
+ DensityUnit.GramPerCubicCentimeter => (GramsPerCubicCentimeterInOneKilogramPerCubicMeter, GramsPerCubicCentimeterTolerance),
+ DensityUnit.GramPerCubicFoot => (GramsPerCubicFootInOneKilogramPerCubicMeter, GramsPerCubicFootTolerance),
+ DensityUnit.GramPerCubicInch => (GramsPerCubicInchInOneKilogramPerCubicMeter, GramsPerCubicInchTolerance),
+ DensityUnit.GramPerCubicMeter => (GramsPerCubicMeterInOneKilogramPerCubicMeter, GramsPerCubicMeterTolerance),
+ DensityUnit.GramPerCubicMillimeter => (GramsPerCubicMillimeterInOneKilogramPerCubicMeter, GramsPerCubicMillimeterTolerance),
+ DensityUnit.GramPerDeciliter => (GramsPerDeciLiterInOneKilogramPerCubicMeter, GramsPerDeciLiterTolerance),
+ DensityUnit.GramPerLiter => (GramsPerLiterInOneKilogramPerCubicMeter, GramsPerLiterTolerance),
+ DensityUnit.GramPerMilliliter => (GramsPerMilliliterInOneKilogramPerCubicMeter, GramsPerMilliliterTolerance),
+ DensityUnit.KilogramPerCubicCentimeter => (KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter, KilogramsPerCubicCentimeterTolerance),
+ DensityUnit.KilogramPerCubicMeter => (KilogramsPerCubicMeterInOneKilogramPerCubicMeter, KilogramsPerCubicMeterTolerance),
+ DensityUnit.KilogramPerCubicMillimeter => (KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter, KilogramsPerCubicMillimeterTolerance),
+ DensityUnit.KilogramPerLiter => (KilogramsPerLiterInOneKilogramPerCubicMeter, KilogramsPerLiterTolerance),
+ DensityUnit.KilopoundPerCubicFoot => (KilopoundsPerCubicFootInOneKilogramPerCubicMeter, KilopoundsPerCubicFootTolerance),
+ DensityUnit.KilopoundPerCubicInch => (KilopoundsPerCubicInchInOneKilogramPerCubicMeter, KilopoundsPerCubicInchTolerance),
+ DensityUnit.MicrogramPerCubicMeter => (MicrogramsPerCubicMeterInOneKilogramPerCubicMeter, MicrogramsPerCubicMeterTolerance),
+ DensityUnit.MicrogramPerDeciliter => (MicrogramsPerDeciLiterInOneKilogramPerCubicMeter, MicrogramsPerDeciLiterTolerance),
+ DensityUnit.MicrogramPerLiter => (MicrogramsPerLiterInOneKilogramPerCubicMeter, MicrogramsPerLiterTolerance),
+ DensityUnit.MicrogramPerMilliliter => (MicrogramsPerMilliliterInOneKilogramPerCubicMeter, MicrogramsPerMilliliterTolerance),
+ DensityUnit.MilligramPerCubicMeter => (MilligramsPerCubicMeterInOneKilogramPerCubicMeter, MilligramsPerCubicMeterTolerance),
+ DensityUnit.MilligramPerDeciliter => (MilligramsPerDeciLiterInOneKilogramPerCubicMeter, MilligramsPerDeciLiterTolerance),
+ DensityUnit.MilligramPerLiter => (MilligramsPerLiterInOneKilogramPerCubicMeter, MilligramsPerLiterTolerance),
+ DensityUnit.MilligramPerMilliliter => (MilligramsPerMilliliterInOneKilogramPerCubicMeter, MilligramsPerMilliliterTolerance),
+ DensityUnit.NanogramPerDeciliter => (NanogramsPerDeciLiterInOneKilogramPerCubicMeter, NanogramsPerDeciLiterTolerance),
+ DensityUnit.NanogramPerLiter => (NanogramsPerLiterInOneKilogramPerCubicMeter, NanogramsPerLiterTolerance),
+ DensityUnit.NanogramPerMilliliter => (NanogramsPerMilliliterInOneKilogramPerCubicMeter, NanogramsPerMilliliterTolerance),
+ DensityUnit.PicogramPerDeciliter => (PicogramsPerDeciLiterInOneKilogramPerCubicMeter, PicogramsPerDeciLiterTolerance),
+ DensityUnit.PicogramPerLiter => (PicogramsPerLiterInOneKilogramPerCubicMeter, PicogramsPerLiterTolerance),
+ DensityUnit.PicogramPerMilliliter => (PicogramsPerMilliliterInOneKilogramPerCubicMeter, PicogramsPerMilliliterTolerance),
+ DensityUnit.PoundPerCubicCentimeter => (PoundsPerCubicCentimeterInOneKilogramPerCubicMeter, PoundsPerCubicCentimeterTolerance),
+ DensityUnit.PoundPerCubicFoot => (PoundsPerCubicFootInOneKilogramPerCubicMeter, PoundsPerCubicFootTolerance),
+ DensityUnit.PoundPerCubicInch => (PoundsPerCubicInchInOneKilogramPerCubicMeter, PoundsPerCubicInchTolerance),
+ DensityUnit.PoundPerCubicMeter => (PoundsPerCubicMeterInOneKilogramPerCubicMeter, PoundsPerCubicMeterTolerance),
+ DensityUnit.PoundPerCubicMillimeter => (PoundsPerCubicMillimeterInOneKilogramPerCubicMeter, PoundsPerCubicMillimeterTolerance),
+ DensityUnit.PoundPerImperialGallon => (PoundsPerImperialGallonInOneKilogramPerCubicMeter, PoundsPerImperialGallonTolerance),
+ DensityUnit.PoundPerUSGallon => (PoundsPerUSGallonInOneKilogramPerCubicMeter, PoundsPerUSGallonTolerance),
+ DensityUnit.SlugPerCubicCentimeter => (SlugsPerCubicCentimeterInOneKilogramPerCubicMeter, SlugsPerCubicCentimeterTolerance),
+ DensityUnit.SlugPerCubicFoot => (SlugsPerCubicFootInOneKilogramPerCubicMeter, SlugsPerCubicFootTolerance),
+ DensityUnit.SlugPerCubicInch => (SlugsPerCubicInchInOneKilogramPerCubicMeter, SlugsPerCubicInchTolerance),
+ DensityUnit.SlugPerCubicMeter => (SlugsPerCubicMeterInOneKilogramPerCubicMeter, SlugsPerCubicMeterTolerance),
+ DensityUnit.SlugPerCubicMillimeter => (SlugsPerCubicMillimeterInOneKilogramPerCubicMeter, SlugsPerCubicMillimeterTolerance),
+ DensityUnit.TonnePerCubicCentimeter => (TonnesPerCubicCentimeterInOneKilogramPerCubicMeter, TonnesPerCubicCentimeterTolerance),
+ DensityUnit.TonnePerCubicFoot => (TonnesPerCubicFootInOneKilogramPerCubicMeter, TonnesPerCubicFootTolerance),
+ DensityUnit.TonnePerCubicInch => (TonnesPerCubicInchInOneKilogramPerCubicMeter, TonnesPerCubicInchTolerance),
+ DensityUnit.TonnePerCubicMeter => (TonnesPerCubicMeterInOneKilogramPerCubicMeter, TonnesPerCubicMeterTolerance),
+ DensityUnit.TonnePerCubicMillimeter => (TonnesPerCubicMillimeterInOneKilogramPerCubicMeter, TonnesPerCubicMillimeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { DensityUnit.CentigramPerDeciliter },
+ new object[] { DensityUnit.CentigramPerLiter },
+ new object[] { DensityUnit.CentigramPerMilliliter },
+ new object[] { DensityUnit.DecigramPerDeciliter },
+ new object[] { DensityUnit.DecigramPerLiter },
+ new object[] { DensityUnit.DecigramPerMilliliter },
+ new object[] { DensityUnit.GramPerCubicCentimeter },
+ new object[] { DensityUnit.GramPerCubicFoot },
+ new object[] { DensityUnit.GramPerCubicInch },
+ new object[] { DensityUnit.GramPerCubicMeter },
+ new object[] { DensityUnit.GramPerCubicMillimeter },
+ new object[] { DensityUnit.GramPerDeciliter },
+ new object[] { DensityUnit.GramPerLiter },
+ new object[] { DensityUnit.GramPerMilliliter },
+ new object[] { DensityUnit.KilogramPerCubicCentimeter },
+ new object[] { DensityUnit.KilogramPerCubicMeter },
+ new object[] { DensityUnit.KilogramPerCubicMillimeter },
+ new object[] { DensityUnit.KilogramPerLiter },
+ new object[] { DensityUnit.KilopoundPerCubicFoot },
+ new object[] { DensityUnit.KilopoundPerCubicInch },
+ new object[] { DensityUnit.MicrogramPerCubicMeter },
+ new object[] { DensityUnit.MicrogramPerDeciliter },
+ new object[] { DensityUnit.MicrogramPerLiter },
+ new object[] { DensityUnit.MicrogramPerMilliliter },
+ new object[] { DensityUnit.MilligramPerCubicMeter },
+ new object[] { DensityUnit.MilligramPerDeciliter },
+ new object[] { DensityUnit.MilligramPerLiter },
+ new object[] { DensityUnit.MilligramPerMilliliter },
+ new object[] { DensityUnit.NanogramPerDeciliter },
+ new object[] { DensityUnit.NanogramPerLiter },
+ new object[] { DensityUnit.NanogramPerMilliliter },
+ new object[] { DensityUnit.PicogramPerDeciliter },
+ new object[] { DensityUnit.PicogramPerLiter },
+ new object[] { DensityUnit.PicogramPerMilliliter },
+ new object[] { DensityUnit.PoundPerCubicCentimeter },
+ new object[] { DensityUnit.PoundPerCubicFoot },
+ new object[] { DensityUnit.PoundPerCubicInch },
+ new object[] { DensityUnit.PoundPerCubicMeter },
+ new object[] { DensityUnit.PoundPerCubicMillimeter },
+ new object[] { DensityUnit.PoundPerImperialGallon },
+ new object[] { DensityUnit.PoundPerUSGallon },
+ new object[] { DensityUnit.SlugPerCubicCentimeter },
+ new object[] { DensityUnit.SlugPerCubicFoot },
+ new object[] { DensityUnit.SlugPerCubicInch },
+ new object[] { DensityUnit.SlugPerCubicMeter },
+ new object[] { DensityUnit.SlugPerCubicMillimeter },
+ new object[] { DensityUnit.TonnePerCubicCentimeter },
+ new object[] { DensityUnit.TonnePerCubicFoot },
+ new object[] { DensityUnit.TonnePerCubicInch },
+ new object[] { DensityUnit.TonnePerCubicMeter },
+ new object[] { DensityUnit.TonnePerCubicMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -564,221 +679,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(DensityUnit unit)
{
- var kilogrampercubicmeter = Density.FromKilogramsPerCubicMeter(1);
-
- var centigramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.CentigramPerDeciliter);
- AssertEx.EqualTolerance(CentigramsPerDeciLiterInOneKilogramPerCubicMeter, (double)centigramperdeciliterQuantity.Value, CentigramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.CentigramPerDeciliter, centigramperdeciliterQuantity.Unit);
-
- var centigramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.CentigramPerLiter);
- AssertEx.EqualTolerance(CentigramsPerLiterInOneKilogramPerCubicMeter, (double)centigramperliterQuantity.Value, CentigramsPerLiterTolerance);
- Assert.Equal(DensityUnit.CentigramPerLiter, centigramperliterQuantity.Unit);
-
- var centigrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.CentigramPerMilliliter);
- AssertEx.EqualTolerance(CentigramsPerMilliliterInOneKilogramPerCubicMeter, (double)centigrampermilliliterQuantity.Value, CentigramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.CentigramPerMilliliter, centigrampermilliliterQuantity.Unit);
-
- var decigramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.DecigramPerDeciliter);
- AssertEx.EqualTolerance(DecigramsPerDeciLiterInOneKilogramPerCubicMeter, (double)decigramperdeciliterQuantity.Value, DecigramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.DecigramPerDeciliter, decigramperdeciliterQuantity.Unit);
-
- var decigramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.DecigramPerLiter);
- AssertEx.EqualTolerance(DecigramsPerLiterInOneKilogramPerCubicMeter, (double)decigramperliterQuantity.Value, DecigramsPerLiterTolerance);
- Assert.Equal(DensityUnit.DecigramPerLiter, decigramperliterQuantity.Unit);
-
- var decigrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.DecigramPerMilliliter);
- AssertEx.EqualTolerance(DecigramsPerMilliliterInOneKilogramPerCubicMeter, (double)decigrampermilliliterQuantity.Value, DecigramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.DecigramPerMilliliter, decigrampermilliliterQuantity.Unit);
-
- var grampercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerCubicCentimeter);
- AssertEx.EqualTolerance(GramsPerCubicCentimeterInOneKilogramPerCubicMeter, (double)grampercubiccentimeterQuantity.Value, GramsPerCubicCentimeterTolerance);
- Assert.Equal(DensityUnit.GramPerCubicCentimeter, grampercubiccentimeterQuantity.Unit);
-
- var grampercubicfootQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerCubicFoot);
- AssertEx.EqualTolerance(GramsPerCubicFootInOneKilogramPerCubicMeter, (double)grampercubicfootQuantity.Value, GramsPerCubicFootTolerance);
- Assert.Equal(DensityUnit.GramPerCubicFoot, grampercubicfootQuantity.Unit);
-
- var grampercubicinchQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerCubicInch);
- AssertEx.EqualTolerance(GramsPerCubicInchInOneKilogramPerCubicMeter, (double)grampercubicinchQuantity.Value, GramsPerCubicInchTolerance);
- Assert.Equal(DensityUnit.GramPerCubicInch, grampercubicinchQuantity.Unit);
-
- var grampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerCubicMeter);
- AssertEx.EqualTolerance(GramsPerCubicMeterInOneKilogramPerCubicMeter, (double)grampercubicmeterQuantity.Value, GramsPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.GramPerCubicMeter, grampercubicmeterQuantity.Unit);
-
- var grampercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerCubicMillimeter);
- AssertEx.EqualTolerance(GramsPerCubicMillimeterInOneKilogramPerCubicMeter, (double)grampercubicmillimeterQuantity.Value, GramsPerCubicMillimeterTolerance);
- Assert.Equal(DensityUnit.GramPerCubicMillimeter, grampercubicmillimeterQuantity.Unit);
-
- var gramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerDeciliter);
- AssertEx.EqualTolerance(GramsPerDeciLiterInOneKilogramPerCubicMeter, (double)gramperdeciliterQuantity.Value, GramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.GramPerDeciliter, gramperdeciliterQuantity.Unit);
-
- var gramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerLiter);
- AssertEx.EqualTolerance(GramsPerLiterInOneKilogramPerCubicMeter, (double)gramperliterQuantity.Value, GramsPerLiterTolerance);
- Assert.Equal(DensityUnit.GramPerLiter, gramperliterQuantity.Unit);
-
- var grampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.GramPerMilliliter);
- AssertEx.EqualTolerance(GramsPerMilliliterInOneKilogramPerCubicMeter, (double)grampermilliliterQuantity.Value, GramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.GramPerMilliliter, grampermilliliterQuantity.Unit);
-
- var kilogrampercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.KilogramPerCubicCentimeter);
- AssertEx.EqualTolerance(KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter, (double)kilogrampercubiccentimeterQuantity.Value, KilogramsPerCubicCentimeterTolerance);
- Assert.Equal(DensityUnit.KilogramPerCubicCentimeter, kilogrampercubiccentimeterQuantity.Unit);
-
- var kilogrampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.KilogramPerCubicMeter);
- AssertEx.EqualTolerance(KilogramsPerCubicMeterInOneKilogramPerCubicMeter, (double)kilogrampercubicmeterQuantity.Value, KilogramsPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.KilogramPerCubicMeter, kilogrampercubicmeterQuantity.Unit);
-
- var kilogrampercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.KilogramPerCubicMillimeter);
- AssertEx.EqualTolerance(KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter, (double)kilogrampercubicmillimeterQuantity.Value, KilogramsPerCubicMillimeterTolerance);
- Assert.Equal(DensityUnit.KilogramPerCubicMillimeter, kilogrampercubicmillimeterQuantity.Unit);
-
- var kilogramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.KilogramPerLiter);
- AssertEx.EqualTolerance(KilogramsPerLiterInOneKilogramPerCubicMeter, (double)kilogramperliterQuantity.Value, KilogramsPerLiterTolerance);
- Assert.Equal(DensityUnit.KilogramPerLiter, kilogramperliterQuantity.Unit);
-
- var kilopoundpercubicfootQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.KilopoundPerCubicFoot);
- AssertEx.EqualTolerance(KilopoundsPerCubicFootInOneKilogramPerCubicMeter, (double)kilopoundpercubicfootQuantity.Value, KilopoundsPerCubicFootTolerance);
- Assert.Equal(DensityUnit.KilopoundPerCubicFoot, kilopoundpercubicfootQuantity.Unit);
-
- var kilopoundpercubicinchQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.KilopoundPerCubicInch);
- AssertEx.EqualTolerance(KilopoundsPerCubicInchInOneKilogramPerCubicMeter, (double)kilopoundpercubicinchQuantity.Value, KilopoundsPerCubicInchTolerance);
- Assert.Equal(DensityUnit.KilopoundPerCubicInch, kilopoundpercubicinchQuantity.Unit);
-
- var microgrampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MicrogramPerCubicMeter);
- AssertEx.EqualTolerance(MicrogramsPerCubicMeterInOneKilogramPerCubicMeter, (double)microgrampercubicmeterQuantity.Value, MicrogramsPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.MicrogramPerCubicMeter, microgrampercubicmeterQuantity.Unit);
-
- var microgramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MicrogramPerDeciliter);
- AssertEx.EqualTolerance(MicrogramsPerDeciLiterInOneKilogramPerCubicMeter, (double)microgramperdeciliterQuantity.Value, MicrogramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.MicrogramPerDeciliter, microgramperdeciliterQuantity.Unit);
-
- var microgramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MicrogramPerLiter);
- AssertEx.EqualTolerance(MicrogramsPerLiterInOneKilogramPerCubicMeter, (double)microgramperliterQuantity.Value, MicrogramsPerLiterTolerance);
- Assert.Equal(DensityUnit.MicrogramPerLiter, microgramperliterQuantity.Unit);
-
- var microgrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MicrogramPerMilliliter);
- AssertEx.EqualTolerance(MicrogramsPerMilliliterInOneKilogramPerCubicMeter, (double)microgrampermilliliterQuantity.Value, MicrogramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.MicrogramPerMilliliter, microgrampermilliliterQuantity.Unit);
+ var inBaseUnits = Density.From(1.0, Density.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var milligrampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MilligramPerCubicMeter);
- AssertEx.EqualTolerance(MilligramsPerCubicMeterInOneKilogramPerCubicMeter, (double)milligrampercubicmeterQuantity.Value, MilligramsPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.MilligramPerCubicMeter, milligrampercubicmeterQuantity.Unit);
-
- var milligramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MilligramPerDeciliter);
- AssertEx.EqualTolerance(MilligramsPerDeciLiterInOneKilogramPerCubicMeter, (double)milligramperdeciliterQuantity.Value, MilligramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.MilligramPerDeciliter, milligramperdeciliterQuantity.Unit);
-
- var milligramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MilligramPerLiter);
- AssertEx.EqualTolerance(MilligramsPerLiterInOneKilogramPerCubicMeter, (double)milligramperliterQuantity.Value, MilligramsPerLiterTolerance);
- Assert.Equal(DensityUnit.MilligramPerLiter, milligramperliterQuantity.Unit);
-
- var milligrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.MilligramPerMilliliter);
- AssertEx.EqualTolerance(MilligramsPerMilliliterInOneKilogramPerCubicMeter, (double)milligrampermilliliterQuantity.Value, MilligramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.MilligramPerMilliliter, milligrampermilliliterQuantity.Unit);
-
- var nanogramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.NanogramPerDeciliter);
- AssertEx.EqualTolerance(NanogramsPerDeciLiterInOneKilogramPerCubicMeter, (double)nanogramperdeciliterQuantity.Value, NanogramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.NanogramPerDeciliter, nanogramperdeciliterQuantity.Unit);
-
- var nanogramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.NanogramPerLiter);
- AssertEx.EqualTolerance(NanogramsPerLiterInOneKilogramPerCubicMeter, (double)nanogramperliterQuantity.Value, NanogramsPerLiterTolerance);
- Assert.Equal(DensityUnit.NanogramPerLiter, nanogramperliterQuantity.Unit);
-
- var nanogrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.NanogramPerMilliliter);
- AssertEx.EqualTolerance(NanogramsPerMilliliterInOneKilogramPerCubicMeter, (double)nanogrampermilliliterQuantity.Value, NanogramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.NanogramPerMilliliter, nanogrampermilliliterQuantity.Unit);
-
- var picogramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PicogramPerDeciliter);
- AssertEx.EqualTolerance(PicogramsPerDeciLiterInOneKilogramPerCubicMeter, (double)picogramperdeciliterQuantity.Value, PicogramsPerDeciLiterTolerance);
- Assert.Equal(DensityUnit.PicogramPerDeciliter, picogramperdeciliterQuantity.Unit);
-
- var picogramperliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PicogramPerLiter);
- AssertEx.EqualTolerance(PicogramsPerLiterInOneKilogramPerCubicMeter, (double)picogramperliterQuantity.Value, PicogramsPerLiterTolerance);
- Assert.Equal(DensityUnit.PicogramPerLiter, picogramperliterQuantity.Unit);
-
- var picogrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PicogramPerMilliliter);
- AssertEx.EqualTolerance(PicogramsPerMilliliterInOneKilogramPerCubicMeter, (double)picogrampermilliliterQuantity.Value, PicogramsPerMilliliterTolerance);
- Assert.Equal(DensityUnit.PicogramPerMilliliter, picogrampermilliliterQuantity.Unit);
-
- var poundpercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerCubicCentimeter);
- AssertEx.EqualTolerance(PoundsPerCubicCentimeterInOneKilogramPerCubicMeter, (double)poundpercubiccentimeterQuantity.Value, PoundsPerCubicCentimeterTolerance);
- Assert.Equal(DensityUnit.PoundPerCubicCentimeter, poundpercubiccentimeterQuantity.Unit);
-
- var poundpercubicfootQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerCubicFoot);
- AssertEx.EqualTolerance(PoundsPerCubicFootInOneKilogramPerCubicMeter, (double)poundpercubicfootQuantity.Value, PoundsPerCubicFootTolerance);
- Assert.Equal(DensityUnit.PoundPerCubicFoot, poundpercubicfootQuantity.Unit);
-
- var poundpercubicinchQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerCubicInch);
- AssertEx.EqualTolerance(PoundsPerCubicInchInOneKilogramPerCubicMeter, (double)poundpercubicinchQuantity.Value, PoundsPerCubicInchTolerance);
- Assert.Equal(DensityUnit.PoundPerCubicInch, poundpercubicinchQuantity.Unit);
-
- var poundpercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerCubicMeter);
- AssertEx.EqualTolerance(PoundsPerCubicMeterInOneKilogramPerCubicMeter, (double)poundpercubicmeterQuantity.Value, PoundsPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.PoundPerCubicMeter, poundpercubicmeterQuantity.Unit);
-
- var poundpercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerCubicMillimeter);
- AssertEx.EqualTolerance(PoundsPerCubicMillimeterInOneKilogramPerCubicMeter, (double)poundpercubicmillimeterQuantity.Value, PoundsPerCubicMillimeterTolerance);
- Assert.Equal(DensityUnit.PoundPerCubicMillimeter, poundpercubicmillimeterQuantity.Unit);
-
- var poundperimperialgallonQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerImperialGallon);
- AssertEx.EqualTolerance(PoundsPerImperialGallonInOneKilogramPerCubicMeter, (double)poundperimperialgallonQuantity.Value, PoundsPerImperialGallonTolerance);
- Assert.Equal(DensityUnit.PoundPerImperialGallon, poundperimperialgallonQuantity.Unit);
-
- var poundperusgallonQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.PoundPerUSGallon);
- AssertEx.EqualTolerance(PoundsPerUSGallonInOneKilogramPerCubicMeter, (double)poundperusgallonQuantity.Value, PoundsPerUSGallonTolerance);
- Assert.Equal(DensityUnit.PoundPerUSGallon, poundperusgallonQuantity.Unit);
-
- var slugpercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.SlugPerCubicCentimeter);
- AssertEx.EqualTolerance(SlugsPerCubicCentimeterInOneKilogramPerCubicMeter, (double)slugpercubiccentimeterQuantity.Value, SlugsPerCubicCentimeterTolerance);
- Assert.Equal(DensityUnit.SlugPerCubicCentimeter, slugpercubiccentimeterQuantity.Unit);
-
- var slugpercubicfootQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.SlugPerCubicFoot);
- AssertEx.EqualTolerance(SlugsPerCubicFootInOneKilogramPerCubicMeter, (double)slugpercubicfootQuantity.Value, SlugsPerCubicFootTolerance);
- Assert.Equal(DensityUnit.SlugPerCubicFoot, slugpercubicfootQuantity.Unit);
-
- var slugpercubicinchQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.SlugPerCubicInch);
- AssertEx.EqualTolerance(SlugsPerCubicInchInOneKilogramPerCubicMeter, (double)slugpercubicinchQuantity.Value, SlugsPerCubicInchTolerance);
- Assert.Equal(DensityUnit.SlugPerCubicInch, slugpercubicinchQuantity.Unit);
-
- var slugpercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.SlugPerCubicMeter);
- AssertEx.EqualTolerance(SlugsPerCubicMeterInOneKilogramPerCubicMeter, (double)slugpercubicmeterQuantity.Value, SlugsPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.SlugPerCubicMeter, slugpercubicmeterQuantity.Unit);
-
- var slugpercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.SlugPerCubicMillimeter);
- AssertEx.EqualTolerance(SlugsPerCubicMillimeterInOneKilogramPerCubicMeter, (double)slugpercubicmillimeterQuantity.Value, SlugsPerCubicMillimeterTolerance);
- Assert.Equal(DensityUnit.SlugPerCubicMillimeter, slugpercubicmillimeterQuantity.Unit);
-
- var tonnepercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.TonnePerCubicCentimeter);
- AssertEx.EqualTolerance(TonnesPerCubicCentimeterInOneKilogramPerCubicMeter, (double)tonnepercubiccentimeterQuantity.Value, TonnesPerCubicCentimeterTolerance);
- Assert.Equal(DensityUnit.TonnePerCubicCentimeter, tonnepercubiccentimeterQuantity.Unit);
-
- var tonnepercubicfootQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.TonnePerCubicFoot);
- AssertEx.EqualTolerance(TonnesPerCubicFootInOneKilogramPerCubicMeter, (double)tonnepercubicfootQuantity.Value, TonnesPerCubicFootTolerance);
- Assert.Equal(DensityUnit.TonnePerCubicFoot, tonnepercubicfootQuantity.Unit);
-
- var tonnepercubicinchQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.TonnePerCubicInch);
- AssertEx.EqualTolerance(TonnesPerCubicInchInOneKilogramPerCubicMeter, (double)tonnepercubicinchQuantity.Value, TonnesPerCubicInchTolerance);
- Assert.Equal(DensityUnit.TonnePerCubicInch, tonnepercubicinchQuantity.Unit);
-
- var tonnepercubicmeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.TonnePerCubicMeter);
- AssertEx.EqualTolerance(TonnesPerCubicMeterInOneKilogramPerCubicMeter, (double)tonnepercubicmeterQuantity.Value, TonnesPerCubicMeterTolerance);
- Assert.Equal(DensityUnit.TonnePerCubicMeter, tonnepercubicmeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonnepercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(DensityUnit.TonnePerCubicMillimeter);
- AssertEx.EqualTolerance(TonnesPerCubicMillimeterInOneKilogramPerCubicMeter, (double)tonnepercubicmillimeterQuantity.Value, TonnesPerCubicMillimeterTolerance);
- Assert.Equal(DensityUnit.TonnePerCubicMillimeter, tonnepercubicmillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(DensityUnit unit)
+ {
+ var quantity = Density.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(DensityUnit unit)
{
- var quantityInBaseUnit = Density.FromKilogramsPerCubicMeter(1).ToBaseUnit();
- Assert.Equal(Density.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Density.Units.FirstOrDefault(u => u != Density.BaseUnit && u != DensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == DensityUnit.Undefined)
+ fromUnit = Density.BaseUnit;
+
+ var quantity = Density.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs
index 35023e74cb..bb1ca8753d 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -63,6 +64,40 @@ public abstract partial class DurationTestsBase : QuantityTestsBase
protected virtual double Years365Tolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(DurationUnit unit)
+ {
+ return unit switch
+ {
+ DurationUnit.Day => (DaysInOneSecond, DaysTolerance),
+ DurationUnit.Hour => (HoursInOneSecond, HoursTolerance),
+ DurationUnit.JulianYear => (JulianYearsInOneSecond, JulianYearsTolerance),
+ DurationUnit.Microsecond => (MicrosecondsInOneSecond, MicrosecondsTolerance),
+ DurationUnit.Millisecond => (MillisecondsInOneSecond, MillisecondsTolerance),
+ DurationUnit.Minute => (MinutesInOneSecond, MinutesTolerance),
+ DurationUnit.Month30 => (Months30InOneSecond, Months30Tolerance),
+ DurationUnit.Nanosecond => (NanosecondsInOneSecond, NanosecondsTolerance),
+ DurationUnit.Second => (SecondsInOneSecond, SecondsTolerance),
+ DurationUnit.Week => (WeeksInOneSecond, WeeksTolerance),
+ DurationUnit.Year365 => (Years365InOneSecond, Years365Tolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { DurationUnit.Day },
+ new object[] { DurationUnit.Hour },
+ new object[] { DurationUnit.JulianYear },
+ new object[] { DurationUnit.Microsecond },
+ new object[] { DurationUnit.Millisecond },
+ new object[] { DurationUnit.Minute },
+ new object[] { DurationUnit.Month30 },
+ new object[] { DurationUnit.Nanosecond },
+ new object[] { DurationUnit.Second },
+ new object[] { DurationUnit.Week },
+ new object[] { DurationUnit.Year365 },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -244,61 +279,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(DurationUnit unit)
{
- var second = Duration.FromSeconds(1);
-
- var dayQuantity = second.ToUnit(DurationUnit.Day);
- AssertEx.EqualTolerance(DaysInOneSecond, (double)dayQuantity.Value, DaysTolerance);
- Assert.Equal(DurationUnit.Day, dayQuantity.Unit);
-
- var hourQuantity = second.ToUnit(DurationUnit.Hour);
- AssertEx.EqualTolerance(HoursInOneSecond, (double)hourQuantity.Value, HoursTolerance);
- Assert.Equal(DurationUnit.Hour, hourQuantity.Unit);
-
- var julianyearQuantity = second.ToUnit(DurationUnit.JulianYear);
- AssertEx.EqualTolerance(JulianYearsInOneSecond, (double)julianyearQuantity.Value, JulianYearsTolerance);
- Assert.Equal(DurationUnit.JulianYear, julianyearQuantity.Unit);
-
- var microsecondQuantity = second.ToUnit(DurationUnit.Microsecond);
- AssertEx.EqualTolerance(MicrosecondsInOneSecond, (double)microsecondQuantity.Value, MicrosecondsTolerance);
- Assert.Equal(DurationUnit.Microsecond, microsecondQuantity.Unit);
-
- var millisecondQuantity = second.ToUnit(DurationUnit.Millisecond);
- AssertEx.EqualTolerance(MillisecondsInOneSecond, (double)millisecondQuantity.Value, MillisecondsTolerance);
- Assert.Equal(DurationUnit.Millisecond, millisecondQuantity.Unit);
+ var inBaseUnits = Duration.From(1.0, Duration.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var minuteQuantity = second.ToUnit(DurationUnit.Minute);
- AssertEx.EqualTolerance(MinutesInOneSecond, (double)minuteQuantity.Value, MinutesTolerance);
- Assert.Equal(DurationUnit.Minute, minuteQuantity.Unit);
-
- var month30Quantity = second.ToUnit(DurationUnit.Month30);
- AssertEx.EqualTolerance(Months30InOneSecond, (double)month30Quantity.Value, Months30Tolerance);
- Assert.Equal(DurationUnit.Month30, month30Quantity.Unit);
-
- var nanosecondQuantity = second.ToUnit(DurationUnit.Nanosecond);
- AssertEx.EqualTolerance(NanosecondsInOneSecond, (double)nanosecondQuantity.Value, NanosecondsTolerance);
- Assert.Equal(DurationUnit.Nanosecond, nanosecondQuantity.Unit);
-
- var secondQuantity = second.ToUnit(DurationUnit.Second);
- AssertEx.EqualTolerance(SecondsInOneSecond, (double)secondQuantity.Value, SecondsTolerance);
- Assert.Equal(DurationUnit.Second, secondQuantity.Unit);
-
- var weekQuantity = second.ToUnit(DurationUnit.Week);
- AssertEx.EqualTolerance(WeeksInOneSecond, (double)weekQuantity.Value, WeeksTolerance);
- Assert.Equal(DurationUnit.Week, weekQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var year365Quantity = second.ToUnit(DurationUnit.Year365);
- AssertEx.EqualTolerance(Years365InOneSecond, (double)year365Quantity.Value, Years365Tolerance);
- Assert.Equal(DurationUnit.Year365, year365Quantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(DurationUnit unit)
+ {
+ var quantity = Duration.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(DurationUnit unit)
{
- var quantityInBaseUnit = Duration.FromSeconds(1).ToBaseUnit();
- Assert.Equal(Duration.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Duration.Units.FirstOrDefault(u => u != Duration.BaseUnit && u != DurationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == DurationUnit.Undefined)
+ fromUnit = Duration.BaseUnit;
+
+ var quantity = Duration.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs
index 47b7e6934f..743e6be92f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -61,6 +62,38 @@ public abstract partial class DynamicViscosityTestsBase : QuantityTestsBase
protected virtual double ReynsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(DynamicViscosityUnit unit)
+ {
+ return unit switch
+ {
+ DynamicViscosityUnit.Centipoise => (CentipoiseInOneNewtonSecondPerMeterSquared, CentipoiseTolerance),
+ DynamicViscosityUnit.MicropascalSecond => (MicropascalSecondsInOneNewtonSecondPerMeterSquared, MicropascalSecondsTolerance),
+ DynamicViscosityUnit.MillipascalSecond => (MillipascalSecondsInOneNewtonSecondPerMeterSquared, MillipascalSecondsTolerance),
+ DynamicViscosityUnit.NewtonSecondPerMeterSquared => (NewtonSecondsPerMeterSquaredInOneNewtonSecondPerMeterSquared, NewtonSecondsPerMeterSquaredTolerance),
+ DynamicViscosityUnit.PascalSecond => (PascalSecondsInOneNewtonSecondPerMeterSquared, PascalSecondsTolerance),
+ DynamicViscosityUnit.Poise => (PoiseInOneNewtonSecondPerMeterSquared, PoiseTolerance),
+ DynamicViscosityUnit.PoundForceSecondPerSquareFoot => (PoundsForceSecondPerSquareFootInOneNewtonSecondPerMeterSquared, PoundsForceSecondPerSquareFootTolerance),
+ DynamicViscosityUnit.PoundForceSecondPerSquareInch => (PoundsForceSecondPerSquareInchInOneNewtonSecondPerMeterSquared, PoundsForceSecondPerSquareInchTolerance),
+ DynamicViscosityUnit.PoundPerFootSecond => (PoundsPerFootSecondInOneNewtonSecondPerMeterSquared, PoundsPerFootSecondTolerance),
+ DynamicViscosityUnit.Reyn => (ReynsInOneNewtonSecondPerMeterSquared, ReynsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { DynamicViscosityUnit.Centipoise },
+ new object[] { DynamicViscosityUnit.MicropascalSecond },
+ new object[] { DynamicViscosityUnit.MillipascalSecond },
+ new object[] { DynamicViscosityUnit.NewtonSecondPerMeterSquared },
+ new object[] { DynamicViscosityUnit.PascalSecond },
+ new object[] { DynamicViscosityUnit.Poise },
+ new object[] { DynamicViscosityUnit.PoundForceSecondPerSquareFoot },
+ new object[] { DynamicViscosityUnit.PoundForceSecondPerSquareInch },
+ new object[] { DynamicViscosityUnit.PoundPerFootSecond },
+ new object[] { DynamicViscosityUnit.Reyn },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -236,57 +269,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(DynamicViscosityUnit unit)
{
- var newtonsecondpermetersquared = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1);
-
- var centipoiseQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.Centipoise);
- AssertEx.EqualTolerance(CentipoiseInOneNewtonSecondPerMeterSquared, (double)centipoiseQuantity.Value, CentipoiseTolerance);
- Assert.Equal(DynamicViscosityUnit.Centipoise, centipoiseQuantity.Unit);
-
- var micropascalsecondQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.MicropascalSecond);
- AssertEx.EqualTolerance(MicropascalSecondsInOneNewtonSecondPerMeterSquared, (double)micropascalsecondQuantity.Value, MicropascalSecondsTolerance);
- Assert.Equal(DynamicViscosityUnit.MicropascalSecond, micropascalsecondQuantity.Unit);
-
- var millipascalsecondQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.MillipascalSecond);
- AssertEx.EqualTolerance(MillipascalSecondsInOneNewtonSecondPerMeterSquared, (double)millipascalsecondQuantity.Value, MillipascalSecondsTolerance);
- Assert.Equal(DynamicViscosityUnit.MillipascalSecond, millipascalsecondQuantity.Unit);
-
- var newtonsecondpermetersquaredQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.NewtonSecondPerMeterSquared);
- AssertEx.EqualTolerance(NewtonSecondsPerMeterSquaredInOneNewtonSecondPerMeterSquared, (double)newtonsecondpermetersquaredQuantity.Value, NewtonSecondsPerMeterSquaredTolerance);
- Assert.Equal(DynamicViscosityUnit.NewtonSecondPerMeterSquared, newtonsecondpermetersquaredQuantity.Unit);
-
- var pascalsecondQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.PascalSecond);
- AssertEx.EqualTolerance(PascalSecondsInOneNewtonSecondPerMeterSquared, (double)pascalsecondQuantity.Value, PascalSecondsTolerance);
- Assert.Equal(DynamicViscosityUnit.PascalSecond, pascalsecondQuantity.Unit);
+ var inBaseUnits = DynamicViscosity.From(1.0, DynamicViscosity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var poiseQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.Poise);
- AssertEx.EqualTolerance(PoiseInOneNewtonSecondPerMeterSquared, (double)poiseQuantity.Value, PoiseTolerance);
- Assert.Equal(DynamicViscosityUnit.Poise, poiseQuantity.Unit);
-
- var poundforcesecondpersquarefootQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.PoundForceSecondPerSquareFoot);
- AssertEx.EqualTolerance(PoundsForceSecondPerSquareFootInOneNewtonSecondPerMeterSquared, (double)poundforcesecondpersquarefootQuantity.Value, PoundsForceSecondPerSquareFootTolerance);
- Assert.Equal(DynamicViscosityUnit.PoundForceSecondPerSquareFoot, poundforcesecondpersquarefootQuantity.Unit);
-
- var poundforcesecondpersquareinchQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.PoundForceSecondPerSquareInch);
- AssertEx.EqualTolerance(PoundsForceSecondPerSquareInchInOneNewtonSecondPerMeterSquared, (double)poundforcesecondpersquareinchQuantity.Value, PoundsForceSecondPerSquareInchTolerance);
- Assert.Equal(DynamicViscosityUnit.PoundForceSecondPerSquareInch, poundforcesecondpersquareinchQuantity.Unit);
-
- var poundperfootsecondQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.PoundPerFootSecond);
- AssertEx.EqualTolerance(PoundsPerFootSecondInOneNewtonSecondPerMeterSquared, (double)poundperfootsecondQuantity.Value, PoundsPerFootSecondTolerance);
- Assert.Equal(DynamicViscosityUnit.PoundPerFootSecond, poundperfootsecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var reynQuantity = newtonsecondpermetersquared.ToUnit(DynamicViscosityUnit.Reyn);
- AssertEx.EqualTolerance(ReynsInOneNewtonSecondPerMeterSquared, (double)reynQuantity.Value, ReynsTolerance);
- Assert.Equal(DynamicViscosityUnit.Reyn, reynQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(DynamicViscosityUnit unit)
+ {
+ var quantity = DynamicViscosity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(DynamicViscosityUnit unit)
{
- var quantityInBaseUnit = DynamicViscosity.FromNewtonSecondsPerMeterSquared(1).ToBaseUnit();
- Assert.Equal(DynamicViscosity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = DynamicViscosity.Units.FirstOrDefault(u => u != DynamicViscosity.BaseUnit && u != DynamicViscosityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == DynamicViscosityUnit.Undefined)
+ fromUnit = DynamicViscosity.BaseUnit;
+
+ var quantity = DynamicViscosity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricAdmittanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricAdmittanceTestsBase.g.cs
index d855cedc5c..b4c6557bde 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricAdmittanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricAdmittanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class ElectricAdmittanceTestsBase : QuantityTestsBase
protected virtual double SiemensTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricAdmittanceUnit unit)
+ {
+ return unit switch
+ {
+ ElectricAdmittanceUnit.Microsiemens => (MicrosiemensInOneSiemens, MicrosiemensTolerance),
+ ElectricAdmittanceUnit.Millisiemens => (MillisiemensInOneSiemens, MillisiemensTolerance),
+ ElectricAdmittanceUnit.Nanosiemens => (NanosiemensInOneSiemens, NanosiemensTolerance),
+ ElectricAdmittanceUnit.Siemens => (SiemensInOneSiemens, SiemensTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricAdmittanceUnit.Microsiemens },
+ new object[] { ElectricAdmittanceUnit.Millisiemens },
+ new object[] { ElectricAdmittanceUnit.Nanosiemens },
+ new object[] { ElectricAdmittanceUnit.Siemens },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricAdmittanceUnit unit)
{
- var siemens = ElectricAdmittance.FromSiemens(1);
+ var inBaseUnits = ElectricAdmittance.From(1.0, ElectricAdmittance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microsiemensQuantity = siemens.ToUnit(ElectricAdmittanceUnit.Microsiemens);
- AssertEx.EqualTolerance(MicrosiemensInOneSiemens, (double)microsiemensQuantity.Value, MicrosiemensTolerance);
- Assert.Equal(ElectricAdmittanceUnit.Microsiemens, microsiemensQuantity.Unit);
-
- var millisiemensQuantity = siemens.ToUnit(ElectricAdmittanceUnit.Millisiemens);
- AssertEx.EqualTolerance(MillisiemensInOneSiemens, (double)millisiemensQuantity.Value, MillisiemensTolerance);
- Assert.Equal(ElectricAdmittanceUnit.Millisiemens, millisiemensQuantity.Unit);
-
- var nanosiemensQuantity = siemens.ToUnit(ElectricAdmittanceUnit.Nanosiemens);
- AssertEx.EqualTolerance(NanosiemensInOneSiemens, (double)nanosiemensQuantity.Value, NanosiemensTolerance);
- Assert.Equal(ElectricAdmittanceUnit.Nanosiemens, nanosiemensQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var siemensQuantity = siemens.ToUnit(ElectricAdmittanceUnit.Siemens);
- AssertEx.EqualTolerance(SiemensInOneSiemens, (double)siemensQuantity.Value, SiemensTolerance);
- Assert.Equal(ElectricAdmittanceUnit.Siemens, siemensQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricAdmittanceUnit unit)
+ {
+ var quantity = ElectricAdmittance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricAdmittanceUnit unit)
{
- var quantityInBaseUnit = ElectricAdmittance.FromSiemens(1).ToBaseUnit();
- Assert.Equal(ElectricAdmittance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricAdmittance.Units.FirstOrDefault(u => u != ElectricAdmittance.BaseUnit && u != ElectricAdmittanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricAdmittanceUnit.Undefined)
+ fromUnit = ElectricAdmittance.BaseUnit;
+
+ var quantity = ElectricAdmittance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeDensityTestsBase.g.cs
index c714108b47..17e05c7b60 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class ElectricChargeDensityTestsBase : QuantityTestsBase
protected virtual double CoulombsPerCubicMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricChargeDensityUnit unit)
+ {
+ return unit switch
+ {
+ ElectricChargeDensityUnit.CoulombPerCubicMeter => (CoulombsPerCubicMeterInOneCoulombPerCubicMeter, CoulombsPerCubicMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricChargeDensityUnit.CoulombPerCubicMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricChargeDensityUnit unit)
{
- var coulombpercubicmeter = ElectricChargeDensity.FromCoulombsPerCubicMeter(1);
+ var inBaseUnits = ElectricChargeDensity.From(1.0, ElectricChargeDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var coulombpercubicmeterQuantity = coulombpercubicmeter.ToUnit(ElectricChargeDensityUnit.CoulombPerCubicMeter);
- AssertEx.EqualTolerance(CoulombsPerCubicMeterInOneCoulombPerCubicMeter, (double)coulombpercubicmeterQuantity.Value, CoulombsPerCubicMeterTolerance);
- Assert.Equal(ElectricChargeDensityUnit.CoulombPerCubicMeter, coulombpercubicmeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricChargeDensityUnit unit)
+ {
+ var quantity = ElectricChargeDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricChargeDensityUnit unit)
{
- var quantityInBaseUnit = ElectricChargeDensity.FromCoulombsPerCubicMeter(1).ToBaseUnit();
- Assert.Equal(ElectricChargeDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricChargeDensity.Units.FirstOrDefault(u => u != ElectricChargeDensity.BaseUnit && u != ElectricChargeDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricChargeDensityUnit.Undefined)
+ fromUnit = ElectricChargeDensity.BaseUnit;
+
+ var quantity = ElectricChargeDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeTestsBase.g.cs
index ba6b76acbb..7fefdc564b 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricChargeTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -51,6 +52,28 @@ public abstract partial class ElectricChargeTestsBase : QuantityTestsBase
protected virtual double MilliampereHoursTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricChargeUnit unit)
+ {
+ return unit switch
+ {
+ ElectricChargeUnit.AmpereHour => (AmpereHoursInOneCoulomb, AmpereHoursTolerance),
+ ElectricChargeUnit.Coulomb => (CoulombsInOneCoulomb, CoulombsTolerance),
+ ElectricChargeUnit.KiloampereHour => (KiloampereHoursInOneCoulomb, KiloampereHoursTolerance),
+ ElectricChargeUnit.MegaampereHour => (MegaampereHoursInOneCoulomb, MegaampereHoursTolerance),
+ ElectricChargeUnit.MilliampereHour => (MilliampereHoursInOneCoulomb, MilliampereHoursTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricChargeUnit.AmpereHour },
+ new object[] { ElectricChargeUnit.Coulomb },
+ new object[] { ElectricChargeUnit.KiloampereHour },
+ new object[] { ElectricChargeUnit.MegaampereHour },
+ new object[] { ElectricChargeUnit.MilliampereHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -196,37 +219,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricChargeUnit unit)
{
- var coulomb = ElectricCharge.FromCoulombs(1);
-
- var amperehourQuantity = coulomb.ToUnit(ElectricChargeUnit.AmpereHour);
- AssertEx.EqualTolerance(AmpereHoursInOneCoulomb, (double)amperehourQuantity.Value, AmpereHoursTolerance);
- Assert.Equal(ElectricChargeUnit.AmpereHour, amperehourQuantity.Unit);
+ var inBaseUnits = ElectricCharge.From(1.0, ElectricCharge.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var coulombQuantity = coulomb.ToUnit(ElectricChargeUnit.Coulomb);
- AssertEx.EqualTolerance(CoulombsInOneCoulomb, (double)coulombQuantity.Value, CoulombsTolerance);
- Assert.Equal(ElectricChargeUnit.Coulomb, coulombQuantity.Unit);
-
- var kiloamperehourQuantity = coulomb.ToUnit(ElectricChargeUnit.KiloampereHour);
- AssertEx.EqualTolerance(KiloampereHoursInOneCoulomb, (double)kiloamperehourQuantity.Value, KiloampereHoursTolerance);
- Assert.Equal(ElectricChargeUnit.KiloampereHour, kiloamperehourQuantity.Unit);
-
- var megaamperehourQuantity = coulomb.ToUnit(ElectricChargeUnit.MegaampereHour);
- AssertEx.EqualTolerance(MegaampereHoursInOneCoulomb, (double)megaamperehourQuantity.Value, MegaampereHoursTolerance);
- Assert.Equal(ElectricChargeUnit.MegaampereHour, megaamperehourQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var milliamperehourQuantity = coulomb.ToUnit(ElectricChargeUnit.MilliampereHour);
- AssertEx.EqualTolerance(MilliampereHoursInOneCoulomb, (double)milliamperehourQuantity.Value, MilliampereHoursTolerance);
- Assert.Equal(ElectricChargeUnit.MilliampereHour, milliamperehourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricChargeUnit unit)
+ {
+ var quantity = ElectricCharge.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricChargeUnit unit)
{
- var quantityInBaseUnit = ElectricCharge.FromCoulombs(1).ToBaseUnit();
- Assert.Equal(ElectricCharge.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricCharge.Units.FirstOrDefault(u => u != ElectricCharge.BaseUnit && u != ElectricChargeUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricChargeUnit.Undefined)
+ fromUnit = ElectricCharge.BaseUnit;
+
+ var quantity = ElectricCharge.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductanceTestsBase.g.cs
index 3b348ba006..1e4cd156e5 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class ElectricConductanceTestsBase : QuantityTestsBase
protected virtual double SiemensTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricConductanceUnit unit)
+ {
+ return unit switch
+ {
+ ElectricConductanceUnit.Microsiemens => (MicrosiemensInOneSiemens, MicrosiemensTolerance),
+ ElectricConductanceUnit.Millisiemens => (MillisiemensInOneSiemens, MillisiemensTolerance),
+ ElectricConductanceUnit.Siemens => (SiemensInOneSiemens, SiemensTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricConductanceUnit.Microsiemens },
+ new object[] { ElectricConductanceUnit.Millisiemens },
+ new object[] { ElectricConductanceUnit.Siemens },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricConductanceUnit unit)
{
- var siemens = ElectricConductance.FromSiemens(1);
+ var inBaseUnits = ElectricConductance.From(1.0, ElectricConductance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microsiemensQuantity = siemens.ToUnit(ElectricConductanceUnit.Microsiemens);
- AssertEx.EqualTolerance(MicrosiemensInOneSiemens, (double)microsiemensQuantity.Value, MicrosiemensTolerance);
- Assert.Equal(ElectricConductanceUnit.Microsiemens, microsiemensQuantity.Unit);
-
- var millisiemensQuantity = siemens.ToUnit(ElectricConductanceUnit.Millisiemens);
- AssertEx.EqualTolerance(MillisiemensInOneSiemens, (double)millisiemensQuantity.Value, MillisiemensTolerance);
- Assert.Equal(ElectricConductanceUnit.Millisiemens, millisiemensQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var siemensQuantity = siemens.ToUnit(ElectricConductanceUnit.Siemens);
- AssertEx.EqualTolerance(SiemensInOneSiemens, (double)siemensQuantity.Value, SiemensTolerance);
- Assert.Equal(ElectricConductanceUnit.Siemens, siemensQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricConductanceUnit unit)
+ {
+ var quantity = ElectricConductance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricConductanceUnit unit)
{
- var quantityInBaseUnit = ElectricConductance.FromSiemens(1).ToBaseUnit();
- Assert.Equal(ElectricConductance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricConductance.Units.FirstOrDefault(u => u != ElectricConductance.BaseUnit && u != ElectricConductanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricConductanceUnit.Undefined)
+ fromUnit = ElectricConductance.BaseUnit;
+
+ var quantity = ElectricConductance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductivityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductivityTestsBase.g.cs
index 30fe721b4a..55a741771f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductivityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricConductivityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class ElectricConductivityTestsBase : QuantityTestsBase
protected virtual double SiemensPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricConductivityUnit unit)
+ {
+ return unit switch
+ {
+ ElectricConductivityUnit.SiemensPerFoot => (SiemensPerFootInOneSiemensPerMeter, SiemensPerFootTolerance),
+ ElectricConductivityUnit.SiemensPerInch => (SiemensPerInchInOneSiemensPerMeter, SiemensPerInchTolerance),
+ ElectricConductivityUnit.SiemensPerMeter => (SiemensPerMeterInOneSiemensPerMeter, SiemensPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricConductivityUnit.SiemensPerFoot },
+ new object[] { ElectricConductivityUnit.SiemensPerInch },
+ new object[] { ElectricConductivityUnit.SiemensPerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricConductivityUnit unit)
{
- var siemenspermeter = ElectricConductivity.FromSiemensPerMeter(1);
+ var inBaseUnits = ElectricConductivity.From(1.0, ElectricConductivity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var siemensperfootQuantity = siemenspermeter.ToUnit(ElectricConductivityUnit.SiemensPerFoot);
- AssertEx.EqualTolerance(SiemensPerFootInOneSiemensPerMeter, (double)siemensperfootQuantity.Value, SiemensPerFootTolerance);
- Assert.Equal(ElectricConductivityUnit.SiemensPerFoot, siemensperfootQuantity.Unit);
-
- var siemensperinchQuantity = siemenspermeter.ToUnit(ElectricConductivityUnit.SiemensPerInch);
- AssertEx.EqualTolerance(SiemensPerInchInOneSiemensPerMeter, (double)siemensperinchQuantity.Value, SiemensPerInchTolerance);
- Assert.Equal(ElectricConductivityUnit.SiemensPerInch, siemensperinchQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var siemenspermeterQuantity = siemenspermeter.ToUnit(ElectricConductivityUnit.SiemensPerMeter);
- AssertEx.EqualTolerance(SiemensPerMeterInOneSiemensPerMeter, (double)siemenspermeterQuantity.Value, SiemensPerMeterTolerance);
- Assert.Equal(ElectricConductivityUnit.SiemensPerMeter, siemenspermeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricConductivityUnit unit)
+ {
+ var quantity = ElectricConductivity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricConductivityUnit unit)
{
- var quantityInBaseUnit = ElectricConductivity.FromSiemensPerMeter(1).ToBaseUnit();
- Assert.Equal(ElectricConductivity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricConductivity.Units.FirstOrDefault(u => u != ElectricConductivity.BaseUnit && u != ElectricConductivityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricConductivityUnit.Undefined)
+ fromUnit = ElectricConductivity.BaseUnit;
+
+ var quantity = ElectricConductivity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentDensityTestsBase.g.cs
index 2b6a166eac..e3c7067ce5 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class ElectricCurrentDensityTestsBase : QuantityTestsBas
protected virtual double AmperesPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricCurrentDensityUnit unit)
+ {
+ return unit switch
+ {
+ ElectricCurrentDensityUnit.AmperePerSquareFoot => (AmperesPerSquareFootInOneAmperePerSquareMeter, AmperesPerSquareFootTolerance),
+ ElectricCurrentDensityUnit.AmperePerSquareInch => (AmperesPerSquareInchInOneAmperePerSquareMeter, AmperesPerSquareInchTolerance),
+ ElectricCurrentDensityUnit.AmperePerSquareMeter => (AmperesPerSquareMeterInOneAmperePerSquareMeter, AmperesPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricCurrentDensityUnit.AmperePerSquareFoot },
+ new object[] { ElectricCurrentDensityUnit.AmperePerSquareInch },
+ new object[] { ElectricCurrentDensityUnit.AmperePerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricCurrentDensityUnit unit)
{
- var amperepersquaremeter = ElectricCurrentDensity.FromAmperesPerSquareMeter(1);
+ var inBaseUnits = ElectricCurrentDensity.From(1.0, ElectricCurrentDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var amperepersquarefootQuantity = amperepersquaremeter.ToUnit(ElectricCurrentDensityUnit.AmperePerSquareFoot);
- AssertEx.EqualTolerance(AmperesPerSquareFootInOneAmperePerSquareMeter, (double)amperepersquarefootQuantity.Value, AmperesPerSquareFootTolerance);
- Assert.Equal(ElectricCurrentDensityUnit.AmperePerSquareFoot, amperepersquarefootQuantity.Unit);
-
- var amperepersquareinchQuantity = amperepersquaremeter.ToUnit(ElectricCurrentDensityUnit.AmperePerSquareInch);
- AssertEx.EqualTolerance(AmperesPerSquareInchInOneAmperePerSquareMeter, (double)amperepersquareinchQuantity.Value, AmperesPerSquareInchTolerance);
- Assert.Equal(ElectricCurrentDensityUnit.AmperePerSquareInch, amperepersquareinchQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var amperepersquaremeterQuantity = amperepersquaremeter.ToUnit(ElectricCurrentDensityUnit.AmperePerSquareMeter);
- AssertEx.EqualTolerance(AmperesPerSquareMeterInOneAmperePerSquareMeter, (double)amperepersquaremeterQuantity.Value, AmperesPerSquareMeterTolerance);
- Assert.Equal(ElectricCurrentDensityUnit.AmperePerSquareMeter, amperepersquaremeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricCurrentDensityUnit unit)
+ {
+ var quantity = ElectricCurrentDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricCurrentDensityUnit unit)
{
- var quantityInBaseUnit = ElectricCurrentDensity.FromAmperesPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(ElectricCurrentDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricCurrentDensity.Units.FirstOrDefault(u => u != ElectricCurrentDensity.BaseUnit && u != ElectricCurrentDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricCurrentDensityUnit.Undefined)
+ fromUnit = ElectricCurrentDensity.BaseUnit;
+
+ var quantity = ElectricCurrentDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentGradientTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentGradientTestsBase.g.cs
index ab24e643a2..9663d96458 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentGradientTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentGradientTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class ElectricCurrentGradientTestsBase : QuantityTestsBa
protected virtual double AmperesPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricCurrentGradientUnit unit)
+ {
+ return unit switch
+ {
+ ElectricCurrentGradientUnit.AmperePerMicrosecond => (AmperesPerMicrosecondInOneAmperePerSecond, AmperesPerMicrosecondTolerance),
+ ElectricCurrentGradientUnit.AmperePerMillisecond => (AmperesPerMillisecondInOneAmperePerSecond, AmperesPerMillisecondTolerance),
+ ElectricCurrentGradientUnit.AmperePerNanosecond => (AmperesPerNanosecondInOneAmperePerSecond, AmperesPerNanosecondTolerance),
+ ElectricCurrentGradientUnit.AmperePerSecond => (AmperesPerSecondInOneAmperePerSecond, AmperesPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricCurrentGradientUnit.AmperePerMicrosecond },
+ new object[] { ElectricCurrentGradientUnit.AmperePerMillisecond },
+ new object[] { ElectricCurrentGradientUnit.AmperePerNanosecond },
+ new object[] { ElectricCurrentGradientUnit.AmperePerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricCurrentGradientUnit unit)
{
- var amperepersecond = ElectricCurrentGradient.FromAmperesPerSecond(1);
+ var inBaseUnits = ElectricCurrentGradient.From(1.0, ElectricCurrentGradient.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var amperepermicrosecondQuantity = amperepersecond.ToUnit(ElectricCurrentGradientUnit.AmperePerMicrosecond);
- AssertEx.EqualTolerance(AmperesPerMicrosecondInOneAmperePerSecond, (double)amperepermicrosecondQuantity.Value, AmperesPerMicrosecondTolerance);
- Assert.Equal(ElectricCurrentGradientUnit.AmperePerMicrosecond, amperepermicrosecondQuantity.Unit);
-
- var amperepermillisecondQuantity = amperepersecond.ToUnit(ElectricCurrentGradientUnit.AmperePerMillisecond);
- AssertEx.EqualTolerance(AmperesPerMillisecondInOneAmperePerSecond, (double)amperepermillisecondQuantity.Value, AmperesPerMillisecondTolerance);
- Assert.Equal(ElectricCurrentGradientUnit.AmperePerMillisecond, amperepermillisecondQuantity.Unit);
-
- var amperepernanosecondQuantity = amperepersecond.ToUnit(ElectricCurrentGradientUnit.AmperePerNanosecond);
- AssertEx.EqualTolerance(AmperesPerNanosecondInOneAmperePerSecond, (double)amperepernanosecondQuantity.Value, AmperesPerNanosecondTolerance);
- Assert.Equal(ElectricCurrentGradientUnit.AmperePerNanosecond, amperepernanosecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var amperepersecondQuantity = amperepersecond.ToUnit(ElectricCurrentGradientUnit.AmperePerSecond);
- AssertEx.EqualTolerance(AmperesPerSecondInOneAmperePerSecond, (double)amperepersecondQuantity.Value, AmperesPerSecondTolerance);
- Assert.Equal(ElectricCurrentGradientUnit.AmperePerSecond, amperepersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricCurrentGradientUnit unit)
+ {
+ var quantity = ElectricCurrentGradient.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricCurrentGradientUnit unit)
{
- var quantityInBaseUnit = ElectricCurrentGradient.FromAmperesPerSecond(1).ToBaseUnit();
- Assert.Equal(ElectricCurrentGradient.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricCurrentGradient.Units.FirstOrDefault(u => u != ElectricCurrentGradient.BaseUnit && u != ElectricCurrentGradientUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricCurrentGradientUnit.Undefined)
+ fromUnit = ElectricCurrentGradient.BaseUnit;
+
+ var quantity = ElectricCurrentGradient.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentTestsBase.g.cs
index 633f32f1ff..3374cd2321 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricCurrentTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -57,6 +58,34 @@ public abstract partial class ElectricCurrentTestsBase : QuantityTestsBase
protected virtual double PicoamperesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricCurrentUnit unit)
+ {
+ return unit switch
+ {
+ ElectricCurrentUnit.Ampere => (AmperesInOneAmpere, AmperesTolerance),
+ ElectricCurrentUnit.Centiampere => (CentiamperesInOneAmpere, CentiamperesTolerance),
+ ElectricCurrentUnit.Kiloampere => (KiloamperesInOneAmpere, KiloamperesTolerance),
+ ElectricCurrentUnit.Megaampere => (MegaamperesInOneAmpere, MegaamperesTolerance),
+ ElectricCurrentUnit.Microampere => (MicroamperesInOneAmpere, MicroamperesTolerance),
+ ElectricCurrentUnit.Milliampere => (MilliamperesInOneAmpere, MilliamperesTolerance),
+ ElectricCurrentUnit.Nanoampere => (NanoamperesInOneAmpere, NanoamperesTolerance),
+ ElectricCurrentUnit.Picoampere => (PicoamperesInOneAmpere, PicoamperesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricCurrentUnit.Ampere },
+ new object[] { ElectricCurrentUnit.Centiampere },
+ new object[] { ElectricCurrentUnit.Kiloampere },
+ new object[] { ElectricCurrentUnit.Megaampere },
+ new object[] { ElectricCurrentUnit.Microampere },
+ new object[] { ElectricCurrentUnit.Milliampere },
+ new object[] { ElectricCurrentUnit.Nanoampere },
+ new object[] { ElectricCurrentUnit.Picoampere },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -220,49 +249,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricCurrentUnit unit)
{
- var ampere = ElectricCurrent.FromAmperes(1);
-
- var ampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Ampere);
- AssertEx.EqualTolerance(AmperesInOneAmpere, (double)ampereQuantity.Value, AmperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Ampere, ampereQuantity.Unit);
-
- var centiampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Centiampere);
- AssertEx.EqualTolerance(CentiamperesInOneAmpere, (double)centiampereQuantity.Value, CentiamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Centiampere, centiampereQuantity.Unit);
-
- var kiloampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Kiloampere);
- AssertEx.EqualTolerance(KiloamperesInOneAmpere, (double)kiloampereQuantity.Value, KiloamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Kiloampere, kiloampereQuantity.Unit);
-
- var megaampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Megaampere);
- AssertEx.EqualTolerance(MegaamperesInOneAmpere, (double)megaampereQuantity.Value, MegaamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Megaampere, megaampereQuantity.Unit);
+ var inBaseUnits = ElectricCurrent.From(1.0, ElectricCurrent.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Microampere);
- AssertEx.EqualTolerance(MicroamperesInOneAmpere, (double)microampereQuantity.Value, MicroamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Microampere, microampereQuantity.Unit);
-
- var milliampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Milliampere);
- AssertEx.EqualTolerance(MilliamperesInOneAmpere, (double)milliampereQuantity.Value, MilliamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Milliampere, milliampereQuantity.Unit);
-
- var nanoampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Nanoampere);
- AssertEx.EqualTolerance(NanoamperesInOneAmpere, (double)nanoampereQuantity.Value, NanoamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Nanoampere, nanoampereQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var picoampereQuantity = ampere.ToUnit(ElectricCurrentUnit.Picoampere);
- AssertEx.EqualTolerance(PicoamperesInOneAmpere, (double)picoampereQuantity.Value, PicoamperesTolerance);
- Assert.Equal(ElectricCurrentUnit.Picoampere, picoampereQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricCurrentUnit unit)
+ {
+ var quantity = ElectricCurrent.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricCurrentUnit unit)
{
- var quantityInBaseUnit = ElectricCurrent.FromAmperes(1).ToBaseUnit();
- Assert.Equal(ElectricCurrent.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricCurrent.Units.FirstOrDefault(u => u != ElectricCurrent.BaseUnit && u != ElectricCurrentUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricCurrentUnit.Undefined)
+ fromUnit = ElectricCurrent.BaseUnit;
+
+ var quantity = ElectricCurrent.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricFieldTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricFieldTestsBase.g.cs
index 79f369eae9..825ea88e65 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricFieldTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricFieldTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class ElectricFieldTestsBase : QuantityTestsBase
protected virtual double VoltsPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricFieldUnit unit)
+ {
+ return unit switch
+ {
+ ElectricFieldUnit.VoltPerMeter => (VoltsPerMeterInOneVoltPerMeter, VoltsPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricFieldUnit.VoltPerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricFieldUnit unit)
{
- var voltpermeter = ElectricField.FromVoltsPerMeter(1);
+ var inBaseUnits = ElectricField.From(1.0, ElectricField.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var voltpermeterQuantity = voltpermeter.ToUnit(ElectricFieldUnit.VoltPerMeter);
- AssertEx.EqualTolerance(VoltsPerMeterInOneVoltPerMeter, (double)voltpermeterQuantity.Value, VoltsPerMeterTolerance);
- Assert.Equal(ElectricFieldUnit.VoltPerMeter, voltpermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricFieldUnit unit)
+ {
+ var quantity = ElectricField.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricFieldUnit unit)
{
- var quantityInBaseUnit = ElectricField.FromVoltsPerMeter(1).ToBaseUnit();
- Assert.Equal(ElectricField.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricField.Units.FirstOrDefault(u => u != ElectricField.BaseUnit && u != ElectricFieldUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricFieldUnit.Undefined)
+ fromUnit = ElectricField.BaseUnit;
+
+ var quantity = ElectricField.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricInductanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricInductanceTestsBase.g.cs
index 4482b18fd5..8c7f0f8f83 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricInductanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricInductanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class ElectricInductanceTestsBase : QuantityTestsBase
protected virtual double NanohenriesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricInductanceUnit unit)
+ {
+ return unit switch
+ {
+ ElectricInductanceUnit.Henry => (HenriesInOneHenry, HenriesTolerance),
+ ElectricInductanceUnit.Microhenry => (MicrohenriesInOneHenry, MicrohenriesTolerance),
+ ElectricInductanceUnit.Millihenry => (MillihenriesInOneHenry, MillihenriesTolerance),
+ ElectricInductanceUnit.Nanohenry => (NanohenriesInOneHenry, NanohenriesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricInductanceUnit.Henry },
+ new object[] { ElectricInductanceUnit.Microhenry },
+ new object[] { ElectricInductanceUnit.Millihenry },
+ new object[] { ElectricInductanceUnit.Nanohenry },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricInductanceUnit unit)
{
- var henry = ElectricInductance.FromHenries(1);
+ var inBaseUnits = ElectricInductance.From(1.0, ElectricInductance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var henryQuantity = henry.ToUnit(ElectricInductanceUnit.Henry);
- AssertEx.EqualTolerance(HenriesInOneHenry, (double)henryQuantity.Value, HenriesTolerance);
- Assert.Equal(ElectricInductanceUnit.Henry, henryQuantity.Unit);
-
- var microhenryQuantity = henry.ToUnit(ElectricInductanceUnit.Microhenry);
- AssertEx.EqualTolerance(MicrohenriesInOneHenry, (double)microhenryQuantity.Value, MicrohenriesTolerance);
- Assert.Equal(ElectricInductanceUnit.Microhenry, microhenryQuantity.Unit);
-
- var millihenryQuantity = henry.ToUnit(ElectricInductanceUnit.Millihenry);
- AssertEx.EqualTolerance(MillihenriesInOneHenry, (double)millihenryQuantity.Value, MillihenriesTolerance);
- Assert.Equal(ElectricInductanceUnit.Millihenry, millihenryQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var nanohenryQuantity = henry.ToUnit(ElectricInductanceUnit.Nanohenry);
- AssertEx.EqualTolerance(NanohenriesInOneHenry, (double)nanohenryQuantity.Value, NanohenriesTolerance);
- Assert.Equal(ElectricInductanceUnit.Nanohenry, nanohenryQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricInductanceUnit unit)
+ {
+ var quantity = ElectricInductance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricInductanceUnit unit)
{
- var quantityInBaseUnit = ElectricInductance.FromHenries(1).ToBaseUnit();
- Assert.Equal(ElectricInductance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricInductance.Units.FirstOrDefault(u => u != ElectricInductance.BaseUnit && u != ElectricInductanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricInductanceUnit.Undefined)
+ fromUnit = ElectricInductance.BaseUnit;
+
+ var quantity = ElectricInductance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialAcTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialAcTestsBase.g.cs
index ddc23fa2c6..dc62cb8d5d 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialAcTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialAcTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -51,6 +52,28 @@ public abstract partial class ElectricPotentialAcTestsBase : QuantityTestsBase
protected virtual double VoltsAcTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricPotentialAcUnit unit)
+ {
+ return unit switch
+ {
+ ElectricPotentialAcUnit.KilovoltAc => (KilovoltsAcInOneVoltAc, KilovoltsAcTolerance),
+ ElectricPotentialAcUnit.MegavoltAc => (MegavoltsAcInOneVoltAc, MegavoltsAcTolerance),
+ ElectricPotentialAcUnit.MicrovoltAc => (MicrovoltsAcInOneVoltAc, MicrovoltsAcTolerance),
+ ElectricPotentialAcUnit.MillivoltAc => (MillivoltsAcInOneVoltAc, MillivoltsAcTolerance),
+ ElectricPotentialAcUnit.VoltAc => (VoltsAcInOneVoltAc, VoltsAcTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricPotentialAcUnit.KilovoltAc },
+ new object[] { ElectricPotentialAcUnit.MegavoltAc },
+ new object[] { ElectricPotentialAcUnit.MicrovoltAc },
+ new object[] { ElectricPotentialAcUnit.MillivoltAc },
+ new object[] { ElectricPotentialAcUnit.VoltAc },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -196,37 +219,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricPotentialAcUnit unit)
{
- var voltac = ElectricPotentialAc.FromVoltsAc(1);
-
- var kilovoltacQuantity = voltac.ToUnit(ElectricPotentialAcUnit.KilovoltAc);
- AssertEx.EqualTolerance(KilovoltsAcInOneVoltAc, (double)kilovoltacQuantity.Value, KilovoltsAcTolerance);
- Assert.Equal(ElectricPotentialAcUnit.KilovoltAc, kilovoltacQuantity.Unit);
+ var inBaseUnits = ElectricPotentialAc.From(1.0, ElectricPotentialAc.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megavoltacQuantity = voltac.ToUnit(ElectricPotentialAcUnit.MegavoltAc);
- AssertEx.EqualTolerance(MegavoltsAcInOneVoltAc, (double)megavoltacQuantity.Value, MegavoltsAcTolerance);
- Assert.Equal(ElectricPotentialAcUnit.MegavoltAc, megavoltacQuantity.Unit);
-
- var microvoltacQuantity = voltac.ToUnit(ElectricPotentialAcUnit.MicrovoltAc);
- AssertEx.EqualTolerance(MicrovoltsAcInOneVoltAc, (double)microvoltacQuantity.Value, MicrovoltsAcTolerance);
- Assert.Equal(ElectricPotentialAcUnit.MicrovoltAc, microvoltacQuantity.Unit);
-
- var millivoltacQuantity = voltac.ToUnit(ElectricPotentialAcUnit.MillivoltAc);
- AssertEx.EqualTolerance(MillivoltsAcInOneVoltAc, (double)millivoltacQuantity.Value, MillivoltsAcTolerance);
- Assert.Equal(ElectricPotentialAcUnit.MillivoltAc, millivoltacQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltacQuantity = voltac.ToUnit(ElectricPotentialAcUnit.VoltAc);
- AssertEx.EqualTolerance(VoltsAcInOneVoltAc, (double)voltacQuantity.Value, VoltsAcTolerance);
- Assert.Equal(ElectricPotentialAcUnit.VoltAc, voltacQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricPotentialAcUnit unit)
+ {
+ var quantity = ElectricPotentialAc.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricPotentialAcUnit unit)
{
- var quantityInBaseUnit = ElectricPotentialAc.FromVoltsAc(1).ToBaseUnit();
- Assert.Equal(ElectricPotentialAc.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricPotentialAc.Units.FirstOrDefault(u => u != ElectricPotentialAc.BaseUnit && u != ElectricPotentialAcUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricPotentialAcUnit.Undefined)
+ fromUnit = ElectricPotentialAc.BaseUnit;
+
+ var quantity = ElectricPotentialAc.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialChangeRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialChangeRateTestsBase.g.cs
index 987928a2cc..6cbe276aab 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialChangeRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialChangeRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -81,6 +82,58 @@ public abstract partial class ElectricPotentialChangeRateTestsBase : QuantityTes
protected virtual double VoltsPerSecondsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricPotentialChangeRateUnit unit)
+ {
+ return unit switch
+ {
+ ElectricPotentialChangeRateUnit.KilovoltPerHour => (KilovoltsPerHoursInOneVoltPerSecond, KilovoltsPerHoursTolerance),
+ ElectricPotentialChangeRateUnit.KilovoltPerMicrosecond => (KilovoltsPerMicrosecondsInOneVoltPerSecond, KilovoltsPerMicrosecondsTolerance),
+ ElectricPotentialChangeRateUnit.KilovoltPerMinute => (KilovoltsPerMinutesInOneVoltPerSecond, KilovoltsPerMinutesTolerance),
+ ElectricPotentialChangeRateUnit.KilovoltPerSecond => (KilovoltsPerSecondsInOneVoltPerSecond, KilovoltsPerSecondsTolerance),
+ ElectricPotentialChangeRateUnit.MegavoltPerHour => (MegavoltsPerHoursInOneVoltPerSecond, MegavoltsPerHoursTolerance),
+ ElectricPotentialChangeRateUnit.MegavoltPerMicrosecond => (MegavoltsPerMicrosecondsInOneVoltPerSecond, MegavoltsPerMicrosecondsTolerance),
+ ElectricPotentialChangeRateUnit.MegavoltPerMinute => (MegavoltsPerMinutesInOneVoltPerSecond, MegavoltsPerMinutesTolerance),
+ ElectricPotentialChangeRateUnit.MegavoltPerSecond => (MegavoltsPerSecondsInOneVoltPerSecond, MegavoltsPerSecondsTolerance),
+ ElectricPotentialChangeRateUnit.MicrovoltPerHour => (MicrovoltsPerHoursInOneVoltPerSecond, MicrovoltsPerHoursTolerance),
+ ElectricPotentialChangeRateUnit.MicrovoltPerMicrosecond => (MicrovoltsPerMicrosecondsInOneVoltPerSecond, MicrovoltsPerMicrosecondsTolerance),
+ ElectricPotentialChangeRateUnit.MicrovoltPerMinute => (MicrovoltsPerMinutesInOneVoltPerSecond, MicrovoltsPerMinutesTolerance),
+ ElectricPotentialChangeRateUnit.MicrovoltPerSecond => (MicrovoltsPerSecondsInOneVoltPerSecond, MicrovoltsPerSecondsTolerance),
+ ElectricPotentialChangeRateUnit.MillivoltPerHour => (MillivoltsPerHoursInOneVoltPerSecond, MillivoltsPerHoursTolerance),
+ ElectricPotentialChangeRateUnit.MillivoltPerMicrosecond => (MillivoltsPerMicrosecondsInOneVoltPerSecond, MillivoltsPerMicrosecondsTolerance),
+ ElectricPotentialChangeRateUnit.MillivoltPerMinute => (MillivoltsPerMinutesInOneVoltPerSecond, MillivoltsPerMinutesTolerance),
+ ElectricPotentialChangeRateUnit.MillivoltPerSecond => (MillivoltsPerSecondsInOneVoltPerSecond, MillivoltsPerSecondsTolerance),
+ ElectricPotentialChangeRateUnit.VoltPerHour => (VoltsPerHoursInOneVoltPerSecond, VoltsPerHoursTolerance),
+ ElectricPotentialChangeRateUnit.VoltPerMicrosecond => (VoltsPerMicrosecondsInOneVoltPerSecond, VoltsPerMicrosecondsTolerance),
+ ElectricPotentialChangeRateUnit.VoltPerMinute => (VoltsPerMinutesInOneVoltPerSecond, VoltsPerMinutesTolerance),
+ ElectricPotentialChangeRateUnit.VoltPerSecond => (VoltsPerSecondsInOneVoltPerSecond, VoltsPerSecondsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricPotentialChangeRateUnit.KilovoltPerHour },
+ new object[] { ElectricPotentialChangeRateUnit.KilovoltPerMicrosecond },
+ new object[] { ElectricPotentialChangeRateUnit.KilovoltPerMinute },
+ new object[] { ElectricPotentialChangeRateUnit.KilovoltPerSecond },
+ new object[] { ElectricPotentialChangeRateUnit.MegavoltPerHour },
+ new object[] { ElectricPotentialChangeRateUnit.MegavoltPerMicrosecond },
+ new object[] { ElectricPotentialChangeRateUnit.MegavoltPerMinute },
+ new object[] { ElectricPotentialChangeRateUnit.MegavoltPerSecond },
+ new object[] { ElectricPotentialChangeRateUnit.MicrovoltPerHour },
+ new object[] { ElectricPotentialChangeRateUnit.MicrovoltPerMicrosecond },
+ new object[] { ElectricPotentialChangeRateUnit.MicrovoltPerMinute },
+ new object[] { ElectricPotentialChangeRateUnit.MicrovoltPerSecond },
+ new object[] { ElectricPotentialChangeRateUnit.MillivoltPerHour },
+ new object[] { ElectricPotentialChangeRateUnit.MillivoltPerMicrosecond },
+ new object[] { ElectricPotentialChangeRateUnit.MillivoltPerMinute },
+ new object[] { ElectricPotentialChangeRateUnit.MillivoltPerSecond },
+ new object[] { ElectricPotentialChangeRateUnit.VoltPerHour },
+ new object[] { ElectricPotentialChangeRateUnit.VoltPerMicrosecond },
+ new object[] { ElectricPotentialChangeRateUnit.VoltPerMinute },
+ new object[] { ElectricPotentialChangeRateUnit.VoltPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -316,97 +369,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricPotentialChangeRateUnit unit)
{
- var voltpersecond = ElectricPotentialChangeRate.FromVoltsPerSeconds(1);
-
- var kilovoltperhourQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.KilovoltPerHour);
- AssertEx.EqualTolerance(KilovoltsPerHoursInOneVoltPerSecond, (double)kilovoltperhourQuantity.Value, KilovoltsPerHoursTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.KilovoltPerHour, kilovoltperhourQuantity.Unit);
-
- var kilovoltpermicrosecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.KilovoltPerMicrosecond);
- AssertEx.EqualTolerance(KilovoltsPerMicrosecondsInOneVoltPerSecond, (double)kilovoltpermicrosecondQuantity.Value, KilovoltsPerMicrosecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.KilovoltPerMicrosecond, kilovoltpermicrosecondQuantity.Unit);
-
- var kilovoltperminuteQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.KilovoltPerMinute);
- AssertEx.EqualTolerance(KilovoltsPerMinutesInOneVoltPerSecond, (double)kilovoltperminuteQuantity.Value, KilovoltsPerMinutesTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.KilovoltPerMinute, kilovoltperminuteQuantity.Unit);
-
- var kilovoltpersecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.KilovoltPerSecond);
- AssertEx.EqualTolerance(KilovoltsPerSecondsInOneVoltPerSecond, (double)kilovoltpersecondQuantity.Value, KilovoltsPerSecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.KilovoltPerSecond, kilovoltpersecondQuantity.Unit);
-
- var megavoltperhourQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MegavoltPerHour);
- AssertEx.EqualTolerance(MegavoltsPerHoursInOneVoltPerSecond, (double)megavoltperhourQuantity.Value, MegavoltsPerHoursTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MegavoltPerHour, megavoltperhourQuantity.Unit);
-
- var megavoltpermicrosecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MegavoltPerMicrosecond);
- AssertEx.EqualTolerance(MegavoltsPerMicrosecondsInOneVoltPerSecond, (double)megavoltpermicrosecondQuantity.Value, MegavoltsPerMicrosecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MegavoltPerMicrosecond, megavoltpermicrosecondQuantity.Unit);
-
- var megavoltperminuteQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MegavoltPerMinute);
- AssertEx.EqualTolerance(MegavoltsPerMinutesInOneVoltPerSecond, (double)megavoltperminuteQuantity.Value, MegavoltsPerMinutesTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MegavoltPerMinute, megavoltperminuteQuantity.Unit);
-
- var megavoltpersecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MegavoltPerSecond);
- AssertEx.EqualTolerance(MegavoltsPerSecondsInOneVoltPerSecond, (double)megavoltpersecondQuantity.Value, MegavoltsPerSecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MegavoltPerSecond, megavoltpersecondQuantity.Unit);
-
- var microvoltperhourQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MicrovoltPerHour);
- AssertEx.EqualTolerance(MicrovoltsPerHoursInOneVoltPerSecond, (double)microvoltperhourQuantity.Value, MicrovoltsPerHoursTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MicrovoltPerHour, microvoltperhourQuantity.Unit);
-
- var microvoltpermicrosecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MicrovoltPerMicrosecond);
- AssertEx.EqualTolerance(MicrovoltsPerMicrosecondsInOneVoltPerSecond, (double)microvoltpermicrosecondQuantity.Value, MicrovoltsPerMicrosecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MicrovoltPerMicrosecond, microvoltpermicrosecondQuantity.Unit);
+ var inBaseUnits = ElectricPotentialChangeRate.From(1.0, ElectricPotentialChangeRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microvoltperminuteQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MicrovoltPerMinute);
- AssertEx.EqualTolerance(MicrovoltsPerMinutesInOneVoltPerSecond, (double)microvoltperminuteQuantity.Value, MicrovoltsPerMinutesTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MicrovoltPerMinute, microvoltperminuteQuantity.Unit);
-
- var microvoltpersecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MicrovoltPerSecond);
- AssertEx.EqualTolerance(MicrovoltsPerSecondsInOneVoltPerSecond, (double)microvoltpersecondQuantity.Value, MicrovoltsPerSecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MicrovoltPerSecond, microvoltpersecondQuantity.Unit);
-
- var millivoltperhourQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MillivoltPerHour);
- AssertEx.EqualTolerance(MillivoltsPerHoursInOneVoltPerSecond, (double)millivoltperhourQuantity.Value, MillivoltsPerHoursTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MillivoltPerHour, millivoltperhourQuantity.Unit);
-
- var millivoltpermicrosecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MillivoltPerMicrosecond);
- AssertEx.EqualTolerance(MillivoltsPerMicrosecondsInOneVoltPerSecond, (double)millivoltpermicrosecondQuantity.Value, MillivoltsPerMicrosecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MillivoltPerMicrosecond, millivoltpermicrosecondQuantity.Unit);
-
- var millivoltperminuteQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MillivoltPerMinute);
- AssertEx.EqualTolerance(MillivoltsPerMinutesInOneVoltPerSecond, (double)millivoltperminuteQuantity.Value, MillivoltsPerMinutesTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MillivoltPerMinute, millivoltperminuteQuantity.Unit);
-
- var millivoltpersecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.MillivoltPerSecond);
- AssertEx.EqualTolerance(MillivoltsPerSecondsInOneVoltPerSecond, (double)millivoltpersecondQuantity.Value, MillivoltsPerSecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.MillivoltPerSecond, millivoltpersecondQuantity.Unit);
-
- var voltperhourQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.VoltPerHour);
- AssertEx.EqualTolerance(VoltsPerHoursInOneVoltPerSecond, (double)voltperhourQuantity.Value, VoltsPerHoursTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.VoltPerHour, voltperhourQuantity.Unit);
-
- var voltpermicrosecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.VoltPerMicrosecond);
- AssertEx.EqualTolerance(VoltsPerMicrosecondsInOneVoltPerSecond, (double)voltpermicrosecondQuantity.Value, VoltsPerMicrosecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.VoltPerMicrosecond, voltpermicrosecondQuantity.Unit);
-
- var voltperminuteQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.VoltPerMinute);
- AssertEx.EqualTolerance(VoltsPerMinutesInOneVoltPerSecond, (double)voltperminuteQuantity.Value, VoltsPerMinutesTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.VoltPerMinute, voltperminuteQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltpersecondQuantity = voltpersecond.ToUnit(ElectricPotentialChangeRateUnit.VoltPerSecond);
- AssertEx.EqualTolerance(VoltsPerSecondsInOneVoltPerSecond, (double)voltpersecondQuantity.Value, VoltsPerSecondsTolerance);
- Assert.Equal(ElectricPotentialChangeRateUnit.VoltPerSecond, voltpersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricPotentialChangeRateUnit unit)
+ {
+ var quantity = ElectricPotentialChangeRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricPotentialChangeRateUnit unit)
{
- var quantityInBaseUnit = ElectricPotentialChangeRate.FromVoltsPerSeconds(1).ToBaseUnit();
- Assert.Equal(ElectricPotentialChangeRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricPotentialChangeRate.Units.FirstOrDefault(u => u != ElectricPotentialChangeRate.BaseUnit && u != ElectricPotentialChangeRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricPotentialChangeRateUnit.Undefined)
+ fromUnit = ElectricPotentialChangeRate.BaseUnit;
+
+ var quantity = ElectricPotentialChangeRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialDcTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialDcTestsBase.g.cs
index 9028ea50e2..df1cfbd31f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialDcTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialDcTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -51,6 +52,28 @@ public abstract partial class ElectricPotentialDcTestsBase : QuantityTestsBase
protected virtual double VoltsDcTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricPotentialDcUnit unit)
+ {
+ return unit switch
+ {
+ ElectricPotentialDcUnit.KilovoltDc => (KilovoltsDcInOneVoltDc, KilovoltsDcTolerance),
+ ElectricPotentialDcUnit.MegavoltDc => (MegavoltsDcInOneVoltDc, MegavoltsDcTolerance),
+ ElectricPotentialDcUnit.MicrovoltDc => (MicrovoltsDcInOneVoltDc, MicrovoltsDcTolerance),
+ ElectricPotentialDcUnit.MillivoltDc => (MillivoltsDcInOneVoltDc, MillivoltsDcTolerance),
+ ElectricPotentialDcUnit.VoltDc => (VoltsDcInOneVoltDc, VoltsDcTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricPotentialDcUnit.KilovoltDc },
+ new object[] { ElectricPotentialDcUnit.MegavoltDc },
+ new object[] { ElectricPotentialDcUnit.MicrovoltDc },
+ new object[] { ElectricPotentialDcUnit.MillivoltDc },
+ new object[] { ElectricPotentialDcUnit.VoltDc },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -196,37 +219,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricPotentialDcUnit unit)
{
- var voltdc = ElectricPotentialDc.FromVoltsDc(1);
-
- var kilovoltdcQuantity = voltdc.ToUnit(ElectricPotentialDcUnit.KilovoltDc);
- AssertEx.EqualTolerance(KilovoltsDcInOneVoltDc, (double)kilovoltdcQuantity.Value, KilovoltsDcTolerance);
- Assert.Equal(ElectricPotentialDcUnit.KilovoltDc, kilovoltdcQuantity.Unit);
+ var inBaseUnits = ElectricPotentialDc.From(1.0, ElectricPotentialDc.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megavoltdcQuantity = voltdc.ToUnit(ElectricPotentialDcUnit.MegavoltDc);
- AssertEx.EqualTolerance(MegavoltsDcInOneVoltDc, (double)megavoltdcQuantity.Value, MegavoltsDcTolerance);
- Assert.Equal(ElectricPotentialDcUnit.MegavoltDc, megavoltdcQuantity.Unit);
-
- var microvoltdcQuantity = voltdc.ToUnit(ElectricPotentialDcUnit.MicrovoltDc);
- AssertEx.EqualTolerance(MicrovoltsDcInOneVoltDc, (double)microvoltdcQuantity.Value, MicrovoltsDcTolerance);
- Assert.Equal(ElectricPotentialDcUnit.MicrovoltDc, microvoltdcQuantity.Unit);
-
- var millivoltdcQuantity = voltdc.ToUnit(ElectricPotentialDcUnit.MillivoltDc);
- AssertEx.EqualTolerance(MillivoltsDcInOneVoltDc, (double)millivoltdcQuantity.Value, MillivoltsDcTolerance);
- Assert.Equal(ElectricPotentialDcUnit.MillivoltDc, millivoltdcQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltdcQuantity = voltdc.ToUnit(ElectricPotentialDcUnit.VoltDc);
- AssertEx.EqualTolerance(VoltsDcInOneVoltDc, (double)voltdcQuantity.Value, VoltsDcTolerance);
- Assert.Equal(ElectricPotentialDcUnit.VoltDc, voltdcQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricPotentialDcUnit unit)
+ {
+ var quantity = ElectricPotentialDc.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricPotentialDcUnit unit)
{
- var quantityInBaseUnit = ElectricPotentialDc.FromVoltsDc(1).ToBaseUnit();
- Assert.Equal(ElectricPotentialDc.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricPotentialDc.Units.FirstOrDefault(u => u != ElectricPotentialDc.BaseUnit && u != ElectricPotentialDcUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricPotentialDcUnit.Undefined)
+ fromUnit = ElectricPotentialDc.BaseUnit;
+
+ var quantity = ElectricPotentialDc.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialTestsBase.g.cs
index 5c519932bd..89742b883c 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricPotentialTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -51,6 +52,28 @@ public abstract partial class ElectricPotentialTestsBase : QuantityTestsBase
protected virtual double VoltsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricPotentialUnit unit)
+ {
+ return unit switch
+ {
+ ElectricPotentialUnit.Kilovolt => (KilovoltsInOneVolt, KilovoltsTolerance),
+ ElectricPotentialUnit.Megavolt => (MegavoltsInOneVolt, MegavoltsTolerance),
+ ElectricPotentialUnit.Microvolt => (MicrovoltsInOneVolt, MicrovoltsTolerance),
+ ElectricPotentialUnit.Millivolt => (MillivoltsInOneVolt, MillivoltsTolerance),
+ ElectricPotentialUnit.Volt => (VoltsInOneVolt, VoltsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricPotentialUnit.Kilovolt },
+ new object[] { ElectricPotentialUnit.Megavolt },
+ new object[] { ElectricPotentialUnit.Microvolt },
+ new object[] { ElectricPotentialUnit.Millivolt },
+ new object[] { ElectricPotentialUnit.Volt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -196,37 +219,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricPotentialUnit unit)
{
- var volt = ElectricPotential.FromVolts(1);
-
- var kilovoltQuantity = volt.ToUnit(ElectricPotentialUnit.Kilovolt);
- AssertEx.EqualTolerance(KilovoltsInOneVolt, (double)kilovoltQuantity.Value, KilovoltsTolerance);
- Assert.Equal(ElectricPotentialUnit.Kilovolt, kilovoltQuantity.Unit);
+ var inBaseUnits = ElectricPotential.From(1.0, ElectricPotential.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megavoltQuantity = volt.ToUnit(ElectricPotentialUnit.Megavolt);
- AssertEx.EqualTolerance(MegavoltsInOneVolt, (double)megavoltQuantity.Value, MegavoltsTolerance);
- Assert.Equal(ElectricPotentialUnit.Megavolt, megavoltQuantity.Unit);
-
- var microvoltQuantity = volt.ToUnit(ElectricPotentialUnit.Microvolt);
- AssertEx.EqualTolerance(MicrovoltsInOneVolt, (double)microvoltQuantity.Value, MicrovoltsTolerance);
- Assert.Equal(ElectricPotentialUnit.Microvolt, microvoltQuantity.Unit);
-
- var millivoltQuantity = volt.ToUnit(ElectricPotentialUnit.Millivolt);
- AssertEx.EqualTolerance(MillivoltsInOneVolt, (double)millivoltQuantity.Value, MillivoltsTolerance);
- Assert.Equal(ElectricPotentialUnit.Millivolt, millivoltQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltQuantity = volt.ToUnit(ElectricPotentialUnit.Volt);
- AssertEx.EqualTolerance(VoltsInOneVolt, (double)voltQuantity.Value, VoltsTolerance);
- Assert.Equal(ElectricPotentialUnit.Volt, voltQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricPotentialUnit unit)
+ {
+ var quantity = ElectricPotential.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricPotentialUnit unit)
{
- var quantityInBaseUnit = ElectricPotential.FromVolts(1).ToBaseUnit();
- Assert.Equal(ElectricPotential.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricPotential.Units.FirstOrDefault(u => u != ElectricPotential.BaseUnit && u != ElectricPotentialUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricPotentialUnit.Undefined)
+ fromUnit = ElectricPotential.BaseUnit;
+
+ var quantity = ElectricPotential.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistanceTestsBase.g.cs
index 7a656c484d..fedf7e6908 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -53,6 +54,30 @@ public abstract partial class ElectricResistanceTestsBase : QuantityTestsBase
protected virtual double OhmsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricResistanceUnit unit)
+ {
+ return unit switch
+ {
+ ElectricResistanceUnit.Gigaohm => (GigaohmsInOneOhm, GigaohmsTolerance),
+ ElectricResistanceUnit.Kiloohm => (KiloohmsInOneOhm, KiloohmsTolerance),
+ ElectricResistanceUnit.Megaohm => (MegaohmsInOneOhm, MegaohmsTolerance),
+ ElectricResistanceUnit.Microohm => (MicroohmsInOneOhm, MicroohmsTolerance),
+ ElectricResistanceUnit.Milliohm => (MilliohmsInOneOhm, MilliohmsTolerance),
+ ElectricResistanceUnit.Ohm => (OhmsInOneOhm, OhmsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricResistanceUnit.Gigaohm },
+ new object[] { ElectricResistanceUnit.Kiloohm },
+ new object[] { ElectricResistanceUnit.Megaohm },
+ new object[] { ElectricResistanceUnit.Microohm },
+ new object[] { ElectricResistanceUnit.Milliohm },
+ new object[] { ElectricResistanceUnit.Ohm },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -204,41 +229,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricResistanceUnit unit)
{
- var ohm = ElectricResistance.FromOhms(1);
-
- var gigaohmQuantity = ohm.ToUnit(ElectricResistanceUnit.Gigaohm);
- AssertEx.EqualTolerance(GigaohmsInOneOhm, (double)gigaohmQuantity.Value, GigaohmsTolerance);
- Assert.Equal(ElectricResistanceUnit.Gigaohm, gigaohmQuantity.Unit);
+ var inBaseUnits = ElectricResistance.From(1.0, ElectricResistance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kiloohmQuantity = ohm.ToUnit(ElectricResistanceUnit.Kiloohm);
- AssertEx.EqualTolerance(KiloohmsInOneOhm, (double)kiloohmQuantity.Value, KiloohmsTolerance);
- Assert.Equal(ElectricResistanceUnit.Kiloohm, kiloohmQuantity.Unit);
-
- var megaohmQuantity = ohm.ToUnit(ElectricResistanceUnit.Megaohm);
- AssertEx.EqualTolerance(MegaohmsInOneOhm, (double)megaohmQuantity.Value, MegaohmsTolerance);
- Assert.Equal(ElectricResistanceUnit.Megaohm, megaohmQuantity.Unit);
-
- var microohmQuantity = ohm.ToUnit(ElectricResistanceUnit.Microohm);
- AssertEx.EqualTolerance(MicroohmsInOneOhm, (double)microohmQuantity.Value, MicroohmsTolerance);
- Assert.Equal(ElectricResistanceUnit.Microohm, microohmQuantity.Unit);
-
- var milliohmQuantity = ohm.ToUnit(ElectricResistanceUnit.Milliohm);
- AssertEx.EqualTolerance(MilliohmsInOneOhm, (double)milliohmQuantity.Value, MilliohmsTolerance);
- Assert.Equal(ElectricResistanceUnit.Milliohm, milliohmQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var ohmQuantity = ohm.ToUnit(ElectricResistanceUnit.Ohm);
- AssertEx.EqualTolerance(OhmsInOneOhm, (double)ohmQuantity.Value, OhmsTolerance);
- Assert.Equal(ElectricResistanceUnit.Ohm, ohmQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricResistanceUnit unit)
+ {
+ var quantity = ElectricResistance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricResistanceUnit unit)
{
- var quantityInBaseUnit = ElectricResistance.FromOhms(1).ToBaseUnit();
- Assert.Equal(ElectricResistance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricResistance.Units.FirstOrDefault(u => u != ElectricResistance.BaseUnit && u != ElectricResistanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricResistanceUnit.Undefined)
+ fromUnit = ElectricResistance.BaseUnit;
+
+ var quantity = ElectricResistance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistivityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistivityTestsBase.g.cs
index 6bdd2c7cd0..3c9463fa71 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistivityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricResistivityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class ElectricResistivityTestsBase : QuantityTestsBase
protected virtual double PicoohmMetersTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricResistivityUnit unit)
+ {
+ return unit switch
+ {
+ ElectricResistivityUnit.KiloohmCentimeter => (KiloohmsCentimeterInOneOhmMeter, KiloohmsCentimeterTolerance),
+ ElectricResistivityUnit.KiloohmMeter => (KiloohmMetersInOneOhmMeter, KiloohmMetersTolerance),
+ ElectricResistivityUnit.MegaohmCentimeter => (MegaohmsCentimeterInOneOhmMeter, MegaohmsCentimeterTolerance),
+ ElectricResistivityUnit.MegaohmMeter => (MegaohmMetersInOneOhmMeter, MegaohmMetersTolerance),
+ ElectricResistivityUnit.MicroohmCentimeter => (MicroohmsCentimeterInOneOhmMeter, MicroohmsCentimeterTolerance),
+ ElectricResistivityUnit.MicroohmMeter => (MicroohmMetersInOneOhmMeter, MicroohmMetersTolerance),
+ ElectricResistivityUnit.MilliohmCentimeter => (MilliohmsCentimeterInOneOhmMeter, MilliohmsCentimeterTolerance),
+ ElectricResistivityUnit.MilliohmMeter => (MilliohmMetersInOneOhmMeter, MilliohmMetersTolerance),
+ ElectricResistivityUnit.NanoohmCentimeter => (NanoohmsCentimeterInOneOhmMeter, NanoohmsCentimeterTolerance),
+ ElectricResistivityUnit.NanoohmMeter => (NanoohmMetersInOneOhmMeter, NanoohmMetersTolerance),
+ ElectricResistivityUnit.OhmCentimeter => (OhmsCentimeterInOneOhmMeter, OhmsCentimeterTolerance),
+ ElectricResistivityUnit.OhmMeter => (OhmMetersInOneOhmMeter, OhmMetersTolerance),
+ ElectricResistivityUnit.PicoohmCentimeter => (PicoohmsCentimeterInOneOhmMeter, PicoohmsCentimeterTolerance),
+ ElectricResistivityUnit.PicoohmMeter => (PicoohmMetersInOneOhmMeter, PicoohmMetersTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricResistivityUnit.KiloohmCentimeter },
+ new object[] { ElectricResistivityUnit.KiloohmMeter },
+ new object[] { ElectricResistivityUnit.MegaohmCentimeter },
+ new object[] { ElectricResistivityUnit.MegaohmMeter },
+ new object[] { ElectricResistivityUnit.MicroohmCentimeter },
+ new object[] { ElectricResistivityUnit.MicroohmMeter },
+ new object[] { ElectricResistivityUnit.MilliohmCentimeter },
+ new object[] { ElectricResistivityUnit.MilliohmMeter },
+ new object[] { ElectricResistivityUnit.NanoohmCentimeter },
+ new object[] { ElectricResistivityUnit.NanoohmMeter },
+ new object[] { ElectricResistivityUnit.OhmCentimeter },
+ new object[] { ElectricResistivityUnit.OhmMeter },
+ new object[] { ElectricResistivityUnit.PicoohmCentimeter },
+ new object[] { ElectricResistivityUnit.PicoohmMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricResistivityUnit unit)
{
- var ohmmeter = ElectricResistivity.FromOhmMeters(1);
-
- var kiloohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.KiloohmCentimeter);
- AssertEx.EqualTolerance(KiloohmsCentimeterInOneOhmMeter, (double)kiloohmcentimeterQuantity.Value, KiloohmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.KiloohmCentimeter, kiloohmcentimeterQuantity.Unit);
-
- var kiloohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.KiloohmMeter);
- AssertEx.EqualTolerance(KiloohmMetersInOneOhmMeter, (double)kiloohmmeterQuantity.Value, KiloohmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.KiloohmMeter, kiloohmmeterQuantity.Unit);
-
- var megaohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.MegaohmCentimeter);
- AssertEx.EqualTolerance(MegaohmsCentimeterInOneOhmMeter, (double)megaohmcentimeterQuantity.Value, MegaohmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.MegaohmCentimeter, megaohmcentimeterQuantity.Unit);
-
- var megaohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.MegaohmMeter);
- AssertEx.EqualTolerance(MegaohmMetersInOneOhmMeter, (double)megaohmmeterQuantity.Value, MegaohmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.MegaohmMeter, megaohmmeterQuantity.Unit);
-
- var microohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.MicroohmCentimeter);
- AssertEx.EqualTolerance(MicroohmsCentimeterInOneOhmMeter, (double)microohmcentimeterQuantity.Value, MicroohmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.MicroohmCentimeter, microohmcentimeterQuantity.Unit);
-
- var microohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.MicroohmMeter);
- AssertEx.EqualTolerance(MicroohmMetersInOneOhmMeter, (double)microohmmeterQuantity.Value, MicroohmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.MicroohmMeter, microohmmeterQuantity.Unit);
-
- var milliohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.MilliohmCentimeter);
- AssertEx.EqualTolerance(MilliohmsCentimeterInOneOhmMeter, (double)milliohmcentimeterQuantity.Value, MilliohmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.MilliohmCentimeter, milliohmcentimeterQuantity.Unit);
+ var inBaseUnits = ElectricResistivity.From(1.0, ElectricResistivity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var milliohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.MilliohmMeter);
- AssertEx.EqualTolerance(MilliohmMetersInOneOhmMeter, (double)milliohmmeterQuantity.Value, MilliohmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.MilliohmMeter, milliohmmeterQuantity.Unit);
-
- var nanoohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.NanoohmCentimeter);
- AssertEx.EqualTolerance(NanoohmsCentimeterInOneOhmMeter, (double)nanoohmcentimeterQuantity.Value, NanoohmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.NanoohmCentimeter, nanoohmcentimeterQuantity.Unit);
-
- var nanoohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.NanoohmMeter);
- AssertEx.EqualTolerance(NanoohmMetersInOneOhmMeter, (double)nanoohmmeterQuantity.Value, NanoohmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.NanoohmMeter, nanoohmmeterQuantity.Unit);
-
- var ohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.OhmCentimeter);
- AssertEx.EqualTolerance(OhmsCentimeterInOneOhmMeter, (double)ohmcentimeterQuantity.Value, OhmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.OhmCentimeter, ohmcentimeterQuantity.Unit);
-
- var ohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.OhmMeter);
- AssertEx.EqualTolerance(OhmMetersInOneOhmMeter, (double)ohmmeterQuantity.Value, OhmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.OhmMeter, ohmmeterQuantity.Unit);
-
- var picoohmcentimeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.PicoohmCentimeter);
- AssertEx.EqualTolerance(PicoohmsCentimeterInOneOhmMeter, (double)picoohmcentimeterQuantity.Value, PicoohmsCentimeterTolerance);
- Assert.Equal(ElectricResistivityUnit.PicoohmCentimeter, picoohmcentimeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var picoohmmeterQuantity = ohmmeter.ToUnit(ElectricResistivityUnit.PicoohmMeter);
- AssertEx.EqualTolerance(PicoohmMetersInOneOhmMeter, (double)picoohmmeterQuantity.Value, PicoohmMetersTolerance);
- Assert.Equal(ElectricResistivityUnit.PicoohmMeter, picoohmmeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricResistivityUnit unit)
+ {
+ var quantity = ElectricResistivity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricResistivityUnit unit)
{
- var quantityInBaseUnit = ElectricResistivity.FromOhmMeters(1).ToBaseUnit();
- Assert.Equal(ElectricResistivity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricResistivity.Units.FirstOrDefault(u => u != ElectricResistivity.BaseUnit && u != ElectricResistivityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricResistivityUnit.Undefined)
+ fromUnit = ElectricResistivity.BaseUnit;
+
+ var quantity = ElectricResistivity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricSurfaceChargeDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricSurfaceChargeDensityTestsBase.g.cs
index efb9b9f710..baf69ed89a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricSurfaceChargeDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ElectricSurfaceChargeDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class ElectricSurfaceChargeDensityTestsBase : QuantityTe
protected virtual double CoulombsPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ElectricSurfaceChargeDensityUnit unit)
+ {
+ return unit switch
+ {
+ ElectricSurfaceChargeDensityUnit.CoulombPerSquareCentimeter => (CoulombsPerSquareCentimeterInOneCoulombPerSquareMeter, CoulombsPerSquareCentimeterTolerance),
+ ElectricSurfaceChargeDensityUnit.CoulombPerSquareInch => (CoulombsPerSquareInchInOneCoulombPerSquareMeter, CoulombsPerSquareInchTolerance),
+ ElectricSurfaceChargeDensityUnit.CoulombPerSquareMeter => (CoulombsPerSquareMeterInOneCoulombPerSquareMeter, CoulombsPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ElectricSurfaceChargeDensityUnit.CoulombPerSquareCentimeter },
+ new object[] { ElectricSurfaceChargeDensityUnit.CoulombPerSquareInch },
+ new object[] { ElectricSurfaceChargeDensityUnit.CoulombPerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ElectricSurfaceChargeDensityUnit unit)
{
- var coulombpersquaremeter = ElectricSurfaceChargeDensity.FromCoulombsPerSquareMeter(1);
+ var inBaseUnits = ElectricSurfaceChargeDensity.From(1.0, ElectricSurfaceChargeDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var coulombpersquarecentimeterQuantity = coulombpersquaremeter.ToUnit(ElectricSurfaceChargeDensityUnit.CoulombPerSquareCentimeter);
- AssertEx.EqualTolerance(CoulombsPerSquareCentimeterInOneCoulombPerSquareMeter, (double)coulombpersquarecentimeterQuantity.Value, CoulombsPerSquareCentimeterTolerance);
- Assert.Equal(ElectricSurfaceChargeDensityUnit.CoulombPerSquareCentimeter, coulombpersquarecentimeterQuantity.Unit);
-
- var coulombpersquareinchQuantity = coulombpersquaremeter.ToUnit(ElectricSurfaceChargeDensityUnit.CoulombPerSquareInch);
- AssertEx.EqualTolerance(CoulombsPerSquareInchInOneCoulombPerSquareMeter, (double)coulombpersquareinchQuantity.Value, CoulombsPerSquareInchTolerance);
- Assert.Equal(ElectricSurfaceChargeDensityUnit.CoulombPerSquareInch, coulombpersquareinchQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var coulombpersquaremeterQuantity = coulombpersquaremeter.ToUnit(ElectricSurfaceChargeDensityUnit.CoulombPerSquareMeter);
- AssertEx.EqualTolerance(CoulombsPerSquareMeterInOneCoulombPerSquareMeter, (double)coulombpersquaremeterQuantity.Value, CoulombsPerSquareMeterTolerance);
- Assert.Equal(ElectricSurfaceChargeDensityUnit.CoulombPerSquareMeter, coulombpersquaremeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ElectricSurfaceChargeDensityUnit unit)
+ {
+ var quantity = ElectricSurfaceChargeDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ElectricSurfaceChargeDensityUnit unit)
{
- var quantityInBaseUnit = ElectricSurfaceChargeDensity.FromCoulombsPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(ElectricSurfaceChargeDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ElectricSurfaceChargeDensity.Units.FirstOrDefault(u => u != ElectricSurfaceChargeDensity.BaseUnit && u != ElectricSurfaceChargeDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ElectricSurfaceChargeDensityUnit.Undefined)
+ fromUnit = ElectricSurfaceChargeDensity.BaseUnit;
+
+ var quantity = ElectricSurfaceChargeDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/EnergyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/EnergyTestsBase.g.cs
index 18f4ef6623..4f31580566 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/EnergyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/EnergyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -113,6 +114,90 @@ public abstract partial class EnergyTestsBase : QuantityTestsBase
protected virtual double WattHoursTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(EnergyUnit unit)
+ {
+ return unit switch
+ {
+ EnergyUnit.BritishThermalUnit => (BritishThermalUnitsInOneJoule, BritishThermalUnitsTolerance),
+ EnergyUnit.Calorie => (CaloriesInOneJoule, CaloriesTolerance),
+ EnergyUnit.DecathermEc => (DecathermsEcInOneJoule, DecathermsEcTolerance),
+ EnergyUnit.DecathermImperial => (DecathermsImperialInOneJoule, DecathermsImperialTolerance),
+ EnergyUnit.DecathermUs => (DecathermsUsInOneJoule, DecathermsUsTolerance),
+ EnergyUnit.ElectronVolt => (ElectronVoltsInOneJoule, ElectronVoltsTolerance),
+ EnergyUnit.Erg => (ErgsInOneJoule, ErgsTolerance),
+ EnergyUnit.FootPound => (FootPoundsInOneJoule, FootPoundsTolerance),
+ EnergyUnit.GigabritishThermalUnit => (GigabritishThermalUnitsInOneJoule, GigabritishThermalUnitsTolerance),
+ EnergyUnit.GigaelectronVolt => (GigaelectronVoltsInOneJoule, GigaelectronVoltsTolerance),
+ EnergyUnit.Gigajoule => (GigajoulesInOneJoule, GigajoulesTolerance),
+ EnergyUnit.GigawattDay => (GigawattDaysInOneJoule, GigawattDaysTolerance),
+ EnergyUnit.GigawattHour => (GigawattHoursInOneJoule, GigawattHoursTolerance),
+ EnergyUnit.HorsepowerHour => (HorsepowerHoursInOneJoule, HorsepowerHoursTolerance),
+ EnergyUnit.Joule => (JoulesInOneJoule, JoulesTolerance),
+ EnergyUnit.KilobritishThermalUnit => (KilobritishThermalUnitsInOneJoule, KilobritishThermalUnitsTolerance),
+ EnergyUnit.Kilocalorie => (KilocaloriesInOneJoule, KilocaloriesTolerance),
+ EnergyUnit.KiloelectronVolt => (KiloelectronVoltsInOneJoule, KiloelectronVoltsTolerance),
+ EnergyUnit.Kilojoule => (KilojoulesInOneJoule, KilojoulesTolerance),
+ EnergyUnit.KilowattDay => (KilowattDaysInOneJoule, KilowattDaysTolerance),
+ EnergyUnit.KilowattHour => (KilowattHoursInOneJoule, KilowattHoursTolerance),
+ EnergyUnit.MegabritishThermalUnit => (MegabritishThermalUnitsInOneJoule, MegabritishThermalUnitsTolerance),
+ EnergyUnit.Megacalorie => (MegacaloriesInOneJoule, MegacaloriesTolerance),
+ EnergyUnit.MegaelectronVolt => (MegaelectronVoltsInOneJoule, MegaelectronVoltsTolerance),
+ EnergyUnit.Megajoule => (MegajoulesInOneJoule, MegajoulesTolerance),
+ EnergyUnit.MegawattDay => (MegawattDaysInOneJoule, MegawattDaysTolerance),
+ EnergyUnit.MegawattHour => (MegawattHoursInOneJoule, MegawattHoursTolerance),
+ EnergyUnit.Millijoule => (MillijoulesInOneJoule, MillijoulesTolerance),
+ EnergyUnit.TeraelectronVolt => (TeraelectronVoltsInOneJoule, TeraelectronVoltsTolerance),
+ EnergyUnit.TerawattDay => (TerawattDaysInOneJoule, TerawattDaysTolerance),
+ EnergyUnit.TerawattHour => (TerawattHoursInOneJoule, TerawattHoursTolerance),
+ EnergyUnit.ThermEc => (ThermsEcInOneJoule, ThermsEcTolerance),
+ EnergyUnit.ThermImperial => (ThermsImperialInOneJoule, ThermsImperialTolerance),
+ EnergyUnit.ThermUs => (ThermsUsInOneJoule, ThermsUsTolerance),
+ EnergyUnit.WattDay => (WattDaysInOneJoule, WattDaysTolerance),
+ EnergyUnit.WattHour => (WattHoursInOneJoule, WattHoursTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { EnergyUnit.BritishThermalUnit },
+ new object[] { EnergyUnit.Calorie },
+ new object[] { EnergyUnit.DecathermEc },
+ new object[] { EnergyUnit.DecathermImperial },
+ new object[] { EnergyUnit.DecathermUs },
+ new object[] { EnergyUnit.ElectronVolt },
+ new object[] { EnergyUnit.Erg },
+ new object[] { EnergyUnit.FootPound },
+ new object[] { EnergyUnit.GigabritishThermalUnit },
+ new object[] { EnergyUnit.GigaelectronVolt },
+ new object[] { EnergyUnit.Gigajoule },
+ new object[] { EnergyUnit.GigawattDay },
+ new object[] { EnergyUnit.GigawattHour },
+ new object[] { EnergyUnit.HorsepowerHour },
+ new object[] { EnergyUnit.Joule },
+ new object[] { EnergyUnit.KilobritishThermalUnit },
+ new object[] { EnergyUnit.Kilocalorie },
+ new object[] { EnergyUnit.KiloelectronVolt },
+ new object[] { EnergyUnit.Kilojoule },
+ new object[] { EnergyUnit.KilowattDay },
+ new object[] { EnergyUnit.KilowattHour },
+ new object[] { EnergyUnit.MegabritishThermalUnit },
+ new object[] { EnergyUnit.Megacalorie },
+ new object[] { EnergyUnit.MegaelectronVolt },
+ new object[] { EnergyUnit.Megajoule },
+ new object[] { EnergyUnit.MegawattDay },
+ new object[] { EnergyUnit.MegawattHour },
+ new object[] { EnergyUnit.Millijoule },
+ new object[] { EnergyUnit.TeraelectronVolt },
+ new object[] { EnergyUnit.TerawattDay },
+ new object[] { EnergyUnit.TerawattHour },
+ new object[] { EnergyUnit.ThermEc },
+ new object[] { EnergyUnit.ThermImperial },
+ new object[] { EnergyUnit.ThermUs },
+ new object[] { EnergyUnit.WattDay },
+ new object[] { EnergyUnit.WattHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -444,161 +529,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(EnergyUnit unit)
{
- var joule = Energy.FromJoules(1);
-
- var britishthermalunitQuantity = joule.ToUnit(EnergyUnit.BritishThermalUnit);
- AssertEx.EqualTolerance(BritishThermalUnitsInOneJoule, (double)britishthermalunitQuantity.Value, BritishThermalUnitsTolerance);
- Assert.Equal(EnergyUnit.BritishThermalUnit, britishthermalunitQuantity.Unit);
-
- var calorieQuantity = joule.ToUnit(EnergyUnit.Calorie);
- AssertEx.EqualTolerance(CaloriesInOneJoule, (double)calorieQuantity.Value, CaloriesTolerance);
- Assert.Equal(EnergyUnit.Calorie, calorieQuantity.Unit);
-
- var decathermecQuantity = joule.ToUnit(EnergyUnit.DecathermEc);
- AssertEx.EqualTolerance(DecathermsEcInOneJoule, (double)decathermecQuantity.Value, DecathermsEcTolerance);
- Assert.Equal(EnergyUnit.DecathermEc, decathermecQuantity.Unit);
-
- var decathermimperialQuantity = joule.ToUnit(EnergyUnit.DecathermImperial);
- AssertEx.EqualTolerance(DecathermsImperialInOneJoule, (double)decathermimperialQuantity.Value, DecathermsImperialTolerance);
- Assert.Equal(EnergyUnit.DecathermImperial, decathermimperialQuantity.Unit);
-
- var decathermusQuantity = joule.ToUnit(EnergyUnit.DecathermUs);
- AssertEx.EqualTolerance(DecathermsUsInOneJoule, (double)decathermusQuantity.Value, DecathermsUsTolerance);
- Assert.Equal(EnergyUnit.DecathermUs, decathermusQuantity.Unit);
-
- var electronvoltQuantity = joule.ToUnit(EnergyUnit.ElectronVolt);
- AssertEx.EqualTolerance(ElectronVoltsInOneJoule, (double)electronvoltQuantity.Value, ElectronVoltsTolerance);
- Assert.Equal(EnergyUnit.ElectronVolt, electronvoltQuantity.Unit);
-
- var ergQuantity = joule.ToUnit(EnergyUnit.Erg);
- AssertEx.EqualTolerance(ErgsInOneJoule, (double)ergQuantity.Value, ErgsTolerance);
- Assert.Equal(EnergyUnit.Erg, ergQuantity.Unit);
-
- var footpoundQuantity = joule.ToUnit(EnergyUnit.FootPound);
- AssertEx.EqualTolerance(FootPoundsInOneJoule, (double)footpoundQuantity.Value, FootPoundsTolerance);
- Assert.Equal(EnergyUnit.FootPound, footpoundQuantity.Unit);
-
- var gigabritishthermalunitQuantity = joule.ToUnit(EnergyUnit.GigabritishThermalUnit);
- AssertEx.EqualTolerance(GigabritishThermalUnitsInOneJoule, (double)gigabritishthermalunitQuantity.Value, GigabritishThermalUnitsTolerance);
- Assert.Equal(EnergyUnit.GigabritishThermalUnit, gigabritishthermalunitQuantity.Unit);
-
- var gigaelectronvoltQuantity = joule.ToUnit(EnergyUnit.GigaelectronVolt);
- AssertEx.EqualTolerance(GigaelectronVoltsInOneJoule, (double)gigaelectronvoltQuantity.Value, GigaelectronVoltsTolerance);
- Assert.Equal(EnergyUnit.GigaelectronVolt, gigaelectronvoltQuantity.Unit);
-
- var gigajouleQuantity = joule.ToUnit(EnergyUnit.Gigajoule);
- AssertEx.EqualTolerance(GigajoulesInOneJoule, (double)gigajouleQuantity.Value, GigajoulesTolerance);
- Assert.Equal(EnergyUnit.Gigajoule, gigajouleQuantity.Unit);
-
- var gigawattdayQuantity = joule.ToUnit(EnergyUnit.GigawattDay);
- AssertEx.EqualTolerance(GigawattDaysInOneJoule, (double)gigawattdayQuantity.Value, GigawattDaysTolerance);
- Assert.Equal(EnergyUnit.GigawattDay, gigawattdayQuantity.Unit);
-
- var gigawatthourQuantity = joule.ToUnit(EnergyUnit.GigawattHour);
- AssertEx.EqualTolerance(GigawattHoursInOneJoule, (double)gigawatthourQuantity.Value, GigawattHoursTolerance);
- Assert.Equal(EnergyUnit.GigawattHour, gigawatthourQuantity.Unit);
-
- var horsepowerhourQuantity = joule.ToUnit(EnergyUnit.HorsepowerHour);
- AssertEx.EqualTolerance(HorsepowerHoursInOneJoule, (double)horsepowerhourQuantity.Value, HorsepowerHoursTolerance);
- Assert.Equal(EnergyUnit.HorsepowerHour, horsepowerhourQuantity.Unit);
-
- var jouleQuantity = joule.ToUnit(EnergyUnit.Joule);
- AssertEx.EqualTolerance(JoulesInOneJoule, (double)jouleQuantity.Value, JoulesTolerance);
- Assert.Equal(EnergyUnit.Joule, jouleQuantity.Unit);
-
- var kilobritishthermalunitQuantity = joule.ToUnit(EnergyUnit.KilobritishThermalUnit);
- AssertEx.EqualTolerance(KilobritishThermalUnitsInOneJoule, (double)kilobritishthermalunitQuantity.Value, KilobritishThermalUnitsTolerance);
- Assert.Equal(EnergyUnit.KilobritishThermalUnit, kilobritishthermalunitQuantity.Unit);
+ var inBaseUnits = Energy.From(1.0, Energy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilocalorieQuantity = joule.ToUnit(EnergyUnit.Kilocalorie);
- AssertEx.EqualTolerance(KilocaloriesInOneJoule, (double)kilocalorieQuantity.Value, KilocaloriesTolerance);
- Assert.Equal(EnergyUnit.Kilocalorie, kilocalorieQuantity.Unit);
-
- var kiloelectronvoltQuantity = joule.ToUnit(EnergyUnit.KiloelectronVolt);
- AssertEx.EqualTolerance(KiloelectronVoltsInOneJoule, (double)kiloelectronvoltQuantity.Value, KiloelectronVoltsTolerance);
- Assert.Equal(EnergyUnit.KiloelectronVolt, kiloelectronvoltQuantity.Unit);
-
- var kilojouleQuantity = joule.ToUnit(EnergyUnit.Kilojoule);
- AssertEx.EqualTolerance(KilojoulesInOneJoule, (double)kilojouleQuantity.Value, KilojoulesTolerance);
- Assert.Equal(EnergyUnit.Kilojoule, kilojouleQuantity.Unit);
-
- var kilowattdayQuantity = joule.ToUnit(EnergyUnit.KilowattDay);
- AssertEx.EqualTolerance(KilowattDaysInOneJoule, (double)kilowattdayQuantity.Value, KilowattDaysTolerance);
- Assert.Equal(EnergyUnit.KilowattDay, kilowattdayQuantity.Unit);
-
- var kilowatthourQuantity = joule.ToUnit(EnergyUnit.KilowattHour);
- AssertEx.EqualTolerance(KilowattHoursInOneJoule, (double)kilowatthourQuantity.Value, KilowattHoursTolerance);
- Assert.Equal(EnergyUnit.KilowattHour, kilowatthourQuantity.Unit);
-
- var megabritishthermalunitQuantity = joule.ToUnit(EnergyUnit.MegabritishThermalUnit);
- AssertEx.EqualTolerance(MegabritishThermalUnitsInOneJoule, (double)megabritishthermalunitQuantity.Value, MegabritishThermalUnitsTolerance);
- Assert.Equal(EnergyUnit.MegabritishThermalUnit, megabritishthermalunitQuantity.Unit);
-
- var megacalorieQuantity = joule.ToUnit(EnergyUnit.Megacalorie);
- AssertEx.EqualTolerance(MegacaloriesInOneJoule, (double)megacalorieQuantity.Value, MegacaloriesTolerance);
- Assert.Equal(EnergyUnit.Megacalorie, megacalorieQuantity.Unit);
-
- var megaelectronvoltQuantity = joule.ToUnit(EnergyUnit.MegaelectronVolt);
- AssertEx.EqualTolerance(MegaelectronVoltsInOneJoule, (double)megaelectronvoltQuantity.Value, MegaelectronVoltsTolerance);
- Assert.Equal(EnergyUnit.MegaelectronVolt, megaelectronvoltQuantity.Unit);
-
- var megajouleQuantity = joule.ToUnit(EnergyUnit.Megajoule);
- AssertEx.EqualTolerance(MegajoulesInOneJoule, (double)megajouleQuantity.Value, MegajoulesTolerance);
- Assert.Equal(EnergyUnit.Megajoule, megajouleQuantity.Unit);
-
- var megawattdayQuantity = joule.ToUnit(EnergyUnit.MegawattDay);
- AssertEx.EqualTolerance(MegawattDaysInOneJoule, (double)megawattdayQuantity.Value, MegawattDaysTolerance);
- Assert.Equal(EnergyUnit.MegawattDay, megawattdayQuantity.Unit);
-
- var megawatthourQuantity = joule.ToUnit(EnergyUnit.MegawattHour);
- AssertEx.EqualTolerance(MegawattHoursInOneJoule, (double)megawatthourQuantity.Value, MegawattHoursTolerance);
- Assert.Equal(EnergyUnit.MegawattHour, megawatthourQuantity.Unit);
-
- var millijouleQuantity = joule.ToUnit(EnergyUnit.Millijoule);
- AssertEx.EqualTolerance(MillijoulesInOneJoule, (double)millijouleQuantity.Value, MillijoulesTolerance);
- Assert.Equal(EnergyUnit.Millijoule, millijouleQuantity.Unit);
-
- var teraelectronvoltQuantity = joule.ToUnit(EnergyUnit.TeraelectronVolt);
- AssertEx.EqualTolerance(TeraelectronVoltsInOneJoule, (double)teraelectronvoltQuantity.Value, TeraelectronVoltsTolerance);
- Assert.Equal(EnergyUnit.TeraelectronVolt, teraelectronvoltQuantity.Unit);
-
- var terawattdayQuantity = joule.ToUnit(EnergyUnit.TerawattDay);
- AssertEx.EqualTolerance(TerawattDaysInOneJoule, (double)terawattdayQuantity.Value, TerawattDaysTolerance);
- Assert.Equal(EnergyUnit.TerawattDay, terawattdayQuantity.Unit);
-
- var terawatthourQuantity = joule.ToUnit(EnergyUnit.TerawattHour);
- AssertEx.EqualTolerance(TerawattHoursInOneJoule, (double)terawatthourQuantity.Value, TerawattHoursTolerance);
- Assert.Equal(EnergyUnit.TerawattHour, terawatthourQuantity.Unit);
-
- var thermecQuantity = joule.ToUnit(EnergyUnit.ThermEc);
- AssertEx.EqualTolerance(ThermsEcInOneJoule, (double)thermecQuantity.Value, ThermsEcTolerance);
- Assert.Equal(EnergyUnit.ThermEc, thermecQuantity.Unit);
-
- var thermimperialQuantity = joule.ToUnit(EnergyUnit.ThermImperial);
- AssertEx.EqualTolerance(ThermsImperialInOneJoule, (double)thermimperialQuantity.Value, ThermsImperialTolerance);
- Assert.Equal(EnergyUnit.ThermImperial, thermimperialQuantity.Unit);
-
- var thermusQuantity = joule.ToUnit(EnergyUnit.ThermUs);
- AssertEx.EqualTolerance(ThermsUsInOneJoule, (double)thermusQuantity.Value, ThermsUsTolerance);
- Assert.Equal(EnergyUnit.ThermUs, thermusQuantity.Unit);
-
- var wattdayQuantity = joule.ToUnit(EnergyUnit.WattDay);
- AssertEx.EqualTolerance(WattDaysInOneJoule, (double)wattdayQuantity.Value, WattDaysTolerance);
- Assert.Equal(EnergyUnit.WattDay, wattdayQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var watthourQuantity = joule.ToUnit(EnergyUnit.WattHour);
- AssertEx.EqualTolerance(WattHoursInOneJoule, (double)watthourQuantity.Value, WattHoursTolerance);
- Assert.Equal(EnergyUnit.WattHour, watthourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(EnergyUnit unit)
+ {
+ var quantity = Energy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(EnergyUnit unit)
{
- var quantityInBaseUnit = Energy.FromJoules(1).ToBaseUnit();
- Assert.Equal(Energy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Energy.Units.FirstOrDefault(u => u != Energy.BaseUnit && u != EnergyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == EnergyUnit.Undefined)
+ fromUnit = Energy.BaseUnit;
+
+ var quantity = Energy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/EntropyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/EntropyTestsBase.g.cs
index 5e44a83900..55859a487e 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/EntropyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/EntropyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -55,6 +56,32 @@ public abstract partial class EntropyTestsBase : QuantityTestsBase
protected virtual double MegajoulesPerKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(EntropyUnit unit)
+ {
+ return unit switch
+ {
+ EntropyUnit.CaloriePerKelvin => (CaloriesPerKelvinInOneJoulePerKelvin, CaloriesPerKelvinTolerance),
+ EntropyUnit.JoulePerDegreeCelsius => (JoulesPerDegreeCelsiusInOneJoulePerKelvin, JoulesPerDegreeCelsiusTolerance),
+ EntropyUnit.JoulePerKelvin => (JoulesPerKelvinInOneJoulePerKelvin, JoulesPerKelvinTolerance),
+ EntropyUnit.KilocaloriePerKelvin => (KilocaloriesPerKelvinInOneJoulePerKelvin, KilocaloriesPerKelvinTolerance),
+ EntropyUnit.KilojoulePerDegreeCelsius => (KilojoulesPerDegreeCelsiusInOneJoulePerKelvin, KilojoulesPerDegreeCelsiusTolerance),
+ EntropyUnit.KilojoulePerKelvin => (KilojoulesPerKelvinInOneJoulePerKelvin, KilojoulesPerKelvinTolerance),
+ EntropyUnit.MegajoulePerKelvin => (MegajoulesPerKelvinInOneJoulePerKelvin, MegajoulesPerKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { EntropyUnit.CaloriePerKelvin },
+ new object[] { EntropyUnit.JoulePerDegreeCelsius },
+ new object[] { EntropyUnit.JoulePerKelvin },
+ new object[] { EntropyUnit.KilocaloriePerKelvin },
+ new object[] { EntropyUnit.KilojoulePerDegreeCelsius },
+ new object[] { EntropyUnit.KilojoulePerKelvin },
+ new object[] { EntropyUnit.MegajoulePerKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -212,45 +239,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(EntropyUnit unit)
{
- var jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
-
- var calorieperkelvinQuantity = jouleperkelvin.ToUnit(EntropyUnit.CaloriePerKelvin);
- AssertEx.EqualTolerance(CaloriesPerKelvinInOneJoulePerKelvin, (double)calorieperkelvinQuantity.Value, CaloriesPerKelvinTolerance);
- Assert.Equal(EntropyUnit.CaloriePerKelvin, calorieperkelvinQuantity.Unit);
-
- var jouleperdegreecelsiusQuantity = jouleperkelvin.ToUnit(EntropyUnit.JoulePerDegreeCelsius);
- AssertEx.EqualTolerance(JoulesPerDegreeCelsiusInOneJoulePerKelvin, (double)jouleperdegreecelsiusQuantity.Value, JoulesPerDegreeCelsiusTolerance);
- Assert.Equal(EntropyUnit.JoulePerDegreeCelsius, jouleperdegreecelsiusQuantity.Unit);
+ var inBaseUnits = Entropy.From(1.0, Entropy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var jouleperkelvinQuantity = jouleperkelvin.ToUnit(EntropyUnit.JoulePerKelvin);
- AssertEx.EqualTolerance(JoulesPerKelvinInOneJoulePerKelvin, (double)jouleperkelvinQuantity.Value, JoulesPerKelvinTolerance);
- Assert.Equal(EntropyUnit.JoulePerKelvin, jouleperkelvinQuantity.Unit);
-
- var kilocalorieperkelvinQuantity = jouleperkelvin.ToUnit(EntropyUnit.KilocaloriePerKelvin);
- AssertEx.EqualTolerance(KilocaloriesPerKelvinInOneJoulePerKelvin, (double)kilocalorieperkelvinQuantity.Value, KilocaloriesPerKelvinTolerance);
- Assert.Equal(EntropyUnit.KilocaloriePerKelvin, kilocalorieperkelvinQuantity.Unit);
-
- var kilojouleperdegreecelsiusQuantity = jouleperkelvin.ToUnit(EntropyUnit.KilojoulePerDegreeCelsius);
- AssertEx.EqualTolerance(KilojoulesPerDegreeCelsiusInOneJoulePerKelvin, (double)kilojouleperdegreecelsiusQuantity.Value, KilojoulesPerDegreeCelsiusTolerance);
- Assert.Equal(EntropyUnit.KilojoulePerDegreeCelsius, kilojouleperdegreecelsiusQuantity.Unit);
-
- var kilojouleperkelvinQuantity = jouleperkelvin.ToUnit(EntropyUnit.KilojoulePerKelvin);
- AssertEx.EqualTolerance(KilojoulesPerKelvinInOneJoulePerKelvin, (double)kilojouleperkelvinQuantity.Value, KilojoulesPerKelvinTolerance);
- Assert.Equal(EntropyUnit.KilojoulePerKelvin, kilojouleperkelvinQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var megajouleperkelvinQuantity = jouleperkelvin.ToUnit(EntropyUnit.MegajoulePerKelvin);
- AssertEx.EqualTolerance(MegajoulesPerKelvinInOneJoulePerKelvin, (double)megajouleperkelvinQuantity.Value, MegajoulesPerKelvinTolerance);
- Assert.Equal(EntropyUnit.MegajoulePerKelvin, megajouleperkelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(EntropyUnit unit)
+ {
+ var quantity = Entropy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(EntropyUnit unit)
{
- var quantityInBaseUnit = Entropy.FromJoulesPerKelvin(1).ToBaseUnit();
- Assert.Equal(Entropy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Entropy.Units.FirstOrDefault(u => u != Entropy.BaseUnit && u != EntropyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == EntropyUnit.Undefined)
+ fromUnit = Entropy.BaseUnit;
+
+ var quantity = Entropy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ForceChangeRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ForceChangeRateTestsBase.g.cs
index ebdfab01de..fe9203fc90 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ForceChangeRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ForceChangeRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -71,6 +72,48 @@ public abstract partial class ForceChangeRateTestsBase : QuantityTestsBase
protected virtual double PoundsForcePerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ForceChangeRateUnit unit)
+ {
+ return unit switch
+ {
+ ForceChangeRateUnit.CentinewtonPerSecond => (CentinewtonsPerSecondInOneNewtonPerSecond, CentinewtonsPerSecondTolerance),
+ ForceChangeRateUnit.DecanewtonPerMinute => (DecanewtonsPerMinuteInOneNewtonPerSecond, DecanewtonsPerMinuteTolerance),
+ ForceChangeRateUnit.DecanewtonPerSecond => (DecanewtonsPerSecondInOneNewtonPerSecond, DecanewtonsPerSecondTolerance),
+ ForceChangeRateUnit.DecinewtonPerSecond => (DecinewtonsPerSecondInOneNewtonPerSecond, DecinewtonsPerSecondTolerance),
+ ForceChangeRateUnit.KilonewtonPerMinute => (KilonewtonsPerMinuteInOneNewtonPerSecond, KilonewtonsPerMinuteTolerance),
+ ForceChangeRateUnit.KilonewtonPerSecond => (KilonewtonsPerSecondInOneNewtonPerSecond, KilonewtonsPerSecondTolerance),
+ ForceChangeRateUnit.KilopoundForcePerMinute => (KilopoundsForcePerMinuteInOneNewtonPerSecond, KilopoundsForcePerMinuteTolerance),
+ ForceChangeRateUnit.KilopoundForcePerSecond => (KilopoundsForcePerSecondInOneNewtonPerSecond, KilopoundsForcePerSecondTolerance),
+ ForceChangeRateUnit.MicronewtonPerSecond => (MicronewtonsPerSecondInOneNewtonPerSecond, MicronewtonsPerSecondTolerance),
+ ForceChangeRateUnit.MillinewtonPerSecond => (MillinewtonsPerSecondInOneNewtonPerSecond, MillinewtonsPerSecondTolerance),
+ ForceChangeRateUnit.NanonewtonPerSecond => (NanonewtonsPerSecondInOneNewtonPerSecond, NanonewtonsPerSecondTolerance),
+ ForceChangeRateUnit.NewtonPerMinute => (NewtonsPerMinuteInOneNewtonPerSecond, NewtonsPerMinuteTolerance),
+ ForceChangeRateUnit.NewtonPerSecond => (NewtonsPerSecondInOneNewtonPerSecond, NewtonsPerSecondTolerance),
+ ForceChangeRateUnit.PoundForcePerMinute => (PoundsForcePerMinuteInOneNewtonPerSecond, PoundsForcePerMinuteTolerance),
+ ForceChangeRateUnit.PoundForcePerSecond => (PoundsForcePerSecondInOneNewtonPerSecond, PoundsForcePerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ForceChangeRateUnit.CentinewtonPerSecond },
+ new object[] { ForceChangeRateUnit.DecanewtonPerMinute },
+ new object[] { ForceChangeRateUnit.DecanewtonPerSecond },
+ new object[] { ForceChangeRateUnit.DecinewtonPerSecond },
+ new object[] { ForceChangeRateUnit.KilonewtonPerMinute },
+ new object[] { ForceChangeRateUnit.KilonewtonPerSecond },
+ new object[] { ForceChangeRateUnit.KilopoundForcePerMinute },
+ new object[] { ForceChangeRateUnit.KilopoundForcePerSecond },
+ new object[] { ForceChangeRateUnit.MicronewtonPerSecond },
+ new object[] { ForceChangeRateUnit.MillinewtonPerSecond },
+ new object[] { ForceChangeRateUnit.NanonewtonPerSecond },
+ new object[] { ForceChangeRateUnit.NewtonPerMinute },
+ new object[] { ForceChangeRateUnit.NewtonPerSecond },
+ new object[] { ForceChangeRateUnit.PoundForcePerMinute },
+ new object[] { ForceChangeRateUnit.PoundForcePerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -276,77 +319,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ForceChangeRateUnit unit)
{
- var newtonpersecond = ForceChangeRate.FromNewtonsPerSecond(1);
-
- var centinewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.CentinewtonPerSecond);
- AssertEx.EqualTolerance(CentinewtonsPerSecondInOneNewtonPerSecond, (double)centinewtonpersecondQuantity.Value, CentinewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.CentinewtonPerSecond, centinewtonpersecondQuantity.Unit);
-
- var decanewtonperminuteQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.DecanewtonPerMinute);
- AssertEx.EqualTolerance(DecanewtonsPerMinuteInOneNewtonPerSecond, (double)decanewtonperminuteQuantity.Value, DecanewtonsPerMinuteTolerance);
- Assert.Equal(ForceChangeRateUnit.DecanewtonPerMinute, decanewtonperminuteQuantity.Unit);
-
- var decanewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.DecanewtonPerSecond);
- AssertEx.EqualTolerance(DecanewtonsPerSecondInOneNewtonPerSecond, (double)decanewtonpersecondQuantity.Value, DecanewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.DecanewtonPerSecond, decanewtonpersecondQuantity.Unit);
-
- var decinewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.DecinewtonPerSecond);
- AssertEx.EqualTolerance(DecinewtonsPerSecondInOneNewtonPerSecond, (double)decinewtonpersecondQuantity.Value, DecinewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.DecinewtonPerSecond, decinewtonpersecondQuantity.Unit);
-
- var kilonewtonperminuteQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.KilonewtonPerMinute);
- AssertEx.EqualTolerance(KilonewtonsPerMinuteInOneNewtonPerSecond, (double)kilonewtonperminuteQuantity.Value, KilonewtonsPerMinuteTolerance);
- Assert.Equal(ForceChangeRateUnit.KilonewtonPerMinute, kilonewtonperminuteQuantity.Unit);
-
- var kilonewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.KilonewtonPerSecond);
- AssertEx.EqualTolerance(KilonewtonsPerSecondInOneNewtonPerSecond, (double)kilonewtonpersecondQuantity.Value, KilonewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.KilonewtonPerSecond, kilonewtonpersecondQuantity.Unit);
-
- var kilopoundforceperminuteQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.KilopoundForcePerMinute);
- AssertEx.EqualTolerance(KilopoundsForcePerMinuteInOneNewtonPerSecond, (double)kilopoundforceperminuteQuantity.Value, KilopoundsForcePerMinuteTolerance);
- Assert.Equal(ForceChangeRateUnit.KilopoundForcePerMinute, kilopoundforceperminuteQuantity.Unit);
+ var inBaseUnits = ForceChangeRate.From(1.0, ForceChangeRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilopoundforcepersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.KilopoundForcePerSecond);
- AssertEx.EqualTolerance(KilopoundsForcePerSecondInOneNewtonPerSecond, (double)kilopoundforcepersecondQuantity.Value, KilopoundsForcePerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.KilopoundForcePerSecond, kilopoundforcepersecondQuantity.Unit);
-
- var micronewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.MicronewtonPerSecond);
- AssertEx.EqualTolerance(MicronewtonsPerSecondInOneNewtonPerSecond, (double)micronewtonpersecondQuantity.Value, MicronewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.MicronewtonPerSecond, micronewtonpersecondQuantity.Unit);
-
- var millinewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.MillinewtonPerSecond);
- AssertEx.EqualTolerance(MillinewtonsPerSecondInOneNewtonPerSecond, (double)millinewtonpersecondQuantity.Value, MillinewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.MillinewtonPerSecond, millinewtonpersecondQuantity.Unit);
-
- var nanonewtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.NanonewtonPerSecond);
- AssertEx.EqualTolerance(NanonewtonsPerSecondInOneNewtonPerSecond, (double)nanonewtonpersecondQuantity.Value, NanonewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.NanonewtonPerSecond, nanonewtonpersecondQuantity.Unit);
-
- var newtonperminuteQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.NewtonPerMinute);
- AssertEx.EqualTolerance(NewtonsPerMinuteInOneNewtonPerSecond, (double)newtonperminuteQuantity.Value, NewtonsPerMinuteTolerance);
- Assert.Equal(ForceChangeRateUnit.NewtonPerMinute, newtonperminuteQuantity.Unit);
-
- var newtonpersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.NewtonPerSecond);
- AssertEx.EqualTolerance(NewtonsPerSecondInOneNewtonPerSecond, (double)newtonpersecondQuantity.Value, NewtonsPerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.NewtonPerSecond, newtonpersecondQuantity.Unit);
-
- var poundforceperminuteQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.PoundForcePerMinute);
- AssertEx.EqualTolerance(PoundsForcePerMinuteInOneNewtonPerSecond, (double)poundforceperminuteQuantity.Value, PoundsForcePerMinuteTolerance);
- Assert.Equal(ForceChangeRateUnit.PoundForcePerMinute, poundforceperminuteQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundforcepersecondQuantity = newtonpersecond.ToUnit(ForceChangeRateUnit.PoundForcePerSecond);
- AssertEx.EqualTolerance(PoundsForcePerSecondInOneNewtonPerSecond, (double)poundforcepersecondQuantity.Value, PoundsForcePerSecondTolerance);
- Assert.Equal(ForceChangeRateUnit.PoundForcePerSecond, poundforcepersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ForceChangeRateUnit unit)
+ {
+ var quantity = ForceChangeRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ForceChangeRateUnit unit)
{
- var quantityInBaseUnit = ForceChangeRate.FromNewtonsPerSecond(1).ToBaseUnit();
- Assert.Equal(ForceChangeRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ForceChangeRate.Units.FirstOrDefault(u => u != ForceChangeRate.BaseUnit && u != ForceChangeRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ForceChangeRateUnit.Undefined)
+ fromUnit = ForceChangeRate.BaseUnit;
+
+ var quantity = ForceChangeRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ForcePerLengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ForcePerLengthTestsBase.g.cs
index 1ba699c782..7ddeacc15a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ForcePerLengthTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ForcePerLengthTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -117,6 +118,94 @@ public abstract partial class ForcePerLengthTestsBase : QuantityTestsBase
protected virtual double TonnesForcePerMillimeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ForcePerLengthUnit unit)
+ {
+ return unit switch
+ {
+ ForcePerLengthUnit.CentinewtonPerCentimeter => (CentinewtonsPerCentimeterInOneNewtonPerMeter, CentinewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.CentinewtonPerMeter => (CentinewtonsPerMeterInOneNewtonPerMeter, CentinewtonsPerMeterTolerance),
+ ForcePerLengthUnit.CentinewtonPerMillimeter => (CentinewtonsPerMillimeterInOneNewtonPerMeter, CentinewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.DecanewtonPerCentimeter => (DecanewtonsPerCentimeterInOneNewtonPerMeter, DecanewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.DecanewtonPerMeter => (DecanewtonsPerMeterInOneNewtonPerMeter, DecanewtonsPerMeterTolerance),
+ ForcePerLengthUnit.DecanewtonPerMillimeter => (DecanewtonsPerMillimeterInOneNewtonPerMeter, DecanewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.DecinewtonPerCentimeter => (DecinewtonsPerCentimeterInOneNewtonPerMeter, DecinewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.DecinewtonPerMeter => (DecinewtonsPerMeterInOneNewtonPerMeter, DecinewtonsPerMeterTolerance),
+ ForcePerLengthUnit.DecinewtonPerMillimeter => (DecinewtonsPerMillimeterInOneNewtonPerMeter, DecinewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.KilogramForcePerCentimeter => (KilogramsForcePerCentimeterInOneNewtonPerMeter, KilogramsForcePerCentimeterTolerance),
+ ForcePerLengthUnit.KilogramForcePerMeter => (KilogramsForcePerMeterInOneNewtonPerMeter, KilogramsForcePerMeterTolerance),
+ ForcePerLengthUnit.KilogramForcePerMillimeter => (KilogramsForcePerMillimeterInOneNewtonPerMeter, KilogramsForcePerMillimeterTolerance),
+ ForcePerLengthUnit.KilonewtonPerCentimeter => (KilonewtonsPerCentimeterInOneNewtonPerMeter, KilonewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.KilonewtonPerMeter => (KilonewtonsPerMeterInOneNewtonPerMeter, KilonewtonsPerMeterTolerance),
+ ForcePerLengthUnit.KilonewtonPerMillimeter => (KilonewtonsPerMillimeterInOneNewtonPerMeter, KilonewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.KilopoundForcePerFoot => (KilopoundsForcePerFootInOneNewtonPerMeter, KilopoundsForcePerFootTolerance),
+ ForcePerLengthUnit.KilopoundForcePerInch => (KilopoundsForcePerInchInOneNewtonPerMeter, KilopoundsForcePerInchTolerance),
+ ForcePerLengthUnit.MeganewtonPerCentimeter => (MeganewtonsPerCentimeterInOneNewtonPerMeter, MeganewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.MeganewtonPerMeter => (MeganewtonsPerMeterInOneNewtonPerMeter, MeganewtonsPerMeterTolerance),
+ ForcePerLengthUnit.MeganewtonPerMillimeter => (MeganewtonsPerMillimeterInOneNewtonPerMeter, MeganewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.MicronewtonPerCentimeter => (MicronewtonsPerCentimeterInOneNewtonPerMeter, MicronewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.MicronewtonPerMeter => (MicronewtonsPerMeterInOneNewtonPerMeter, MicronewtonsPerMeterTolerance),
+ ForcePerLengthUnit.MicronewtonPerMillimeter => (MicronewtonsPerMillimeterInOneNewtonPerMeter, MicronewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.MillinewtonPerCentimeter => (MillinewtonsPerCentimeterInOneNewtonPerMeter, MillinewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.MillinewtonPerMeter => (MillinewtonsPerMeterInOneNewtonPerMeter, MillinewtonsPerMeterTolerance),
+ ForcePerLengthUnit.MillinewtonPerMillimeter => (MillinewtonsPerMillimeterInOneNewtonPerMeter, MillinewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.NanonewtonPerCentimeter => (NanonewtonsPerCentimeterInOneNewtonPerMeter, NanonewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.NanonewtonPerMeter => (NanonewtonsPerMeterInOneNewtonPerMeter, NanonewtonsPerMeterTolerance),
+ ForcePerLengthUnit.NanonewtonPerMillimeter => (NanonewtonsPerMillimeterInOneNewtonPerMeter, NanonewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.NewtonPerCentimeter => (NewtonsPerCentimeterInOneNewtonPerMeter, NewtonsPerCentimeterTolerance),
+ ForcePerLengthUnit.NewtonPerMeter => (NewtonsPerMeterInOneNewtonPerMeter, NewtonsPerMeterTolerance),
+ ForcePerLengthUnit.NewtonPerMillimeter => (NewtonsPerMillimeterInOneNewtonPerMeter, NewtonsPerMillimeterTolerance),
+ ForcePerLengthUnit.PoundForcePerFoot => (PoundsForcePerFootInOneNewtonPerMeter, PoundsForcePerFootTolerance),
+ ForcePerLengthUnit.PoundForcePerInch => (PoundsForcePerInchInOneNewtonPerMeter, PoundsForcePerInchTolerance),
+ ForcePerLengthUnit.PoundForcePerYard => (PoundsForcePerYardInOneNewtonPerMeter, PoundsForcePerYardTolerance),
+ ForcePerLengthUnit.TonneForcePerCentimeter => (TonnesForcePerCentimeterInOneNewtonPerMeter, TonnesForcePerCentimeterTolerance),
+ ForcePerLengthUnit.TonneForcePerMeter => (TonnesForcePerMeterInOneNewtonPerMeter, TonnesForcePerMeterTolerance),
+ ForcePerLengthUnit.TonneForcePerMillimeter => (TonnesForcePerMillimeterInOneNewtonPerMeter, TonnesForcePerMillimeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ForcePerLengthUnit.CentinewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.CentinewtonPerMeter },
+ new object[] { ForcePerLengthUnit.CentinewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.DecanewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.DecanewtonPerMeter },
+ new object[] { ForcePerLengthUnit.DecanewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.DecinewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.DecinewtonPerMeter },
+ new object[] { ForcePerLengthUnit.DecinewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.KilogramForcePerCentimeter },
+ new object[] { ForcePerLengthUnit.KilogramForcePerMeter },
+ new object[] { ForcePerLengthUnit.KilogramForcePerMillimeter },
+ new object[] { ForcePerLengthUnit.KilonewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.KilonewtonPerMeter },
+ new object[] { ForcePerLengthUnit.KilonewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.KilopoundForcePerFoot },
+ new object[] { ForcePerLengthUnit.KilopoundForcePerInch },
+ new object[] { ForcePerLengthUnit.MeganewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.MeganewtonPerMeter },
+ new object[] { ForcePerLengthUnit.MeganewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.MicronewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.MicronewtonPerMeter },
+ new object[] { ForcePerLengthUnit.MicronewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.MillinewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.MillinewtonPerMeter },
+ new object[] { ForcePerLengthUnit.MillinewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.NanonewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.NanonewtonPerMeter },
+ new object[] { ForcePerLengthUnit.NanonewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.NewtonPerCentimeter },
+ new object[] { ForcePerLengthUnit.NewtonPerMeter },
+ new object[] { ForcePerLengthUnit.NewtonPerMillimeter },
+ new object[] { ForcePerLengthUnit.PoundForcePerFoot },
+ new object[] { ForcePerLengthUnit.PoundForcePerInch },
+ new object[] { ForcePerLengthUnit.PoundForcePerYard },
+ new object[] { ForcePerLengthUnit.TonneForcePerCentimeter },
+ new object[] { ForcePerLengthUnit.TonneForcePerMeter },
+ new object[] { ForcePerLengthUnit.TonneForcePerMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -460,169 +549,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ForcePerLengthUnit unit)
{
- var newtonpermeter = ForcePerLength.FromNewtonsPerMeter(1);
-
- var centinewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.CentinewtonPerCentimeter);
- AssertEx.EqualTolerance(CentinewtonsPerCentimeterInOneNewtonPerMeter, (double)centinewtonpercentimeterQuantity.Value, CentinewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.CentinewtonPerCentimeter, centinewtonpercentimeterQuantity.Unit);
-
- var centinewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.CentinewtonPerMeter);
- AssertEx.EqualTolerance(CentinewtonsPerMeterInOneNewtonPerMeter, (double)centinewtonpermeterQuantity.Value, CentinewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.CentinewtonPerMeter, centinewtonpermeterQuantity.Unit);
-
- var centinewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.CentinewtonPerMillimeter);
- AssertEx.EqualTolerance(CentinewtonsPerMillimeterInOneNewtonPerMeter, (double)centinewtonpermillimeterQuantity.Value, CentinewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.CentinewtonPerMillimeter, centinewtonpermillimeterQuantity.Unit);
-
- var decanewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.DecanewtonPerCentimeter);
- AssertEx.EqualTolerance(DecanewtonsPerCentimeterInOneNewtonPerMeter, (double)decanewtonpercentimeterQuantity.Value, DecanewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.DecanewtonPerCentimeter, decanewtonpercentimeterQuantity.Unit);
-
- var decanewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.DecanewtonPerMeter);
- AssertEx.EqualTolerance(DecanewtonsPerMeterInOneNewtonPerMeter, (double)decanewtonpermeterQuantity.Value, DecanewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.DecanewtonPerMeter, decanewtonpermeterQuantity.Unit);
-
- var decanewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.DecanewtonPerMillimeter);
- AssertEx.EqualTolerance(DecanewtonsPerMillimeterInOneNewtonPerMeter, (double)decanewtonpermillimeterQuantity.Value, DecanewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.DecanewtonPerMillimeter, decanewtonpermillimeterQuantity.Unit);
-
- var decinewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.DecinewtonPerCentimeter);
- AssertEx.EqualTolerance(DecinewtonsPerCentimeterInOneNewtonPerMeter, (double)decinewtonpercentimeterQuantity.Value, DecinewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.DecinewtonPerCentimeter, decinewtonpercentimeterQuantity.Unit);
-
- var decinewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.DecinewtonPerMeter);
- AssertEx.EqualTolerance(DecinewtonsPerMeterInOneNewtonPerMeter, (double)decinewtonpermeterQuantity.Value, DecinewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.DecinewtonPerMeter, decinewtonpermeterQuantity.Unit);
-
- var decinewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.DecinewtonPerMillimeter);
- AssertEx.EqualTolerance(DecinewtonsPerMillimeterInOneNewtonPerMeter, (double)decinewtonpermillimeterQuantity.Value, DecinewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.DecinewtonPerMillimeter, decinewtonpermillimeterQuantity.Unit);
-
- var kilogramforcepercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilogramForcePerCentimeter);
- AssertEx.EqualTolerance(KilogramsForcePerCentimeterInOneNewtonPerMeter, (double)kilogramforcepercentimeterQuantity.Value, KilogramsForcePerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.KilogramForcePerCentimeter, kilogramforcepercentimeterQuantity.Unit);
-
- var kilogramforcepermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilogramForcePerMeter);
- AssertEx.EqualTolerance(KilogramsForcePerMeterInOneNewtonPerMeter, (double)kilogramforcepermeterQuantity.Value, KilogramsForcePerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.KilogramForcePerMeter, kilogramforcepermeterQuantity.Unit);
-
- var kilogramforcepermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilogramForcePerMillimeter);
- AssertEx.EqualTolerance(KilogramsForcePerMillimeterInOneNewtonPerMeter, (double)kilogramforcepermillimeterQuantity.Value, KilogramsForcePerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.KilogramForcePerMillimeter, kilogramforcepermillimeterQuantity.Unit);
-
- var kilonewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilonewtonPerCentimeter);
- AssertEx.EqualTolerance(KilonewtonsPerCentimeterInOneNewtonPerMeter, (double)kilonewtonpercentimeterQuantity.Value, KilonewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.KilonewtonPerCentimeter, kilonewtonpercentimeterQuantity.Unit);
-
- var kilonewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilonewtonPerMeter);
- AssertEx.EqualTolerance(KilonewtonsPerMeterInOneNewtonPerMeter, (double)kilonewtonpermeterQuantity.Value, KilonewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.KilonewtonPerMeter, kilonewtonpermeterQuantity.Unit);
-
- var kilonewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilonewtonPerMillimeter);
- AssertEx.EqualTolerance(KilonewtonsPerMillimeterInOneNewtonPerMeter, (double)kilonewtonpermillimeterQuantity.Value, KilonewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.KilonewtonPerMillimeter, kilonewtonpermillimeterQuantity.Unit);
-
- var kilopoundforceperfootQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilopoundForcePerFoot);
- AssertEx.EqualTolerance(KilopoundsForcePerFootInOneNewtonPerMeter, (double)kilopoundforceperfootQuantity.Value, KilopoundsForcePerFootTolerance);
- Assert.Equal(ForcePerLengthUnit.KilopoundForcePerFoot, kilopoundforceperfootQuantity.Unit);
-
- var kilopoundforceperinchQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.KilopoundForcePerInch);
- AssertEx.EqualTolerance(KilopoundsForcePerInchInOneNewtonPerMeter, (double)kilopoundforceperinchQuantity.Value, KilopoundsForcePerInchTolerance);
- Assert.Equal(ForcePerLengthUnit.KilopoundForcePerInch, kilopoundforceperinchQuantity.Unit);
+ var inBaseUnits = ForcePerLength.From(1.0, ForcePerLength.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var meganewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MeganewtonPerCentimeter);
- AssertEx.EqualTolerance(MeganewtonsPerCentimeterInOneNewtonPerMeter, (double)meganewtonpercentimeterQuantity.Value, MeganewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MeganewtonPerCentimeter, meganewtonpercentimeterQuantity.Unit);
-
- var meganewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MeganewtonPerMeter);
- AssertEx.EqualTolerance(MeganewtonsPerMeterInOneNewtonPerMeter, (double)meganewtonpermeterQuantity.Value, MeganewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MeganewtonPerMeter, meganewtonpermeterQuantity.Unit);
-
- var meganewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MeganewtonPerMillimeter);
- AssertEx.EqualTolerance(MeganewtonsPerMillimeterInOneNewtonPerMeter, (double)meganewtonpermillimeterQuantity.Value, MeganewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MeganewtonPerMillimeter, meganewtonpermillimeterQuantity.Unit);
-
- var micronewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MicronewtonPerCentimeter);
- AssertEx.EqualTolerance(MicronewtonsPerCentimeterInOneNewtonPerMeter, (double)micronewtonpercentimeterQuantity.Value, MicronewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MicronewtonPerCentimeter, micronewtonpercentimeterQuantity.Unit);
-
- var micronewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MicronewtonPerMeter);
- AssertEx.EqualTolerance(MicronewtonsPerMeterInOneNewtonPerMeter, (double)micronewtonpermeterQuantity.Value, MicronewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MicronewtonPerMeter, micronewtonpermeterQuantity.Unit);
-
- var micronewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MicronewtonPerMillimeter);
- AssertEx.EqualTolerance(MicronewtonsPerMillimeterInOneNewtonPerMeter, (double)micronewtonpermillimeterQuantity.Value, MicronewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MicronewtonPerMillimeter, micronewtonpermillimeterQuantity.Unit);
-
- var millinewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MillinewtonPerCentimeter);
- AssertEx.EqualTolerance(MillinewtonsPerCentimeterInOneNewtonPerMeter, (double)millinewtonpercentimeterQuantity.Value, MillinewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MillinewtonPerCentimeter, millinewtonpercentimeterQuantity.Unit);
-
- var millinewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MillinewtonPerMeter);
- AssertEx.EqualTolerance(MillinewtonsPerMeterInOneNewtonPerMeter, (double)millinewtonpermeterQuantity.Value, MillinewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MillinewtonPerMeter, millinewtonpermeterQuantity.Unit);
-
- var millinewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.MillinewtonPerMillimeter);
- AssertEx.EqualTolerance(MillinewtonsPerMillimeterInOneNewtonPerMeter, (double)millinewtonpermillimeterQuantity.Value, MillinewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.MillinewtonPerMillimeter, millinewtonpermillimeterQuantity.Unit);
-
- var nanonewtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.NanonewtonPerCentimeter);
- AssertEx.EqualTolerance(NanonewtonsPerCentimeterInOneNewtonPerMeter, (double)nanonewtonpercentimeterQuantity.Value, NanonewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.NanonewtonPerCentimeter, nanonewtonpercentimeterQuantity.Unit);
-
- var nanonewtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.NanonewtonPerMeter);
- AssertEx.EqualTolerance(NanonewtonsPerMeterInOneNewtonPerMeter, (double)nanonewtonpermeterQuantity.Value, NanonewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.NanonewtonPerMeter, nanonewtonpermeterQuantity.Unit);
-
- var nanonewtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.NanonewtonPerMillimeter);
- AssertEx.EqualTolerance(NanonewtonsPerMillimeterInOneNewtonPerMeter, (double)nanonewtonpermillimeterQuantity.Value, NanonewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.NanonewtonPerMillimeter, nanonewtonpermillimeterQuantity.Unit);
-
- var newtonpercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.NewtonPerCentimeter);
- AssertEx.EqualTolerance(NewtonsPerCentimeterInOneNewtonPerMeter, (double)newtonpercentimeterQuantity.Value, NewtonsPerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.NewtonPerCentimeter, newtonpercentimeterQuantity.Unit);
-
- var newtonpermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.NewtonPerMeter);
- AssertEx.EqualTolerance(NewtonsPerMeterInOneNewtonPerMeter, (double)newtonpermeterQuantity.Value, NewtonsPerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.NewtonPerMeter, newtonpermeterQuantity.Unit);
-
- var newtonpermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.NewtonPerMillimeter);
- AssertEx.EqualTolerance(NewtonsPerMillimeterInOneNewtonPerMeter, (double)newtonpermillimeterQuantity.Value, NewtonsPerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.NewtonPerMillimeter, newtonpermillimeterQuantity.Unit);
-
- var poundforceperfootQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.PoundForcePerFoot);
- AssertEx.EqualTolerance(PoundsForcePerFootInOneNewtonPerMeter, (double)poundforceperfootQuantity.Value, PoundsForcePerFootTolerance);
- Assert.Equal(ForcePerLengthUnit.PoundForcePerFoot, poundforceperfootQuantity.Unit);
-
- var poundforceperinchQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.PoundForcePerInch);
- AssertEx.EqualTolerance(PoundsForcePerInchInOneNewtonPerMeter, (double)poundforceperinchQuantity.Value, PoundsForcePerInchTolerance);
- Assert.Equal(ForcePerLengthUnit.PoundForcePerInch, poundforceperinchQuantity.Unit);
-
- var poundforceperyardQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.PoundForcePerYard);
- AssertEx.EqualTolerance(PoundsForcePerYardInOneNewtonPerMeter, (double)poundforceperyardQuantity.Value, PoundsForcePerYardTolerance);
- Assert.Equal(ForcePerLengthUnit.PoundForcePerYard, poundforceperyardQuantity.Unit);
-
- var tonneforcepercentimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.TonneForcePerCentimeter);
- AssertEx.EqualTolerance(TonnesForcePerCentimeterInOneNewtonPerMeter, (double)tonneforcepercentimeterQuantity.Value, TonnesForcePerCentimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.TonneForcePerCentimeter, tonneforcepercentimeterQuantity.Unit);
-
- var tonneforcepermeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.TonneForcePerMeter);
- AssertEx.EqualTolerance(TonnesForcePerMeterInOneNewtonPerMeter, (double)tonneforcepermeterQuantity.Value, TonnesForcePerMeterTolerance);
- Assert.Equal(ForcePerLengthUnit.TonneForcePerMeter, tonneforcepermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneforcepermillimeterQuantity = newtonpermeter.ToUnit(ForcePerLengthUnit.TonneForcePerMillimeter);
- AssertEx.EqualTolerance(TonnesForcePerMillimeterInOneNewtonPerMeter, (double)tonneforcepermillimeterQuantity.Value, TonnesForcePerMillimeterTolerance);
- Assert.Equal(ForcePerLengthUnit.TonneForcePerMillimeter, tonneforcepermillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ForcePerLengthUnit unit)
+ {
+ var quantity = ForcePerLength.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ForcePerLengthUnit unit)
{
- var quantityInBaseUnit = ForcePerLength.FromNewtonsPerMeter(1).ToBaseUnit();
- Assert.Equal(ForcePerLength.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ForcePerLength.Units.FirstOrDefault(u => u != ForcePerLength.BaseUnit && u != ForcePerLengthUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ForcePerLengthUnit.Undefined)
+ fromUnit = ForcePerLength.BaseUnit;
+
+ var quantity = ForcePerLength.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ForceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ForceTestsBase.g.cs
index e99f00c8e2..38541fa42f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ForceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ForceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -71,6 +72,48 @@ public abstract partial class ForceTestsBase : QuantityTestsBase
protected virtual double TonnesForceTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ForceUnit unit)
+ {
+ return unit switch
+ {
+ ForceUnit.Decanewton => (DecanewtonsInOneNewton, DecanewtonsTolerance),
+ ForceUnit.Dyn => (DyneInOneNewton, DyneTolerance),
+ ForceUnit.KilogramForce => (KilogramsForceInOneNewton, KilogramsForceTolerance),
+ ForceUnit.Kilonewton => (KilonewtonsInOneNewton, KilonewtonsTolerance),
+ ForceUnit.KiloPond => (KiloPondsInOneNewton, KiloPondsTolerance),
+ ForceUnit.KilopoundForce => (KilopoundsForceInOneNewton, KilopoundsForceTolerance),
+ ForceUnit.Meganewton => (MeganewtonsInOneNewton, MeganewtonsTolerance),
+ ForceUnit.Micronewton => (MicronewtonsInOneNewton, MicronewtonsTolerance),
+ ForceUnit.Millinewton => (MillinewtonsInOneNewton, MillinewtonsTolerance),
+ ForceUnit.Newton => (NewtonsInOneNewton, NewtonsTolerance),
+ ForceUnit.OunceForce => (OunceForceInOneNewton, OunceForceTolerance),
+ ForceUnit.Poundal => (PoundalsInOneNewton, PoundalsTolerance),
+ ForceUnit.PoundForce => (PoundsForceInOneNewton, PoundsForceTolerance),
+ ForceUnit.ShortTonForce => (ShortTonsForceInOneNewton, ShortTonsForceTolerance),
+ ForceUnit.TonneForce => (TonnesForceInOneNewton, TonnesForceTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ForceUnit.Decanewton },
+ new object[] { ForceUnit.Dyn },
+ new object[] { ForceUnit.KilogramForce },
+ new object[] { ForceUnit.Kilonewton },
+ new object[] { ForceUnit.KiloPond },
+ new object[] { ForceUnit.KilopoundForce },
+ new object[] { ForceUnit.Meganewton },
+ new object[] { ForceUnit.Micronewton },
+ new object[] { ForceUnit.Millinewton },
+ new object[] { ForceUnit.Newton },
+ new object[] { ForceUnit.OunceForce },
+ new object[] { ForceUnit.Poundal },
+ new object[] { ForceUnit.PoundForce },
+ new object[] { ForceUnit.ShortTonForce },
+ new object[] { ForceUnit.TonneForce },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -276,77 +319,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ForceUnit unit)
{
- var newton = Force.FromNewtons(1);
-
- var decanewtonQuantity = newton.ToUnit(ForceUnit.Decanewton);
- AssertEx.EqualTolerance(DecanewtonsInOneNewton, (double)decanewtonQuantity.Value, DecanewtonsTolerance);
- Assert.Equal(ForceUnit.Decanewton, decanewtonQuantity.Unit);
-
- var dynQuantity = newton.ToUnit(ForceUnit.Dyn);
- AssertEx.EqualTolerance(DyneInOneNewton, (double)dynQuantity.Value, DyneTolerance);
- Assert.Equal(ForceUnit.Dyn, dynQuantity.Unit);
-
- var kilogramforceQuantity = newton.ToUnit(ForceUnit.KilogramForce);
- AssertEx.EqualTolerance(KilogramsForceInOneNewton, (double)kilogramforceQuantity.Value, KilogramsForceTolerance);
- Assert.Equal(ForceUnit.KilogramForce, kilogramforceQuantity.Unit);
-
- var kilonewtonQuantity = newton.ToUnit(ForceUnit.Kilonewton);
- AssertEx.EqualTolerance(KilonewtonsInOneNewton, (double)kilonewtonQuantity.Value, KilonewtonsTolerance);
- Assert.Equal(ForceUnit.Kilonewton, kilonewtonQuantity.Unit);
-
- var kilopondQuantity = newton.ToUnit(ForceUnit.KiloPond);
- AssertEx.EqualTolerance(KiloPondsInOneNewton, (double)kilopondQuantity.Value, KiloPondsTolerance);
- Assert.Equal(ForceUnit.KiloPond, kilopondQuantity.Unit);
-
- var kilopoundforceQuantity = newton.ToUnit(ForceUnit.KilopoundForce);
- AssertEx.EqualTolerance(KilopoundsForceInOneNewton, (double)kilopoundforceQuantity.Value, KilopoundsForceTolerance);
- Assert.Equal(ForceUnit.KilopoundForce, kilopoundforceQuantity.Unit);
-
- var meganewtonQuantity = newton.ToUnit(ForceUnit.Meganewton);
- AssertEx.EqualTolerance(MeganewtonsInOneNewton, (double)meganewtonQuantity.Value, MeganewtonsTolerance);
- Assert.Equal(ForceUnit.Meganewton, meganewtonQuantity.Unit);
+ var inBaseUnits = Force.From(1.0, Force.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var micronewtonQuantity = newton.ToUnit(ForceUnit.Micronewton);
- AssertEx.EqualTolerance(MicronewtonsInOneNewton, (double)micronewtonQuantity.Value, MicronewtonsTolerance);
- Assert.Equal(ForceUnit.Micronewton, micronewtonQuantity.Unit);
-
- var millinewtonQuantity = newton.ToUnit(ForceUnit.Millinewton);
- AssertEx.EqualTolerance(MillinewtonsInOneNewton, (double)millinewtonQuantity.Value, MillinewtonsTolerance);
- Assert.Equal(ForceUnit.Millinewton, millinewtonQuantity.Unit);
-
- var newtonQuantity = newton.ToUnit(ForceUnit.Newton);
- AssertEx.EqualTolerance(NewtonsInOneNewton, (double)newtonQuantity.Value, NewtonsTolerance);
- Assert.Equal(ForceUnit.Newton, newtonQuantity.Unit);
-
- var ounceforceQuantity = newton.ToUnit(ForceUnit.OunceForce);
- AssertEx.EqualTolerance(OunceForceInOneNewton, (double)ounceforceQuantity.Value, OunceForceTolerance);
- Assert.Equal(ForceUnit.OunceForce, ounceforceQuantity.Unit);
-
- var poundalQuantity = newton.ToUnit(ForceUnit.Poundal);
- AssertEx.EqualTolerance(PoundalsInOneNewton, (double)poundalQuantity.Value, PoundalsTolerance);
- Assert.Equal(ForceUnit.Poundal, poundalQuantity.Unit);
-
- var poundforceQuantity = newton.ToUnit(ForceUnit.PoundForce);
- AssertEx.EqualTolerance(PoundsForceInOneNewton, (double)poundforceQuantity.Value, PoundsForceTolerance);
- Assert.Equal(ForceUnit.PoundForce, poundforceQuantity.Unit);
-
- var shorttonforceQuantity = newton.ToUnit(ForceUnit.ShortTonForce);
- AssertEx.EqualTolerance(ShortTonsForceInOneNewton, (double)shorttonforceQuantity.Value, ShortTonsForceTolerance);
- Assert.Equal(ForceUnit.ShortTonForce, shorttonforceQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneforceQuantity = newton.ToUnit(ForceUnit.TonneForce);
- AssertEx.EqualTolerance(TonnesForceInOneNewton, (double)tonneforceQuantity.Value, TonnesForceTolerance);
- Assert.Equal(ForceUnit.TonneForce, tonneforceQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ForceUnit unit)
+ {
+ var quantity = Force.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ForceUnit unit)
{
- var quantityInBaseUnit = Force.FromNewtons(1).ToBaseUnit();
- Assert.Equal(Force.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Force.Units.FirstOrDefault(u => u != Force.BaseUnit && u != ForceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ForceUnit.Undefined)
+ fromUnit = Force.BaseUnit;
+
+ var quantity = Force.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/FrequencyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/FrequencyTestsBase.g.cs
index 80d4ed8863..b98ac9dc61 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/FrequencyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/FrequencyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -63,6 +64,40 @@ public abstract partial class FrequencyTestsBase : QuantityTestsBase
protected virtual double TerahertzTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(FrequencyUnit unit)
+ {
+ return unit switch
+ {
+ FrequencyUnit.BeatPerMinute => (BeatsPerMinuteInOneHertz, BeatsPerMinuteTolerance),
+ FrequencyUnit.BUnit => (BUnitsInOneHertz, BUnitsTolerance),
+ FrequencyUnit.CyclePerHour => (CyclesPerHourInOneHertz, CyclesPerHourTolerance),
+ FrequencyUnit.CyclePerMinute => (CyclesPerMinuteInOneHertz, CyclesPerMinuteTolerance),
+ FrequencyUnit.Gigahertz => (GigahertzInOneHertz, GigahertzTolerance),
+ FrequencyUnit.Hertz => (HertzInOneHertz, HertzTolerance),
+ FrequencyUnit.Kilohertz => (KilohertzInOneHertz, KilohertzTolerance),
+ FrequencyUnit.Megahertz => (MegahertzInOneHertz, MegahertzTolerance),
+ FrequencyUnit.PerSecond => (PerSecondInOneHertz, PerSecondTolerance),
+ FrequencyUnit.RadianPerSecond => (RadiansPerSecondInOneHertz, RadiansPerSecondTolerance),
+ FrequencyUnit.Terahertz => (TerahertzInOneHertz, TerahertzTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { FrequencyUnit.BeatPerMinute },
+ new object[] { FrequencyUnit.BUnit },
+ new object[] { FrequencyUnit.CyclePerHour },
+ new object[] { FrequencyUnit.CyclePerMinute },
+ new object[] { FrequencyUnit.Gigahertz },
+ new object[] { FrequencyUnit.Hertz },
+ new object[] { FrequencyUnit.Kilohertz },
+ new object[] { FrequencyUnit.Megahertz },
+ new object[] { FrequencyUnit.PerSecond },
+ new object[] { FrequencyUnit.RadianPerSecond },
+ new object[] { FrequencyUnit.Terahertz },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -244,61 +279,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(FrequencyUnit unit)
{
- var hertz = Frequency.FromHertz(1);
-
- var beatperminuteQuantity = hertz.ToUnit(FrequencyUnit.BeatPerMinute);
- AssertEx.EqualTolerance(BeatsPerMinuteInOneHertz, (double)beatperminuteQuantity.Value, BeatsPerMinuteTolerance);
- Assert.Equal(FrequencyUnit.BeatPerMinute, beatperminuteQuantity.Unit);
-
- var bunitQuantity = hertz.ToUnit(FrequencyUnit.BUnit);
- AssertEx.EqualTolerance(BUnitsInOneHertz, (double)bunitQuantity.Value, BUnitsTolerance);
- Assert.Equal(FrequencyUnit.BUnit, bunitQuantity.Unit);
-
- var cycleperhourQuantity = hertz.ToUnit(FrequencyUnit.CyclePerHour);
- AssertEx.EqualTolerance(CyclesPerHourInOneHertz, (double)cycleperhourQuantity.Value, CyclesPerHourTolerance);
- Assert.Equal(FrequencyUnit.CyclePerHour, cycleperhourQuantity.Unit);
-
- var cycleperminuteQuantity = hertz.ToUnit(FrequencyUnit.CyclePerMinute);
- AssertEx.EqualTolerance(CyclesPerMinuteInOneHertz, (double)cycleperminuteQuantity.Value, CyclesPerMinuteTolerance);
- Assert.Equal(FrequencyUnit.CyclePerMinute, cycleperminuteQuantity.Unit);
-
- var gigahertzQuantity = hertz.ToUnit(FrequencyUnit.Gigahertz);
- AssertEx.EqualTolerance(GigahertzInOneHertz, (double)gigahertzQuantity.Value, GigahertzTolerance);
- Assert.Equal(FrequencyUnit.Gigahertz, gigahertzQuantity.Unit);
+ var inBaseUnits = Frequency.From(1.0, Frequency.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var hertzQuantity = hertz.ToUnit(FrequencyUnit.Hertz);
- AssertEx.EqualTolerance(HertzInOneHertz, (double)hertzQuantity.Value, HertzTolerance);
- Assert.Equal(FrequencyUnit.Hertz, hertzQuantity.Unit);
-
- var kilohertzQuantity = hertz.ToUnit(FrequencyUnit.Kilohertz);
- AssertEx.EqualTolerance(KilohertzInOneHertz, (double)kilohertzQuantity.Value, KilohertzTolerance);
- Assert.Equal(FrequencyUnit.Kilohertz, kilohertzQuantity.Unit);
-
- var megahertzQuantity = hertz.ToUnit(FrequencyUnit.Megahertz);
- AssertEx.EqualTolerance(MegahertzInOneHertz, (double)megahertzQuantity.Value, MegahertzTolerance);
- Assert.Equal(FrequencyUnit.Megahertz, megahertzQuantity.Unit);
-
- var persecondQuantity = hertz.ToUnit(FrequencyUnit.PerSecond);
- AssertEx.EqualTolerance(PerSecondInOneHertz, (double)persecondQuantity.Value, PerSecondTolerance);
- Assert.Equal(FrequencyUnit.PerSecond, persecondQuantity.Unit);
-
- var radianpersecondQuantity = hertz.ToUnit(FrequencyUnit.RadianPerSecond);
- AssertEx.EqualTolerance(RadiansPerSecondInOneHertz, (double)radianpersecondQuantity.Value, RadiansPerSecondTolerance);
- Assert.Equal(FrequencyUnit.RadianPerSecond, radianpersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var terahertzQuantity = hertz.ToUnit(FrequencyUnit.Terahertz);
- AssertEx.EqualTolerance(TerahertzInOneHertz, (double)terahertzQuantity.Value, TerahertzTolerance);
- Assert.Equal(FrequencyUnit.Terahertz, terahertzQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(FrequencyUnit unit)
+ {
+ var quantity = Frequency.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(FrequencyUnit unit)
{
- var quantityInBaseUnit = Frequency.FromHertz(1).ToBaseUnit();
- Assert.Equal(Frequency.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Frequency.Units.FirstOrDefault(u => u != Frequency.BaseUnit && u != FrequencyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == FrequencyUnit.Undefined)
+ fromUnit = Frequency.BaseUnit;
+
+ var quantity = Frequency.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/FuelEfficiencyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/FuelEfficiencyTestsBase.g.cs
index 1c989ea4b2..3cb1885b6f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/FuelEfficiencyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/FuelEfficiencyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class FuelEfficiencyTestsBase : QuantityTestsBase
protected virtual double MilesPerUsGallonTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(FuelEfficiencyUnit unit)
+ {
+ return unit switch
+ {
+ FuelEfficiencyUnit.KilometerPerLiter => (KilometersPerLitersInOneLiterPer100Kilometers, KilometersPerLitersTolerance),
+ FuelEfficiencyUnit.LiterPer100Kilometers => (LitersPer100KilometersInOneLiterPer100Kilometers, LitersPer100KilometersTolerance),
+ FuelEfficiencyUnit.MilePerUkGallon => (MilesPerUkGallonInOneLiterPer100Kilometers, MilesPerUkGallonTolerance),
+ FuelEfficiencyUnit.MilePerUsGallon => (MilesPerUsGallonInOneLiterPer100Kilometers, MilesPerUsGallonTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { FuelEfficiencyUnit.KilometerPerLiter },
+ new object[] { FuelEfficiencyUnit.LiterPer100Kilometers },
+ new object[] { FuelEfficiencyUnit.MilePerUkGallon },
+ new object[] { FuelEfficiencyUnit.MilePerUsGallon },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(FuelEfficiencyUnit unit)
{
- var literper100kilometers = FuelEfficiency.FromLitersPer100Kilometers(1);
+ var inBaseUnits = FuelEfficiency.From(1.0, FuelEfficiency.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilometerperliterQuantity = literper100kilometers.ToUnit(FuelEfficiencyUnit.KilometerPerLiter);
- AssertEx.EqualTolerance(KilometersPerLitersInOneLiterPer100Kilometers, (double)kilometerperliterQuantity.Value, KilometersPerLitersTolerance);
- Assert.Equal(FuelEfficiencyUnit.KilometerPerLiter, kilometerperliterQuantity.Unit);
-
- var literper100kilometersQuantity = literper100kilometers.ToUnit(FuelEfficiencyUnit.LiterPer100Kilometers);
- AssertEx.EqualTolerance(LitersPer100KilometersInOneLiterPer100Kilometers, (double)literper100kilometersQuantity.Value, LitersPer100KilometersTolerance);
- Assert.Equal(FuelEfficiencyUnit.LiterPer100Kilometers, literper100kilometersQuantity.Unit);
-
- var mileperukgallonQuantity = literper100kilometers.ToUnit(FuelEfficiencyUnit.MilePerUkGallon);
- AssertEx.EqualTolerance(MilesPerUkGallonInOneLiterPer100Kilometers, (double)mileperukgallonQuantity.Value, MilesPerUkGallonTolerance);
- Assert.Equal(FuelEfficiencyUnit.MilePerUkGallon, mileperukgallonQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var mileperusgallonQuantity = literper100kilometers.ToUnit(FuelEfficiencyUnit.MilePerUsGallon);
- AssertEx.EqualTolerance(MilesPerUsGallonInOneLiterPer100Kilometers, (double)mileperusgallonQuantity.Value, MilesPerUsGallonTolerance);
- Assert.Equal(FuelEfficiencyUnit.MilePerUsGallon, mileperusgallonQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(FuelEfficiencyUnit unit)
+ {
+ var quantity = FuelEfficiency.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(FuelEfficiencyUnit unit)
{
- var quantityInBaseUnit = FuelEfficiency.FromLitersPer100Kilometers(1).ToBaseUnit();
- Assert.Equal(FuelEfficiency.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = FuelEfficiency.Units.FirstOrDefault(u => u != FuelEfficiency.BaseUnit && u != FuelEfficiencyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == FuelEfficiencyUnit.Undefined)
+ fromUnit = FuelEfficiency.BaseUnit;
+
+ var quantity = FuelEfficiency.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/HeatFluxTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/HeatFluxTestsBase.g.cs
index ef5b021573..327cc30781 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/HeatFluxTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/HeatFluxTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -77,6 +78,54 @@ public abstract partial class HeatFluxTestsBase : QuantityTestsBase
protected virtual double WattsPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(HeatFluxUnit unit)
+ {
+ return unit switch
+ {
+ HeatFluxUnit.BtuPerHourSquareFoot => (BtusPerHourSquareFootInOneWattPerSquareMeter, BtusPerHourSquareFootTolerance),
+ HeatFluxUnit.BtuPerMinuteSquareFoot => (BtusPerMinuteSquareFootInOneWattPerSquareMeter, BtusPerMinuteSquareFootTolerance),
+ HeatFluxUnit.BtuPerSecondSquareFoot => (BtusPerSecondSquareFootInOneWattPerSquareMeter, BtusPerSecondSquareFootTolerance),
+ HeatFluxUnit.BtuPerSecondSquareInch => (BtusPerSecondSquareInchInOneWattPerSquareMeter, BtusPerSecondSquareInchTolerance),
+ HeatFluxUnit.CaloriePerSecondSquareCentimeter => (CaloriesPerSecondSquareCentimeterInOneWattPerSquareMeter, CaloriesPerSecondSquareCentimeterTolerance),
+ HeatFluxUnit.CentiwattPerSquareMeter => (CentiwattsPerSquareMeterInOneWattPerSquareMeter, CentiwattsPerSquareMeterTolerance),
+ HeatFluxUnit.DeciwattPerSquareMeter => (DeciwattsPerSquareMeterInOneWattPerSquareMeter, DeciwattsPerSquareMeterTolerance),
+ HeatFluxUnit.KilocaloriePerHourSquareMeter => (KilocaloriesPerHourSquareMeterInOneWattPerSquareMeter, KilocaloriesPerHourSquareMeterTolerance),
+ HeatFluxUnit.KilocaloriePerSecondSquareCentimeter => (KilocaloriesPerSecondSquareCentimeterInOneWattPerSquareMeter, KilocaloriesPerSecondSquareCentimeterTolerance),
+ HeatFluxUnit.KilowattPerSquareMeter => (KilowattsPerSquareMeterInOneWattPerSquareMeter, KilowattsPerSquareMeterTolerance),
+ HeatFluxUnit.MicrowattPerSquareMeter => (MicrowattsPerSquareMeterInOneWattPerSquareMeter, MicrowattsPerSquareMeterTolerance),
+ HeatFluxUnit.MilliwattPerSquareMeter => (MilliwattsPerSquareMeterInOneWattPerSquareMeter, MilliwattsPerSquareMeterTolerance),
+ HeatFluxUnit.NanowattPerSquareMeter => (NanowattsPerSquareMeterInOneWattPerSquareMeter, NanowattsPerSquareMeterTolerance),
+ HeatFluxUnit.PoundForcePerFootSecond => (PoundsForcePerFootSecondInOneWattPerSquareMeter, PoundsForcePerFootSecondTolerance),
+ HeatFluxUnit.PoundPerSecondCubed => (PoundsPerSecondCubedInOneWattPerSquareMeter, PoundsPerSecondCubedTolerance),
+ HeatFluxUnit.WattPerSquareFoot => (WattsPerSquareFootInOneWattPerSquareMeter, WattsPerSquareFootTolerance),
+ HeatFluxUnit.WattPerSquareInch => (WattsPerSquareInchInOneWattPerSquareMeter, WattsPerSquareInchTolerance),
+ HeatFluxUnit.WattPerSquareMeter => (WattsPerSquareMeterInOneWattPerSquareMeter, WattsPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { HeatFluxUnit.BtuPerHourSquareFoot },
+ new object[] { HeatFluxUnit.BtuPerMinuteSquareFoot },
+ new object[] { HeatFluxUnit.BtuPerSecondSquareFoot },
+ new object[] { HeatFluxUnit.BtuPerSecondSquareInch },
+ new object[] { HeatFluxUnit.CaloriePerSecondSquareCentimeter },
+ new object[] { HeatFluxUnit.CentiwattPerSquareMeter },
+ new object[] { HeatFluxUnit.DeciwattPerSquareMeter },
+ new object[] { HeatFluxUnit.KilocaloriePerHourSquareMeter },
+ new object[] { HeatFluxUnit.KilocaloriePerSecondSquareCentimeter },
+ new object[] { HeatFluxUnit.KilowattPerSquareMeter },
+ new object[] { HeatFluxUnit.MicrowattPerSquareMeter },
+ new object[] { HeatFluxUnit.MilliwattPerSquareMeter },
+ new object[] { HeatFluxUnit.NanowattPerSquareMeter },
+ new object[] { HeatFluxUnit.PoundForcePerFootSecond },
+ new object[] { HeatFluxUnit.PoundPerSecondCubed },
+ new object[] { HeatFluxUnit.WattPerSquareFoot },
+ new object[] { HeatFluxUnit.WattPerSquareInch },
+ new object[] { HeatFluxUnit.WattPerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -300,89 +349,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(HeatFluxUnit unit)
{
- var wattpersquaremeter = HeatFlux.FromWattsPerSquareMeter(1);
-
- var btuperhoursquarefootQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.BtuPerHourSquareFoot);
- AssertEx.EqualTolerance(BtusPerHourSquareFootInOneWattPerSquareMeter, (double)btuperhoursquarefootQuantity.Value, BtusPerHourSquareFootTolerance);
- Assert.Equal(HeatFluxUnit.BtuPerHourSquareFoot, btuperhoursquarefootQuantity.Unit);
-
- var btuperminutesquarefootQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.BtuPerMinuteSquareFoot);
- AssertEx.EqualTolerance(BtusPerMinuteSquareFootInOneWattPerSquareMeter, (double)btuperminutesquarefootQuantity.Value, BtusPerMinuteSquareFootTolerance);
- Assert.Equal(HeatFluxUnit.BtuPerMinuteSquareFoot, btuperminutesquarefootQuantity.Unit);
-
- var btupersecondsquarefootQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.BtuPerSecondSquareFoot);
- AssertEx.EqualTolerance(BtusPerSecondSquareFootInOneWattPerSquareMeter, (double)btupersecondsquarefootQuantity.Value, BtusPerSecondSquareFootTolerance);
- Assert.Equal(HeatFluxUnit.BtuPerSecondSquareFoot, btupersecondsquarefootQuantity.Unit);
-
- var btupersecondsquareinchQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.BtuPerSecondSquareInch);
- AssertEx.EqualTolerance(BtusPerSecondSquareInchInOneWattPerSquareMeter, (double)btupersecondsquareinchQuantity.Value, BtusPerSecondSquareInchTolerance);
- Assert.Equal(HeatFluxUnit.BtuPerSecondSquareInch, btupersecondsquareinchQuantity.Unit);
-
- var caloriepersecondsquarecentimeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.CaloriePerSecondSquareCentimeter);
- AssertEx.EqualTolerance(CaloriesPerSecondSquareCentimeterInOneWattPerSquareMeter, (double)caloriepersecondsquarecentimeterQuantity.Value, CaloriesPerSecondSquareCentimeterTolerance);
- Assert.Equal(HeatFluxUnit.CaloriePerSecondSquareCentimeter, caloriepersecondsquarecentimeterQuantity.Unit);
-
- var centiwattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.CentiwattPerSquareMeter);
- AssertEx.EqualTolerance(CentiwattsPerSquareMeterInOneWattPerSquareMeter, (double)centiwattpersquaremeterQuantity.Value, CentiwattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.CentiwattPerSquareMeter, centiwattpersquaremeterQuantity.Unit);
-
- var deciwattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.DeciwattPerSquareMeter);
- AssertEx.EqualTolerance(DeciwattsPerSquareMeterInOneWattPerSquareMeter, (double)deciwattpersquaremeterQuantity.Value, DeciwattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.DeciwattPerSquareMeter, deciwattpersquaremeterQuantity.Unit);
-
- var kilocalorieperhoursquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.KilocaloriePerHourSquareMeter);
- AssertEx.EqualTolerance(KilocaloriesPerHourSquareMeterInOneWattPerSquareMeter, (double)kilocalorieperhoursquaremeterQuantity.Value, KilocaloriesPerHourSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.KilocaloriePerHourSquareMeter, kilocalorieperhoursquaremeterQuantity.Unit);
-
- var kilocaloriepersecondsquarecentimeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.KilocaloriePerSecondSquareCentimeter);
- AssertEx.EqualTolerance(KilocaloriesPerSecondSquareCentimeterInOneWattPerSquareMeter, (double)kilocaloriepersecondsquarecentimeterQuantity.Value, KilocaloriesPerSecondSquareCentimeterTolerance);
- Assert.Equal(HeatFluxUnit.KilocaloriePerSecondSquareCentimeter, kilocaloriepersecondsquarecentimeterQuantity.Unit);
+ var inBaseUnits = HeatFlux.From(1.0, HeatFlux.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilowattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.KilowattPerSquareMeter);
- AssertEx.EqualTolerance(KilowattsPerSquareMeterInOneWattPerSquareMeter, (double)kilowattpersquaremeterQuantity.Value, KilowattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.KilowattPerSquareMeter, kilowattpersquaremeterQuantity.Unit);
-
- var microwattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.MicrowattPerSquareMeter);
- AssertEx.EqualTolerance(MicrowattsPerSquareMeterInOneWattPerSquareMeter, (double)microwattpersquaremeterQuantity.Value, MicrowattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.MicrowattPerSquareMeter, microwattpersquaremeterQuantity.Unit);
-
- var milliwattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.MilliwattPerSquareMeter);
- AssertEx.EqualTolerance(MilliwattsPerSquareMeterInOneWattPerSquareMeter, (double)milliwattpersquaremeterQuantity.Value, MilliwattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.MilliwattPerSquareMeter, milliwattpersquaremeterQuantity.Unit);
-
- var nanowattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.NanowattPerSquareMeter);
- AssertEx.EqualTolerance(NanowattsPerSquareMeterInOneWattPerSquareMeter, (double)nanowattpersquaremeterQuantity.Value, NanowattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.NanowattPerSquareMeter, nanowattpersquaremeterQuantity.Unit);
-
- var poundforceperfootsecondQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.PoundForcePerFootSecond);
- AssertEx.EqualTolerance(PoundsForcePerFootSecondInOneWattPerSquareMeter, (double)poundforceperfootsecondQuantity.Value, PoundsForcePerFootSecondTolerance);
- Assert.Equal(HeatFluxUnit.PoundForcePerFootSecond, poundforceperfootsecondQuantity.Unit);
-
- var poundpersecondcubedQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.PoundPerSecondCubed);
- AssertEx.EqualTolerance(PoundsPerSecondCubedInOneWattPerSquareMeter, (double)poundpersecondcubedQuantity.Value, PoundsPerSecondCubedTolerance);
- Assert.Equal(HeatFluxUnit.PoundPerSecondCubed, poundpersecondcubedQuantity.Unit);
-
- var wattpersquarefootQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.WattPerSquareFoot);
- AssertEx.EqualTolerance(WattsPerSquareFootInOneWattPerSquareMeter, (double)wattpersquarefootQuantity.Value, WattsPerSquareFootTolerance);
- Assert.Equal(HeatFluxUnit.WattPerSquareFoot, wattpersquarefootQuantity.Unit);
-
- var wattpersquareinchQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.WattPerSquareInch);
- AssertEx.EqualTolerance(WattsPerSquareInchInOneWattPerSquareMeter, (double)wattpersquareinchQuantity.Value, WattsPerSquareInchTolerance);
- Assert.Equal(HeatFluxUnit.WattPerSquareInch, wattpersquareinchQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattpersquaremeterQuantity = wattpersquaremeter.ToUnit(HeatFluxUnit.WattPerSquareMeter);
- AssertEx.EqualTolerance(WattsPerSquareMeterInOneWattPerSquareMeter, (double)wattpersquaremeterQuantity.Value, WattsPerSquareMeterTolerance);
- Assert.Equal(HeatFluxUnit.WattPerSquareMeter, wattpersquaremeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(HeatFluxUnit unit)
+ {
+ var quantity = HeatFlux.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(HeatFluxUnit unit)
{
- var quantityInBaseUnit = HeatFlux.FromWattsPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(HeatFlux.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = HeatFlux.Units.FirstOrDefault(u => u != HeatFlux.BaseUnit && u != HeatFluxUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == HeatFluxUnit.Undefined)
+ fromUnit = HeatFlux.BaseUnit;
+
+ var quantity = HeatFlux.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/HeatTransferCoefficientTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/HeatTransferCoefficientTestsBase.g.cs
index b0ed942d4c..5688e459b1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/HeatTransferCoefficientTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/HeatTransferCoefficientTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class HeatTransferCoefficientTestsBase : QuantityTestsBa
protected virtual double WattsPerSquareMeterKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(HeatTransferCoefficientUnit unit)
+ {
+ return unit switch
+ {
+ HeatTransferCoefficientUnit.BtuPerSquareFootDegreeFahrenheit => (BtusPerSquareFootDegreeFahrenheitInOneWattPerSquareMeterKelvin, BtusPerSquareFootDegreeFahrenheitTolerance),
+ HeatTransferCoefficientUnit.WattPerSquareMeterCelsius => (WattsPerSquareMeterCelsiusInOneWattPerSquareMeterKelvin, WattsPerSquareMeterCelsiusTolerance),
+ HeatTransferCoefficientUnit.WattPerSquareMeterKelvin => (WattsPerSquareMeterKelvinInOneWattPerSquareMeterKelvin, WattsPerSquareMeterKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { HeatTransferCoefficientUnit.BtuPerSquareFootDegreeFahrenheit },
+ new object[] { HeatTransferCoefficientUnit.WattPerSquareMeterCelsius },
+ new object[] { HeatTransferCoefficientUnit.WattPerSquareMeterKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(HeatTransferCoefficientUnit unit)
{
- var wattpersquaremeterkelvin = HeatTransferCoefficient.FromWattsPerSquareMeterKelvin(1);
+ var inBaseUnits = HeatTransferCoefficient.From(1.0, HeatTransferCoefficient.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var btupersquarefootdegreefahrenheitQuantity = wattpersquaremeterkelvin.ToUnit(HeatTransferCoefficientUnit.BtuPerSquareFootDegreeFahrenheit);
- AssertEx.EqualTolerance(BtusPerSquareFootDegreeFahrenheitInOneWattPerSquareMeterKelvin, (double)btupersquarefootdegreefahrenheitQuantity.Value, BtusPerSquareFootDegreeFahrenheitTolerance);
- Assert.Equal(HeatTransferCoefficientUnit.BtuPerSquareFootDegreeFahrenheit, btupersquarefootdegreefahrenheitQuantity.Unit);
-
- var wattpersquaremetercelsiusQuantity = wattpersquaremeterkelvin.ToUnit(HeatTransferCoefficientUnit.WattPerSquareMeterCelsius);
- AssertEx.EqualTolerance(WattsPerSquareMeterCelsiusInOneWattPerSquareMeterKelvin, (double)wattpersquaremetercelsiusQuantity.Value, WattsPerSquareMeterCelsiusTolerance);
- Assert.Equal(HeatTransferCoefficientUnit.WattPerSquareMeterCelsius, wattpersquaremetercelsiusQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattpersquaremeterkelvinQuantity = wattpersquaremeterkelvin.ToUnit(HeatTransferCoefficientUnit.WattPerSquareMeterKelvin);
- AssertEx.EqualTolerance(WattsPerSquareMeterKelvinInOneWattPerSquareMeterKelvin, (double)wattpersquaremeterkelvinQuantity.Value, WattsPerSquareMeterKelvinTolerance);
- Assert.Equal(HeatTransferCoefficientUnit.WattPerSquareMeterKelvin, wattpersquaremeterkelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(HeatTransferCoefficientUnit unit)
+ {
+ var quantity = HeatTransferCoefficient.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(HeatTransferCoefficientUnit unit)
{
- var quantityInBaseUnit = HeatTransferCoefficient.FromWattsPerSquareMeterKelvin(1).ToBaseUnit();
- Assert.Equal(HeatTransferCoefficient.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = HeatTransferCoefficient.Units.FirstOrDefault(u => u != HeatTransferCoefficient.BaseUnit && u != HeatTransferCoefficientUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == HeatTransferCoefficientUnit.Undefined)
+ fromUnit = HeatTransferCoefficient.BaseUnit;
+
+ var quantity = HeatTransferCoefficient.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/IlluminanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/IlluminanceTestsBase.g.cs
index 5537fc12bd..5dad5be5cd 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/IlluminanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/IlluminanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class IlluminanceTestsBase : QuantityTestsBase
protected virtual double MilliluxTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(IlluminanceUnit unit)
+ {
+ return unit switch
+ {
+ IlluminanceUnit.Kilolux => (KiloluxInOneLux, KiloluxTolerance),
+ IlluminanceUnit.Lux => (LuxInOneLux, LuxTolerance),
+ IlluminanceUnit.Megalux => (MegaluxInOneLux, MegaluxTolerance),
+ IlluminanceUnit.Millilux => (MilliluxInOneLux, MilliluxTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { IlluminanceUnit.Kilolux },
+ new object[] { IlluminanceUnit.Lux },
+ new object[] { IlluminanceUnit.Megalux },
+ new object[] { IlluminanceUnit.Millilux },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(IlluminanceUnit unit)
{
- var lux = Illuminance.FromLux(1);
+ var inBaseUnits = Illuminance.From(1.0, Illuminance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kiloluxQuantity = lux.ToUnit(IlluminanceUnit.Kilolux);
- AssertEx.EqualTolerance(KiloluxInOneLux, (double)kiloluxQuantity.Value, KiloluxTolerance);
- Assert.Equal(IlluminanceUnit.Kilolux, kiloluxQuantity.Unit);
-
- var luxQuantity = lux.ToUnit(IlluminanceUnit.Lux);
- AssertEx.EqualTolerance(LuxInOneLux, (double)luxQuantity.Value, LuxTolerance);
- Assert.Equal(IlluminanceUnit.Lux, luxQuantity.Unit);
-
- var megaluxQuantity = lux.ToUnit(IlluminanceUnit.Megalux);
- AssertEx.EqualTolerance(MegaluxInOneLux, (double)megaluxQuantity.Value, MegaluxTolerance);
- Assert.Equal(IlluminanceUnit.Megalux, megaluxQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var milliluxQuantity = lux.ToUnit(IlluminanceUnit.Millilux);
- AssertEx.EqualTolerance(MilliluxInOneLux, (double)milliluxQuantity.Value, MilliluxTolerance);
- Assert.Equal(IlluminanceUnit.Millilux, milliluxQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(IlluminanceUnit unit)
+ {
+ var quantity = Illuminance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(IlluminanceUnit unit)
{
- var quantityInBaseUnit = Illuminance.FromLux(1).ToBaseUnit();
- Assert.Equal(Illuminance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Illuminance.Units.FirstOrDefault(u => u != Illuminance.BaseUnit && u != IlluminanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == IlluminanceUnit.Undefined)
+ fromUnit = Illuminance.BaseUnit;
+
+ var quantity = Illuminance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/InformationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/InformationTestsBase.g.cs
index b362e00824..d42510c69f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/InformationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/InformationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -93,6 +94,70 @@ public abstract partial class InformationTestsBase : QuantityTestsBase
protected virtual double TerabytesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(InformationUnit unit)
+ {
+ return unit switch
+ {
+ InformationUnit.Bit => (BitsInOneBit, BitsTolerance),
+ InformationUnit.Byte => (BytesInOneBit, BytesTolerance),
+ InformationUnit.Exabit => (ExabitsInOneBit, ExabitsTolerance),
+ InformationUnit.Exabyte => (ExabytesInOneBit, ExabytesTolerance),
+ InformationUnit.Exbibit => (ExbibitsInOneBit, ExbibitsTolerance),
+ InformationUnit.Exbibyte => (ExbibytesInOneBit, ExbibytesTolerance),
+ InformationUnit.Gibibit => (GibibitsInOneBit, GibibitsTolerance),
+ InformationUnit.Gibibyte => (GibibytesInOneBit, GibibytesTolerance),
+ InformationUnit.Gigabit => (GigabitsInOneBit, GigabitsTolerance),
+ InformationUnit.Gigabyte => (GigabytesInOneBit, GigabytesTolerance),
+ InformationUnit.Kibibit => (KibibitsInOneBit, KibibitsTolerance),
+ InformationUnit.Kibibyte => (KibibytesInOneBit, KibibytesTolerance),
+ InformationUnit.Kilobit => (KilobitsInOneBit, KilobitsTolerance),
+ InformationUnit.Kilobyte => (KilobytesInOneBit, KilobytesTolerance),
+ InformationUnit.Mebibit => (MebibitsInOneBit, MebibitsTolerance),
+ InformationUnit.Mebibyte => (MebibytesInOneBit, MebibytesTolerance),
+ InformationUnit.Megabit => (MegabitsInOneBit, MegabitsTolerance),
+ InformationUnit.Megabyte => (MegabytesInOneBit, MegabytesTolerance),
+ InformationUnit.Pebibit => (PebibitsInOneBit, PebibitsTolerance),
+ InformationUnit.Pebibyte => (PebibytesInOneBit, PebibytesTolerance),
+ InformationUnit.Petabit => (PetabitsInOneBit, PetabitsTolerance),
+ InformationUnit.Petabyte => (PetabytesInOneBit, PetabytesTolerance),
+ InformationUnit.Tebibit => (TebibitsInOneBit, TebibitsTolerance),
+ InformationUnit.Tebibyte => (TebibytesInOneBit, TebibytesTolerance),
+ InformationUnit.Terabit => (TerabitsInOneBit, TerabitsTolerance),
+ InformationUnit.Terabyte => (TerabytesInOneBit, TerabytesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { InformationUnit.Bit },
+ new object[] { InformationUnit.Byte },
+ new object[] { InformationUnit.Exabit },
+ new object[] { InformationUnit.Exabyte },
+ new object[] { InformationUnit.Exbibit },
+ new object[] { InformationUnit.Exbibyte },
+ new object[] { InformationUnit.Gibibit },
+ new object[] { InformationUnit.Gibibyte },
+ new object[] { InformationUnit.Gigabit },
+ new object[] { InformationUnit.Gigabyte },
+ new object[] { InformationUnit.Kibibit },
+ new object[] { InformationUnit.Kibibyte },
+ new object[] { InformationUnit.Kilobit },
+ new object[] { InformationUnit.Kilobyte },
+ new object[] { InformationUnit.Mebibit },
+ new object[] { InformationUnit.Mebibyte },
+ new object[] { InformationUnit.Megabit },
+ new object[] { InformationUnit.Megabyte },
+ new object[] { InformationUnit.Pebibit },
+ new object[] { InformationUnit.Pebibyte },
+ new object[] { InformationUnit.Petabit },
+ new object[] { InformationUnit.Petabyte },
+ new object[] { InformationUnit.Tebibit },
+ new object[] { InformationUnit.Tebibyte },
+ new object[] { InformationUnit.Terabit },
+ new object[] { InformationUnit.Terabyte },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -339,121 +404,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(InformationUnit unit)
{
- var bit = Information.FromBits(1);
-
- var bitQuantity = bit.ToUnit(InformationUnit.Bit);
- AssertEx.EqualTolerance(BitsInOneBit, (double)bitQuantity.Value, BitsTolerance);
- Assert.Equal(InformationUnit.Bit, bitQuantity.Unit);
-
- var byteQuantity = bit.ToUnit(InformationUnit.Byte);
- AssertEx.EqualTolerance(BytesInOneBit, (double)byteQuantity.Value, BytesTolerance);
- Assert.Equal(InformationUnit.Byte, byteQuantity.Unit);
-
- var exabitQuantity = bit.ToUnit(InformationUnit.Exabit);
- AssertEx.EqualTolerance(ExabitsInOneBit, (double)exabitQuantity.Value, ExabitsTolerance);
- Assert.Equal(InformationUnit.Exabit, exabitQuantity.Unit);
-
- var exabyteQuantity = bit.ToUnit(InformationUnit.Exabyte);
- AssertEx.EqualTolerance(ExabytesInOneBit, (double)exabyteQuantity.Value, ExabytesTolerance);
- Assert.Equal(InformationUnit.Exabyte, exabyteQuantity.Unit);
-
- var exbibitQuantity = bit.ToUnit(InformationUnit.Exbibit);
- AssertEx.EqualTolerance(ExbibitsInOneBit, (double)exbibitQuantity.Value, ExbibitsTolerance);
- Assert.Equal(InformationUnit.Exbibit, exbibitQuantity.Unit);
-
- var exbibyteQuantity = bit.ToUnit(InformationUnit.Exbibyte);
- AssertEx.EqualTolerance(ExbibytesInOneBit, (double)exbibyteQuantity.Value, ExbibytesTolerance);
- Assert.Equal(InformationUnit.Exbibyte, exbibyteQuantity.Unit);
-
- var gibibitQuantity = bit.ToUnit(InformationUnit.Gibibit);
- AssertEx.EqualTolerance(GibibitsInOneBit, (double)gibibitQuantity.Value, GibibitsTolerance);
- Assert.Equal(InformationUnit.Gibibit, gibibitQuantity.Unit);
-
- var gibibyteQuantity = bit.ToUnit(InformationUnit.Gibibyte);
- AssertEx.EqualTolerance(GibibytesInOneBit, (double)gibibyteQuantity.Value, GibibytesTolerance);
- Assert.Equal(InformationUnit.Gibibyte, gibibyteQuantity.Unit);
-
- var gigabitQuantity = bit.ToUnit(InformationUnit.Gigabit);
- AssertEx.EqualTolerance(GigabitsInOneBit, (double)gigabitQuantity.Value, GigabitsTolerance);
- Assert.Equal(InformationUnit.Gigabit, gigabitQuantity.Unit);
-
- var gigabyteQuantity = bit.ToUnit(InformationUnit.Gigabyte);
- AssertEx.EqualTolerance(GigabytesInOneBit, (double)gigabyteQuantity.Value, GigabytesTolerance);
- Assert.Equal(InformationUnit.Gigabyte, gigabyteQuantity.Unit);
-
- var kibibitQuantity = bit.ToUnit(InformationUnit.Kibibit);
- AssertEx.EqualTolerance(KibibitsInOneBit, (double)kibibitQuantity.Value, KibibitsTolerance);
- Assert.Equal(InformationUnit.Kibibit, kibibitQuantity.Unit);
-
- var kibibyteQuantity = bit.ToUnit(InformationUnit.Kibibyte);
- AssertEx.EqualTolerance(KibibytesInOneBit, (double)kibibyteQuantity.Value, KibibytesTolerance);
- Assert.Equal(InformationUnit.Kibibyte, kibibyteQuantity.Unit);
-
- var kilobitQuantity = bit.ToUnit(InformationUnit.Kilobit);
- AssertEx.EqualTolerance(KilobitsInOneBit, (double)kilobitQuantity.Value, KilobitsTolerance);
- Assert.Equal(InformationUnit.Kilobit, kilobitQuantity.Unit);
+ var inBaseUnits = Information.From(1.0, Information.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilobyteQuantity = bit.ToUnit(InformationUnit.Kilobyte);
- AssertEx.EqualTolerance(KilobytesInOneBit, (double)kilobyteQuantity.Value, KilobytesTolerance);
- Assert.Equal(InformationUnit.Kilobyte, kilobyteQuantity.Unit);
-
- var mebibitQuantity = bit.ToUnit(InformationUnit.Mebibit);
- AssertEx.EqualTolerance(MebibitsInOneBit, (double)mebibitQuantity.Value, MebibitsTolerance);
- Assert.Equal(InformationUnit.Mebibit, mebibitQuantity.Unit);
-
- var mebibyteQuantity = bit.ToUnit(InformationUnit.Mebibyte);
- AssertEx.EqualTolerance(MebibytesInOneBit, (double)mebibyteQuantity.Value, MebibytesTolerance);
- Assert.Equal(InformationUnit.Mebibyte, mebibyteQuantity.Unit);
-
- var megabitQuantity = bit.ToUnit(InformationUnit.Megabit);
- AssertEx.EqualTolerance(MegabitsInOneBit, (double)megabitQuantity.Value, MegabitsTolerance);
- Assert.Equal(InformationUnit.Megabit, megabitQuantity.Unit);
-
- var megabyteQuantity = bit.ToUnit(InformationUnit.Megabyte);
- AssertEx.EqualTolerance(MegabytesInOneBit, (double)megabyteQuantity.Value, MegabytesTolerance);
- Assert.Equal(InformationUnit.Megabyte, megabyteQuantity.Unit);
-
- var pebibitQuantity = bit.ToUnit(InformationUnit.Pebibit);
- AssertEx.EqualTolerance(PebibitsInOneBit, (double)pebibitQuantity.Value, PebibitsTolerance);
- Assert.Equal(InformationUnit.Pebibit, pebibitQuantity.Unit);
-
- var pebibyteQuantity = bit.ToUnit(InformationUnit.Pebibyte);
- AssertEx.EqualTolerance(PebibytesInOneBit, (double)pebibyteQuantity.Value, PebibytesTolerance);
- Assert.Equal(InformationUnit.Pebibyte, pebibyteQuantity.Unit);
-
- var petabitQuantity = bit.ToUnit(InformationUnit.Petabit);
- AssertEx.EqualTolerance(PetabitsInOneBit, (double)petabitQuantity.Value, PetabitsTolerance);
- Assert.Equal(InformationUnit.Petabit, petabitQuantity.Unit);
-
- var petabyteQuantity = bit.ToUnit(InformationUnit.Petabyte);
- AssertEx.EqualTolerance(PetabytesInOneBit, (double)petabyteQuantity.Value, PetabytesTolerance);
- Assert.Equal(InformationUnit.Petabyte, petabyteQuantity.Unit);
-
- var tebibitQuantity = bit.ToUnit(InformationUnit.Tebibit);
- AssertEx.EqualTolerance(TebibitsInOneBit, (double)tebibitQuantity.Value, TebibitsTolerance);
- Assert.Equal(InformationUnit.Tebibit, tebibitQuantity.Unit);
-
- var tebibyteQuantity = bit.ToUnit(InformationUnit.Tebibyte);
- AssertEx.EqualTolerance(TebibytesInOneBit, (double)tebibyteQuantity.Value, TebibytesTolerance);
- Assert.Equal(InformationUnit.Tebibyte, tebibyteQuantity.Unit);
-
- var terabitQuantity = bit.ToUnit(InformationUnit.Terabit);
- AssertEx.EqualTolerance(TerabitsInOneBit, (double)terabitQuantity.Value, TerabitsTolerance);
- Assert.Equal(InformationUnit.Terabit, terabitQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var terabyteQuantity = bit.ToUnit(InformationUnit.Terabyte);
- AssertEx.EqualTolerance(TerabytesInOneBit, (double)terabyteQuantity.Value, TerabytesTolerance);
- Assert.Equal(InformationUnit.Terabyte, terabyteQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(InformationUnit unit)
+ {
+ var quantity = Information.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(InformationUnit unit)
{
- var quantityInBaseUnit = Information.FromBits(1).ToBaseUnit();
- Assert.Equal(Information.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Information.Units.FirstOrDefault(u => u != Information.BaseUnit && u != InformationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == InformationUnit.Undefined)
+ fromUnit = Information.BaseUnit;
+
+ var quantity = Information.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/IrradianceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/IrradianceTestsBase.g.cs
index 76e0cd7403..6e6f49a82a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/IrradianceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/IrradianceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class IrradianceTestsBase : QuantityTestsBase
protected virtual double WattsPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(IrradianceUnit unit)
+ {
+ return unit switch
+ {
+ IrradianceUnit.KilowattPerSquareCentimeter => (KilowattsPerSquareCentimeterInOneWattPerSquareMeter, KilowattsPerSquareCentimeterTolerance),
+ IrradianceUnit.KilowattPerSquareMeter => (KilowattsPerSquareMeterInOneWattPerSquareMeter, KilowattsPerSquareMeterTolerance),
+ IrradianceUnit.MegawattPerSquareCentimeter => (MegawattsPerSquareCentimeterInOneWattPerSquareMeter, MegawattsPerSquareCentimeterTolerance),
+ IrradianceUnit.MegawattPerSquareMeter => (MegawattsPerSquareMeterInOneWattPerSquareMeter, MegawattsPerSquareMeterTolerance),
+ IrradianceUnit.MicrowattPerSquareCentimeter => (MicrowattsPerSquareCentimeterInOneWattPerSquareMeter, MicrowattsPerSquareCentimeterTolerance),
+ IrradianceUnit.MicrowattPerSquareMeter => (MicrowattsPerSquareMeterInOneWattPerSquareMeter, MicrowattsPerSquareMeterTolerance),
+ IrradianceUnit.MilliwattPerSquareCentimeter => (MilliwattsPerSquareCentimeterInOneWattPerSquareMeter, MilliwattsPerSquareCentimeterTolerance),
+ IrradianceUnit.MilliwattPerSquareMeter => (MilliwattsPerSquareMeterInOneWattPerSquareMeter, MilliwattsPerSquareMeterTolerance),
+ IrradianceUnit.NanowattPerSquareCentimeter => (NanowattsPerSquareCentimeterInOneWattPerSquareMeter, NanowattsPerSquareCentimeterTolerance),
+ IrradianceUnit.NanowattPerSquareMeter => (NanowattsPerSquareMeterInOneWattPerSquareMeter, NanowattsPerSquareMeterTolerance),
+ IrradianceUnit.PicowattPerSquareCentimeter => (PicowattsPerSquareCentimeterInOneWattPerSquareMeter, PicowattsPerSquareCentimeterTolerance),
+ IrradianceUnit.PicowattPerSquareMeter => (PicowattsPerSquareMeterInOneWattPerSquareMeter, PicowattsPerSquareMeterTolerance),
+ IrradianceUnit.WattPerSquareCentimeter => (WattsPerSquareCentimeterInOneWattPerSquareMeter, WattsPerSquareCentimeterTolerance),
+ IrradianceUnit.WattPerSquareMeter => (WattsPerSquareMeterInOneWattPerSquareMeter, WattsPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { IrradianceUnit.KilowattPerSquareCentimeter },
+ new object[] { IrradianceUnit.KilowattPerSquareMeter },
+ new object[] { IrradianceUnit.MegawattPerSquareCentimeter },
+ new object[] { IrradianceUnit.MegawattPerSquareMeter },
+ new object[] { IrradianceUnit.MicrowattPerSquareCentimeter },
+ new object[] { IrradianceUnit.MicrowattPerSquareMeter },
+ new object[] { IrradianceUnit.MilliwattPerSquareCentimeter },
+ new object[] { IrradianceUnit.MilliwattPerSquareMeter },
+ new object[] { IrradianceUnit.NanowattPerSquareCentimeter },
+ new object[] { IrradianceUnit.NanowattPerSquareMeter },
+ new object[] { IrradianceUnit.PicowattPerSquareCentimeter },
+ new object[] { IrradianceUnit.PicowattPerSquareMeter },
+ new object[] { IrradianceUnit.WattPerSquareCentimeter },
+ new object[] { IrradianceUnit.WattPerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(IrradianceUnit unit)
{
- var wattpersquaremeter = Irradiance.FromWattsPerSquareMeter(1);
-
- var kilowattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.KilowattPerSquareCentimeter);
- AssertEx.EqualTolerance(KilowattsPerSquareCentimeterInOneWattPerSquareMeter, (double)kilowattpersquarecentimeterQuantity.Value, KilowattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.KilowattPerSquareCentimeter, kilowattpersquarecentimeterQuantity.Unit);
-
- var kilowattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.KilowattPerSquareMeter);
- AssertEx.EqualTolerance(KilowattsPerSquareMeterInOneWattPerSquareMeter, (double)kilowattpersquaremeterQuantity.Value, KilowattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.KilowattPerSquareMeter, kilowattpersquaremeterQuantity.Unit);
-
- var megawattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.MegawattPerSquareCentimeter);
- AssertEx.EqualTolerance(MegawattsPerSquareCentimeterInOneWattPerSquareMeter, (double)megawattpersquarecentimeterQuantity.Value, MegawattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.MegawattPerSquareCentimeter, megawattpersquarecentimeterQuantity.Unit);
-
- var megawattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.MegawattPerSquareMeter);
- AssertEx.EqualTolerance(MegawattsPerSquareMeterInOneWattPerSquareMeter, (double)megawattpersquaremeterQuantity.Value, MegawattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.MegawattPerSquareMeter, megawattpersquaremeterQuantity.Unit);
-
- var microwattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.MicrowattPerSquareCentimeter);
- AssertEx.EqualTolerance(MicrowattsPerSquareCentimeterInOneWattPerSquareMeter, (double)microwattpersquarecentimeterQuantity.Value, MicrowattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.MicrowattPerSquareCentimeter, microwattpersquarecentimeterQuantity.Unit);
-
- var microwattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.MicrowattPerSquareMeter);
- AssertEx.EqualTolerance(MicrowattsPerSquareMeterInOneWattPerSquareMeter, (double)microwattpersquaremeterQuantity.Value, MicrowattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.MicrowattPerSquareMeter, microwattpersquaremeterQuantity.Unit);
-
- var milliwattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.MilliwattPerSquareCentimeter);
- AssertEx.EqualTolerance(MilliwattsPerSquareCentimeterInOneWattPerSquareMeter, (double)milliwattpersquarecentimeterQuantity.Value, MilliwattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.MilliwattPerSquareCentimeter, milliwattpersquarecentimeterQuantity.Unit);
+ var inBaseUnits = Irradiance.From(1.0, Irradiance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var milliwattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.MilliwattPerSquareMeter);
- AssertEx.EqualTolerance(MilliwattsPerSquareMeterInOneWattPerSquareMeter, (double)milliwattpersquaremeterQuantity.Value, MilliwattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.MilliwattPerSquareMeter, milliwattpersquaremeterQuantity.Unit);
-
- var nanowattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.NanowattPerSquareCentimeter);
- AssertEx.EqualTolerance(NanowattsPerSquareCentimeterInOneWattPerSquareMeter, (double)nanowattpersquarecentimeterQuantity.Value, NanowattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.NanowattPerSquareCentimeter, nanowattpersquarecentimeterQuantity.Unit);
-
- var nanowattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.NanowattPerSquareMeter);
- AssertEx.EqualTolerance(NanowattsPerSquareMeterInOneWattPerSquareMeter, (double)nanowattpersquaremeterQuantity.Value, NanowattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.NanowattPerSquareMeter, nanowattpersquaremeterQuantity.Unit);
-
- var picowattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.PicowattPerSquareCentimeter);
- AssertEx.EqualTolerance(PicowattsPerSquareCentimeterInOneWattPerSquareMeter, (double)picowattpersquarecentimeterQuantity.Value, PicowattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.PicowattPerSquareCentimeter, picowattpersquarecentimeterQuantity.Unit);
-
- var picowattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.PicowattPerSquareMeter);
- AssertEx.EqualTolerance(PicowattsPerSquareMeterInOneWattPerSquareMeter, (double)picowattpersquaremeterQuantity.Value, PicowattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.PicowattPerSquareMeter, picowattpersquaremeterQuantity.Unit);
-
- var wattpersquarecentimeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.WattPerSquareCentimeter);
- AssertEx.EqualTolerance(WattsPerSquareCentimeterInOneWattPerSquareMeter, (double)wattpersquarecentimeterQuantity.Value, WattsPerSquareCentimeterTolerance);
- Assert.Equal(IrradianceUnit.WattPerSquareCentimeter, wattpersquarecentimeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattpersquaremeterQuantity = wattpersquaremeter.ToUnit(IrradianceUnit.WattPerSquareMeter);
- AssertEx.EqualTolerance(WattsPerSquareMeterInOneWattPerSquareMeter, (double)wattpersquaremeterQuantity.Value, WattsPerSquareMeterTolerance);
- Assert.Equal(IrradianceUnit.WattPerSquareMeter, wattpersquaremeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(IrradianceUnit unit)
+ {
+ var quantity = Irradiance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(IrradianceUnit unit)
{
- var quantityInBaseUnit = Irradiance.FromWattsPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(Irradiance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Irradiance.Units.FirstOrDefault(u => u != Irradiance.BaseUnit && u != IrradianceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == IrradianceUnit.Undefined)
+ fromUnit = Irradiance.BaseUnit;
+
+ var quantity = Irradiance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/IrradiationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/IrradiationTestsBase.g.cs
index afdd0b60c3..1fa8cb53cd 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/IrradiationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/IrradiationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -55,6 +56,32 @@ public abstract partial class IrradiationTestsBase : QuantityTestsBase
protected virtual double WattHoursPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(IrradiationUnit unit)
+ {
+ return unit switch
+ {
+ IrradiationUnit.JoulePerSquareCentimeter => (JoulesPerSquareCentimeterInOneJoulePerSquareMeter, JoulesPerSquareCentimeterTolerance),
+ IrradiationUnit.JoulePerSquareMeter => (JoulesPerSquareMeterInOneJoulePerSquareMeter, JoulesPerSquareMeterTolerance),
+ IrradiationUnit.JoulePerSquareMillimeter => (JoulesPerSquareMillimeterInOneJoulePerSquareMeter, JoulesPerSquareMillimeterTolerance),
+ IrradiationUnit.KilojoulePerSquareMeter => (KilojoulesPerSquareMeterInOneJoulePerSquareMeter, KilojoulesPerSquareMeterTolerance),
+ IrradiationUnit.KilowattHourPerSquareMeter => (KilowattHoursPerSquareMeterInOneJoulePerSquareMeter, KilowattHoursPerSquareMeterTolerance),
+ IrradiationUnit.MillijoulePerSquareCentimeter => (MillijoulesPerSquareCentimeterInOneJoulePerSquareMeter, MillijoulesPerSquareCentimeterTolerance),
+ IrradiationUnit.WattHourPerSquareMeter => (WattHoursPerSquareMeterInOneJoulePerSquareMeter, WattHoursPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { IrradiationUnit.JoulePerSquareCentimeter },
+ new object[] { IrradiationUnit.JoulePerSquareMeter },
+ new object[] { IrradiationUnit.JoulePerSquareMillimeter },
+ new object[] { IrradiationUnit.KilojoulePerSquareMeter },
+ new object[] { IrradiationUnit.KilowattHourPerSquareMeter },
+ new object[] { IrradiationUnit.MillijoulePerSquareCentimeter },
+ new object[] { IrradiationUnit.WattHourPerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -212,45 +239,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(IrradiationUnit unit)
{
- var joulepersquaremeter = Irradiation.FromJoulesPerSquareMeter(1);
-
- var joulepersquarecentimeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.JoulePerSquareCentimeter);
- AssertEx.EqualTolerance(JoulesPerSquareCentimeterInOneJoulePerSquareMeter, (double)joulepersquarecentimeterQuantity.Value, JoulesPerSquareCentimeterTolerance);
- Assert.Equal(IrradiationUnit.JoulePerSquareCentimeter, joulepersquarecentimeterQuantity.Unit);
-
- var joulepersquaremeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.JoulePerSquareMeter);
- AssertEx.EqualTolerance(JoulesPerSquareMeterInOneJoulePerSquareMeter, (double)joulepersquaremeterQuantity.Value, JoulesPerSquareMeterTolerance);
- Assert.Equal(IrradiationUnit.JoulePerSquareMeter, joulepersquaremeterQuantity.Unit);
+ var inBaseUnits = Irradiation.From(1.0, Irradiation.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var joulepersquaremillimeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.JoulePerSquareMillimeter);
- AssertEx.EqualTolerance(JoulesPerSquareMillimeterInOneJoulePerSquareMeter, (double)joulepersquaremillimeterQuantity.Value, JoulesPerSquareMillimeterTolerance);
- Assert.Equal(IrradiationUnit.JoulePerSquareMillimeter, joulepersquaremillimeterQuantity.Unit);
-
- var kilojoulepersquaremeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.KilojoulePerSquareMeter);
- AssertEx.EqualTolerance(KilojoulesPerSquareMeterInOneJoulePerSquareMeter, (double)kilojoulepersquaremeterQuantity.Value, KilojoulesPerSquareMeterTolerance);
- Assert.Equal(IrradiationUnit.KilojoulePerSquareMeter, kilojoulepersquaremeterQuantity.Unit);
-
- var kilowatthourpersquaremeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.KilowattHourPerSquareMeter);
- AssertEx.EqualTolerance(KilowattHoursPerSquareMeterInOneJoulePerSquareMeter, (double)kilowatthourpersquaremeterQuantity.Value, KilowattHoursPerSquareMeterTolerance);
- Assert.Equal(IrradiationUnit.KilowattHourPerSquareMeter, kilowatthourpersquaremeterQuantity.Unit);
-
- var millijoulepersquarecentimeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.MillijoulePerSquareCentimeter);
- AssertEx.EqualTolerance(MillijoulesPerSquareCentimeterInOneJoulePerSquareMeter, (double)millijoulepersquarecentimeterQuantity.Value, MillijoulesPerSquareCentimeterTolerance);
- Assert.Equal(IrradiationUnit.MillijoulePerSquareCentimeter, millijoulepersquarecentimeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var watthourpersquaremeterQuantity = joulepersquaremeter.ToUnit(IrradiationUnit.WattHourPerSquareMeter);
- AssertEx.EqualTolerance(WattHoursPerSquareMeterInOneJoulePerSquareMeter, (double)watthourpersquaremeterQuantity.Value, WattHoursPerSquareMeterTolerance);
- Assert.Equal(IrradiationUnit.WattHourPerSquareMeter, watthourpersquaremeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(IrradiationUnit unit)
+ {
+ var quantity = Irradiation.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(IrradiationUnit unit)
{
- var quantityInBaseUnit = Irradiation.FromJoulesPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(Irradiation.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Irradiation.Units.FirstOrDefault(u => u != Irradiation.BaseUnit && u != IrradiationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == IrradiationUnit.Undefined)
+ fromUnit = Irradiation.BaseUnit;
+
+ var quantity = Irradiation.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/KinematicViscosityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/KinematicViscosityTestsBase.g.cs
index 34e19f82df..75b9a0be85 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/KinematicViscosityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/KinematicViscosityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -59,6 +60,36 @@ public abstract partial class KinematicViscosityTestsBase : QuantityTestsBase
protected virtual double StokesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(KinematicViscosityUnit unit)
+ {
+ return unit switch
+ {
+ KinematicViscosityUnit.Centistokes => (CentistokesInOneSquareMeterPerSecond, CentistokesTolerance),
+ KinematicViscosityUnit.Decistokes => (DecistokesInOneSquareMeterPerSecond, DecistokesTolerance),
+ KinematicViscosityUnit.Kilostokes => (KilostokesInOneSquareMeterPerSecond, KilostokesTolerance),
+ KinematicViscosityUnit.Microstokes => (MicrostokesInOneSquareMeterPerSecond, MicrostokesTolerance),
+ KinematicViscosityUnit.Millistokes => (MillistokesInOneSquareMeterPerSecond, MillistokesTolerance),
+ KinematicViscosityUnit.Nanostokes => (NanostokesInOneSquareMeterPerSecond, NanostokesTolerance),
+ KinematicViscosityUnit.SquareFootPerSecond => (SquareFeetPerSecondInOneSquareMeterPerSecond, SquareFeetPerSecondTolerance),
+ KinematicViscosityUnit.SquareMeterPerSecond => (SquareMetersPerSecondInOneSquareMeterPerSecond, SquareMetersPerSecondTolerance),
+ KinematicViscosityUnit.Stokes => (StokesInOneSquareMeterPerSecond, StokesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { KinematicViscosityUnit.Centistokes },
+ new object[] { KinematicViscosityUnit.Decistokes },
+ new object[] { KinematicViscosityUnit.Kilostokes },
+ new object[] { KinematicViscosityUnit.Microstokes },
+ new object[] { KinematicViscosityUnit.Millistokes },
+ new object[] { KinematicViscosityUnit.Nanostokes },
+ new object[] { KinematicViscosityUnit.SquareFootPerSecond },
+ new object[] { KinematicViscosityUnit.SquareMeterPerSecond },
+ new object[] { KinematicViscosityUnit.Stokes },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -228,53 +259,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(KinematicViscosityUnit unit)
{
- var squaremeterpersecond = KinematicViscosity.FromSquareMetersPerSecond(1);
-
- var centistokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Centistokes);
- AssertEx.EqualTolerance(CentistokesInOneSquareMeterPerSecond, (double)centistokesQuantity.Value, CentistokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Centistokes, centistokesQuantity.Unit);
-
- var decistokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Decistokes);
- AssertEx.EqualTolerance(DecistokesInOneSquareMeterPerSecond, (double)decistokesQuantity.Value, DecistokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Decistokes, decistokesQuantity.Unit);
-
- var kilostokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Kilostokes);
- AssertEx.EqualTolerance(KilostokesInOneSquareMeterPerSecond, (double)kilostokesQuantity.Value, KilostokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Kilostokes, kilostokesQuantity.Unit);
-
- var microstokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Microstokes);
- AssertEx.EqualTolerance(MicrostokesInOneSquareMeterPerSecond, (double)microstokesQuantity.Value, MicrostokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Microstokes, microstokesQuantity.Unit);
+ var inBaseUnits = KinematicViscosity.From(1.0, KinematicViscosity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var millistokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Millistokes);
- AssertEx.EqualTolerance(MillistokesInOneSquareMeterPerSecond, (double)millistokesQuantity.Value, MillistokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Millistokes, millistokesQuantity.Unit);
-
- var nanostokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Nanostokes);
- AssertEx.EqualTolerance(NanostokesInOneSquareMeterPerSecond, (double)nanostokesQuantity.Value, NanostokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Nanostokes, nanostokesQuantity.Unit);
-
- var squarefootpersecondQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.SquareFootPerSecond);
- AssertEx.EqualTolerance(SquareFeetPerSecondInOneSquareMeterPerSecond, (double)squarefootpersecondQuantity.Value, SquareFeetPerSecondTolerance);
- Assert.Equal(KinematicViscosityUnit.SquareFootPerSecond, squarefootpersecondQuantity.Unit);
-
- var squaremeterpersecondQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.SquareMeterPerSecond);
- AssertEx.EqualTolerance(SquareMetersPerSecondInOneSquareMeterPerSecond, (double)squaremeterpersecondQuantity.Value, SquareMetersPerSecondTolerance);
- Assert.Equal(KinematicViscosityUnit.SquareMeterPerSecond, squaremeterpersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var stokesQuantity = squaremeterpersecond.ToUnit(KinematicViscosityUnit.Stokes);
- AssertEx.EqualTolerance(StokesInOneSquareMeterPerSecond, (double)stokesQuantity.Value, StokesTolerance);
- Assert.Equal(KinematicViscosityUnit.Stokes, stokesQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(KinematicViscosityUnit unit)
+ {
+ var quantity = KinematicViscosity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(KinematicViscosityUnit unit)
{
- var quantityInBaseUnit = KinematicViscosity.FromSquareMetersPerSecond(1).ToBaseUnit();
- Assert.Equal(KinematicViscosity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = KinematicViscosity.Units.FirstOrDefault(u => u != KinematicViscosity.BaseUnit && u != KinematicViscosityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == KinematicViscosityUnit.Undefined)
+ fromUnit = KinematicViscosity.BaseUnit;
+
+ var quantity = KinematicViscosity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LapseRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LapseRateTestsBase.g.cs
index 0012197a39..abd317b6b1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LapseRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LapseRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class LapseRateTestsBase : QuantityTestsBase
protected virtual double DegreesCelciusPerKilometerTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LapseRateUnit unit)
+ {
+ return unit switch
+ {
+ LapseRateUnit.DegreeCelsiusPerKilometer => (DegreesCelciusPerKilometerInOneDegreeCelsiusPerKilometer, DegreesCelciusPerKilometerTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LapseRateUnit.DegreeCelsiusPerKilometer },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LapseRateUnit unit)
{
- var degreecelsiusperkilometer = LapseRate.FromDegreesCelciusPerKilometer(1);
+ var inBaseUnits = LapseRate.From(1.0, LapseRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var degreecelsiusperkilometerQuantity = degreecelsiusperkilometer.ToUnit(LapseRateUnit.DegreeCelsiusPerKilometer);
- AssertEx.EqualTolerance(DegreesCelciusPerKilometerInOneDegreeCelsiusPerKilometer, (double)degreecelsiusperkilometerQuantity.Value, DegreesCelciusPerKilometerTolerance);
- Assert.Equal(LapseRateUnit.DegreeCelsiusPerKilometer, degreecelsiusperkilometerQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LapseRateUnit unit)
+ {
+ var quantity = LapseRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LapseRateUnit unit)
{
- var quantityInBaseUnit = LapseRate.FromDegreesCelciusPerKilometer(1).ToBaseUnit();
- Assert.Equal(LapseRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = LapseRate.Units.FirstOrDefault(u => u != LapseRate.BaseUnit && u != LapseRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LapseRateUnit.Undefined)
+ fromUnit = LapseRate.BaseUnit;
+
+ var quantity = LapseRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LengthTestsBase.g.cs
index 1cb7481f7c..4c7526e104 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LengthTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LengthTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -107,6 +108,84 @@ public abstract partial class LengthTestsBase : QuantityTestsBase
protected virtual double YardsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LengthUnit unit)
+ {
+ return unit switch
+ {
+ LengthUnit.AstronomicalUnit => (AstronomicalUnitsInOneMeter, AstronomicalUnitsTolerance),
+ LengthUnit.Centimeter => (CentimetersInOneMeter, CentimetersTolerance),
+ LengthUnit.Chain => (ChainsInOneMeter, ChainsTolerance),
+ LengthUnit.Decimeter => (DecimetersInOneMeter, DecimetersTolerance),
+ LengthUnit.DtpPica => (DtpPicasInOneMeter, DtpPicasTolerance),
+ LengthUnit.DtpPoint => (DtpPointsInOneMeter, DtpPointsTolerance),
+ LengthUnit.Fathom => (FathomsInOneMeter, FathomsTolerance),
+ LengthUnit.Foot => (FeetInOneMeter, FeetTolerance),
+ LengthUnit.Hand => (HandsInOneMeter, HandsTolerance),
+ LengthUnit.Hectometer => (HectometersInOneMeter, HectometersTolerance),
+ LengthUnit.Inch => (InchesInOneMeter, InchesTolerance),
+ LengthUnit.KilolightYear => (KilolightYearsInOneMeter, KilolightYearsTolerance),
+ LengthUnit.Kilometer => (KilometersInOneMeter, KilometersTolerance),
+ LengthUnit.Kiloparsec => (KiloparsecsInOneMeter, KiloparsecsTolerance),
+ LengthUnit.LightYear => (LightYearsInOneMeter, LightYearsTolerance),
+ LengthUnit.MegalightYear => (MegalightYearsInOneMeter, MegalightYearsTolerance),
+ LengthUnit.Megaparsec => (MegaparsecsInOneMeter, MegaparsecsTolerance),
+ LengthUnit.Meter => (MetersInOneMeter, MetersTolerance),
+ LengthUnit.Microinch => (MicroinchesInOneMeter, MicroinchesTolerance),
+ LengthUnit.Micrometer => (MicrometersInOneMeter, MicrometersTolerance),
+ LengthUnit.Mil => (MilsInOneMeter, MilsTolerance),
+ LengthUnit.Mile => (MilesInOneMeter, MilesTolerance),
+ LengthUnit.Millimeter => (MillimetersInOneMeter, MillimetersTolerance),
+ LengthUnit.Nanometer => (NanometersInOneMeter, NanometersTolerance),
+ LengthUnit.NauticalMile => (NauticalMilesInOneMeter, NauticalMilesTolerance),
+ LengthUnit.Parsec => (ParsecsInOneMeter, ParsecsTolerance),
+ LengthUnit.PrinterPica => (PrinterPicasInOneMeter, PrinterPicasTolerance),
+ LengthUnit.PrinterPoint => (PrinterPointsInOneMeter, PrinterPointsTolerance),
+ LengthUnit.Shackle => (ShacklesInOneMeter, ShacklesTolerance),
+ LengthUnit.SolarRadius => (SolarRadiusesInOneMeter, SolarRadiusesTolerance),
+ LengthUnit.Twip => (TwipsInOneMeter, TwipsTolerance),
+ LengthUnit.UsSurveyFoot => (UsSurveyFeetInOneMeter, UsSurveyFeetTolerance),
+ LengthUnit.Yard => (YardsInOneMeter, YardsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LengthUnit.AstronomicalUnit },
+ new object[] { LengthUnit.Centimeter },
+ new object[] { LengthUnit.Chain },
+ new object[] { LengthUnit.Decimeter },
+ new object[] { LengthUnit.DtpPica },
+ new object[] { LengthUnit.DtpPoint },
+ new object[] { LengthUnit.Fathom },
+ new object[] { LengthUnit.Foot },
+ new object[] { LengthUnit.Hand },
+ new object[] { LengthUnit.Hectometer },
+ new object[] { LengthUnit.Inch },
+ new object[] { LengthUnit.KilolightYear },
+ new object[] { LengthUnit.Kilometer },
+ new object[] { LengthUnit.Kiloparsec },
+ new object[] { LengthUnit.LightYear },
+ new object[] { LengthUnit.MegalightYear },
+ new object[] { LengthUnit.Megaparsec },
+ new object[] { LengthUnit.Meter },
+ new object[] { LengthUnit.Microinch },
+ new object[] { LengthUnit.Micrometer },
+ new object[] { LengthUnit.Mil },
+ new object[] { LengthUnit.Mile },
+ new object[] { LengthUnit.Millimeter },
+ new object[] { LengthUnit.Nanometer },
+ new object[] { LengthUnit.NauticalMile },
+ new object[] { LengthUnit.Parsec },
+ new object[] { LengthUnit.PrinterPica },
+ new object[] { LengthUnit.PrinterPoint },
+ new object[] { LengthUnit.Shackle },
+ new object[] { LengthUnit.SolarRadius },
+ new object[] { LengthUnit.Twip },
+ new object[] { LengthUnit.UsSurveyFoot },
+ new object[] { LengthUnit.Yard },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -420,149 +499,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LengthUnit unit)
{
- var meter = Length.FromMeters(1);
-
- var astronomicalunitQuantity = meter.ToUnit(LengthUnit.AstronomicalUnit);
- AssertEx.EqualTolerance(AstronomicalUnitsInOneMeter, (double)astronomicalunitQuantity.Value, AstronomicalUnitsTolerance);
- Assert.Equal(LengthUnit.AstronomicalUnit, astronomicalunitQuantity.Unit);
-
- var centimeterQuantity = meter.ToUnit(LengthUnit.Centimeter);
- AssertEx.EqualTolerance(CentimetersInOneMeter, (double)centimeterQuantity.Value, CentimetersTolerance);
- Assert.Equal(LengthUnit.Centimeter, centimeterQuantity.Unit);
-
- var chainQuantity = meter.ToUnit(LengthUnit.Chain);
- AssertEx.EqualTolerance(ChainsInOneMeter, (double)chainQuantity.Value, ChainsTolerance);
- Assert.Equal(LengthUnit.Chain, chainQuantity.Unit);
-
- var decimeterQuantity = meter.ToUnit(LengthUnit.Decimeter);
- AssertEx.EqualTolerance(DecimetersInOneMeter, (double)decimeterQuantity.Value, DecimetersTolerance);
- Assert.Equal(LengthUnit.Decimeter, decimeterQuantity.Unit);
-
- var dtppicaQuantity = meter.ToUnit(LengthUnit.DtpPica);
- AssertEx.EqualTolerance(DtpPicasInOneMeter, (double)dtppicaQuantity.Value, DtpPicasTolerance);
- Assert.Equal(LengthUnit.DtpPica, dtppicaQuantity.Unit);
-
- var dtppointQuantity = meter.ToUnit(LengthUnit.DtpPoint);
- AssertEx.EqualTolerance(DtpPointsInOneMeter, (double)dtppointQuantity.Value, DtpPointsTolerance);
- Assert.Equal(LengthUnit.DtpPoint, dtppointQuantity.Unit);
-
- var fathomQuantity = meter.ToUnit(LengthUnit.Fathom);
- AssertEx.EqualTolerance(FathomsInOneMeter, (double)fathomQuantity.Value, FathomsTolerance);
- Assert.Equal(LengthUnit.Fathom, fathomQuantity.Unit);
-
- var footQuantity = meter.ToUnit(LengthUnit.Foot);
- AssertEx.EqualTolerance(FeetInOneMeter, (double)footQuantity.Value, FeetTolerance);
- Assert.Equal(LengthUnit.Foot, footQuantity.Unit);
-
- var handQuantity = meter.ToUnit(LengthUnit.Hand);
- AssertEx.EqualTolerance(HandsInOneMeter, (double)handQuantity.Value, HandsTolerance);
- Assert.Equal(LengthUnit.Hand, handQuantity.Unit);
-
- var hectometerQuantity = meter.ToUnit(LengthUnit.Hectometer);
- AssertEx.EqualTolerance(HectometersInOneMeter, (double)hectometerQuantity.Value, HectometersTolerance);
- Assert.Equal(LengthUnit.Hectometer, hectometerQuantity.Unit);
-
- var inchQuantity = meter.ToUnit(LengthUnit.Inch);
- AssertEx.EqualTolerance(InchesInOneMeter, (double)inchQuantity.Value, InchesTolerance);
- Assert.Equal(LengthUnit.Inch, inchQuantity.Unit);
-
- var kilolightyearQuantity = meter.ToUnit(LengthUnit.KilolightYear);
- AssertEx.EqualTolerance(KilolightYearsInOneMeter, (double)kilolightyearQuantity.Value, KilolightYearsTolerance);
- Assert.Equal(LengthUnit.KilolightYear, kilolightyearQuantity.Unit);
-
- var kilometerQuantity = meter.ToUnit(LengthUnit.Kilometer);
- AssertEx.EqualTolerance(KilometersInOneMeter, (double)kilometerQuantity.Value, KilometersTolerance);
- Assert.Equal(LengthUnit.Kilometer, kilometerQuantity.Unit);
-
- var kiloparsecQuantity = meter.ToUnit(LengthUnit.Kiloparsec);
- AssertEx.EqualTolerance(KiloparsecsInOneMeter, (double)kiloparsecQuantity.Value, KiloparsecsTolerance);
- Assert.Equal(LengthUnit.Kiloparsec, kiloparsecQuantity.Unit);
-
- var lightyearQuantity = meter.ToUnit(LengthUnit.LightYear);
- AssertEx.EqualTolerance(LightYearsInOneMeter, (double)lightyearQuantity.Value, LightYearsTolerance);
- Assert.Equal(LengthUnit.LightYear, lightyearQuantity.Unit);
+ var inBaseUnits = Length.From(1.0, Length.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megalightyearQuantity = meter.ToUnit(LengthUnit.MegalightYear);
- AssertEx.EqualTolerance(MegalightYearsInOneMeter, (double)megalightyearQuantity.Value, MegalightYearsTolerance);
- Assert.Equal(LengthUnit.MegalightYear, megalightyearQuantity.Unit);
-
- var megaparsecQuantity = meter.ToUnit(LengthUnit.Megaparsec);
- AssertEx.EqualTolerance(MegaparsecsInOneMeter, (double)megaparsecQuantity.Value, MegaparsecsTolerance);
- Assert.Equal(LengthUnit.Megaparsec, megaparsecQuantity.Unit);
-
- var meterQuantity = meter.ToUnit(LengthUnit.Meter);
- AssertEx.EqualTolerance(MetersInOneMeter, (double)meterQuantity.Value, MetersTolerance);
- Assert.Equal(LengthUnit.Meter, meterQuantity.Unit);
-
- var microinchQuantity = meter.ToUnit(LengthUnit.Microinch);
- AssertEx.EqualTolerance(MicroinchesInOneMeter, (double)microinchQuantity.Value, MicroinchesTolerance);
- Assert.Equal(LengthUnit.Microinch, microinchQuantity.Unit);
-
- var micrometerQuantity = meter.ToUnit(LengthUnit.Micrometer);
- AssertEx.EqualTolerance(MicrometersInOneMeter, (double)micrometerQuantity.Value, MicrometersTolerance);
- Assert.Equal(LengthUnit.Micrometer, micrometerQuantity.Unit);
-
- var milQuantity = meter.ToUnit(LengthUnit.Mil);
- AssertEx.EqualTolerance(MilsInOneMeter, (double)milQuantity.Value, MilsTolerance);
- Assert.Equal(LengthUnit.Mil, milQuantity.Unit);
-
- var mileQuantity = meter.ToUnit(LengthUnit.Mile);
- AssertEx.EqualTolerance(MilesInOneMeter, (double)mileQuantity.Value, MilesTolerance);
- Assert.Equal(LengthUnit.Mile, mileQuantity.Unit);
-
- var millimeterQuantity = meter.ToUnit(LengthUnit.Millimeter);
- AssertEx.EqualTolerance(MillimetersInOneMeter, (double)millimeterQuantity.Value, MillimetersTolerance);
- Assert.Equal(LengthUnit.Millimeter, millimeterQuantity.Unit);
-
- var nanometerQuantity = meter.ToUnit(LengthUnit.Nanometer);
- AssertEx.EqualTolerance(NanometersInOneMeter, (double)nanometerQuantity.Value, NanometersTolerance);
- Assert.Equal(LengthUnit.Nanometer, nanometerQuantity.Unit);
-
- var nauticalmileQuantity = meter.ToUnit(LengthUnit.NauticalMile);
- AssertEx.EqualTolerance(NauticalMilesInOneMeter, (double)nauticalmileQuantity.Value, NauticalMilesTolerance);
- Assert.Equal(LengthUnit.NauticalMile, nauticalmileQuantity.Unit);
-
- var parsecQuantity = meter.ToUnit(LengthUnit.Parsec);
- AssertEx.EqualTolerance(ParsecsInOneMeter, (double)parsecQuantity.Value, ParsecsTolerance);
- Assert.Equal(LengthUnit.Parsec, parsecQuantity.Unit);
-
- var printerpicaQuantity = meter.ToUnit(LengthUnit.PrinterPica);
- AssertEx.EqualTolerance(PrinterPicasInOneMeter, (double)printerpicaQuantity.Value, PrinterPicasTolerance);
- Assert.Equal(LengthUnit.PrinterPica, printerpicaQuantity.Unit);
-
- var printerpointQuantity = meter.ToUnit(LengthUnit.PrinterPoint);
- AssertEx.EqualTolerance(PrinterPointsInOneMeter, (double)printerpointQuantity.Value, PrinterPointsTolerance);
- Assert.Equal(LengthUnit.PrinterPoint, printerpointQuantity.Unit);
-
- var shackleQuantity = meter.ToUnit(LengthUnit.Shackle);
- AssertEx.EqualTolerance(ShacklesInOneMeter, (double)shackleQuantity.Value, ShacklesTolerance);
- Assert.Equal(LengthUnit.Shackle, shackleQuantity.Unit);
-
- var solarradiusQuantity = meter.ToUnit(LengthUnit.SolarRadius);
- AssertEx.EqualTolerance(SolarRadiusesInOneMeter, (double)solarradiusQuantity.Value, SolarRadiusesTolerance);
- Assert.Equal(LengthUnit.SolarRadius, solarradiusQuantity.Unit);
-
- var twipQuantity = meter.ToUnit(LengthUnit.Twip);
- AssertEx.EqualTolerance(TwipsInOneMeter, (double)twipQuantity.Value, TwipsTolerance);
- Assert.Equal(LengthUnit.Twip, twipQuantity.Unit);
-
- var ussurveyfootQuantity = meter.ToUnit(LengthUnit.UsSurveyFoot);
- AssertEx.EqualTolerance(UsSurveyFeetInOneMeter, (double)ussurveyfootQuantity.Value, UsSurveyFeetTolerance);
- Assert.Equal(LengthUnit.UsSurveyFoot, ussurveyfootQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var yardQuantity = meter.ToUnit(LengthUnit.Yard);
- AssertEx.EqualTolerance(YardsInOneMeter, (double)yardQuantity.Value, YardsTolerance);
- Assert.Equal(LengthUnit.Yard, yardQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LengthUnit unit)
+ {
+ var quantity = Length.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LengthUnit unit)
{
- var quantityInBaseUnit = Length.FromMeters(1).ToBaseUnit();
- Assert.Equal(Length.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Length.Units.FirstOrDefault(u => u != Length.BaseUnit && u != LengthUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LengthUnit.Undefined)
+ fromUnit = Length.BaseUnit;
+
+ var quantity = Length.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LevelTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LevelTestsBase.g.cs
index 3cc0e5f9bb..27aca88bbf 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LevelTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LevelTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -45,6 +46,22 @@ public abstract partial class LevelTestsBase : QuantityTestsBase
protected virtual double NepersTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LevelUnit unit)
+ {
+ return unit switch
+ {
+ LevelUnit.Decibel => (DecibelsInOneDecibel, DecibelsTolerance),
+ LevelUnit.Neper => (NepersInOneDecibel, NepersTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LevelUnit.Decibel },
+ new object[] { LevelUnit.Neper },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -172,25 +189,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LevelUnit unit)
{
- var decibel = Level.FromDecibels(1);
+ var inBaseUnits = Level.From(1.0, Level.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var decibelQuantity = decibel.ToUnit(LevelUnit.Decibel);
- AssertEx.EqualTolerance(DecibelsInOneDecibel, (double)decibelQuantity.Value, DecibelsTolerance);
- Assert.Equal(LevelUnit.Decibel, decibelQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var neperQuantity = decibel.ToUnit(LevelUnit.Neper);
- AssertEx.EqualTolerance(NepersInOneDecibel, (double)neperQuantity.Value, NepersTolerance);
- Assert.Equal(LevelUnit.Neper, neperQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LevelUnit unit)
+ {
+ var quantity = Level.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LevelUnit unit)
{
- var quantityInBaseUnit = Level.FromDecibels(1).ToBaseUnit();
- Assert.Equal(Level.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Level.Units.FirstOrDefault(u => u != Level.BaseUnit && u != LevelUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LevelUnit.Undefined)
+ fromUnit = Level.BaseUnit;
+
+ var quantity = Level.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LinearDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LinearDensityTestsBase.g.cs
index 55b42a0dcd..94e1aab652 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LinearDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LinearDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class LinearDensityTestsBase : QuantityTestsBase
protected virtual double PoundsPerInchTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LinearDensityUnit unit)
+ {
+ return unit switch
+ {
+ LinearDensityUnit.GramPerCentimeter => (GramsPerCentimeterInOneKilogramPerMeter, GramsPerCentimeterTolerance),
+ LinearDensityUnit.GramPerMeter => (GramsPerMeterInOneKilogramPerMeter, GramsPerMeterTolerance),
+ LinearDensityUnit.GramPerMillimeter => (GramsPerMillimeterInOneKilogramPerMeter, GramsPerMillimeterTolerance),
+ LinearDensityUnit.KilogramPerCentimeter => (KilogramsPerCentimeterInOneKilogramPerMeter, KilogramsPerCentimeterTolerance),
+ LinearDensityUnit.KilogramPerMeter => (KilogramsPerMeterInOneKilogramPerMeter, KilogramsPerMeterTolerance),
+ LinearDensityUnit.KilogramPerMillimeter => (KilogramsPerMillimeterInOneKilogramPerMeter, KilogramsPerMillimeterTolerance),
+ LinearDensityUnit.MicrogramPerCentimeter => (MicrogramsPerCentimeterInOneKilogramPerMeter, MicrogramsPerCentimeterTolerance),
+ LinearDensityUnit.MicrogramPerMeter => (MicrogramsPerMeterInOneKilogramPerMeter, MicrogramsPerMeterTolerance),
+ LinearDensityUnit.MicrogramPerMillimeter => (MicrogramsPerMillimeterInOneKilogramPerMeter, MicrogramsPerMillimeterTolerance),
+ LinearDensityUnit.MilligramPerCentimeter => (MilligramsPerCentimeterInOneKilogramPerMeter, MilligramsPerCentimeterTolerance),
+ LinearDensityUnit.MilligramPerMeter => (MilligramsPerMeterInOneKilogramPerMeter, MilligramsPerMeterTolerance),
+ LinearDensityUnit.MilligramPerMillimeter => (MilligramsPerMillimeterInOneKilogramPerMeter, MilligramsPerMillimeterTolerance),
+ LinearDensityUnit.PoundPerFoot => (PoundsPerFootInOneKilogramPerMeter, PoundsPerFootTolerance),
+ LinearDensityUnit.PoundPerInch => (PoundsPerInchInOneKilogramPerMeter, PoundsPerInchTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LinearDensityUnit.GramPerCentimeter },
+ new object[] { LinearDensityUnit.GramPerMeter },
+ new object[] { LinearDensityUnit.GramPerMillimeter },
+ new object[] { LinearDensityUnit.KilogramPerCentimeter },
+ new object[] { LinearDensityUnit.KilogramPerMeter },
+ new object[] { LinearDensityUnit.KilogramPerMillimeter },
+ new object[] { LinearDensityUnit.MicrogramPerCentimeter },
+ new object[] { LinearDensityUnit.MicrogramPerMeter },
+ new object[] { LinearDensityUnit.MicrogramPerMillimeter },
+ new object[] { LinearDensityUnit.MilligramPerCentimeter },
+ new object[] { LinearDensityUnit.MilligramPerMeter },
+ new object[] { LinearDensityUnit.MilligramPerMillimeter },
+ new object[] { LinearDensityUnit.PoundPerFoot },
+ new object[] { LinearDensityUnit.PoundPerInch },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LinearDensityUnit unit)
{
- var kilogrampermeter = LinearDensity.FromKilogramsPerMeter(1);
-
- var grampercentimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.GramPerCentimeter);
- AssertEx.EqualTolerance(GramsPerCentimeterInOneKilogramPerMeter, (double)grampercentimeterQuantity.Value, GramsPerCentimeterTolerance);
- Assert.Equal(LinearDensityUnit.GramPerCentimeter, grampercentimeterQuantity.Unit);
-
- var grampermeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.GramPerMeter);
- AssertEx.EqualTolerance(GramsPerMeterInOneKilogramPerMeter, (double)grampermeterQuantity.Value, GramsPerMeterTolerance);
- Assert.Equal(LinearDensityUnit.GramPerMeter, grampermeterQuantity.Unit);
-
- var grampermillimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.GramPerMillimeter);
- AssertEx.EqualTolerance(GramsPerMillimeterInOneKilogramPerMeter, (double)grampermillimeterQuantity.Value, GramsPerMillimeterTolerance);
- Assert.Equal(LinearDensityUnit.GramPerMillimeter, grampermillimeterQuantity.Unit);
-
- var kilogrampercentimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.KilogramPerCentimeter);
- AssertEx.EqualTolerance(KilogramsPerCentimeterInOneKilogramPerMeter, (double)kilogrampercentimeterQuantity.Value, KilogramsPerCentimeterTolerance);
- Assert.Equal(LinearDensityUnit.KilogramPerCentimeter, kilogrampercentimeterQuantity.Unit);
-
- var kilogrampermeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.KilogramPerMeter);
- AssertEx.EqualTolerance(KilogramsPerMeterInOneKilogramPerMeter, (double)kilogrampermeterQuantity.Value, KilogramsPerMeterTolerance);
- Assert.Equal(LinearDensityUnit.KilogramPerMeter, kilogrampermeterQuantity.Unit);
-
- var kilogrampermillimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.KilogramPerMillimeter);
- AssertEx.EqualTolerance(KilogramsPerMillimeterInOneKilogramPerMeter, (double)kilogrampermillimeterQuantity.Value, KilogramsPerMillimeterTolerance);
- Assert.Equal(LinearDensityUnit.KilogramPerMillimeter, kilogrampermillimeterQuantity.Unit);
-
- var microgrampercentimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.MicrogramPerCentimeter);
- AssertEx.EqualTolerance(MicrogramsPerCentimeterInOneKilogramPerMeter, (double)microgrampercentimeterQuantity.Value, MicrogramsPerCentimeterTolerance);
- Assert.Equal(LinearDensityUnit.MicrogramPerCentimeter, microgrampercentimeterQuantity.Unit);
+ var inBaseUnits = LinearDensity.From(1.0, LinearDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microgrampermeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.MicrogramPerMeter);
- AssertEx.EqualTolerance(MicrogramsPerMeterInOneKilogramPerMeter, (double)microgrampermeterQuantity.Value, MicrogramsPerMeterTolerance);
- Assert.Equal(LinearDensityUnit.MicrogramPerMeter, microgrampermeterQuantity.Unit);
-
- var microgrampermillimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.MicrogramPerMillimeter);
- AssertEx.EqualTolerance(MicrogramsPerMillimeterInOneKilogramPerMeter, (double)microgrampermillimeterQuantity.Value, MicrogramsPerMillimeterTolerance);
- Assert.Equal(LinearDensityUnit.MicrogramPerMillimeter, microgrampermillimeterQuantity.Unit);
-
- var milligrampercentimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.MilligramPerCentimeter);
- AssertEx.EqualTolerance(MilligramsPerCentimeterInOneKilogramPerMeter, (double)milligrampercentimeterQuantity.Value, MilligramsPerCentimeterTolerance);
- Assert.Equal(LinearDensityUnit.MilligramPerCentimeter, milligrampercentimeterQuantity.Unit);
-
- var milligrampermeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.MilligramPerMeter);
- AssertEx.EqualTolerance(MilligramsPerMeterInOneKilogramPerMeter, (double)milligrampermeterQuantity.Value, MilligramsPerMeterTolerance);
- Assert.Equal(LinearDensityUnit.MilligramPerMeter, milligrampermeterQuantity.Unit);
-
- var milligrampermillimeterQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.MilligramPerMillimeter);
- AssertEx.EqualTolerance(MilligramsPerMillimeterInOneKilogramPerMeter, (double)milligrampermillimeterQuantity.Value, MilligramsPerMillimeterTolerance);
- Assert.Equal(LinearDensityUnit.MilligramPerMillimeter, milligrampermillimeterQuantity.Unit);
-
- var poundperfootQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.PoundPerFoot);
- AssertEx.EqualTolerance(PoundsPerFootInOneKilogramPerMeter, (double)poundperfootQuantity.Value, PoundsPerFootTolerance);
- Assert.Equal(LinearDensityUnit.PoundPerFoot, poundperfootQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundperinchQuantity = kilogrampermeter.ToUnit(LinearDensityUnit.PoundPerInch);
- AssertEx.EqualTolerance(PoundsPerInchInOneKilogramPerMeter, (double)poundperinchQuantity.Value, PoundsPerInchTolerance);
- Assert.Equal(LinearDensityUnit.PoundPerInch, poundperinchQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LinearDensityUnit unit)
+ {
+ var quantity = LinearDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LinearDensityUnit unit)
{
- var quantityInBaseUnit = LinearDensity.FromKilogramsPerMeter(1).ToBaseUnit();
- Assert.Equal(LinearDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = LinearDensity.Units.FirstOrDefault(u => u != LinearDensity.BaseUnit && u != LinearDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LinearDensityUnit.Undefined)
+ fromUnit = LinearDensity.BaseUnit;
+
+ var quantity = LinearDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LinearPowerDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LinearPowerDensityTestsBase.g.cs
index d6b7623329..9ced6699e2 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LinearPowerDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LinearPowerDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -91,6 +92,68 @@ public abstract partial class LinearPowerDensityTestsBase : QuantityTestsBase
protected virtual double WattsPerMillimeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LinearPowerDensityUnit unit)
+ {
+ return unit switch
+ {
+ LinearPowerDensityUnit.GigawattPerCentimeter => (GigawattsPerCentimeterInOneWattPerMeter, GigawattsPerCentimeterTolerance),
+ LinearPowerDensityUnit.GigawattPerFoot => (GigawattsPerFootInOneWattPerMeter, GigawattsPerFootTolerance),
+ LinearPowerDensityUnit.GigawattPerInch => (GigawattsPerInchInOneWattPerMeter, GigawattsPerInchTolerance),
+ LinearPowerDensityUnit.GigawattPerMeter => (GigawattsPerMeterInOneWattPerMeter, GigawattsPerMeterTolerance),
+ LinearPowerDensityUnit.GigawattPerMillimeter => (GigawattsPerMillimeterInOneWattPerMeter, GigawattsPerMillimeterTolerance),
+ LinearPowerDensityUnit.KilowattPerCentimeter => (KilowattsPerCentimeterInOneWattPerMeter, KilowattsPerCentimeterTolerance),
+ LinearPowerDensityUnit.KilowattPerFoot => (KilowattsPerFootInOneWattPerMeter, KilowattsPerFootTolerance),
+ LinearPowerDensityUnit.KilowattPerInch => (KilowattsPerInchInOneWattPerMeter, KilowattsPerInchTolerance),
+ LinearPowerDensityUnit.KilowattPerMeter => (KilowattsPerMeterInOneWattPerMeter, KilowattsPerMeterTolerance),
+ LinearPowerDensityUnit.KilowattPerMillimeter => (KilowattsPerMillimeterInOneWattPerMeter, KilowattsPerMillimeterTolerance),
+ LinearPowerDensityUnit.MegawattPerCentimeter => (MegawattsPerCentimeterInOneWattPerMeter, MegawattsPerCentimeterTolerance),
+ LinearPowerDensityUnit.MegawattPerFoot => (MegawattsPerFootInOneWattPerMeter, MegawattsPerFootTolerance),
+ LinearPowerDensityUnit.MegawattPerInch => (MegawattsPerInchInOneWattPerMeter, MegawattsPerInchTolerance),
+ LinearPowerDensityUnit.MegawattPerMeter => (MegawattsPerMeterInOneWattPerMeter, MegawattsPerMeterTolerance),
+ LinearPowerDensityUnit.MegawattPerMillimeter => (MegawattsPerMillimeterInOneWattPerMeter, MegawattsPerMillimeterTolerance),
+ LinearPowerDensityUnit.MilliwattPerCentimeter => (MilliwattsPerCentimeterInOneWattPerMeter, MilliwattsPerCentimeterTolerance),
+ LinearPowerDensityUnit.MilliwattPerFoot => (MilliwattsPerFootInOneWattPerMeter, MilliwattsPerFootTolerance),
+ LinearPowerDensityUnit.MilliwattPerInch => (MilliwattsPerInchInOneWattPerMeter, MilliwattsPerInchTolerance),
+ LinearPowerDensityUnit.MilliwattPerMeter => (MilliwattsPerMeterInOneWattPerMeter, MilliwattsPerMeterTolerance),
+ LinearPowerDensityUnit.MilliwattPerMillimeter => (MilliwattsPerMillimeterInOneWattPerMeter, MilliwattsPerMillimeterTolerance),
+ LinearPowerDensityUnit.WattPerCentimeter => (WattsPerCentimeterInOneWattPerMeter, WattsPerCentimeterTolerance),
+ LinearPowerDensityUnit.WattPerFoot => (WattsPerFootInOneWattPerMeter, WattsPerFootTolerance),
+ LinearPowerDensityUnit.WattPerInch => (WattsPerInchInOneWattPerMeter, WattsPerInchTolerance),
+ LinearPowerDensityUnit.WattPerMeter => (WattsPerMeterInOneWattPerMeter, WattsPerMeterTolerance),
+ LinearPowerDensityUnit.WattPerMillimeter => (WattsPerMillimeterInOneWattPerMeter, WattsPerMillimeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LinearPowerDensityUnit.GigawattPerCentimeter },
+ new object[] { LinearPowerDensityUnit.GigawattPerFoot },
+ new object[] { LinearPowerDensityUnit.GigawattPerInch },
+ new object[] { LinearPowerDensityUnit.GigawattPerMeter },
+ new object[] { LinearPowerDensityUnit.GigawattPerMillimeter },
+ new object[] { LinearPowerDensityUnit.KilowattPerCentimeter },
+ new object[] { LinearPowerDensityUnit.KilowattPerFoot },
+ new object[] { LinearPowerDensityUnit.KilowattPerInch },
+ new object[] { LinearPowerDensityUnit.KilowattPerMeter },
+ new object[] { LinearPowerDensityUnit.KilowattPerMillimeter },
+ new object[] { LinearPowerDensityUnit.MegawattPerCentimeter },
+ new object[] { LinearPowerDensityUnit.MegawattPerFoot },
+ new object[] { LinearPowerDensityUnit.MegawattPerInch },
+ new object[] { LinearPowerDensityUnit.MegawattPerMeter },
+ new object[] { LinearPowerDensityUnit.MegawattPerMillimeter },
+ new object[] { LinearPowerDensityUnit.MilliwattPerCentimeter },
+ new object[] { LinearPowerDensityUnit.MilliwattPerFoot },
+ new object[] { LinearPowerDensityUnit.MilliwattPerInch },
+ new object[] { LinearPowerDensityUnit.MilliwattPerMeter },
+ new object[] { LinearPowerDensityUnit.MilliwattPerMillimeter },
+ new object[] { LinearPowerDensityUnit.WattPerCentimeter },
+ new object[] { LinearPowerDensityUnit.WattPerFoot },
+ new object[] { LinearPowerDensityUnit.WattPerInch },
+ new object[] { LinearPowerDensityUnit.WattPerMeter },
+ new object[] { LinearPowerDensityUnit.WattPerMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -356,117 +419,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LinearPowerDensityUnit unit)
{
- var wattpermeter = LinearPowerDensity.FromWattsPerMeter(1);
-
- var gigawattpercentimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.GigawattPerCentimeter);
- AssertEx.EqualTolerance(GigawattsPerCentimeterInOneWattPerMeter, (double)gigawattpercentimeterQuantity.Value, GigawattsPerCentimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.GigawattPerCentimeter, gigawattpercentimeterQuantity.Unit);
-
- var gigawattperfootQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.GigawattPerFoot);
- AssertEx.EqualTolerance(GigawattsPerFootInOneWattPerMeter, (double)gigawattperfootQuantity.Value, GigawattsPerFootTolerance);
- Assert.Equal(LinearPowerDensityUnit.GigawattPerFoot, gigawattperfootQuantity.Unit);
-
- var gigawattperinchQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.GigawattPerInch);
- AssertEx.EqualTolerance(GigawattsPerInchInOneWattPerMeter, (double)gigawattperinchQuantity.Value, GigawattsPerInchTolerance);
- Assert.Equal(LinearPowerDensityUnit.GigawattPerInch, gigawattperinchQuantity.Unit);
-
- var gigawattpermeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.GigawattPerMeter);
- AssertEx.EqualTolerance(GigawattsPerMeterInOneWattPerMeter, (double)gigawattpermeterQuantity.Value, GigawattsPerMeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.GigawattPerMeter, gigawattpermeterQuantity.Unit);
-
- var gigawattpermillimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.GigawattPerMillimeter);
- AssertEx.EqualTolerance(GigawattsPerMillimeterInOneWattPerMeter, (double)gigawattpermillimeterQuantity.Value, GigawattsPerMillimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.GigawattPerMillimeter, gigawattpermillimeterQuantity.Unit);
-
- var kilowattpercentimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.KilowattPerCentimeter);
- AssertEx.EqualTolerance(KilowattsPerCentimeterInOneWattPerMeter, (double)kilowattpercentimeterQuantity.Value, KilowattsPerCentimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.KilowattPerCentimeter, kilowattpercentimeterQuantity.Unit);
-
- var kilowattperfootQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.KilowattPerFoot);
- AssertEx.EqualTolerance(KilowattsPerFootInOneWattPerMeter, (double)kilowattperfootQuantity.Value, KilowattsPerFootTolerance);
- Assert.Equal(LinearPowerDensityUnit.KilowattPerFoot, kilowattperfootQuantity.Unit);
-
- var kilowattperinchQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.KilowattPerInch);
- AssertEx.EqualTolerance(KilowattsPerInchInOneWattPerMeter, (double)kilowattperinchQuantity.Value, KilowattsPerInchTolerance);
- Assert.Equal(LinearPowerDensityUnit.KilowattPerInch, kilowattperinchQuantity.Unit);
-
- var kilowattpermeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.KilowattPerMeter);
- AssertEx.EqualTolerance(KilowattsPerMeterInOneWattPerMeter, (double)kilowattpermeterQuantity.Value, KilowattsPerMeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.KilowattPerMeter, kilowattpermeterQuantity.Unit);
-
- var kilowattpermillimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.KilowattPerMillimeter);
- AssertEx.EqualTolerance(KilowattsPerMillimeterInOneWattPerMeter, (double)kilowattpermillimeterQuantity.Value, KilowattsPerMillimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.KilowattPerMillimeter, kilowattpermillimeterQuantity.Unit);
-
- var megawattpercentimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MegawattPerCentimeter);
- AssertEx.EqualTolerance(MegawattsPerCentimeterInOneWattPerMeter, (double)megawattpercentimeterQuantity.Value, MegawattsPerCentimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.MegawattPerCentimeter, megawattpercentimeterQuantity.Unit);
-
- var megawattperfootQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MegawattPerFoot);
- AssertEx.EqualTolerance(MegawattsPerFootInOneWattPerMeter, (double)megawattperfootQuantity.Value, MegawattsPerFootTolerance);
- Assert.Equal(LinearPowerDensityUnit.MegawattPerFoot, megawattperfootQuantity.Unit);
+ var inBaseUnits = LinearPowerDensity.From(1.0, LinearPowerDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megawattperinchQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MegawattPerInch);
- AssertEx.EqualTolerance(MegawattsPerInchInOneWattPerMeter, (double)megawattperinchQuantity.Value, MegawattsPerInchTolerance);
- Assert.Equal(LinearPowerDensityUnit.MegawattPerInch, megawattperinchQuantity.Unit);
-
- var megawattpermeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MegawattPerMeter);
- AssertEx.EqualTolerance(MegawattsPerMeterInOneWattPerMeter, (double)megawattpermeterQuantity.Value, MegawattsPerMeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.MegawattPerMeter, megawattpermeterQuantity.Unit);
-
- var megawattpermillimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MegawattPerMillimeter);
- AssertEx.EqualTolerance(MegawattsPerMillimeterInOneWattPerMeter, (double)megawattpermillimeterQuantity.Value, MegawattsPerMillimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.MegawattPerMillimeter, megawattpermillimeterQuantity.Unit);
-
- var milliwattpercentimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MilliwattPerCentimeter);
- AssertEx.EqualTolerance(MilliwattsPerCentimeterInOneWattPerMeter, (double)milliwattpercentimeterQuantity.Value, MilliwattsPerCentimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.MilliwattPerCentimeter, milliwattpercentimeterQuantity.Unit);
-
- var milliwattperfootQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MilliwattPerFoot);
- AssertEx.EqualTolerance(MilliwattsPerFootInOneWattPerMeter, (double)milliwattperfootQuantity.Value, MilliwattsPerFootTolerance);
- Assert.Equal(LinearPowerDensityUnit.MilliwattPerFoot, milliwattperfootQuantity.Unit);
-
- var milliwattperinchQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MilliwattPerInch);
- AssertEx.EqualTolerance(MilliwattsPerInchInOneWattPerMeter, (double)milliwattperinchQuantity.Value, MilliwattsPerInchTolerance);
- Assert.Equal(LinearPowerDensityUnit.MilliwattPerInch, milliwattperinchQuantity.Unit);
-
- var milliwattpermeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MilliwattPerMeter);
- AssertEx.EqualTolerance(MilliwattsPerMeterInOneWattPerMeter, (double)milliwattpermeterQuantity.Value, MilliwattsPerMeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.MilliwattPerMeter, milliwattpermeterQuantity.Unit);
-
- var milliwattpermillimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.MilliwattPerMillimeter);
- AssertEx.EqualTolerance(MilliwattsPerMillimeterInOneWattPerMeter, (double)milliwattpermillimeterQuantity.Value, MilliwattsPerMillimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.MilliwattPerMillimeter, milliwattpermillimeterQuantity.Unit);
-
- var wattpercentimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.WattPerCentimeter);
- AssertEx.EqualTolerance(WattsPerCentimeterInOneWattPerMeter, (double)wattpercentimeterQuantity.Value, WattsPerCentimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.WattPerCentimeter, wattpercentimeterQuantity.Unit);
-
- var wattperfootQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.WattPerFoot);
- AssertEx.EqualTolerance(WattsPerFootInOneWattPerMeter, (double)wattperfootQuantity.Value, WattsPerFootTolerance);
- Assert.Equal(LinearPowerDensityUnit.WattPerFoot, wattperfootQuantity.Unit);
-
- var wattperinchQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.WattPerInch);
- AssertEx.EqualTolerance(WattsPerInchInOneWattPerMeter, (double)wattperinchQuantity.Value, WattsPerInchTolerance);
- Assert.Equal(LinearPowerDensityUnit.WattPerInch, wattperinchQuantity.Unit);
-
- var wattpermeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.WattPerMeter);
- AssertEx.EqualTolerance(WattsPerMeterInOneWattPerMeter, (double)wattpermeterQuantity.Value, WattsPerMeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.WattPerMeter, wattpermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattpermillimeterQuantity = wattpermeter.ToUnit(LinearPowerDensityUnit.WattPerMillimeter);
- AssertEx.EqualTolerance(WattsPerMillimeterInOneWattPerMeter, (double)wattpermillimeterQuantity.Value, WattsPerMillimeterTolerance);
- Assert.Equal(LinearPowerDensityUnit.WattPerMillimeter, wattpermillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LinearPowerDensityUnit unit)
+ {
+ var quantity = LinearPowerDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LinearPowerDensityUnit unit)
{
- var quantityInBaseUnit = LinearPowerDensity.FromWattsPerMeter(1).ToBaseUnit();
- Assert.Equal(LinearPowerDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = LinearPowerDensity.Units.FirstOrDefault(u => u != LinearPowerDensity.BaseUnit && u != LinearPowerDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LinearPowerDensityUnit.Undefined)
+ fromUnit = LinearPowerDensity.BaseUnit;
+
+ var quantity = LinearPowerDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LuminosityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LuminosityTestsBase.g.cs
index ef5509b976..28c34e2b5c 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LuminosityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LuminosityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class LuminosityTestsBase : QuantityTestsBase
protected virtual double WattsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LuminosityUnit unit)
+ {
+ return unit switch
+ {
+ LuminosityUnit.Decawatt => (DecawattsInOneWatt, DecawattsTolerance),
+ LuminosityUnit.Deciwatt => (DeciwattsInOneWatt, DeciwattsTolerance),
+ LuminosityUnit.Femtowatt => (FemtowattsInOneWatt, FemtowattsTolerance),
+ LuminosityUnit.Gigawatt => (GigawattsInOneWatt, GigawattsTolerance),
+ LuminosityUnit.Kilowatt => (KilowattsInOneWatt, KilowattsTolerance),
+ LuminosityUnit.Megawatt => (MegawattsInOneWatt, MegawattsTolerance),
+ LuminosityUnit.Microwatt => (MicrowattsInOneWatt, MicrowattsTolerance),
+ LuminosityUnit.Milliwatt => (MilliwattsInOneWatt, MilliwattsTolerance),
+ LuminosityUnit.Nanowatt => (NanowattsInOneWatt, NanowattsTolerance),
+ LuminosityUnit.Petawatt => (PetawattsInOneWatt, PetawattsTolerance),
+ LuminosityUnit.Picowatt => (PicowattsInOneWatt, PicowattsTolerance),
+ LuminosityUnit.SolarLuminosity => (SolarLuminositiesInOneWatt, SolarLuminositiesTolerance),
+ LuminosityUnit.Terawatt => (TerawattsInOneWatt, TerawattsTolerance),
+ LuminosityUnit.Watt => (WattsInOneWatt, WattsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LuminosityUnit.Decawatt },
+ new object[] { LuminosityUnit.Deciwatt },
+ new object[] { LuminosityUnit.Femtowatt },
+ new object[] { LuminosityUnit.Gigawatt },
+ new object[] { LuminosityUnit.Kilowatt },
+ new object[] { LuminosityUnit.Megawatt },
+ new object[] { LuminosityUnit.Microwatt },
+ new object[] { LuminosityUnit.Milliwatt },
+ new object[] { LuminosityUnit.Nanowatt },
+ new object[] { LuminosityUnit.Petawatt },
+ new object[] { LuminosityUnit.Picowatt },
+ new object[] { LuminosityUnit.SolarLuminosity },
+ new object[] { LuminosityUnit.Terawatt },
+ new object[] { LuminosityUnit.Watt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LuminosityUnit unit)
{
- var watt = Luminosity.FromWatts(1);
-
- var decawattQuantity = watt.ToUnit(LuminosityUnit.Decawatt);
- AssertEx.EqualTolerance(DecawattsInOneWatt, (double)decawattQuantity.Value, DecawattsTolerance);
- Assert.Equal(LuminosityUnit.Decawatt, decawattQuantity.Unit);
-
- var deciwattQuantity = watt.ToUnit(LuminosityUnit.Deciwatt);
- AssertEx.EqualTolerance(DeciwattsInOneWatt, (double)deciwattQuantity.Value, DeciwattsTolerance);
- Assert.Equal(LuminosityUnit.Deciwatt, deciwattQuantity.Unit);
-
- var femtowattQuantity = watt.ToUnit(LuminosityUnit.Femtowatt);
- AssertEx.EqualTolerance(FemtowattsInOneWatt, (double)femtowattQuantity.Value, FemtowattsTolerance);
- Assert.Equal(LuminosityUnit.Femtowatt, femtowattQuantity.Unit);
-
- var gigawattQuantity = watt.ToUnit(LuminosityUnit.Gigawatt);
- AssertEx.EqualTolerance(GigawattsInOneWatt, (double)gigawattQuantity.Value, GigawattsTolerance);
- Assert.Equal(LuminosityUnit.Gigawatt, gigawattQuantity.Unit);
-
- var kilowattQuantity = watt.ToUnit(LuminosityUnit.Kilowatt);
- AssertEx.EqualTolerance(KilowattsInOneWatt, (double)kilowattQuantity.Value, KilowattsTolerance);
- Assert.Equal(LuminosityUnit.Kilowatt, kilowattQuantity.Unit);
-
- var megawattQuantity = watt.ToUnit(LuminosityUnit.Megawatt);
- AssertEx.EqualTolerance(MegawattsInOneWatt, (double)megawattQuantity.Value, MegawattsTolerance);
- Assert.Equal(LuminosityUnit.Megawatt, megawattQuantity.Unit);
-
- var microwattQuantity = watt.ToUnit(LuminosityUnit.Microwatt);
- AssertEx.EqualTolerance(MicrowattsInOneWatt, (double)microwattQuantity.Value, MicrowattsTolerance);
- Assert.Equal(LuminosityUnit.Microwatt, microwattQuantity.Unit);
+ var inBaseUnits = Luminosity.From(1.0, Luminosity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var milliwattQuantity = watt.ToUnit(LuminosityUnit.Milliwatt);
- AssertEx.EqualTolerance(MilliwattsInOneWatt, (double)milliwattQuantity.Value, MilliwattsTolerance);
- Assert.Equal(LuminosityUnit.Milliwatt, milliwattQuantity.Unit);
-
- var nanowattQuantity = watt.ToUnit(LuminosityUnit.Nanowatt);
- AssertEx.EqualTolerance(NanowattsInOneWatt, (double)nanowattQuantity.Value, NanowattsTolerance);
- Assert.Equal(LuminosityUnit.Nanowatt, nanowattQuantity.Unit);
-
- var petawattQuantity = watt.ToUnit(LuminosityUnit.Petawatt);
- AssertEx.EqualTolerance(PetawattsInOneWatt, (double)petawattQuantity.Value, PetawattsTolerance);
- Assert.Equal(LuminosityUnit.Petawatt, petawattQuantity.Unit);
-
- var picowattQuantity = watt.ToUnit(LuminosityUnit.Picowatt);
- AssertEx.EqualTolerance(PicowattsInOneWatt, (double)picowattQuantity.Value, PicowattsTolerance);
- Assert.Equal(LuminosityUnit.Picowatt, picowattQuantity.Unit);
-
- var solarluminosityQuantity = watt.ToUnit(LuminosityUnit.SolarLuminosity);
- AssertEx.EqualTolerance(SolarLuminositiesInOneWatt, (double)solarluminosityQuantity.Value, SolarLuminositiesTolerance);
- Assert.Equal(LuminosityUnit.SolarLuminosity, solarluminosityQuantity.Unit);
-
- var terawattQuantity = watt.ToUnit(LuminosityUnit.Terawatt);
- AssertEx.EqualTolerance(TerawattsInOneWatt, (double)terawattQuantity.Value, TerawattsTolerance);
- Assert.Equal(LuminosityUnit.Terawatt, terawattQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattQuantity = watt.ToUnit(LuminosityUnit.Watt);
- AssertEx.EqualTolerance(WattsInOneWatt, (double)wattQuantity.Value, WattsTolerance);
- Assert.Equal(LuminosityUnit.Watt, wattQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LuminosityUnit unit)
+ {
+ var quantity = Luminosity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LuminosityUnit unit)
{
- var quantityInBaseUnit = Luminosity.FromWatts(1).ToBaseUnit();
- Assert.Equal(Luminosity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Luminosity.Units.FirstOrDefault(u => u != Luminosity.BaseUnit && u != LuminosityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LuminosityUnit.Undefined)
+ fromUnit = Luminosity.BaseUnit;
+
+ var quantity = Luminosity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousFluxTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousFluxTestsBase.g.cs
index 6ce9dae421..4cbf4ecf65 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousFluxTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousFluxTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class LuminousFluxTestsBase : QuantityTestsBase
protected virtual double LumensTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LuminousFluxUnit unit)
+ {
+ return unit switch
+ {
+ LuminousFluxUnit.Lumen => (LumensInOneLumen, LumensTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LuminousFluxUnit.Lumen },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LuminousFluxUnit unit)
{
- var lumen = LuminousFlux.FromLumens(1);
+ var inBaseUnits = LuminousFlux.From(1.0, LuminousFlux.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var lumenQuantity = lumen.ToUnit(LuminousFluxUnit.Lumen);
- AssertEx.EqualTolerance(LumensInOneLumen, (double)lumenQuantity.Value, LumensTolerance);
- Assert.Equal(LuminousFluxUnit.Lumen, lumenQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LuminousFluxUnit unit)
+ {
+ var quantity = LuminousFlux.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LuminousFluxUnit unit)
{
- var quantityInBaseUnit = LuminousFlux.FromLumens(1).ToBaseUnit();
- Assert.Equal(LuminousFlux.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = LuminousFlux.Units.FirstOrDefault(u => u != LuminousFlux.BaseUnit && u != LuminousFluxUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LuminousFluxUnit.Undefined)
+ fromUnit = LuminousFlux.BaseUnit;
+
+ var quantity = LuminousFlux.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousIntensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousIntensityTestsBase.g.cs
index 7aa01f9f41..6b4462baa1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousIntensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/LuminousIntensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class LuminousIntensityTestsBase : QuantityTestsBase
protected virtual double CandelaTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(LuminousIntensityUnit unit)
+ {
+ return unit switch
+ {
+ LuminousIntensityUnit.Candela => (CandelaInOneCandela, CandelaTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { LuminousIntensityUnit.Candela },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(LuminousIntensityUnit unit)
{
- var candela = LuminousIntensity.FromCandela(1);
+ var inBaseUnits = LuminousIntensity.From(1.0, LuminousIntensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var candelaQuantity = candela.ToUnit(LuminousIntensityUnit.Candela);
- AssertEx.EqualTolerance(CandelaInOneCandela, (double)candelaQuantity.Value, CandelaTolerance);
- Assert.Equal(LuminousIntensityUnit.Candela, candelaQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(LuminousIntensityUnit unit)
+ {
+ var quantity = LuminousIntensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(LuminousIntensityUnit unit)
{
- var quantityInBaseUnit = LuminousIntensity.FromCandela(1).ToBaseUnit();
- Assert.Equal(LuminousIntensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = LuminousIntensity.Units.FirstOrDefault(u => u != LuminousIntensity.BaseUnit && u != LuminousIntensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == LuminousIntensityUnit.Undefined)
+ fromUnit = LuminousIntensity.BaseUnit;
+
+ var quantity = LuminousIntensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFieldTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFieldTestsBase.g.cs
index fb0b6872b0..535a956905 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFieldTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFieldTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -53,6 +54,30 @@ public abstract partial class MagneticFieldTestsBase : QuantityTestsBase
protected virtual double TeslasTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MagneticFieldUnit unit)
+ {
+ return unit switch
+ {
+ MagneticFieldUnit.Gauss => (GaussesInOneTesla, GaussesTolerance),
+ MagneticFieldUnit.Microtesla => (MicroteslasInOneTesla, MicroteslasTolerance),
+ MagneticFieldUnit.Milligauss => (MilligaussesInOneTesla, MilligaussesTolerance),
+ MagneticFieldUnit.Millitesla => (MilliteslasInOneTesla, MilliteslasTolerance),
+ MagneticFieldUnit.Nanotesla => (NanoteslasInOneTesla, NanoteslasTolerance),
+ MagneticFieldUnit.Tesla => (TeslasInOneTesla, TeslasTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MagneticFieldUnit.Gauss },
+ new object[] { MagneticFieldUnit.Microtesla },
+ new object[] { MagneticFieldUnit.Milligauss },
+ new object[] { MagneticFieldUnit.Millitesla },
+ new object[] { MagneticFieldUnit.Nanotesla },
+ new object[] { MagneticFieldUnit.Tesla },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -204,41 +229,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MagneticFieldUnit unit)
{
- var tesla = MagneticField.FromTeslas(1);
-
- var gaussQuantity = tesla.ToUnit(MagneticFieldUnit.Gauss);
- AssertEx.EqualTolerance(GaussesInOneTesla, (double)gaussQuantity.Value, GaussesTolerance);
- Assert.Equal(MagneticFieldUnit.Gauss, gaussQuantity.Unit);
+ var inBaseUnits = MagneticField.From(1.0, MagneticField.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microteslaQuantity = tesla.ToUnit(MagneticFieldUnit.Microtesla);
- AssertEx.EqualTolerance(MicroteslasInOneTesla, (double)microteslaQuantity.Value, MicroteslasTolerance);
- Assert.Equal(MagneticFieldUnit.Microtesla, microteslaQuantity.Unit);
-
- var milligaussQuantity = tesla.ToUnit(MagneticFieldUnit.Milligauss);
- AssertEx.EqualTolerance(MilligaussesInOneTesla, (double)milligaussQuantity.Value, MilligaussesTolerance);
- Assert.Equal(MagneticFieldUnit.Milligauss, milligaussQuantity.Unit);
-
- var milliteslaQuantity = tesla.ToUnit(MagneticFieldUnit.Millitesla);
- AssertEx.EqualTolerance(MilliteslasInOneTesla, (double)milliteslaQuantity.Value, MilliteslasTolerance);
- Assert.Equal(MagneticFieldUnit.Millitesla, milliteslaQuantity.Unit);
-
- var nanoteslaQuantity = tesla.ToUnit(MagneticFieldUnit.Nanotesla);
- AssertEx.EqualTolerance(NanoteslasInOneTesla, (double)nanoteslaQuantity.Value, NanoteslasTolerance);
- Assert.Equal(MagneticFieldUnit.Nanotesla, nanoteslaQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var teslaQuantity = tesla.ToUnit(MagneticFieldUnit.Tesla);
- AssertEx.EqualTolerance(TeslasInOneTesla, (double)teslaQuantity.Value, TeslasTolerance);
- Assert.Equal(MagneticFieldUnit.Tesla, teslaQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MagneticFieldUnit unit)
+ {
+ var quantity = MagneticField.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MagneticFieldUnit unit)
{
- var quantityInBaseUnit = MagneticField.FromTeslas(1).ToBaseUnit();
- Assert.Equal(MagneticField.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MagneticField.Units.FirstOrDefault(u => u != MagneticField.BaseUnit && u != MagneticFieldUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MagneticFieldUnit.Undefined)
+ fromUnit = MagneticField.BaseUnit;
+
+ var quantity = MagneticField.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFluxTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFluxTestsBase.g.cs
index ec2730dae4..53cceaa13a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFluxTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MagneticFluxTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class MagneticFluxTestsBase : QuantityTestsBase
protected virtual double WebersTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MagneticFluxUnit unit)
+ {
+ return unit switch
+ {
+ MagneticFluxUnit.Weber => (WebersInOneWeber, WebersTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MagneticFluxUnit.Weber },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MagneticFluxUnit unit)
{
- var weber = MagneticFlux.FromWebers(1);
+ var inBaseUnits = MagneticFlux.From(1.0, MagneticFlux.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var weberQuantity = weber.ToUnit(MagneticFluxUnit.Weber);
- AssertEx.EqualTolerance(WebersInOneWeber, (double)weberQuantity.Value, WebersTolerance);
- Assert.Equal(MagneticFluxUnit.Weber, weberQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MagneticFluxUnit unit)
+ {
+ var quantity = MagneticFlux.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MagneticFluxUnit unit)
{
- var quantityInBaseUnit = MagneticFlux.FromWebers(1).ToBaseUnit();
- Assert.Equal(MagneticFlux.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MagneticFlux.Units.FirstOrDefault(u => u != MagneticFlux.BaseUnit && u != MagneticFluxUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MagneticFluxUnit.Undefined)
+ fromUnit = MagneticFlux.BaseUnit;
+
+ var quantity = MagneticFlux.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MagnetizationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MagnetizationTestsBase.g.cs
index a1af95cb71..f77864e13f 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MagnetizationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MagnetizationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class MagnetizationTestsBase : QuantityTestsBase
protected virtual double AmperesPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MagnetizationUnit unit)
+ {
+ return unit switch
+ {
+ MagnetizationUnit.AmperePerMeter => (AmperesPerMeterInOneAmperePerMeter, AmperesPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MagnetizationUnit.AmperePerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MagnetizationUnit unit)
{
- var amperepermeter = Magnetization.FromAmperesPerMeter(1);
+ var inBaseUnits = Magnetization.From(1.0, Magnetization.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var amperepermeterQuantity = amperepermeter.ToUnit(MagnetizationUnit.AmperePerMeter);
- AssertEx.EqualTolerance(AmperesPerMeterInOneAmperePerMeter, (double)amperepermeterQuantity.Value, AmperesPerMeterTolerance);
- Assert.Equal(MagnetizationUnit.AmperePerMeter, amperepermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MagnetizationUnit unit)
+ {
+ var quantity = Magnetization.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MagnetizationUnit unit)
{
- var quantityInBaseUnit = Magnetization.FromAmperesPerMeter(1).ToBaseUnit();
- Assert.Equal(Magnetization.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Magnetization.Units.FirstOrDefault(u => u != Magnetization.BaseUnit && u != MagnetizationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MagnetizationUnit.Undefined)
+ fromUnit = Magnetization.BaseUnit;
+
+ var quantity = Magnetization.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MassConcentrationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MassConcentrationTestsBase.g.cs
index 49c606599d..8281d5947a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MassConcentrationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MassConcentrationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -139,6 +140,116 @@ public abstract partial class MassConcentrationTestsBase : QuantityTestsBase
protected virtual double TonnesPerCubicMillimeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MassConcentrationUnit unit)
+ {
+ return unit switch
+ {
+ MassConcentrationUnit.CentigramPerDeciliter => (CentigramsPerDeciliterInOneKilogramPerCubicMeter, CentigramsPerDeciliterTolerance),
+ MassConcentrationUnit.CentigramPerLiter => (CentigramsPerLiterInOneKilogramPerCubicMeter, CentigramsPerLiterTolerance),
+ MassConcentrationUnit.CentigramPerMicroliter => (CentigramsPerMicroliterInOneKilogramPerCubicMeter, CentigramsPerMicroliterTolerance),
+ MassConcentrationUnit.CentigramPerMilliliter => (CentigramsPerMilliliterInOneKilogramPerCubicMeter, CentigramsPerMilliliterTolerance),
+ MassConcentrationUnit.DecigramPerDeciliter => (DecigramsPerDeciliterInOneKilogramPerCubicMeter, DecigramsPerDeciliterTolerance),
+ MassConcentrationUnit.DecigramPerLiter => (DecigramsPerLiterInOneKilogramPerCubicMeter, DecigramsPerLiterTolerance),
+ MassConcentrationUnit.DecigramPerMicroliter => (DecigramsPerMicroliterInOneKilogramPerCubicMeter, DecigramsPerMicroliterTolerance),
+ MassConcentrationUnit.DecigramPerMilliliter => (DecigramsPerMilliliterInOneKilogramPerCubicMeter, DecigramsPerMilliliterTolerance),
+ MassConcentrationUnit.GramPerCubicCentimeter => (GramsPerCubicCentimeterInOneKilogramPerCubicMeter, GramsPerCubicCentimeterTolerance),
+ MassConcentrationUnit.GramPerCubicMeter => (GramsPerCubicMeterInOneKilogramPerCubicMeter, GramsPerCubicMeterTolerance),
+ MassConcentrationUnit.GramPerCubicMillimeter => (GramsPerCubicMillimeterInOneKilogramPerCubicMeter, GramsPerCubicMillimeterTolerance),
+ MassConcentrationUnit.GramPerDeciliter => (GramsPerDeciliterInOneKilogramPerCubicMeter, GramsPerDeciliterTolerance),
+ MassConcentrationUnit.GramPerLiter => (GramsPerLiterInOneKilogramPerCubicMeter, GramsPerLiterTolerance),
+ MassConcentrationUnit.GramPerMicroliter => (GramsPerMicroliterInOneKilogramPerCubicMeter, GramsPerMicroliterTolerance),
+ MassConcentrationUnit.GramPerMilliliter => (GramsPerMilliliterInOneKilogramPerCubicMeter, GramsPerMilliliterTolerance),
+ MassConcentrationUnit.KilogramPerCubicCentimeter => (KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter, KilogramsPerCubicCentimeterTolerance),
+ MassConcentrationUnit.KilogramPerCubicMeter => (KilogramsPerCubicMeterInOneKilogramPerCubicMeter, KilogramsPerCubicMeterTolerance),
+ MassConcentrationUnit.KilogramPerCubicMillimeter => (KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter, KilogramsPerCubicMillimeterTolerance),
+ MassConcentrationUnit.KilogramPerLiter => (KilogramsPerLiterInOneKilogramPerCubicMeter, KilogramsPerLiterTolerance),
+ MassConcentrationUnit.KilopoundPerCubicFoot => (KilopoundsPerCubicFootInOneKilogramPerCubicMeter, KilopoundsPerCubicFootTolerance),
+ MassConcentrationUnit.KilopoundPerCubicInch => (KilopoundsPerCubicInchInOneKilogramPerCubicMeter, KilopoundsPerCubicInchTolerance),
+ MassConcentrationUnit.MicrogramPerCubicMeter => (MicrogramsPerCubicMeterInOneKilogramPerCubicMeter, MicrogramsPerCubicMeterTolerance),
+ MassConcentrationUnit.MicrogramPerDeciliter => (MicrogramsPerDeciliterInOneKilogramPerCubicMeter, MicrogramsPerDeciliterTolerance),
+ MassConcentrationUnit.MicrogramPerLiter => (MicrogramsPerLiterInOneKilogramPerCubicMeter, MicrogramsPerLiterTolerance),
+ MassConcentrationUnit.MicrogramPerMicroliter => (MicrogramsPerMicroliterInOneKilogramPerCubicMeter, MicrogramsPerMicroliterTolerance),
+ MassConcentrationUnit.MicrogramPerMilliliter => (MicrogramsPerMilliliterInOneKilogramPerCubicMeter, MicrogramsPerMilliliterTolerance),
+ MassConcentrationUnit.MilligramPerCubicMeter => (MilligramsPerCubicMeterInOneKilogramPerCubicMeter, MilligramsPerCubicMeterTolerance),
+ MassConcentrationUnit.MilligramPerDeciliter => (MilligramsPerDeciliterInOneKilogramPerCubicMeter, MilligramsPerDeciliterTolerance),
+ MassConcentrationUnit.MilligramPerLiter => (MilligramsPerLiterInOneKilogramPerCubicMeter, MilligramsPerLiterTolerance),
+ MassConcentrationUnit.MilligramPerMicroliter => (MilligramsPerMicroliterInOneKilogramPerCubicMeter, MilligramsPerMicroliterTolerance),
+ MassConcentrationUnit.MilligramPerMilliliter => (MilligramsPerMilliliterInOneKilogramPerCubicMeter, MilligramsPerMilliliterTolerance),
+ MassConcentrationUnit.NanogramPerDeciliter => (NanogramsPerDeciliterInOneKilogramPerCubicMeter, NanogramsPerDeciliterTolerance),
+ MassConcentrationUnit.NanogramPerLiter => (NanogramsPerLiterInOneKilogramPerCubicMeter, NanogramsPerLiterTolerance),
+ MassConcentrationUnit.NanogramPerMicroliter => (NanogramsPerMicroliterInOneKilogramPerCubicMeter, NanogramsPerMicroliterTolerance),
+ MassConcentrationUnit.NanogramPerMilliliter => (NanogramsPerMilliliterInOneKilogramPerCubicMeter, NanogramsPerMilliliterTolerance),
+ MassConcentrationUnit.OuncePerImperialGallon => (OuncesPerImperialGallonInOneKilogramPerCubicMeter, OuncesPerImperialGallonTolerance),
+ MassConcentrationUnit.OuncePerUSGallon => (OuncesPerUSGallonInOneKilogramPerCubicMeter, OuncesPerUSGallonTolerance),
+ MassConcentrationUnit.PicogramPerDeciliter => (PicogramsPerDeciliterInOneKilogramPerCubicMeter, PicogramsPerDeciliterTolerance),
+ MassConcentrationUnit.PicogramPerLiter => (PicogramsPerLiterInOneKilogramPerCubicMeter, PicogramsPerLiterTolerance),
+ MassConcentrationUnit.PicogramPerMicroliter => (PicogramsPerMicroliterInOneKilogramPerCubicMeter, PicogramsPerMicroliterTolerance),
+ MassConcentrationUnit.PicogramPerMilliliter => (PicogramsPerMilliliterInOneKilogramPerCubicMeter, PicogramsPerMilliliterTolerance),
+ MassConcentrationUnit.PoundPerCubicFoot => (PoundsPerCubicFootInOneKilogramPerCubicMeter, PoundsPerCubicFootTolerance),
+ MassConcentrationUnit.PoundPerCubicInch => (PoundsPerCubicInchInOneKilogramPerCubicMeter, PoundsPerCubicInchTolerance),
+ MassConcentrationUnit.PoundPerImperialGallon => (PoundsPerImperialGallonInOneKilogramPerCubicMeter, PoundsPerImperialGallonTolerance),
+ MassConcentrationUnit.PoundPerUSGallon => (PoundsPerUSGallonInOneKilogramPerCubicMeter, PoundsPerUSGallonTolerance),
+ MassConcentrationUnit.SlugPerCubicFoot => (SlugsPerCubicFootInOneKilogramPerCubicMeter, SlugsPerCubicFootTolerance),
+ MassConcentrationUnit.TonnePerCubicCentimeter => (TonnesPerCubicCentimeterInOneKilogramPerCubicMeter, TonnesPerCubicCentimeterTolerance),
+ MassConcentrationUnit.TonnePerCubicMeter => (TonnesPerCubicMeterInOneKilogramPerCubicMeter, TonnesPerCubicMeterTolerance),
+ MassConcentrationUnit.TonnePerCubicMillimeter => (TonnesPerCubicMillimeterInOneKilogramPerCubicMeter, TonnesPerCubicMillimeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MassConcentrationUnit.CentigramPerDeciliter },
+ new object[] { MassConcentrationUnit.CentigramPerLiter },
+ new object[] { MassConcentrationUnit.CentigramPerMicroliter },
+ new object[] { MassConcentrationUnit.CentigramPerMilliliter },
+ new object[] { MassConcentrationUnit.DecigramPerDeciliter },
+ new object[] { MassConcentrationUnit.DecigramPerLiter },
+ new object[] { MassConcentrationUnit.DecigramPerMicroliter },
+ new object[] { MassConcentrationUnit.DecigramPerMilliliter },
+ new object[] { MassConcentrationUnit.GramPerCubicCentimeter },
+ new object[] { MassConcentrationUnit.GramPerCubicMeter },
+ new object[] { MassConcentrationUnit.GramPerCubicMillimeter },
+ new object[] { MassConcentrationUnit.GramPerDeciliter },
+ new object[] { MassConcentrationUnit.GramPerLiter },
+ new object[] { MassConcentrationUnit.GramPerMicroliter },
+ new object[] { MassConcentrationUnit.GramPerMilliliter },
+ new object[] { MassConcentrationUnit.KilogramPerCubicCentimeter },
+ new object[] { MassConcentrationUnit.KilogramPerCubicMeter },
+ new object[] { MassConcentrationUnit.KilogramPerCubicMillimeter },
+ new object[] { MassConcentrationUnit.KilogramPerLiter },
+ new object[] { MassConcentrationUnit.KilopoundPerCubicFoot },
+ new object[] { MassConcentrationUnit.KilopoundPerCubicInch },
+ new object[] { MassConcentrationUnit.MicrogramPerCubicMeter },
+ new object[] { MassConcentrationUnit.MicrogramPerDeciliter },
+ new object[] { MassConcentrationUnit.MicrogramPerLiter },
+ new object[] { MassConcentrationUnit.MicrogramPerMicroliter },
+ new object[] { MassConcentrationUnit.MicrogramPerMilliliter },
+ new object[] { MassConcentrationUnit.MilligramPerCubicMeter },
+ new object[] { MassConcentrationUnit.MilligramPerDeciliter },
+ new object[] { MassConcentrationUnit.MilligramPerLiter },
+ new object[] { MassConcentrationUnit.MilligramPerMicroliter },
+ new object[] { MassConcentrationUnit.MilligramPerMilliliter },
+ new object[] { MassConcentrationUnit.NanogramPerDeciliter },
+ new object[] { MassConcentrationUnit.NanogramPerLiter },
+ new object[] { MassConcentrationUnit.NanogramPerMicroliter },
+ new object[] { MassConcentrationUnit.NanogramPerMilliliter },
+ new object[] { MassConcentrationUnit.OuncePerImperialGallon },
+ new object[] { MassConcentrationUnit.OuncePerUSGallon },
+ new object[] { MassConcentrationUnit.PicogramPerDeciliter },
+ new object[] { MassConcentrationUnit.PicogramPerLiter },
+ new object[] { MassConcentrationUnit.PicogramPerMicroliter },
+ new object[] { MassConcentrationUnit.PicogramPerMilliliter },
+ new object[] { MassConcentrationUnit.PoundPerCubicFoot },
+ new object[] { MassConcentrationUnit.PoundPerCubicInch },
+ new object[] { MassConcentrationUnit.PoundPerImperialGallon },
+ new object[] { MassConcentrationUnit.PoundPerUSGallon },
+ new object[] { MassConcentrationUnit.SlugPerCubicFoot },
+ new object[] { MassConcentrationUnit.TonnePerCubicCentimeter },
+ new object[] { MassConcentrationUnit.TonnePerCubicMeter },
+ new object[] { MassConcentrationUnit.TonnePerCubicMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -548,213 +659,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MassConcentrationUnit unit)
{
- var kilogrampercubicmeter = MassConcentration.FromKilogramsPerCubicMeter(1);
-
- var centigramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.CentigramPerDeciliter);
- AssertEx.EqualTolerance(CentigramsPerDeciliterInOneKilogramPerCubicMeter, (double)centigramperdeciliterQuantity.Value, CentigramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.CentigramPerDeciliter, centigramperdeciliterQuantity.Unit);
-
- var centigramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.CentigramPerLiter);
- AssertEx.EqualTolerance(CentigramsPerLiterInOneKilogramPerCubicMeter, (double)centigramperliterQuantity.Value, CentigramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.CentigramPerLiter, centigramperliterQuantity.Unit);
-
- var centigrampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.CentigramPerMicroliter);
- AssertEx.EqualTolerance(CentigramsPerMicroliterInOneKilogramPerCubicMeter, (double)centigrampermicroliterQuantity.Value, CentigramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.CentigramPerMicroliter, centigrampermicroliterQuantity.Unit);
-
- var centigrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.CentigramPerMilliliter);
- AssertEx.EqualTolerance(CentigramsPerMilliliterInOneKilogramPerCubicMeter, (double)centigrampermilliliterQuantity.Value, CentigramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.CentigramPerMilliliter, centigrampermilliliterQuantity.Unit);
-
- var decigramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.DecigramPerDeciliter);
- AssertEx.EqualTolerance(DecigramsPerDeciliterInOneKilogramPerCubicMeter, (double)decigramperdeciliterQuantity.Value, DecigramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.DecigramPerDeciliter, decigramperdeciliterQuantity.Unit);
-
- var decigramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.DecigramPerLiter);
- AssertEx.EqualTolerance(DecigramsPerLiterInOneKilogramPerCubicMeter, (double)decigramperliterQuantity.Value, DecigramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.DecigramPerLiter, decigramperliterQuantity.Unit);
-
- var decigrampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.DecigramPerMicroliter);
- AssertEx.EqualTolerance(DecigramsPerMicroliterInOneKilogramPerCubicMeter, (double)decigrampermicroliterQuantity.Value, DecigramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.DecigramPerMicroliter, decigrampermicroliterQuantity.Unit);
-
- var decigrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.DecigramPerMilliliter);
- AssertEx.EqualTolerance(DecigramsPerMilliliterInOneKilogramPerCubicMeter, (double)decigrampermilliliterQuantity.Value, DecigramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.DecigramPerMilliliter, decigrampermilliliterQuantity.Unit);
-
- var grampercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerCubicCentimeter);
- AssertEx.EqualTolerance(GramsPerCubicCentimeterInOneKilogramPerCubicMeter, (double)grampercubiccentimeterQuantity.Value, GramsPerCubicCentimeterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerCubicCentimeter, grampercubiccentimeterQuantity.Unit);
-
- var grampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerCubicMeter);
- AssertEx.EqualTolerance(GramsPerCubicMeterInOneKilogramPerCubicMeter, (double)grampercubicmeterQuantity.Value, GramsPerCubicMeterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerCubicMeter, grampercubicmeterQuantity.Unit);
-
- var grampercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerCubicMillimeter);
- AssertEx.EqualTolerance(GramsPerCubicMillimeterInOneKilogramPerCubicMeter, (double)grampercubicmillimeterQuantity.Value, GramsPerCubicMillimeterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerCubicMillimeter, grampercubicmillimeterQuantity.Unit);
-
- var gramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerDeciliter);
- AssertEx.EqualTolerance(GramsPerDeciliterInOneKilogramPerCubicMeter, (double)gramperdeciliterQuantity.Value, GramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerDeciliter, gramperdeciliterQuantity.Unit);
-
- var gramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerLiter);
- AssertEx.EqualTolerance(GramsPerLiterInOneKilogramPerCubicMeter, (double)gramperliterQuantity.Value, GramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerLiter, gramperliterQuantity.Unit);
-
- var grampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerMicroliter);
- AssertEx.EqualTolerance(GramsPerMicroliterInOneKilogramPerCubicMeter, (double)grampermicroliterQuantity.Value, GramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerMicroliter, grampermicroliterQuantity.Unit);
-
- var grampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.GramPerMilliliter);
- AssertEx.EqualTolerance(GramsPerMilliliterInOneKilogramPerCubicMeter, (double)grampermilliliterQuantity.Value, GramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.GramPerMilliliter, grampermilliliterQuantity.Unit);
-
- var kilogrampercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.KilogramPerCubicCentimeter);
- AssertEx.EqualTolerance(KilogramsPerCubicCentimeterInOneKilogramPerCubicMeter, (double)kilogrampercubiccentimeterQuantity.Value, KilogramsPerCubicCentimeterTolerance);
- Assert.Equal(MassConcentrationUnit.KilogramPerCubicCentimeter, kilogrampercubiccentimeterQuantity.Unit);
-
- var kilogrampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.KilogramPerCubicMeter);
- AssertEx.EqualTolerance(KilogramsPerCubicMeterInOneKilogramPerCubicMeter, (double)kilogrampercubicmeterQuantity.Value, KilogramsPerCubicMeterTolerance);
- Assert.Equal(MassConcentrationUnit.KilogramPerCubicMeter, kilogrampercubicmeterQuantity.Unit);
-
- var kilogrampercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.KilogramPerCubicMillimeter);
- AssertEx.EqualTolerance(KilogramsPerCubicMillimeterInOneKilogramPerCubicMeter, (double)kilogrampercubicmillimeterQuantity.Value, KilogramsPerCubicMillimeterTolerance);
- Assert.Equal(MassConcentrationUnit.KilogramPerCubicMillimeter, kilogrampercubicmillimeterQuantity.Unit);
-
- var kilogramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.KilogramPerLiter);
- AssertEx.EqualTolerance(KilogramsPerLiterInOneKilogramPerCubicMeter, (double)kilogramperliterQuantity.Value, KilogramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.KilogramPerLiter, kilogramperliterQuantity.Unit);
-
- var kilopoundpercubicfootQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.KilopoundPerCubicFoot);
- AssertEx.EqualTolerance(KilopoundsPerCubicFootInOneKilogramPerCubicMeter, (double)kilopoundpercubicfootQuantity.Value, KilopoundsPerCubicFootTolerance);
- Assert.Equal(MassConcentrationUnit.KilopoundPerCubicFoot, kilopoundpercubicfootQuantity.Unit);
-
- var kilopoundpercubicinchQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.KilopoundPerCubicInch);
- AssertEx.EqualTolerance(KilopoundsPerCubicInchInOneKilogramPerCubicMeter, (double)kilopoundpercubicinchQuantity.Value, KilopoundsPerCubicInchTolerance);
- Assert.Equal(MassConcentrationUnit.KilopoundPerCubicInch, kilopoundpercubicinchQuantity.Unit);
-
- var microgrampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MicrogramPerCubicMeter);
- AssertEx.EqualTolerance(MicrogramsPerCubicMeterInOneKilogramPerCubicMeter, (double)microgrampercubicmeterQuantity.Value, MicrogramsPerCubicMeterTolerance);
- Assert.Equal(MassConcentrationUnit.MicrogramPerCubicMeter, microgrampercubicmeterQuantity.Unit);
-
- var microgramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MicrogramPerDeciliter);
- AssertEx.EqualTolerance(MicrogramsPerDeciliterInOneKilogramPerCubicMeter, (double)microgramperdeciliterQuantity.Value, MicrogramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.MicrogramPerDeciliter, microgramperdeciliterQuantity.Unit);
+ var inBaseUnits = MassConcentration.From(1.0, MassConcentration.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microgramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MicrogramPerLiter);
- AssertEx.EqualTolerance(MicrogramsPerLiterInOneKilogramPerCubicMeter, (double)microgramperliterQuantity.Value, MicrogramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.MicrogramPerLiter, microgramperliterQuantity.Unit);
-
- var microgrampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MicrogramPerMicroliter);
- AssertEx.EqualTolerance(MicrogramsPerMicroliterInOneKilogramPerCubicMeter, (double)microgrampermicroliterQuantity.Value, MicrogramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.MicrogramPerMicroliter, microgrampermicroliterQuantity.Unit);
-
- var microgrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MicrogramPerMilliliter);
- AssertEx.EqualTolerance(MicrogramsPerMilliliterInOneKilogramPerCubicMeter, (double)microgrampermilliliterQuantity.Value, MicrogramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.MicrogramPerMilliliter, microgrampermilliliterQuantity.Unit);
-
- var milligrampercubicmeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MilligramPerCubicMeter);
- AssertEx.EqualTolerance(MilligramsPerCubicMeterInOneKilogramPerCubicMeter, (double)milligrampercubicmeterQuantity.Value, MilligramsPerCubicMeterTolerance);
- Assert.Equal(MassConcentrationUnit.MilligramPerCubicMeter, milligrampercubicmeterQuantity.Unit);
-
- var milligramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MilligramPerDeciliter);
- AssertEx.EqualTolerance(MilligramsPerDeciliterInOneKilogramPerCubicMeter, (double)milligramperdeciliterQuantity.Value, MilligramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.MilligramPerDeciliter, milligramperdeciliterQuantity.Unit);
-
- var milligramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MilligramPerLiter);
- AssertEx.EqualTolerance(MilligramsPerLiterInOneKilogramPerCubicMeter, (double)milligramperliterQuantity.Value, MilligramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.MilligramPerLiter, milligramperliterQuantity.Unit);
-
- var milligrampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MilligramPerMicroliter);
- AssertEx.EqualTolerance(MilligramsPerMicroliterInOneKilogramPerCubicMeter, (double)milligrampermicroliterQuantity.Value, MilligramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.MilligramPerMicroliter, milligrampermicroliterQuantity.Unit);
-
- var milligrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.MilligramPerMilliliter);
- AssertEx.EqualTolerance(MilligramsPerMilliliterInOneKilogramPerCubicMeter, (double)milligrampermilliliterQuantity.Value, MilligramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.MilligramPerMilliliter, milligrampermilliliterQuantity.Unit);
-
- var nanogramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.NanogramPerDeciliter);
- AssertEx.EqualTolerance(NanogramsPerDeciliterInOneKilogramPerCubicMeter, (double)nanogramperdeciliterQuantity.Value, NanogramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.NanogramPerDeciliter, nanogramperdeciliterQuantity.Unit);
-
- var nanogramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.NanogramPerLiter);
- AssertEx.EqualTolerance(NanogramsPerLiterInOneKilogramPerCubicMeter, (double)nanogramperliterQuantity.Value, NanogramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.NanogramPerLiter, nanogramperliterQuantity.Unit);
-
- var nanogrampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.NanogramPerMicroliter);
- AssertEx.EqualTolerance(NanogramsPerMicroliterInOneKilogramPerCubicMeter, (double)nanogrampermicroliterQuantity.Value, NanogramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.NanogramPerMicroliter, nanogrampermicroliterQuantity.Unit);
-
- var nanogrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.NanogramPerMilliliter);
- AssertEx.EqualTolerance(NanogramsPerMilliliterInOneKilogramPerCubicMeter, (double)nanogrampermilliliterQuantity.Value, NanogramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.NanogramPerMilliliter, nanogrampermilliliterQuantity.Unit);
-
- var ounceperimperialgallonQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.OuncePerImperialGallon);
- AssertEx.EqualTolerance(OuncesPerImperialGallonInOneKilogramPerCubicMeter, (double)ounceperimperialgallonQuantity.Value, OuncesPerImperialGallonTolerance);
- Assert.Equal(MassConcentrationUnit.OuncePerImperialGallon, ounceperimperialgallonQuantity.Unit);
-
- var ounceperusgallonQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.OuncePerUSGallon);
- AssertEx.EqualTolerance(OuncesPerUSGallonInOneKilogramPerCubicMeter, (double)ounceperusgallonQuantity.Value, OuncesPerUSGallonTolerance);
- Assert.Equal(MassConcentrationUnit.OuncePerUSGallon, ounceperusgallonQuantity.Unit);
-
- var picogramperdeciliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PicogramPerDeciliter);
- AssertEx.EqualTolerance(PicogramsPerDeciliterInOneKilogramPerCubicMeter, (double)picogramperdeciliterQuantity.Value, PicogramsPerDeciliterTolerance);
- Assert.Equal(MassConcentrationUnit.PicogramPerDeciliter, picogramperdeciliterQuantity.Unit);
-
- var picogramperliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PicogramPerLiter);
- AssertEx.EqualTolerance(PicogramsPerLiterInOneKilogramPerCubicMeter, (double)picogramperliterQuantity.Value, PicogramsPerLiterTolerance);
- Assert.Equal(MassConcentrationUnit.PicogramPerLiter, picogramperliterQuantity.Unit);
-
- var picogrampermicroliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PicogramPerMicroliter);
- AssertEx.EqualTolerance(PicogramsPerMicroliterInOneKilogramPerCubicMeter, (double)picogrampermicroliterQuantity.Value, PicogramsPerMicroliterTolerance);
- Assert.Equal(MassConcentrationUnit.PicogramPerMicroliter, picogrampermicroliterQuantity.Unit);
-
- var picogrampermilliliterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PicogramPerMilliliter);
- AssertEx.EqualTolerance(PicogramsPerMilliliterInOneKilogramPerCubicMeter, (double)picogrampermilliliterQuantity.Value, PicogramsPerMilliliterTolerance);
- Assert.Equal(MassConcentrationUnit.PicogramPerMilliliter, picogrampermilliliterQuantity.Unit);
-
- var poundpercubicfootQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PoundPerCubicFoot);
- AssertEx.EqualTolerance(PoundsPerCubicFootInOneKilogramPerCubicMeter, (double)poundpercubicfootQuantity.Value, PoundsPerCubicFootTolerance);
- Assert.Equal(MassConcentrationUnit.PoundPerCubicFoot, poundpercubicfootQuantity.Unit);
-
- var poundpercubicinchQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PoundPerCubicInch);
- AssertEx.EqualTolerance(PoundsPerCubicInchInOneKilogramPerCubicMeter, (double)poundpercubicinchQuantity.Value, PoundsPerCubicInchTolerance);
- Assert.Equal(MassConcentrationUnit.PoundPerCubicInch, poundpercubicinchQuantity.Unit);
-
- var poundperimperialgallonQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PoundPerImperialGallon);
- AssertEx.EqualTolerance(PoundsPerImperialGallonInOneKilogramPerCubicMeter, (double)poundperimperialgallonQuantity.Value, PoundsPerImperialGallonTolerance);
- Assert.Equal(MassConcentrationUnit.PoundPerImperialGallon, poundperimperialgallonQuantity.Unit);
-
- var poundperusgallonQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.PoundPerUSGallon);
- AssertEx.EqualTolerance(PoundsPerUSGallonInOneKilogramPerCubicMeter, (double)poundperusgallonQuantity.Value, PoundsPerUSGallonTolerance);
- Assert.Equal(MassConcentrationUnit.PoundPerUSGallon, poundperusgallonQuantity.Unit);
-
- var slugpercubicfootQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.SlugPerCubicFoot);
- AssertEx.EqualTolerance(SlugsPerCubicFootInOneKilogramPerCubicMeter, (double)slugpercubicfootQuantity.Value, SlugsPerCubicFootTolerance);
- Assert.Equal(MassConcentrationUnit.SlugPerCubicFoot, slugpercubicfootQuantity.Unit);
-
- var tonnepercubiccentimeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.TonnePerCubicCentimeter);
- AssertEx.EqualTolerance(TonnesPerCubicCentimeterInOneKilogramPerCubicMeter, (double)tonnepercubiccentimeterQuantity.Value, TonnesPerCubicCentimeterTolerance);
- Assert.Equal(MassConcentrationUnit.TonnePerCubicCentimeter, tonnepercubiccentimeterQuantity.Unit);
-
- var tonnepercubicmeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.TonnePerCubicMeter);
- AssertEx.EqualTolerance(TonnesPerCubicMeterInOneKilogramPerCubicMeter, (double)tonnepercubicmeterQuantity.Value, TonnesPerCubicMeterTolerance);
- Assert.Equal(MassConcentrationUnit.TonnePerCubicMeter, tonnepercubicmeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonnepercubicmillimeterQuantity = kilogrampercubicmeter.ToUnit(MassConcentrationUnit.TonnePerCubicMillimeter);
- AssertEx.EqualTolerance(TonnesPerCubicMillimeterInOneKilogramPerCubicMeter, (double)tonnepercubicmillimeterQuantity.Value, TonnesPerCubicMillimeterTolerance);
- Assert.Equal(MassConcentrationUnit.TonnePerCubicMillimeter, tonnepercubicmillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MassConcentrationUnit unit)
+ {
+ var quantity = MassConcentration.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MassConcentrationUnit unit)
{
- var quantityInBaseUnit = MassConcentration.FromKilogramsPerCubicMeter(1).ToBaseUnit();
- Assert.Equal(MassConcentration.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MassConcentration.Units.FirstOrDefault(u => u != MassConcentration.BaseUnit && u != MassConcentrationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MassConcentrationUnit.Undefined)
+ fromUnit = MassConcentration.BaseUnit;
+
+ var quantity = MassConcentration.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MassFlowTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MassFlowTestsBase.g.cs
index 306ad7e5e6..8ca2653751 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MassFlowTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MassFlowTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -107,6 +108,84 @@ public abstract partial class MassFlowTestsBase : QuantityTestsBase
protected virtual double TonnesPerHourTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MassFlowUnit unit)
+ {
+ return unit switch
+ {
+ MassFlowUnit.CentigramPerDay => (CentigramsPerDayInOneGramPerSecond, CentigramsPerDayTolerance),
+ MassFlowUnit.CentigramPerSecond => (CentigramsPerSecondInOneGramPerSecond, CentigramsPerSecondTolerance),
+ MassFlowUnit.DecagramPerDay => (DecagramsPerDayInOneGramPerSecond, DecagramsPerDayTolerance),
+ MassFlowUnit.DecagramPerSecond => (DecagramsPerSecondInOneGramPerSecond, DecagramsPerSecondTolerance),
+ MassFlowUnit.DecigramPerDay => (DecigramsPerDayInOneGramPerSecond, DecigramsPerDayTolerance),
+ MassFlowUnit.DecigramPerSecond => (DecigramsPerSecondInOneGramPerSecond, DecigramsPerSecondTolerance),
+ MassFlowUnit.GramPerDay => (GramsPerDayInOneGramPerSecond, GramsPerDayTolerance),
+ MassFlowUnit.GramPerHour => (GramsPerHourInOneGramPerSecond, GramsPerHourTolerance),
+ MassFlowUnit.GramPerSecond => (GramsPerSecondInOneGramPerSecond, GramsPerSecondTolerance),
+ MassFlowUnit.HectogramPerDay => (HectogramsPerDayInOneGramPerSecond, HectogramsPerDayTolerance),
+ MassFlowUnit.HectogramPerSecond => (HectogramsPerSecondInOneGramPerSecond, HectogramsPerSecondTolerance),
+ MassFlowUnit.KilogramPerDay => (KilogramsPerDayInOneGramPerSecond, KilogramsPerDayTolerance),
+ MassFlowUnit.KilogramPerHour => (KilogramsPerHourInOneGramPerSecond, KilogramsPerHourTolerance),
+ MassFlowUnit.KilogramPerMinute => (KilogramsPerMinuteInOneGramPerSecond, KilogramsPerMinuteTolerance),
+ MassFlowUnit.KilogramPerSecond => (KilogramsPerSecondInOneGramPerSecond, KilogramsPerSecondTolerance),
+ MassFlowUnit.MegagramPerDay => (MegagramsPerDayInOneGramPerSecond, MegagramsPerDayTolerance),
+ MassFlowUnit.MegapoundPerDay => (MegapoundsPerDayInOneGramPerSecond, MegapoundsPerDayTolerance),
+ MassFlowUnit.MegapoundPerHour => (MegapoundsPerHourInOneGramPerSecond, MegapoundsPerHourTolerance),
+ MassFlowUnit.MegapoundPerMinute => (MegapoundsPerMinuteInOneGramPerSecond, MegapoundsPerMinuteTolerance),
+ MassFlowUnit.MegapoundPerSecond => (MegapoundsPerSecondInOneGramPerSecond, MegapoundsPerSecondTolerance),
+ MassFlowUnit.MicrogramPerDay => (MicrogramsPerDayInOneGramPerSecond, MicrogramsPerDayTolerance),
+ MassFlowUnit.MicrogramPerSecond => (MicrogramsPerSecondInOneGramPerSecond, MicrogramsPerSecondTolerance),
+ MassFlowUnit.MilligramPerDay => (MilligramsPerDayInOneGramPerSecond, MilligramsPerDayTolerance),
+ MassFlowUnit.MilligramPerSecond => (MilligramsPerSecondInOneGramPerSecond, MilligramsPerSecondTolerance),
+ MassFlowUnit.NanogramPerDay => (NanogramsPerDayInOneGramPerSecond, NanogramsPerDayTolerance),
+ MassFlowUnit.NanogramPerSecond => (NanogramsPerSecondInOneGramPerSecond, NanogramsPerSecondTolerance),
+ MassFlowUnit.PoundPerDay => (PoundsPerDayInOneGramPerSecond, PoundsPerDayTolerance),
+ MassFlowUnit.PoundPerHour => (PoundsPerHourInOneGramPerSecond, PoundsPerHourTolerance),
+ MassFlowUnit.PoundPerMinute => (PoundsPerMinuteInOneGramPerSecond, PoundsPerMinuteTolerance),
+ MassFlowUnit.PoundPerSecond => (PoundsPerSecondInOneGramPerSecond, PoundsPerSecondTolerance),
+ MassFlowUnit.ShortTonPerHour => (ShortTonsPerHourInOneGramPerSecond, ShortTonsPerHourTolerance),
+ MassFlowUnit.TonnePerDay => (TonnesPerDayInOneGramPerSecond, TonnesPerDayTolerance),
+ MassFlowUnit.TonnePerHour => (TonnesPerHourInOneGramPerSecond, TonnesPerHourTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MassFlowUnit.CentigramPerDay },
+ new object[] { MassFlowUnit.CentigramPerSecond },
+ new object[] { MassFlowUnit.DecagramPerDay },
+ new object[] { MassFlowUnit.DecagramPerSecond },
+ new object[] { MassFlowUnit.DecigramPerDay },
+ new object[] { MassFlowUnit.DecigramPerSecond },
+ new object[] { MassFlowUnit.GramPerDay },
+ new object[] { MassFlowUnit.GramPerHour },
+ new object[] { MassFlowUnit.GramPerSecond },
+ new object[] { MassFlowUnit.HectogramPerDay },
+ new object[] { MassFlowUnit.HectogramPerSecond },
+ new object[] { MassFlowUnit.KilogramPerDay },
+ new object[] { MassFlowUnit.KilogramPerHour },
+ new object[] { MassFlowUnit.KilogramPerMinute },
+ new object[] { MassFlowUnit.KilogramPerSecond },
+ new object[] { MassFlowUnit.MegagramPerDay },
+ new object[] { MassFlowUnit.MegapoundPerDay },
+ new object[] { MassFlowUnit.MegapoundPerHour },
+ new object[] { MassFlowUnit.MegapoundPerMinute },
+ new object[] { MassFlowUnit.MegapoundPerSecond },
+ new object[] { MassFlowUnit.MicrogramPerDay },
+ new object[] { MassFlowUnit.MicrogramPerSecond },
+ new object[] { MassFlowUnit.MilligramPerDay },
+ new object[] { MassFlowUnit.MilligramPerSecond },
+ new object[] { MassFlowUnit.NanogramPerDay },
+ new object[] { MassFlowUnit.NanogramPerSecond },
+ new object[] { MassFlowUnit.PoundPerDay },
+ new object[] { MassFlowUnit.PoundPerHour },
+ new object[] { MassFlowUnit.PoundPerMinute },
+ new object[] { MassFlowUnit.PoundPerSecond },
+ new object[] { MassFlowUnit.ShortTonPerHour },
+ new object[] { MassFlowUnit.TonnePerDay },
+ new object[] { MassFlowUnit.TonnePerHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -420,149 +499,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MassFlowUnit unit)
{
- var grampersecond = MassFlow.FromGramsPerSecond(1);
-
- var centigramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.CentigramPerDay);
- AssertEx.EqualTolerance(CentigramsPerDayInOneGramPerSecond, (double)centigramperdayQuantity.Value, CentigramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.CentigramPerDay, centigramperdayQuantity.Unit);
-
- var centigrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.CentigramPerSecond);
- AssertEx.EqualTolerance(CentigramsPerSecondInOneGramPerSecond, (double)centigrampersecondQuantity.Value, CentigramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.CentigramPerSecond, centigrampersecondQuantity.Unit);
-
- var decagramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.DecagramPerDay);
- AssertEx.EqualTolerance(DecagramsPerDayInOneGramPerSecond, (double)decagramperdayQuantity.Value, DecagramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.DecagramPerDay, decagramperdayQuantity.Unit);
-
- var decagrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.DecagramPerSecond);
- AssertEx.EqualTolerance(DecagramsPerSecondInOneGramPerSecond, (double)decagrampersecondQuantity.Value, DecagramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.DecagramPerSecond, decagrampersecondQuantity.Unit);
-
- var decigramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.DecigramPerDay);
- AssertEx.EqualTolerance(DecigramsPerDayInOneGramPerSecond, (double)decigramperdayQuantity.Value, DecigramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.DecigramPerDay, decigramperdayQuantity.Unit);
-
- var decigrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.DecigramPerSecond);
- AssertEx.EqualTolerance(DecigramsPerSecondInOneGramPerSecond, (double)decigrampersecondQuantity.Value, DecigramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.DecigramPerSecond, decigrampersecondQuantity.Unit);
-
- var gramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.GramPerDay);
- AssertEx.EqualTolerance(GramsPerDayInOneGramPerSecond, (double)gramperdayQuantity.Value, GramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.GramPerDay, gramperdayQuantity.Unit);
-
- var gramperhourQuantity = grampersecond.ToUnit(MassFlowUnit.GramPerHour);
- AssertEx.EqualTolerance(GramsPerHourInOneGramPerSecond, (double)gramperhourQuantity.Value, GramsPerHourTolerance);
- Assert.Equal(MassFlowUnit.GramPerHour, gramperhourQuantity.Unit);
-
- var grampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.GramPerSecond);
- AssertEx.EqualTolerance(GramsPerSecondInOneGramPerSecond, (double)grampersecondQuantity.Value, GramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.GramPerSecond, grampersecondQuantity.Unit);
-
- var hectogramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.HectogramPerDay);
- AssertEx.EqualTolerance(HectogramsPerDayInOneGramPerSecond, (double)hectogramperdayQuantity.Value, HectogramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.HectogramPerDay, hectogramperdayQuantity.Unit);
-
- var hectogrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.HectogramPerSecond);
- AssertEx.EqualTolerance(HectogramsPerSecondInOneGramPerSecond, (double)hectogrampersecondQuantity.Value, HectogramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.HectogramPerSecond, hectogrampersecondQuantity.Unit);
-
- var kilogramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.KilogramPerDay);
- AssertEx.EqualTolerance(KilogramsPerDayInOneGramPerSecond, (double)kilogramperdayQuantity.Value, KilogramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.KilogramPerDay, kilogramperdayQuantity.Unit);
-
- var kilogramperhourQuantity = grampersecond.ToUnit(MassFlowUnit.KilogramPerHour);
- AssertEx.EqualTolerance(KilogramsPerHourInOneGramPerSecond, (double)kilogramperhourQuantity.Value, KilogramsPerHourTolerance);
- Assert.Equal(MassFlowUnit.KilogramPerHour, kilogramperhourQuantity.Unit);
-
- var kilogramperminuteQuantity = grampersecond.ToUnit(MassFlowUnit.KilogramPerMinute);
- AssertEx.EqualTolerance(KilogramsPerMinuteInOneGramPerSecond, (double)kilogramperminuteQuantity.Value, KilogramsPerMinuteTolerance);
- Assert.Equal(MassFlowUnit.KilogramPerMinute, kilogramperminuteQuantity.Unit);
-
- var kilogrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.KilogramPerSecond);
- AssertEx.EqualTolerance(KilogramsPerSecondInOneGramPerSecond, (double)kilogrampersecondQuantity.Value, KilogramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.KilogramPerSecond, kilogrampersecondQuantity.Unit);
+ var inBaseUnits = MassFlow.From(1.0, MassFlow.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megagramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.MegagramPerDay);
- AssertEx.EqualTolerance(MegagramsPerDayInOneGramPerSecond, (double)megagramperdayQuantity.Value, MegagramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.MegagramPerDay, megagramperdayQuantity.Unit);
-
- var megapoundperdayQuantity = grampersecond.ToUnit(MassFlowUnit.MegapoundPerDay);
- AssertEx.EqualTolerance(MegapoundsPerDayInOneGramPerSecond, (double)megapoundperdayQuantity.Value, MegapoundsPerDayTolerance);
- Assert.Equal(MassFlowUnit.MegapoundPerDay, megapoundperdayQuantity.Unit);
-
- var megapoundperhourQuantity = grampersecond.ToUnit(MassFlowUnit.MegapoundPerHour);
- AssertEx.EqualTolerance(MegapoundsPerHourInOneGramPerSecond, (double)megapoundperhourQuantity.Value, MegapoundsPerHourTolerance);
- Assert.Equal(MassFlowUnit.MegapoundPerHour, megapoundperhourQuantity.Unit);
-
- var megapoundperminuteQuantity = grampersecond.ToUnit(MassFlowUnit.MegapoundPerMinute);
- AssertEx.EqualTolerance(MegapoundsPerMinuteInOneGramPerSecond, (double)megapoundperminuteQuantity.Value, MegapoundsPerMinuteTolerance);
- Assert.Equal(MassFlowUnit.MegapoundPerMinute, megapoundperminuteQuantity.Unit);
-
- var megapoundpersecondQuantity = grampersecond.ToUnit(MassFlowUnit.MegapoundPerSecond);
- AssertEx.EqualTolerance(MegapoundsPerSecondInOneGramPerSecond, (double)megapoundpersecondQuantity.Value, MegapoundsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.MegapoundPerSecond, megapoundpersecondQuantity.Unit);
-
- var microgramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.MicrogramPerDay);
- AssertEx.EqualTolerance(MicrogramsPerDayInOneGramPerSecond, (double)microgramperdayQuantity.Value, MicrogramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.MicrogramPerDay, microgramperdayQuantity.Unit);
-
- var microgrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.MicrogramPerSecond);
- AssertEx.EqualTolerance(MicrogramsPerSecondInOneGramPerSecond, (double)microgrampersecondQuantity.Value, MicrogramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.MicrogramPerSecond, microgrampersecondQuantity.Unit);
-
- var milligramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.MilligramPerDay);
- AssertEx.EqualTolerance(MilligramsPerDayInOneGramPerSecond, (double)milligramperdayQuantity.Value, MilligramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.MilligramPerDay, milligramperdayQuantity.Unit);
-
- var milligrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.MilligramPerSecond);
- AssertEx.EqualTolerance(MilligramsPerSecondInOneGramPerSecond, (double)milligrampersecondQuantity.Value, MilligramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.MilligramPerSecond, milligrampersecondQuantity.Unit);
-
- var nanogramperdayQuantity = grampersecond.ToUnit(MassFlowUnit.NanogramPerDay);
- AssertEx.EqualTolerance(NanogramsPerDayInOneGramPerSecond, (double)nanogramperdayQuantity.Value, NanogramsPerDayTolerance);
- Assert.Equal(MassFlowUnit.NanogramPerDay, nanogramperdayQuantity.Unit);
-
- var nanogrampersecondQuantity = grampersecond.ToUnit(MassFlowUnit.NanogramPerSecond);
- AssertEx.EqualTolerance(NanogramsPerSecondInOneGramPerSecond, (double)nanogrampersecondQuantity.Value, NanogramsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.NanogramPerSecond, nanogrampersecondQuantity.Unit);
-
- var poundperdayQuantity = grampersecond.ToUnit(MassFlowUnit.PoundPerDay);
- AssertEx.EqualTolerance(PoundsPerDayInOneGramPerSecond, (double)poundperdayQuantity.Value, PoundsPerDayTolerance);
- Assert.Equal(MassFlowUnit.PoundPerDay, poundperdayQuantity.Unit);
-
- var poundperhourQuantity = grampersecond.ToUnit(MassFlowUnit.PoundPerHour);
- AssertEx.EqualTolerance(PoundsPerHourInOneGramPerSecond, (double)poundperhourQuantity.Value, PoundsPerHourTolerance);
- Assert.Equal(MassFlowUnit.PoundPerHour, poundperhourQuantity.Unit);
-
- var poundperminuteQuantity = grampersecond.ToUnit(MassFlowUnit.PoundPerMinute);
- AssertEx.EqualTolerance(PoundsPerMinuteInOneGramPerSecond, (double)poundperminuteQuantity.Value, PoundsPerMinuteTolerance);
- Assert.Equal(MassFlowUnit.PoundPerMinute, poundperminuteQuantity.Unit);
-
- var poundpersecondQuantity = grampersecond.ToUnit(MassFlowUnit.PoundPerSecond);
- AssertEx.EqualTolerance(PoundsPerSecondInOneGramPerSecond, (double)poundpersecondQuantity.Value, PoundsPerSecondTolerance);
- Assert.Equal(MassFlowUnit.PoundPerSecond, poundpersecondQuantity.Unit);
-
- var shorttonperhourQuantity = grampersecond.ToUnit(MassFlowUnit.ShortTonPerHour);
- AssertEx.EqualTolerance(ShortTonsPerHourInOneGramPerSecond, (double)shorttonperhourQuantity.Value, ShortTonsPerHourTolerance);
- Assert.Equal(MassFlowUnit.ShortTonPerHour, shorttonperhourQuantity.Unit);
-
- var tonneperdayQuantity = grampersecond.ToUnit(MassFlowUnit.TonnePerDay);
- AssertEx.EqualTolerance(TonnesPerDayInOneGramPerSecond, (double)tonneperdayQuantity.Value, TonnesPerDayTolerance);
- Assert.Equal(MassFlowUnit.TonnePerDay, tonneperdayQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneperhourQuantity = grampersecond.ToUnit(MassFlowUnit.TonnePerHour);
- AssertEx.EqualTolerance(TonnesPerHourInOneGramPerSecond, (double)tonneperhourQuantity.Value, TonnesPerHourTolerance);
- Assert.Equal(MassFlowUnit.TonnePerHour, tonneperhourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MassFlowUnit unit)
+ {
+ var quantity = MassFlow.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MassFlowUnit unit)
{
- var quantityInBaseUnit = MassFlow.FromGramsPerSecond(1).ToBaseUnit();
- Assert.Equal(MassFlow.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MassFlow.Units.FirstOrDefault(u => u != MassFlow.BaseUnit && u != MassFlowUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MassFlowUnit.Undefined)
+ fromUnit = MassFlow.BaseUnit;
+
+ var quantity = MassFlow.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MassFluxTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MassFluxTestsBase.g.cs
index 854af922ff..e4ee54db7d 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MassFluxTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MassFluxTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -65,6 +66,42 @@ public abstract partial class MassFluxTestsBase : QuantityTestsBase
protected virtual double KilogramsPerSecondPerSquareMillimeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MassFluxUnit unit)
+ {
+ return unit switch
+ {
+ MassFluxUnit.GramPerHourPerSquareCentimeter => (GramsPerHourPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, GramsPerHourPerSquareCentimeterTolerance),
+ MassFluxUnit.GramPerHourPerSquareMeter => (GramsPerHourPerSquareMeterInOneKilogramPerSecondPerSquareMeter, GramsPerHourPerSquareMeterTolerance),
+ MassFluxUnit.GramPerHourPerSquareMillimeter => (GramsPerHourPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, GramsPerHourPerSquareMillimeterTolerance),
+ MassFluxUnit.GramPerSecondPerSquareCentimeter => (GramsPerSecondPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, GramsPerSecondPerSquareCentimeterTolerance),
+ MassFluxUnit.GramPerSecondPerSquareMeter => (GramsPerSecondPerSquareMeterInOneKilogramPerSecondPerSquareMeter, GramsPerSecondPerSquareMeterTolerance),
+ MassFluxUnit.GramPerSecondPerSquareMillimeter => (GramsPerSecondPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, GramsPerSecondPerSquareMillimeterTolerance),
+ MassFluxUnit.KilogramPerHourPerSquareCentimeter => (KilogramsPerHourPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, KilogramsPerHourPerSquareCentimeterTolerance),
+ MassFluxUnit.KilogramPerHourPerSquareMeter => (KilogramsPerHourPerSquareMeterInOneKilogramPerSecondPerSquareMeter, KilogramsPerHourPerSquareMeterTolerance),
+ MassFluxUnit.KilogramPerHourPerSquareMillimeter => (KilogramsPerHourPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, KilogramsPerHourPerSquareMillimeterTolerance),
+ MassFluxUnit.KilogramPerSecondPerSquareCentimeter => (KilogramsPerSecondPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, KilogramsPerSecondPerSquareCentimeterTolerance),
+ MassFluxUnit.KilogramPerSecondPerSquareMeter => (KilogramsPerSecondPerSquareMeterInOneKilogramPerSecondPerSquareMeter, KilogramsPerSecondPerSquareMeterTolerance),
+ MassFluxUnit.KilogramPerSecondPerSquareMillimeter => (KilogramsPerSecondPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, KilogramsPerSecondPerSquareMillimeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MassFluxUnit.GramPerHourPerSquareCentimeter },
+ new object[] { MassFluxUnit.GramPerHourPerSquareMeter },
+ new object[] { MassFluxUnit.GramPerHourPerSquareMillimeter },
+ new object[] { MassFluxUnit.GramPerSecondPerSquareCentimeter },
+ new object[] { MassFluxUnit.GramPerSecondPerSquareMeter },
+ new object[] { MassFluxUnit.GramPerSecondPerSquareMillimeter },
+ new object[] { MassFluxUnit.KilogramPerHourPerSquareCentimeter },
+ new object[] { MassFluxUnit.KilogramPerHourPerSquareMeter },
+ new object[] { MassFluxUnit.KilogramPerHourPerSquareMillimeter },
+ new object[] { MassFluxUnit.KilogramPerSecondPerSquareCentimeter },
+ new object[] { MassFluxUnit.KilogramPerSecondPerSquareMeter },
+ new object[] { MassFluxUnit.KilogramPerSecondPerSquareMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -252,65 +289,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MassFluxUnit unit)
{
- var kilogrampersecondpersquaremeter = MassFlux.FromKilogramsPerSecondPerSquareMeter(1);
-
- var gramperhourpersquarecentimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.GramPerHourPerSquareCentimeter);
- AssertEx.EqualTolerance(GramsPerHourPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, (double)gramperhourpersquarecentimeterQuantity.Value, GramsPerHourPerSquareCentimeterTolerance);
- Assert.Equal(MassFluxUnit.GramPerHourPerSquareCentimeter, gramperhourpersquarecentimeterQuantity.Unit);
-
- var gramperhourpersquaremeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.GramPerHourPerSquareMeter);
- AssertEx.EqualTolerance(GramsPerHourPerSquareMeterInOneKilogramPerSecondPerSquareMeter, (double)gramperhourpersquaremeterQuantity.Value, GramsPerHourPerSquareMeterTolerance);
- Assert.Equal(MassFluxUnit.GramPerHourPerSquareMeter, gramperhourpersquaremeterQuantity.Unit);
-
- var gramperhourpersquaremillimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.GramPerHourPerSquareMillimeter);
- AssertEx.EqualTolerance(GramsPerHourPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, (double)gramperhourpersquaremillimeterQuantity.Value, GramsPerHourPerSquareMillimeterTolerance);
- Assert.Equal(MassFluxUnit.GramPerHourPerSquareMillimeter, gramperhourpersquaremillimeterQuantity.Unit);
-
- var grampersecondpersquarecentimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.GramPerSecondPerSquareCentimeter);
- AssertEx.EqualTolerance(GramsPerSecondPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, (double)grampersecondpersquarecentimeterQuantity.Value, GramsPerSecondPerSquareCentimeterTolerance);
- Assert.Equal(MassFluxUnit.GramPerSecondPerSquareCentimeter, grampersecondpersquarecentimeterQuantity.Unit);
-
- var grampersecondpersquaremeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.GramPerSecondPerSquareMeter);
- AssertEx.EqualTolerance(GramsPerSecondPerSquareMeterInOneKilogramPerSecondPerSquareMeter, (double)grampersecondpersquaremeterQuantity.Value, GramsPerSecondPerSquareMeterTolerance);
- Assert.Equal(MassFluxUnit.GramPerSecondPerSquareMeter, grampersecondpersquaremeterQuantity.Unit);
-
- var grampersecondpersquaremillimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.GramPerSecondPerSquareMillimeter);
- AssertEx.EqualTolerance(GramsPerSecondPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, (double)grampersecondpersquaremillimeterQuantity.Value, GramsPerSecondPerSquareMillimeterTolerance);
- Assert.Equal(MassFluxUnit.GramPerSecondPerSquareMillimeter, grampersecondpersquaremillimeterQuantity.Unit);
+ var inBaseUnits = MassFlux.From(1.0, MassFlux.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilogramperhourpersquarecentimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.KilogramPerHourPerSquareCentimeter);
- AssertEx.EqualTolerance(KilogramsPerHourPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, (double)kilogramperhourpersquarecentimeterQuantity.Value, KilogramsPerHourPerSquareCentimeterTolerance);
- Assert.Equal(MassFluxUnit.KilogramPerHourPerSquareCentimeter, kilogramperhourpersquarecentimeterQuantity.Unit);
-
- var kilogramperhourpersquaremeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.KilogramPerHourPerSquareMeter);
- AssertEx.EqualTolerance(KilogramsPerHourPerSquareMeterInOneKilogramPerSecondPerSquareMeter, (double)kilogramperhourpersquaremeterQuantity.Value, KilogramsPerHourPerSquareMeterTolerance);
- Assert.Equal(MassFluxUnit.KilogramPerHourPerSquareMeter, kilogramperhourpersquaremeterQuantity.Unit);
-
- var kilogramperhourpersquaremillimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.KilogramPerHourPerSquareMillimeter);
- AssertEx.EqualTolerance(KilogramsPerHourPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, (double)kilogramperhourpersquaremillimeterQuantity.Value, KilogramsPerHourPerSquareMillimeterTolerance);
- Assert.Equal(MassFluxUnit.KilogramPerHourPerSquareMillimeter, kilogramperhourpersquaremillimeterQuantity.Unit);
-
- var kilogrampersecondpersquarecentimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.KilogramPerSecondPerSquareCentimeter);
- AssertEx.EqualTolerance(KilogramsPerSecondPerSquareCentimeterInOneKilogramPerSecondPerSquareMeter, (double)kilogrampersecondpersquarecentimeterQuantity.Value, KilogramsPerSecondPerSquareCentimeterTolerance);
- Assert.Equal(MassFluxUnit.KilogramPerSecondPerSquareCentimeter, kilogrampersecondpersquarecentimeterQuantity.Unit);
-
- var kilogrampersecondpersquaremeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.KilogramPerSecondPerSquareMeter);
- AssertEx.EqualTolerance(KilogramsPerSecondPerSquareMeterInOneKilogramPerSecondPerSquareMeter, (double)kilogrampersecondpersquaremeterQuantity.Value, KilogramsPerSecondPerSquareMeterTolerance);
- Assert.Equal(MassFluxUnit.KilogramPerSecondPerSquareMeter, kilogrampersecondpersquaremeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var kilogrampersecondpersquaremillimeterQuantity = kilogrampersecondpersquaremeter.ToUnit(MassFluxUnit.KilogramPerSecondPerSquareMillimeter);
- AssertEx.EqualTolerance(KilogramsPerSecondPerSquareMillimeterInOneKilogramPerSecondPerSquareMeter, (double)kilogrampersecondpersquaremillimeterQuantity.Value, KilogramsPerSecondPerSquareMillimeterTolerance);
- Assert.Equal(MassFluxUnit.KilogramPerSecondPerSquareMillimeter, kilogrampersecondpersquaremillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MassFluxUnit unit)
+ {
+ var quantity = MassFlux.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MassFluxUnit unit)
{
- var quantityInBaseUnit = MassFlux.FromKilogramsPerSecondPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(MassFlux.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MassFlux.Units.FirstOrDefault(u => u != MassFlux.BaseUnit && u != MassFluxUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MassFluxUnit.Undefined)
+ fromUnit = MassFlux.BaseUnit;
+
+ var quantity = MassFlux.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MassFractionTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MassFractionTestsBase.g.cs
index 7644821a54..2e1f094d53 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MassFractionTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MassFractionTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -89,6 +90,66 @@ public abstract partial class MassFractionTestsBase : QuantityTestsBase
protected virtual double PercentTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MassFractionUnit unit)
+ {
+ return unit switch
+ {
+ MassFractionUnit.CentigramPerGram => (CentigramsPerGramInOneDecimalFraction, CentigramsPerGramTolerance),
+ MassFractionUnit.CentigramPerKilogram => (CentigramsPerKilogramInOneDecimalFraction, CentigramsPerKilogramTolerance),
+ MassFractionUnit.DecagramPerGram => (DecagramsPerGramInOneDecimalFraction, DecagramsPerGramTolerance),
+ MassFractionUnit.DecagramPerKilogram => (DecagramsPerKilogramInOneDecimalFraction, DecagramsPerKilogramTolerance),
+ MassFractionUnit.DecigramPerGram => (DecigramsPerGramInOneDecimalFraction, DecigramsPerGramTolerance),
+ MassFractionUnit.DecigramPerKilogram => (DecigramsPerKilogramInOneDecimalFraction, DecigramsPerKilogramTolerance),
+ MassFractionUnit.DecimalFraction => (DecimalFractionsInOneDecimalFraction, DecimalFractionsTolerance),
+ MassFractionUnit.GramPerGram => (GramsPerGramInOneDecimalFraction, GramsPerGramTolerance),
+ MassFractionUnit.GramPerKilogram => (GramsPerKilogramInOneDecimalFraction, GramsPerKilogramTolerance),
+ MassFractionUnit.HectogramPerGram => (HectogramsPerGramInOneDecimalFraction, HectogramsPerGramTolerance),
+ MassFractionUnit.HectogramPerKilogram => (HectogramsPerKilogramInOneDecimalFraction, HectogramsPerKilogramTolerance),
+ MassFractionUnit.KilogramPerGram => (KilogramsPerGramInOneDecimalFraction, KilogramsPerGramTolerance),
+ MassFractionUnit.KilogramPerKilogram => (KilogramsPerKilogramInOneDecimalFraction, KilogramsPerKilogramTolerance),
+ MassFractionUnit.MicrogramPerGram => (MicrogramsPerGramInOneDecimalFraction, MicrogramsPerGramTolerance),
+ MassFractionUnit.MicrogramPerKilogram => (MicrogramsPerKilogramInOneDecimalFraction, MicrogramsPerKilogramTolerance),
+ MassFractionUnit.MilligramPerGram => (MilligramsPerGramInOneDecimalFraction, MilligramsPerGramTolerance),
+ MassFractionUnit.MilligramPerKilogram => (MilligramsPerKilogramInOneDecimalFraction, MilligramsPerKilogramTolerance),
+ MassFractionUnit.NanogramPerGram => (NanogramsPerGramInOneDecimalFraction, NanogramsPerGramTolerance),
+ MassFractionUnit.NanogramPerKilogram => (NanogramsPerKilogramInOneDecimalFraction, NanogramsPerKilogramTolerance),
+ MassFractionUnit.PartPerBillion => (PartsPerBillionInOneDecimalFraction, PartsPerBillionTolerance),
+ MassFractionUnit.PartPerMillion => (PartsPerMillionInOneDecimalFraction, PartsPerMillionTolerance),
+ MassFractionUnit.PartPerThousand => (PartsPerThousandInOneDecimalFraction, PartsPerThousandTolerance),
+ MassFractionUnit.PartPerTrillion => (PartsPerTrillionInOneDecimalFraction, PartsPerTrillionTolerance),
+ MassFractionUnit.Percent => (PercentInOneDecimalFraction, PercentTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MassFractionUnit.CentigramPerGram },
+ new object[] { MassFractionUnit.CentigramPerKilogram },
+ new object[] { MassFractionUnit.DecagramPerGram },
+ new object[] { MassFractionUnit.DecagramPerKilogram },
+ new object[] { MassFractionUnit.DecigramPerGram },
+ new object[] { MassFractionUnit.DecigramPerKilogram },
+ new object[] { MassFractionUnit.DecimalFraction },
+ new object[] { MassFractionUnit.GramPerGram },
+ new object[] { MassFractionUnit.GramPerKilogram },
+ new object[] { MassFractionUnit.HectogramPerGram },
+ new object[] { MassFractionUnit.HectogramPerKilogram },
+ new object[] { MassFractionUnit.KilogramPerGram },
+ new object[] { MassFractionUnit.KilogramPerKilogram },
+ new object[] { MassFractionUnit.MicrogramPerGram },
+ new object[] { MassFractionUnit.MicrogramPerKilogram },
+ new object[] { MassFractionUnit.MilligramPerGram },
+ new object[] { MassFractionUnit.MilligramPerKilogram },
+ new object[] { MassFractionUnit.NanogramPerGram },
+ new object[] { MassFractionUnit.NanogramPerKilogram },
+ new object[] { MassFractionUnit.PartPerBillion },
+ new object[] { MassFractionUnit.PartPerMillion },
+ new object[] { MassFractionUnit.PartPerThousand },
+ new object[] { MassFractionUnit.PartPerTrillion },
+ new object[] { MassFractionUnit.Percent },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -348,113 +409,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MassFractionUnit unit)
{
- var decimalfraction = MassFraction.FromDecimalFractions(1);
-
- var centigrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.CentigramPerGram);
- AssertEx.EqualTolerance(CentigramsPerGramInOneDecimalFraction, (double)centigrampergramQuantity.Value, CentigramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.CentigramPerGram, centigrampergramQuantity.Unit);
-
- var centigramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.CentigramPerKilogram);
- AssertEx.EqualTolerance(CentigramsPerKilogramInOneDecimalFraction, (double)centigramperkilogramQuantity.Value, CentigramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.CentigramPerKilogram, centigramperkilogramQuantity.Unit);
-
- var decagrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.DecagramPerGram);
- AssertEx.EqualTolerance(DecagramsPerGramInOneDecimalFraction, (double)decagrampergramQuantity.Value, DecagramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.DecagramPerGram, decagrampergramQuantity.Unit);
-
- var decagramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.DecagramPerKilogram);
- AssertEx.EqualTolerance(DecagramsPerKilogramInOneDecimalFraction, (double)decagramperkilogramQuantity.Value, DecagramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.DecagramPerKilogram, decagramperkilogramQuantity.Unit);
-
- var decigrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.DecigramPerGram);
- AssertEx.EqualTolerance(DecigramsPerGramInOneDecimalFraction, (double)decigrampergramQuantity.Value, DecigramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.DecigramPerGram, decigrampergramQuantity.Unit);
-
- var decigramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.DecigramPerKilogram);
- AssertEx.EqualTolerance(DecigramsPerKilogramInOneDecimalFraction, (double)decigramperkilogramQuantity.Value, DecigramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.DecigramPerKilogram, decigramperkilogramQuantity.Unit);
-
- var decimalfractionQuantity = decimalfraction.ToUnit(MassFractionUnit.DecimalFraction);
- AssertEx.EqualTolerance(DecimalFractionsInOneDecimalFraction, (double)decimalfractionQuantity.Value, DecimalFractionsTolerance);
- Assert.Equal(MassFractionUnit.DecimalFraction, decimalfractionQuantity.Unit);
-
- var grampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.GramPerGram);
- AssertEx.EqualTolerance(GramsPerGramInOneDecimalFraction, (double)grampergramQuantity.Value, GramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.GramPerGram, grampergramQuantity.Unit);
-
- var gramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.GramPerKilogram);
- AssertEx.EqualTolerance(GramsPerKilogramInOneDecimalFraction, (double)gramperkilogramQuantity.Value, GramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.GramPerKilogram, gramperkilogramQuantity.Unit);
-
- var hectogrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.HectogramPerGram);
- AssertEx.EqualTolerance(HectogramsPerGramInOneDecimalFraction, (double)hectogrampergramQuantity.Value, HectogramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.HectogramPerGram, hectogrampergramQuantity.Unit);
-
- var hectogramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.HectogramPerKilogram);
- AssertEx.EqualTolerance(HectogramsPerKilogramInOneDecimalFraction, (double)hectogramperkilogramQuantity.Value, HectogramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.HectogramPerKilogram, hectogramperkilogramQuantity.Unit);
-
- var kilogrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.KilogramPerGram);
- AssertEx.EqualTolerance(KilogramsPerGramInOneDecimalFraction, (double)kilogrampergramQuantity.Value, KilogramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.KilogramPerGram, kilogrampergramQuantity.Unit);
+ var inBaseUnits = MassFraction.From(1.0, MassFraction.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilogramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.KilogramPerKilogram);
- AssertEx.EqualTolerance(KilogramsPerKilogramInOneDecimalFraction, (double)kilogramperkilogramQuantity.Value, KilogramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.KilogramPerKilogram, kilogramperkilogramQuantity.Unit);
-
- var microgrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.MicrogramPerGram);
- AssertEx.EqualTolerance(MicrogramsPerGramInOneDecimalFraction, (double)microgrampergramQuantity.Value, MicrogramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.MicrogramPerGram, microgrampergramQuantity.Unit);
-
- var microgramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.MicrogramPerKilogram);
- AssertEx.EqualTolerance(MicrogramsPerKilogramInOneDecimalFraction, (double)microgramperkilogramQuantity.Value, MicrogramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.MicrogramPerKilogram, microgramperkilogramQuantity.Unit);
-
- var milligrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.MilligramPerGram);
- AssertEx.EqualTolerance(MilligramsPerGramInOneDecimalFraction, (double)milligrampergramQuantity.Value, MilligramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.MilligramPerGram, milligrampergramQuantity.Unit);
-
- var milligramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.MilligramPerKilogram);
- AssertEx.EqualTolerance(MilligramsPerKilogramInOneDecimalFraction, (double)milligramperkilogramQuantity.Value, MilligramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.MilligramPerKilogram, milligramperkilogramQuantity.Unit);
-
- var nanogrampergramQuantity = decimalfraction.ToUnit(MassFractionUnit.NanogramPerGram);
- AssertEx.EqualTolerance(NanogramsPerGramInOneDecimalFraction, (double)nanogrampergramQuantity.Value, NanogramsPerGramTolerance);
- Assert.Equal(MassFractionUnit.NanogramPerGram, nanogrampergramQuantity.Unit);
-
- var nanogramperkilogramQuantity = decimalfraction.ToUnit(MassFractionUnit.NanogramPerKilogram);
- AssertEx.EqualTolerance(NanogramsPerKilogramInOneDecimalFraction, (double)nanogramperkilogramQuantity.Value, NanogramsPerKilogramTolerance);
- Assert.Equal(MassFractionUnit.NanogramPerKilogram, nanogramperkilogramQuantity.Unit);
-
- var partperbillionQuantity = decimalfraction.ToUnit(MassFractionUnit.PartPerBillion);
- AssertEx.EqualTolerance(PartsPerBillionInOneDecimalFraction, (double)partperbillionQuantity.Value, PartsPerBillionTolerance);
- Assert.Equal(MassFractionUnit.PartPerBillion, partperbillionQuantity.Unit);
-
- var partpermillionQuantity = decimalfraction.ToUnit(MassFractionUnit.PartPerMillion);
- AssertEx.EqualTolerance(PartsPerMillionInOneDecimalFraction, (double)partpermillionQuantity.Value, PartsPerMillionTolerance);
- Assert.Equal(MassFractionUnit.PartPerMillion, partpermillionQuantity.Unit);
-
- var partperthousandQuantity = decimalfraction.ToUnit(MassFractionUnit.PartPerThousand);
- AssertEx.EqualTolerance(PartsPerThousandInOneDecimalFraction, (double)partperthousandQuantity.Value, PartsPerThousandTolerance);
- Assert.Equal(MassFractionUnit.PartPerThousand, partperthousandQuantity.Unit);
-
- var partpertrillionQuantity = decimalfraction.ToUnit(MassFractionUnit.PartPerTrillion);
- AssertEx.EqualTolerance(PartsPerTrillionInOneDecimalFraction, (double)partpertrillionQuantity.Value, PartsPerTrillionTolerance);
- Assert.Equal(MassFractionUnit.PartPerTrillion, partpertrillionQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var percentQuantity = decimalfraction.ToUnit(MassFractionUnit.Percent);
- AssertEx.EqualTolerance(PercentInOneDecimalFraction, (double)percentQuantity.Value, PercentTolerance);
- Assert.Equal(MassFractionUnit.Percent, percentQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MassFractionUnit unit)
+ {
+ var quantity = MassFraction.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MassFractionUnit unit)
{
- var quantityInBaseUnit = MassFraction.FromDecimalFractions(1).ToBaseUnit();
- Assert.Equal(MassFraction.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MassFraction.Units.FirstOrDefault(u => u != MassFraction.BaseUnit && u != MassFractionUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MassFractionUnit.Undefined)
+ fromUnit = MassFraction.BaseUnit;
+
+ var quantity = MassFraction.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MassMomentOfInertiaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MassMomentOfInertiaTestsBase.g.cs
index ff9b74d7df..f4b8bb0263 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MassMomentOfInertiaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MassMomentOfInertiaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -97,6 +98,74 @@ public abstract partial class MassMomentOfInertiaTestsBase : QuantityTestsBase
protected virtual double TonneSquareMilimetersTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MassMomentOfInertiaUnit unit)
+ {
+ return unit switch
+ {
+ MassMomentOfInertiaUnit.GramSquareCentimeter => (GramSquareCentimetersInOneKilogramSquareMeter, GramSquareCentimetersTolerance),
+ MassMomentOfInertiaUnit.GramSquareDecimeter => (GramSquareDecimetersInOneKilogramSquareMeter, GramSquareDecimetersTolerance),
+ MassMomentOfInertiaUnit.GramSquareMeter => (GramSquareMetersInOneKilogramSquareMeter, GramSquareMetersTolerance),
+ MassMomentOfInertiaUnit.GramSquareMillimeter => (GramSquareMillimetersInOneKilogramSquareMeter, GramSquareMillimetersTolerance),
+ MassMomentOfInertiaUnit.KilogramSquareCentimeter => (KilogramSquareCentimetersInOneKilogramSquareMeter, KilogramSquareCentimetersTolerance),
+ MassMomentOfInertiaUnit.KilogramSquareDecimeter => (KilogramSquareDecimetersInOneKilogramSquareMeter, KilogramSquareDecimetersTolerance),
+ MassMomentOfInertiaUnit.KilogramSquareMeter => (KilogramSquareMetersInOneKilogramSquareMeter, KilogramSquareMetersTolerance),
+ MassMomentOfInertiaUnit.KilogramSquareMillimeter => (KilogramSquareMillimetersInOneKilogramSquareMeter, KilogramSquareMillimetersTolerance),
+ MassMomentOfInertiaUnit.KilotonneSquareCentimeter => (KilotonneSquareCentimetersInOneKilogramSquareMeter, KilotonneSquareCentimetersTolerance),
+ MassMomentOfInertiaUnit.KilotonneSquareDecimeter => (KilotonneSquareDecimetersInOneKilogramSquareMeter, KilotonneSquareDecimetersTolerance),
+ MassMomentOfInertiaUnit.KilotonneSquareMeter => (KilotonneSquareMetersInOneKilogramSquareMeter, KilotonneSquareMetersTolerance),
+ MassMomentOfInertiaUnit.KilotonneSquareMilimeter => (KilotonneSquareMilimetersInOneKilogramSquareMeter, KilotonneSquareMilimetersTolerance),
+ MassMomentOfInertiaUnit.MegatonneSquareCentimeter => (MegatonneSquareCentimetersInOneKilogramSquareMeter, MegatonneSquareCentimetersTolerance),
+ MassMomentOfInertiaUnit.MegatonneSquareDecimeter => (MegatonneSquareDecimetersInOneKilogramSquareMeter, MegatonneSquareDecimetersTolerance),
+ MassMomentOfInertiaUnit.MegatonneSquareMeter => (MegatonneSquareMetersInOneKilogramSquareMeter, MegatonneSquareMetersTolerance),
+ MassMomentOfInertiaUnit.MegatonneSquareMilimeter => (MegatonneSquareMilimetersInOneKilogramSquareMeter, MegatonneSquareMilimetersTolerance),
+ MassMomentOfInertiaUnit.MilligramSquareCentimeter => (MilligramSquareCentimetersInOneKilogramSquareMeter, MilligramSquareCentimetersTolerance),
+ MassMomentOfInertiaUnit.MilligramSquareDecimeter => (MilligramSquareDecimetersInOneKilogramSquareMeter, MilligramSquareDecimetersTolerance),
+ MassMomentOfInertiaUnit.MilligramSquareMeter => (MilligramSquareMetersInOneKilogramSquareMeter, MilligramSquareMetersTolerance),
+ MassMomentOfInertiaUnit.MilligramSquareMillimeter => (MilligramSquareMillimetersInOneKilogramSquareMeter, MilligramSquareMillimetersTolerance),
+ MassMomentOfInertiaUnit.PoundSquareFoot => (PoundSquareFeetInOneKilogramSquareMeter, PoundSquareFeetTolerance),
+ MassMomentOfInertiaUnit.PoundSquareInch => (PoundSquareInchesInOneKilogramSquareMeter, PoundSquareInchesTolerance),
+ MassMomentOfInertiaUnit.SlugSquareFoot => (SlugSquareFeetInOneKilogramSquareMeter, SlugSquareFeetTolerance),
+ MassMomentOfInertiaUnit.SlugSquareInch => (SlugSquareInchesInOneKilogramSquareMeter, SlugSquareInchesTolerance),
+ MassMomentOfInertiaUnit.TonneSquareCentimeter => (TonneSquareCentimetersInOneKilogramSquareMeter, TonneSquareCentimetersTolerance),
+ MassMomentOfInertiaUnit.TonneSquareDecimeter => (TonneSquareDecimetersInOneKilogramSquareMeter, TonneSquareDecimetersTolerance),
+ MassMomentOfInertiaUnit.TonneSquareMeter => (TonneSquareMetersInOneKilogramSquareMeter, TonneSquareMetersTolerance),
+ MassMomentOfInertiaUnit.TonneSquareMilimeter => (TonneSquareMilimetersInOneKilogramSquareMeter, TonneSquareMilimetersTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MassMomentOfInertiaUnit.GramSquareCentimeter },
+ new object[] { MassMomentOfInertiaUnit.GramSquareDecimeter },
+ new object[] { MassMomentOfInertiaUnit.GramSquareMeter },
+ new object[] { MassMomentOfInertiaUnit.GramSquareMillimeter },
+ new object[] { MassMomentOfInertiaUnit.KilogramSquareCentimeter },
+ new object[] { MassMomentOfInertiaUnit.KilogramSquareDecimeter },
+ new object[] { MassMomentOfInertiaUnit.KilogramSquareMeter },
+ new object[] { MassMomentOfInertiaUnit.KilogramSquareMillimeter },
+ new object[] { MassMomentOfInertiaUnit.KilotonneSquareCentimeter },
+ new object[] { MassMomentOfInertiaUnit.KilotonneSquareDecimeter },
+ new object[] { MassMomentOfInertiaUnit.KilotonneSquareMeter },
+ new object[] { MassMomentOfInertiaUnit.KilotonneSquareMilimeter },
+ new object[] { MassMomentOfInertiaUnit.MegatonneSquareCentimeter },
+ new object[] { MassMomentOfInertiaUnit.MegatonneSquareDecimeter },
+ new object[] { MassMomentOfInertiaUnit.MegatonneSquareMeter },
+ new object[] { MassMomentOfInertiaUnit.MegatonneSquareMilimeter },
+ new object[] { MassMomentOfInertiaUnit.MilligramSquareCentimeter },
+ new object[] { MassMomentOfInertiaUnit.MilligramSquareDecimeter },
+ new object[] { MassMomentOfInertiaUnit.MilligramSquareMeter },
+ new object[] { MassMomentOfInertiaUnit.MilligramSquareMillimeter },
+ new object[] { MassMomentOfInertiaUnit.PoundSquareFoot },
+ new object[] { MassMomentOfInertiaUnit.PoundSquareInch },
+ new object[] { MassMomentOfInertiaUnit.SlugSquareFoot },
+ new object[] { MassMomentOfInertiaUnit.SlugSquareInch },
+ new object[] { MassMomentOfInertiaUnit.TonneSquareCentimeter },
+ new object[] { MassMomentOfInertiaUnit.TonneSquareDecimeter },
+ new object[] { MassMomentOfInertiaUnit.TonneSquareMeter },
+ new object[] { MassMomentOfInertiaUnit.TonneSquareMilimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -380,129 +449,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MassMomentOfInertiaUnit unit)
{
- var kilogramsquaremeter = MassMomentOfInertia.FromKilogramSquareMeters(1);
-
- var gramsquarecentimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.GramSquareCentimeter);
- AssertEx.EqualTolerance(GramSquareCentimetersInOneKilogramSquareMeter, (double)gramsquarecentimeterQuantity.Value, GramSquareCentimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.GramSquareCentimeter, gramsquarecentimeterQuantity.Unit);
-
- var gramsquaredecimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.GramSquareDecimeter);
- AssertEx.EqualTolerance(GramSquareDecimetersInOneKilogramSquareMeter, (double)gramsquaredecimeterQuantity.Value, GramSquareDecimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.GramSquareDecimeter, gramsquaredecimeterQuantity.Unit);
-
- var gramsquaremeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.GramSquareMeter);
- AssertEx.EqualTolerance(GramSquareMetersInOneKilogramSquareMeter, (double)gramsquaremeterQuantity.Value, GramSquareMetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.GramSquareMeter, gramsquaremeterQuantity.Unit);
-
- var gramsquaremillimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.GramSquareMillimeter);
- AssertEx.EqualTolerance(GramSquareMillimetersInOneKilogramSquareMeter, (double)gramsquaremillimeterQuantity.Value, GramSquareMillimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.GramSquareMillimeter, gramsquaremillimeterQuantity.Unit);
-
- var kilogramsquarecentimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilogramSquareCentimeter);
- AssertEx.EqualTolerance(KilogramSquareCentimetersInOneKilogramSquareMeter, (double)kilogramsquarecentimeterQuantity.Value, KilogramSquareCentimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilogramSquareCentimeter, kilogramsquarecentimeterQuantity.Unit);
-
- var kilogramsquaredecimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilogramSquareDecimeter);
- AssertEx.EqualTolerance(KilogramSquareDecimetersInOneKilogramSquareMeter, (double)kilogramsquaredecimeterQuantity.Value, KilogramSquareDecimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilogramSquareDecimeter, kilogramsquaredecimeterQuantity.Unit);
-
- var kilogramsquaremeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilogramSquareMeter);
- AssertEx.EqualTolerance(KilogramSquareMetersInOneKilogramSquareMeter, (double)kilogramsquaremeterQuantity.Value, KilogramSquareMetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilogramSquareMeter, kilogramsquaremeterQuantity.Unit);
-
- var kilogramsquaremillimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilogramSquareMillimeter);
- AssertEx.EqualTolerance(KilogramSquareMillimetersInOneKilogramSquareMeter, (double)kilogramsquaremillimeterQuantity.Value, KilogramSquareMillimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilogramSquareMillimeter, kilogramsquaremillimeterQuantity.Unit);
-
- var kilotonnesquarecentimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilotonneSquareCentimeter);
- AssertEx.EqualTolerance(KilotonneSquareCentimetersInOneKilogramSquareMeter, (double)kilotonnesquarecentimeterQuantity.Value, KilotonneSquareCentimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilotonneSquareCentimeter, kilotonnesquarecentimeterQuantity.Unit);
-
- var kilotonnesquaredecimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilotonneSquareDecimeter);
- AssertEx.EqualTolerance(KilotonneSquareDecimetersInOneKilogramSquareMeter, (double)kilotonnesquaredecimeterQuantity.Value, KilotonneSquareDecimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilotonneSquareDecimeter, kilotonnesquaredecimeterQuantity.Unit);
-
- var kilotonnesquaremeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilotonneSquareMeter);
- AssertEx.EqualTolerance(KilotonneSquareMetersInOneKilogramSquareMeter, (double)kilotonnesquaremeterQuantity.Value, KilotonneSquareMetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilotonneSquareMeter, kilotonnesquaremeterQuantity.Unit);
-
- var kilotonnesquaremilimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.KilotonneSquareMilimeter);
- AssertEx.EqualTolerance(KilotonneSquareMilimetersInOneKilogramSquareMeter, (double)kilotonnesquaremilimeterQuantity.Value, KilotonneSquareMilimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.KilotonneSquareMilimeter, kilotonnesquaremilimeterQuantity.Unit);
-
- var megatonnesquarecentimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MegatonneSquareCentimeter);
- AssertEx.EqualTolerance(MegatonneSquareCentimetersInOneKilogramSquareMeter, (double)megatonnesquarecentimeterQuantity.Value, MegatonneSquareCentimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MegatonneSquareCentimeter, megatonnesquarecentimeterQuantity.Unit);
-
- var megatonnesquaredecimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MegatonneSquareDecimeter);
- AssertEx.EqualTolerance(MegatonneSquareDecimetersInOneKilogramSquareMeter, (double)megatonnesquaredecimeterQuantity.Value, MegatonneSquareDecimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MegatonneSquareDecimeter, megatonnesquaredecimeterQuantity.Unit);
+ var inBaseUnits = MassMomentOfInertia.From(1.0, MassMomentOfInertia.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megatonnesquaremeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MegatonneSquareMeter);
- AssertEx.EqualTolerance(MegatonneSquareMetersInOneKilogramSquareMeter, (double)megatonnesquaremeterQuantity.Value, MegatonneSquareMetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MegatonneSquareMeter, megatonnesquaremeterQuantity.Unit);
-
- var megatonnesquaremilimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MegatonneSquareMilimeter);
- AssertEx.EqualTolerance(MegatonneSquareMilimetersInOneKilogramSquareMeter, (double)megatonnesquaremilimeterQuantity.Value, MegatonneSquareMilimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MegatonneSquareMilimeter, megatonnesquaremilimeterQuantity.Unit);
-
- var milligramsquarecentimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MilligramSquareCentimeter);
- AssertEx.EqualTolerance(MilligramSquareCentimetersInOneKilogramSquareMeter, (double)milligramsquarecentimeterQuantity.Value, MilligramSquareCentimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MilligramSquareCentimeter, milligramsquarecentimeterQuantity.Unit);
-
- var milligramsquaredecimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MilligramSquareDecimeter);
- AssertEx.EqualTolerance(MilligramSquareDecimetersInOneKilogramSquareMeter, (double)milligramsquaredecimeterQuantity.Value, MilligramSquareDecimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MilligramSquareDecimeter, milligramsquaredecimeterQuantity.Unit);
-
- var milligramsquaremeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MilligramSquareMeter);
- AssertEx.EqualTolerance(MilligramSquareMetersInOneKilogramSquareMeter, (double)milligramsquaremeterQuantity.Value, MilligramSquareMetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MilligramSquareMeter, milligramsquaremeterQuantity.Unit);
-
- var milligramsquaremillimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.MilligramSquareMillimeter);
- AssertEx.EqualTolerance(MilligramSquareMillimetersInOneKilogramSquareMeter, (double)milligramsquaremillimeterQuantity.Value, MilligramSquareMillimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.MilligramSquareMillimeter, milligramsquaremillimeterQuantity.Unit);
-
- var poundsquarefootQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.PoundSquareFoot);
- AssertEx.EqualTolerance(PoundSquareFeetInOneKilogramSquareMeter, (double)poundsquarefootQuantity.Value, PoundSquareFeetTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.PoundSquareFoot, poundsquarefootQuantity.Unit);
-
- var poundsquareinchQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.PoundSquareInch);
- AssertEx.EqualTolerance(PoundSquareInchesInOneKilogramSquareMeter, (double)poundsquareinchQuantity.Value, PoundSquareInchesTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.PoundSquareInch, poundsquareinchQuantity.Unit);
-
- var slugsquarefootQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.SlugSquareFoot);
- AssertEx.EqualTolerance(SlugSquareFeetInOneKilogramSquareMeter, (double)slugsquarefootQuantity.Value, SlugSquareFeetTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.SlugSquareFoot, slugsquarefootQuantity.Unit);
-
- var slugsquareinchQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.SlugSquareInch);
- AssertEx.EqualTolerance(SlugSquareInchesInOneKilogramSquareMeter, (double)slugsquareinchQuantity.Value, SlugSquareInchesTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.SlugSquareInch, slugsquareinchQuantity.Unit);
-
- var tonnesquarecentimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.TonneSquareCentimeter);
- AssertEx.EqualTolerance(TonneSquareCentimetersInOneKilogramSquareMeter, (double)tonnesquarecentimeterQuantity.Value, TonneSquareCentimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.TonneSquareCentimeter, tonnesquarecentimeterQuantity.Unit);
-
- var tonnesquaredecimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.TonneSquareDecimeter);
- AssertEx.EqualTolerance(TonneSquareDecimetersInOneKilogramSquareMeter, (double)tonnesquaredecimeterQuantity.Value, TonneSquareDecimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.TonneSquareDecimeter, tonnesquaredecimeterQuantity.Unit);
-
- var tonnesquaremeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.TonneSquareMeter);
- AssertEx.EqualTolerance(TonneSquareMetersInOneKilogramSquareMeter, (double)tonnesquaremeterQuantity.Value, TonneSquareMetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.TonneSquareMeter, tonnesquaremeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonnesquaremilimeterQuantity = kilogramsquaremeter.ToUnit(MassMomentOfInertiaUnit.TonneSquareMilimeter);
- AssertEx.EqualTolerance(TonneSquareMilimetersInOneKilogramSquareMeter, (double)tonnesquaremilimeterQuantity.Value, TonneSquareMilimetersTolerance);
- Assert.Equal(MassMomentOfInertiaUnit.TonneSquareMilimeter, tonnesquaremilimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MassMomentOfInertiaUnit unit)
+ {
+ var quantity = MassMomentOfInertia.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MassMomentOfInertiaUnit unit)
{
- var quantityInBaseUnit = MassMomentOfInertia.FromKilogramSquareMeters(1).ToBaseUnit();
- Assert.Equal(MassMomentOfInertia.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MassMomentOfInertia.Units.FirstOrDefault(u => u != MassMomentOfInertia.BaseUnit && u != MassMomentOfInertiaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MassMomentOfInertiaUnit.Undefined)
+ fromUnit = MassMomentOfInertia.BaseUnit;
+
+ var quantity = MassMomentOfInertia.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MassTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MassTestsBase.g.cs
index 0d28d6724e..ae7364df1b 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MassTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MassTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -91,6 +92,68 @@ public abstract partial class MassTestsBase : QuantityTestsBase
protected virtual double TonnesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MassUnit unit)
+ {
+ return unit switch
+ {
+ MassUnit.Centigram => (CentigramsInOneKilogram, CentigramsTolerance),
+ MassUnit.Decagram => (DecagramsInOneKilogram, DecagramsTolerance),
+ MassUnit.Decigram => (DecigramsInOneKilogram, DecigramsTolerance),
+ MassUnit.EarthMass => (EarthMassesInOneKilogram, EarthMassesTolerance),
+ MassUnit.Grain => (GrainsInOneKilogram, GrainsTolerance),
+ MassUnit.Gram => (GramsInOneKilogram, GramsTolerance),
+ MassUnit.Hectogram => (HectogramsInOneKilogram, HectogramsTolerance),
+ MassUnit.Kilogram => (KilogramsInOneKilogram, KilogramsTolerance),
+ MassUnit.Kilopound => (KilopoundsInOneKilogram, KilopoundsTolerance),
+ MassUnit.Kilotonne => (KilotonnesInOneKilogram, KilotonnesTolerance),
+ MassUnit.LongHundredweight => (LongHundredweightInOneKilogram, LongHundredweightTolerance),
+ MassUnit.LongTon => (LongTonsInOneKilogram, LongTonsTolerance),
+ MassUnit.Megapound => (MegapoundsInOneKilogram, MegapoundsTolerance),
+ MassUnit.Megatonne => (MegatonnesInOneKilogram, MegatonnesTolerance),
+ MassUnit.Microgram => (MicrogramsInOneKilogram, MicrogramsTolerance),
+ MassUnit.Milligram => (MilligramsInOneKilogram, MilligramsTolerance),
+ MassUnit.Nanogram => (NanogramsInOneKilogram, NanogramsTolerance),
+ MassUnit.Ounce => (OuncesInOneKilogram, OuncesTolerance),
+ MassUnit.Pound => (PoundsInOneKilogram, PoundsTolerance),
+ MassUnit.ShortHundredweight => (ShortHundredweightInOneKilogram, ShortHundredweightTolerance),
+ MassUnit.ShortTon => (ShortTonsInOneKilogram, ShortTonsTolerance),
+ MassUnit.Slug => (SlugsInOneKilogram, SlugsTolerance),
+ MassUnit.SolarMass => (SolarMassesInOneKilogram, SolarMassesTolerance),
+ MassUnit.Stone => (StoneInOneKilogram, StoneTolerance),
+ MassUnit.Tonne => (TonnesInOneKilogram, TonnesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MassUnit.Centigram },
+ new object[] { MassUnit.Decagram },
+ new object[] { MassUnit.Decigram },
+ new object[] { MassUnit.EarthMass },
+ new object[] { MassUnit.Grain },
+ new object[] { MassUnit.Gram },
+ new object[] { MassUnit.Hectogram },
+ new object[] { MassUnit.Kilogram },
+ new object[] { MassUnit.Kilopound },
+ new object[] { MassUnit.Kilotonne },
+ new object[] { MassUnit.LongHundredweight },
+ new object[] { MassUnit.LongTon },
+ new object[] { MassUnit.Megapound },
+ new object[] { MassUnit.Megatonne },
+ new object[] { MassUnit.Microgram },
+ new object[] { MassUnit.Milligram },
+ new object[] { MassUnit.Nanogram },
+ new object[] { MassUnit.Ounce },
+ new object[] { MassUnit.Pound },
+ new object[] { MassUnit.ShortHundredweight },
+ new object[] { MassUnit.ShortTon },
+ new object[] { MassUnit.Slug },
+ new object[] { MassUnit.SolarMass },
+ new object[] { MassUnit.Stone },
+ new object[] { MassUnit.Tonne },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -356,117 +419,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MassUnit unit)
{
- var kilogram = Mass.FromKilograms(1);
-
- var centigramQuantity = kilogram.ToUnit(MassUnit.Centigram);
- AssertEx.EqualTolerance(CentigramsInOneKilogram, (double)centigramQuantity.Value, CentigramsTolerance);
- Assert.Equal(MassUnit.Centigram, centigramQuantity.Unit);
-
- var decagramQuantity = kilogram.ToUnit(MassUnit.Decagram);
- AssertEx.EqualTolerance(DecagramsInOneKilogram, (double)decagramQuantity.Value, DecagramsTolerance);
- Assert.Equal(MassUnit.Decagram, decagramQuantity.Unit);
-
- var decigramQuantity = kilogram.ToUnit(MassUnit.Decigram);
- AssertEx.EqualTolerance(DecigramsInOneKilogram, (double)decigramQuantity.Value, DecigramsTolerance);
- Assert.Equal(MassUnit.Decigram, decigramQuantity.Unit);
-
- var earthmassQuantity = kilogram.ToUnit(MassUnit.EarthMass);
- AssertEx.EqualTolerance(EarthMassesInOneKilogram, (double)earthmassQuantity.Value, EarthMassesTolerance);
- Assert.Equal(MassUnit.EarthMass, earthmassQuantity.Unit);
-
- var grainQuantity = kilogram.ToUnit(MassUnit.Grain);
- AssertEx.EqualTolerance(GrainsInOneKilogram, (double)grainQuantity.Value, GrainsTolerance);
- Assert.Equal(MassUnit.Grain, grainQuantity.Unit);
-
- var gramQuantity = kilogram.ToUnit(MassUnit.Gram);
- AssertEx.EqualTolerance(GramsInOneKilogram, (double)gramQuantity.Value, GramsTolerance);
- Assert.Equal(MassUnit.Gram, gramQuantity.Unit);
-
- var hectogramQuantity = kilogram.ToUnit(MassUnit.Hectogram);
- AssertEx.EqualTolerance(HectogramsInOneKilogram, (double)hectogramQuantity.Value, HectogramsTolerance);
- Assert.Equal(MassUnit.Hectogram, hectogramQuantity.Unit);
-
- var kilogramQuantity = kilogram.ToUnit(MassUnit.Kilogram);
- AssertEx.EqualTolerance(KilogramsInOneKilogram, (double)kilogramQuantity.Value, KilogramsTolerance);
- Assert.Equal(MassUnit.Kilogram, kilogramQuantity.Unit);
-
- var kilopoundQuantity = kilogram.ToUnit(MassUnit.Kilopound);
- AssertEx.EqualTolerance(KilopoundsInOneKilogram, (double)kilopoundQuantity.Value, KilopoundsTolerance);
- Assert.Equal(MassUnit.Kilopound, kilopoundQuantity.Unit);
-
- var kilotonneQuantity = kilogram.ToUnit(MassUnit.Kilotonne);
- AssertEx.EqualTolerance(KilotonnesInOneKilogram, (double)kilotonneQuantity.Value, KilotonnesTolerance);
- Assert.Equal(MassUnit.Kilotonne, kilotonneQuantity.Unit);
-
- var longhundredweightQuantity = kilogram.ToUnit(MassUnit.LongHundredweight);
- AssertEx.EqualTolerance(LongHundredweightInOneKilogram, (double)longhundredweightQuantity.Value, LongHundredweightTolerance);
- Assert.Equal(MassUnit.LongHundredweight, longhundredweightQuantity.Unit);
-
- var longtonQuantity = kilogram.ToUnit(MassUnit.LongTon);
- AssertEx.EqualTolerance(LongTonsInOneKilogram, (double)longtonQuantity.Value, LongTonsTolerance);
- Assert.Equal(MassUnit.LongTon, longtonQuantity.Unit);
+ var inBaseUnits = Mass.From(1.0, Mass.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megapoundQuantity = kilogram.ToUnit(MassUnit.Megapound);
- AssertEx.EqualTolerance(MegapoundsInOneKilogram, (double)megapoundQuantity.Value, MegapoundsTolerance);
- Assert.Equal(MassUnit.Megapound, megapoundQuantity.Unit);
-
- var megatonneQuantity = kilogram.ToUnit(MassUnit.Megatonne);
- AssertEx.EqualTolerance(MegatonnesInOneKilogram, (double)megatonneQuantity.Value, MegatonnesTolerance);
- Assert.Equal(MassUnit.Megatonne, megatonneQuantity.Unit);
-
- var microgramQuantity = kilogram.ToUnit(MassUnit.Microgram);
- AssertEx.EqualTolerance(MicrogramsInOneKilogram, (double)microgramQuantity.Value, MicrogramsTolerance);
- Assert.Equal(MassUnit.Microgram, microgramQuantity.Unit);
-
- var milligramQuantity = kilogram.ToUnit(MassUnit.Milligram);
- AssertEx.EqualTolerance(MilligramsInOneKilogram, (double)milligramQuantity.Value, MilligramsTolerance);
- Assert.Equal(MassUnit.Milligram, milligramQuantity.Unit);
-
- var nanogramQuantity = kilogram.ToUnit(MassUnit.Nanogram);
- AssertEx.EqualTolerance(NanogramsInOneKilogram, (double)nanogramQuantity.Value, NanogramsTolerance);
- Assert.Equal(MassUnit.Nanogram, nanogramQuantity.Unit);
-
- var ounceQuantity = kilogram.ToUnit(MassUnit.Ounce);
- AssertEx.EqualTolerance(OuncesInOneKilogram, (double)ounceQuantity.Value, OuncesTolerance);
- Assert.Equal(MassUnit.Ounce, ounceQuantity.Unit);
-
- var poundQuantity = kilogram.ToUnit(MassUnit.Pound);
- AssertEx.EqualTolerance(PoundsInOneKilogram, (double)poundQuantity.Value, PoundsTolerance);
- Assert.Equal(MassUnit.Pound, poundQuantity.Unit);
-
- var shorthundredweightQuantity = kilogram.ToUnit(MassUnit.ShortHundredweight);
- AssertEx.EqualTolerance(ShortHundredweightInOneKilogram, (double)shorthundredweightQuantity.Value, ShortHundredweightTolerance);
- Assert.Equal(MassUnit.ShortHundredweight, shorthundredweightQuantity.Unit);
-
- var shorttonQuantity = kilogram.ToUnit(MassUnit.ShortTon);
- AssertEx.EqualTolerance(ShortTonsInOneKilogram, (double)shorttonQuantity.Value, ShortTonsTolerance);
- Assert.Equal(MassUnit.ShortTon, shorttonQuantity.Unit);
-
- var slugQuantity = kilogram.ToUnit(MassUnit.Slug);
- AssertEx.EqualTolerance(SlugsInOneKilogram, (double)slugQuantity.Value, SlugsTolerance);
- Assert.Equal(MassUnit.Slug, slugQuantity.Unit);
-
- var solarmassQuantity = kilogram.ToUnit(MassUnit.SolarMass);
- AssertEx.EqualTolerance(SolarMassesInOneKilogram, (double)solarmassQuantity.Value, SolarMassesTolerance);
- Assert.Equal(MassUnit.SolarMass, solarmassQuantity.Unit);
-
- var stoneQuantity = kilogram.ToUnit(MassUnit.Stone);
- AssertEx.EqualTolerance(StoneInOneKilogram, (double)stoneQuantity.Value, StoneTolerance);
- Assert.Equal(MassUnit.Stone, stoneQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneQuantity = kilogram.ToUnit(MassUnit.Tonne);
- AssertEx.EqualTolerance(TonnesInOneKilogram, (double)tonneQuantity.Value, TonnesTolerance);
- Assert.Equal(MassUnit.Tonne, tonneQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MassUnit unit)
+ {
+ var quantity = Mass.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MassUnit unit)
{
- var quantityInBaseUnit = Mass.FromKilograms(1).ToBaseUnit();
- Assert.Equal(Mass.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Mass.Units.FirstOrDefault(u => u != Mass.BaseUnit && u != MassUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MassUnit.Undefined)
+ fromUnit = Mass.BaseUnit;
+
+ var quantity = Mass.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEnergyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEnergyTestsBase.g.cs
index 2bcf7a5820..fdbfa2178c 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEnergyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEnergyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class MolarEnergyTestsBase : QuantityTestsBase
protected virtual double MegajoulesPerMoleTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MolarEnergyUnit unit)
+ {
+ return unit switch
+ {
+ MolarEnergyUnit.JoulePerMole => (JoulesPerMoleInOneJoulePerMole, JoulesPerMoleTolerance),
+ MolarEnergyUnit.KilojoulePerMole => (KilojoulesPerMoleInOneJoulePerMole, KilojoulesPerMoleTolerance),
+ MolarEnergyUnit.MegajoulePerMole => (MegajoulesPerMoleInOneJoulePerMole, MegajoulesPerMoleTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MolarEnergyUnit.JoulePerMole },
+ new object[] { MolarEnergyUnit.KilojoulePerMole },
+ new object[] { MolarEnergyUnit.MegajoulePerMole },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MolarEnergyUnit unit)
{
- var joulepermole = MolarEnergy.FromJoulesPerMole(1);
+ var inBaseUnits = MolarEnergy.From(1.0, MolarEnergy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var joulepermoleQuantity = joulepermole.ToUnit(MolarEnergyUnit.JoulePerMole);
- AssertEx.EqualTolerance(JoulesPerMoleInOneJoulePerMole, (double)joulepermoleQuantity.Value, JoulesPerMoleTolerance);
- Assert.Equal(MolarEnergyUnit.JoulePerMole, joulepermoleQuantity.Unit);
-
- var kilojoulepermoleQuantity = joulepermole.ToUnit(MolarEnergyUnit.KilojoulePerMole);
- AssertEx.EqualTolerance(KilojoulesPerMoleInOneJoulePerMole, (double)kilojoulepermoleQuantity.Value, KilojoulesPerMoleTolerance);
- Assert.Equal(MolarEnergyUnit.KilojoulePerMole, kilojoulepermoleQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var megajoulepermoleQuantity = joulepermole.ToUnit(MolarEnergyUnit.MegajoulePerMole);
- AssertEx.EqualTolerance(MegajoulesPerMoleInOneJoulePerMole, (double)megajoulepermoleQuantity.Value, MegajoulesPerMoleTolerance);
- Assert.Equal(MolarEnergyUnit.MegajoulePerMole, megajoulepermoleQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MolarEnergyUnit unit)
+ {
+ var quantity = MolarEnergy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MolarEnergyUnit unit)
{
- var quantityInBaseUnit = MolarEnergy.FromJoulesPerMole(1).ToBaseUnit();
- Assert.Equal(MolarEnergy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MolarEnergy.Units.FirstOrDefault(u => u != MolarEnergy.BaseUnit && u != MolarEnergyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MolarEnergyUnit.Undefined)
+ fromUnit = MolarEnergy.BaseUnit;
+
+ var quantity = MolarEnergy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEntropyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEntropyTestsBase.g.cs
index 0a076aae5e..93ef01239e 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEntropyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarEntropyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class MolarEntropyTestsBase : QuantityTestsBase
protected virtual double MegajoulesPerMoleKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MolarEntropyUnit unit)
+ {
+ return unit switch
+ {
+ MolarEntropyUnit.JoulePerMoleKelvin => (JoulesPerMoleKelvinInOneJoulePerMoleKelvin, JoulesPerMoleKelvinTolerance),
+ MolarEntropyUnit.KilojoulePerMoleKelvin => (KilojoulesPerMoleKelvinInOneJoulePerMoleKelvin, KilojoulesPerMoleKelvinTolerance),
+ MolarEntropyUnit.MegajoulePerMoleKelvin => (MegajoulesPerMoleKelvinInOneJoulePerMoleKelvin, MegajoulesPerMoleKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MolarEntropyUnit.JoulePerMoleKelvin },
+ new object[] { MolarEntropyUnit.KilojoulePerMoleKelvin },
+ new object[] { MolarEntropyUnit.MegajoulePerMoleKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MolarEntropyUnit unit)
{
- var joulepermolekelvin = MolarEntropy.FromJoulesPerMoleKelvin(1);
+ var inBaseUnits = MolarEntropy.From(1.0, MolarEntropy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var joulepermolekelvinQuantity = joulepermolekelvin.ToUnit(MolarEntropyUnit.JoulePerMoleKelvin);
- AssertEx.EqualTolerance(JoulesPerMoleKelvinInOneJoulePerMoleKelvin, (double)joulepermolekelvinQuantity.Value, JoulesPerMoleKelvinTolerance);
- Assert.Equal(MolarEntropyUnit.JoulePerMoleKelvin, joulepermolekelvinQuantity.Unit);
-
- var kilojoulepermolekelvinQuantity = joulepermolekelvin.ToUnit(MolarEntropyUnit.KilojoulePerMoleKelvin);
- AssertEx.EqualTolerance(KilojoulesPerMoleKelvinInOneJoulePerMoleKelvin, (double)kilojoulepermolekelvinQuantity.Value, KilojoulesPerMoleKelvinTolerance);
- Assert.Equal(MolarEntropyUnit.KilojoulePerMoleKelvin, kilojoulepermolekelvinQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var megajoulepermolekelvinQuantity = joulepermolekelvin.ToUnit(MolarEntropyUnit.MegajoulePerMoleKelvin);
- AssertEx.EqualTolerance(MegajoulesPerMoleKelvinInOneJoulePerMoleKelvin, (double)megajoulepermolekelvinQuantity.Value, MegajoulesPerMoleKelvinTolerance);
- Assert.Equal(MolarEntropyUnit.MegajoulePerMoleKelvin, megajoulepermolekelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MolarEntropyUnit unit)
+ {
+ var quantity = MolarEntropy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MolarEntropyUnit unit)
{
- var quantityInBaseUnit = MolarEntropy.FromJoulesPerMoleKelvin(1).ToBaseUnit();
- Assert.Equal(MolarEntropy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MolarEntropy.Units.FirstOrDefault(u => u != MolarEntropy.BaseUnit && u != MolarEntropyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MolarEntropyUnit.Undefined)
+ fromUnit = MolarEntropy.BaseUnit;
+
+ var quantity = MolarEntropy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarMassTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarMassTestsBase.g.cs
index 7df39a1cc9..b6b58539ad 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarMassTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarMassTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -65,6 +66,42 @@ public abstract partial class MolarMassTestsBase : QuantityTestsBase
protected virtual double PoundsPerMoleTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MolarMassUnit unit)
+ {
+ return unit switch
+ {
+ MolarMassUnit.CentigramPerMole => (CentigramsPerMoleInOneKilogramPerMole, CentigramsPerMoleTolerance),
+ MolarMassUnit.DecagramPerMole => (DecagramsPerMoleInOneKilogramPerMole, DecagramsPerMoleTolerance),
+ MolarMassUnit.DecigramPerMole => (DecigramsPerMoleInOneKilogramPerMole, DecigramsPerMoleTolerance),
+ MolarMassUnit.GramPerMole => (GramsPerMoleInOneKilogramPerMole, GramsPerMoleTolerance),
+ MolarMassUnit.HectogramPerMole => (HectogramsPerMoleInOneKilogramPerMole, HectogramsPerMoleTolerance),
+ MolarMassUnit.KilogramPerMole => (KilogramsPerMoleInOneKilogramPerMole, KilogramsPerMoleTolerance),
+ MolarMassUnit.KilopoundPerMole => (KilopoundsPerMoleInOneKilogramPerMole, KilopoundsPerMoleTolerance),
+ MolarMassUnit.MegapoundPerMole => (MegapoundsPerMoleInOneKilogramPerMole, MegapoundsPerMoleTolerance),
+ MolarMassUnit.MicrogramPerMole => (MicrogramsPerMoleInOneKilogramPerMole, MicrogramsPerMoleTolerance),
+ MolarMassUnit.MilligramPerMole => (MilligramsPerMoleInOneKilogramPerMole, MilligramsPerMoleTolerance),
+ MolarMassUnit.NanogramPerMole => (NanogramsPerMoleInOneKilogramPerMole, NanogramsPerMoleTolerance),
+ MolarMassUnit.PoundPerMole => (PoundsPerMoleInOneKilogramPerMole, PoundsPerMoleTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MolarMassUnit.CentigramPerMole },
+ new object[] { MolarMassUnit.DecagramPerMole },
+ new object[] { MolarMassUnit.DecigramPerMole },
+ new object[] { MolarMassUnit.GramPerMole },
+ new object[] { MolarMassUnit.HectogramPerMole },
+ new object[] { MolarMassUnit.KilogramPerMole },
+ new object[] { MolarMassUnit.KilopoundPerMole },
+ new object[] { MolarMassUnit.MegapoundPerMole },
+ new object[] { MolarMassUnit.MicrogramPerMole },
+ new object[] { MolarMassUnit.MilligramPerMole },
+ new object[] { MolarMassUnit.NanogramPerMole },
+ new object[] { MolarMassUnit.PoundPerMole },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -252,65 +289,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MolarMassUnit unit)
{
- var kilogrampermole = MolarMass.FromKilogramsPerMole(1);
-
- var centigrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.CentigramPerMole);
- AssertEx.EqualTolerance(CentigramsPerMoleInOneKilogramPerMole, (double)centigrampermoleQuantity.Value, CentigramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.CentigramPerMole, centigrampermoleQuantity.Unit);
-
- var decagrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.DecagramPerMole);
- AssertEx.EqualTolerance(DecagramsPerMoleInOneKilogramPerMole, (double)decagrampermoleQuantity.Value, DecagramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.DecagramPerMole, decagrampermoleQuantity.Unit);
-
- var decigrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.DecigramPerMole);
- AssertEx.EqualTolerance(DecigramsPerMoleInOneKilogramPerMole, (double)decigrampermoleQuantity.Value, DecigramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.DecigramPerMole, decigrampermoleQuantity.Unit);
-
- var grampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.GramPerMole);
- AssertEx.EqualTolerance(GramsPerMoleInOneKilogramPerMole, (double)grampermoleQuantity.Value, GramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.GramPerMole, grampermoleQuantity.Unit);
-
- var hectogrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.HectogramPerMole);
- AssertEx.EqualTolerance(HectogramsPerMoleInOneKilogramPerMole, (double)hectogrampermoleQuantity.Value, HectogramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.HectogramPerMole, hectogrampermoleQuantity.Unit);
-
- var kilogrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.KilogramPerMole);
- AssertEx.EqualTolerance(KilogramsPerMoleInOneKilogramPerMole, (double)kilogrampermoleQuantity.Value, KilogramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.KilogramPerMole, kilogrampermoleQuantity.Unit);
+ var inBaseUnits = MolarMass.From(1.0, MolarMass.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilopoundpermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.KilopoundPerMole);
- AssertEx.EqualTolerance(KilopoundsPerMoleInOneKilogramPerMole, (double)kilopoundpermoleQuantity.Value, KilopoundsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.KilopoundPerMole, kilopoundpermoleQuantity.Unit);
-
- var megapoundpermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.MegapoundPerMole);
- AssertEx.EqualTolerance(MegapoundsPerMoleInOneKilogramPerMole, (double)megapoundpermoleQuantity.Value, MegapoundsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.MegapoundPerMole, megapoundpermoleQuantity.Unit);
-
- var microgrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.MicrogramPerMole);
- AssertEx.EqualTolerance(MicrogramsPerMoleInOneKilogramPerMole, (double)microgrampermoleQuantity.Value, MicrogramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.MicrogramPerMole, microgrampermoleQuantity.Unit);
-
- var milligrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.MilligramPerMole);
- AssertEx.EqualTolerance(MilligramsPerMoleInOneKilogramPerMole, (double)milligrampermoleQuantity.Value, MilligramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.MilligramPerMole, milligrampermoleQuantity.Unit);
-
- var nanogrampermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.NanogramPerMole);
- AssertEx.EqualTolerance(NanogramsPerMoleInOneKilogramPerMole, (double)nanogrampermoleQuantity.Value, NanogramsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.NanogramPerMole, nanogrampermoleQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundpermoleQuantity = kilogrampermole.ToUnit(MolarMassUnit.PoundPerMole);
- AssertEx.EqualTolerance(PoundsPerMoleInOneKilogramPerMole, (double)poundpermoleQuantity.Value, PoundsPerMoleTolerance);
- Assert.Equal(MolarMassUnit.PoundPerMole, poundpermoleQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MolarMassUnit unit)
+ {
+ var quantity = MolarMass.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MolarMassUnit unit)
{
- var quantityInBaseUnit = MolarMass.FromKilogramsPerMole(1).ToBaseUnit();
- Assert.Equal(MolarMass.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = MolarMass.Units.FirstOrDefault(u => u != MolarMass.BaseUnit && u != MolarMassUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MolarMassUnit.Undefined)
+ fromUnit = MolarMass.BaseUnit;
+
+ var quantity = MolarMass.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarityTestsBase.g.cs
index 5e77ce6c5d..f0592f4211 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/MolarityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/MolarityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -57,6 +58,34 @@ public abstract partial class MolarityTestsBase : QuantityTestsBase
protected virtual double PicomolesPerLiterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(MolarityUnit unit)
+ {
+ return unit switch
+ {
+ MolarityUnit.CentimolesPerLiter => (CentimolesPerLiterInOneMolesPerCubicMeter, CentimolesPerLiterTolerance),
+ MolarityUnit.DecimolesPerLiter => (DecimolesPerLiterInOneMolesPerCubicMeter, DecimolesPerLiterTolerance),
+ MolarityUnit.MicromolesPerLiter => (MicromolesPerLiterInOneMolesPerCubicMeter, MicromolesPerLiterTolerance),
+ MolarityUnit.MillimolesPerLiter => (MillimolesPerLiterInOneMolesPerCubicMeter, MillimolesPerLiterTolerance),
+ MolarityUnit.MolesPerCubicMeter => (MolesPerCubicMeterInOneMolesPerCubicMeter, MolesPerCubicMeterTolerance),
+ MolarityUnit.MolesPerLiter => (MolesPerLiterInOneMolesPerCubicMeter, MolesPerLiterTolerance),
+ MolarityUnit.NanomolesPerLiter => (NanomolesPerLiterInOneMolesPerCubicMeter, NanomolesPerLiterTolerance),
+ MolarityUnit.PicomolesPerLiter => (PicomolesPerLiterInOneMolesPerCubicMeter, PicomolesPerLiterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { MolarityUnit.CentimolesPerLiter },
+ new object[] { MolarityUnit.DecimolesPerLiter },
+ new object[] { MolarityUnit.MicromolesPerLiter },
+ new object[] { MolarityUnit.MillimolesPerLiter },
+ new object[] { MolarityUnit.MolesPerCubicMeter },
+ new object[] { MolarityUnit.MolesPerLiter },
+ new object[] { MolarityUnit.NanomolesPerLiter },
+ new object[] { MolarityUnit.PicomolesPerLiter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -220,49 +249,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(MolarityUnit unit)
{
- var molespercubicmeter = Molarity.FromMolesPerCubicMeter(1);
-
- var centimolesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.CentimolesPerLiter);
- AssertEx.EqualTolerance(CentimolesPerLiterInOneMolesPerCubicMeter, (double)centimolesperliterQuantity.Value, CentimolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.CentimolesPerLiter, centimolesperliterQuantity.Unit);
-
- var decimolesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.DecimolesPerLiter);
- AssertEx.EqualTolerance(DecimolesPerLiterInOneMolesPerCubicMeter, (double)decimolesperliterQuantity.Value, DecimolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.DecimolesPerLiter, decimolesperliterQuantity.Unit);
-
- var micromolesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.MicromolesPerLiter);
- AssertEx.EqualTolerance(MicromolesPerLiterInOneMolesPerCubicMeter, (double)micromolesperliterQuantity.Value, MicromolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.MicromolesPerLiter, micromolesperliterQuantity.Unit);
-
- var millimolesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.MillimolesPerLiter);
- AssertEx.EqualTolerance(MillimolesPerLiterInOneMolesPerCubicMeter, (double)millimolesperliterQuantity.Value, MillimolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.MillimolesPerLiter, millimolesperliterQuantity.Unit);
+ var inBaseUnits = Molarity.From(1.0, Molarity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var molespercubicmeterQuantity = molespercubicmeter.ToUnit(MolarityUnit.MolesPerCubicMeter);
- AssertEx.EqualTolerance(MolesPerCubicMeterInOneMolesPerCubicMeter, (double)molespercubicmeterQuantity.Value, MolesPerCubicMeterTolerance);
- Assert.Equal(MolarityUnit.MolesPerCubicMeter, molespercubicmeterQuantity.Unit);
-
- var molesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.MolesPerLiter);
- AssertEx.EqualTolerance(MolesPerLiterInOneMolesPerCubicMeter, (double)molesperliterQuantity.Value, MolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.MolesPerLiter, molesperliterQuantity.Unit);
-
- var nanomolesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.NanomolesPerLiter);
- AssertEx.EqualTolerance(NanomolesPerLiterInOneMolesPerCubicMeter, (double)nanomolesperliterQuantity.Value, NanomolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.NanomolesPerLiter, nanomolesperliterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var picomolesperliterQuantity = molespercubicmeter.ToUnit(MolarityUnit.PicomolesPerLiter);
- AssertEx.EqualTolerance(PicomolesPerLiterInOneMolesPerCubicMeter, (double)picomolesperliterQuantity.Value, PicomolesPerLiterTolerance);
- Assert.Equal(MolarityUnit.PicomolesPerLiter, picomolesperliterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(MolarityUnit unit)
+ {
+ var quantity = Molarity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(MolarityUnit unit)
{
- var quantityInBaseUnit = Molarity.FromMolesPerCubicMeter(1).ToBaseUnit();
- Assert.Equal(Molarity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Molarity.Units.FirstOrDefault(u => u != Molarity.BaseUnit && u != MolarityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == MolarityUnit.Undefined)
+ fromUnit = Molarity.BaseUnit;
+
+ var quantity = Molarity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PermeabilityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PermeabilityTestsBase.g.cs
index 2cd5d7c69e..bfc744d102 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PermeabilityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PermeabilityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class PermeabilityTestsBase : QuantityTestsBase
protected virtual double HenriesPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PermeabilityUnit unit)
+ {
+ return unit switch
+ {
+ PermeabilityUnit.HenryPerMeter => (HenriesPerMeterInOneHenryPerMeter, HenriesPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PermeabilityUnit.HenryPerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PermeabilityUnit unit)
{
- var henrypermeter = Permeability.FromHenriesPerMeter(1);
+ var inBaseUnits = Permeability.From(1.0, Permeability.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var henrypermeterQuantity = henrypermeter.ToUnit(PermeabilityUnit.HenryPerMeter);
- AssertEx.EqualTolerance(HenriesPerMeterInOneHenryPerMeter, (double)henrypermeterQuantity.Value, HenriesPerMeterTolerance);
- Assert.Equal(PermeabilityUnit.HenryPerMeter, henrypermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PermeabilityUnit unit)
+ {
+ var quantity = Permeability.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PermeabilityUnit unit)
{
- var quantityInBaseUnit = Permeability.FromHenriesPerMeter(1).ToBaseUnit();
- Assert.Equal(Permeability.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Permeability.Units.FirstOrDefault(u => u != Permeability.BaseUnit && u != PermeabilityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PermeabilityUnit.Undefined)
+ fromUnit = Permeability.BaseUnit;
+
+ var quantity = Permeability.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PermittivityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PermittivityTestsBase.g.cs
index d42b94bb7b..553dc76b6c 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PermittivityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PermittivityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class PermittivityTestsBase : QuantityTestsBase
protected virtual double FaradsPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PermittivityUnit unit)
+ {
+ return unit switch
+ {
+ PermittivityUnit.FaradPerMeter => (FaradsPerMeterInOneFaradPerMeter, FaradsPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PermittivityUnit.FaradPerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PermittivityUnit unit)
{
- var faradpermeter = Permittivity.FromFaradsPerMeter(1);
+ var inBaseUnits = Permittivity.From(1.0, Permittivity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var faradpermeterQuantity = faradpermeter.ToUnit(PermittivityUnit.FaradPerMeter);
- AssertEx.EqualTolerance(FaradsPerMeterInOneFaradPerMeter, (double)faradpermeterQuantity.Value, FaradsPerMeterTolerance);
- Assert.Equal(PermittivityUnit.FaradPerMeter, faradpermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PermittivityUnit unit)
+ {
+ var quantity = Permittivity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PermittivityUnit unit)
{
- var quantityInBaseUnit = Permittivity.FromFaradsPerMeter(1).ToBaseUnit();
- Assert.Equal(Permittivity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Permittivity.Units.FirstOrDefault(u => u != Permittivity.BaseUnit && u != PermittivityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PermittivityUnit.Undefined)
+ fromUnit = Permittivity.BaseUnit;
+
+ var quantity = Permittivity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PowerDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PowerDensityTestsBase.g.cs
index fe43ad30c0..5a479b3bc7 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PowerDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PowerDensityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -129,6 +130,106 @@ public abstract partial class PowerDensityTestsBase : QuantityTestsBase
protected virtual double WattsPerLiterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PowerDensityUnit unit)
+ {
+ return unit switch
+ {
+ PowerDensityUnit.DecawattPerCubicFoot => (DecawattsPerCubicFootInOneWattPerCubicMeter, DecawattsPerCubicFootTolerance),
+ PowerDensityUnit.DecawattPerCubicInch => (DecawattsPerCubicInchInOneWattPerCubicMeter, DecawattsPerCubicInchTolerance),
+ PowerDensityUnit.DecawattPerCubicMeter => (DecawattsPerCubicMeterInOneWattPerCubicMeter, DecawattsPerCubicMeterTolerance),
+ PowerDensityUnit.DecawattPerLiter => (DecawattsPerLiterInOneWattPerCubicMeter, DecawattsPerLiterTolerance),
+ PowerDensityUnit.DeciwattPerCubicFoot => (DeciwattsPerCubicFootInOneWattPerCubicMeter, DeciwattsPerCubicFootTolerance),
+ PowerDensityUnit.DeciwattPerCubicInch => (DeciwattsPerCubicInchInOneWattPerCubicMeter, DeciwattsPerCubicInchTolerance),
+ PowerDensityUnit.DeciwattPerCubicMeter => (DeciwattsPerCubicMeterInOneWattPerCubicMeter, DeciwattsPerCubicMeterTolerance),
+ PowerDensityUnit.DeciwattPerLiter => (DeciwattsPerLiterInOneWattPerCubicMeter, DeciwattsPerLiterTolerance),
+ PowerDensityUnit.GigawattPerCubicFoot => (GigawattsPerCubicFootInOneWattPerCubicMeter, GigawattsPerCubicFootTolerance),
+ PowerDensityUnit.GigawattPerCubicInch => (GigawattsPerCubicInchInOneWattPerCubicMeter, GigawattsPerCubicInchTolerance),
+ PowerDensityUnit.GigawattPerCubicMeter => (GigawattsPerCubicMeterInOneWattPerCubicMeter, GigawattsPerCubicMeterTolerance),
+ PowerDensityUnit.GigawattPerLiter => (GigawattsPerLiterInOneWattPerCubicMeter, GigawattsPerLiterTolerance),
+ PowerDensityUnit.KilowattPerCubicFoot => (KilowattsPerCubicFootInOneWattPerCubicMeter, KilowattsPerCubicFootTolerance),
+ PowerDensityUnit.KilowattPerCubicInch => (KilowattsPerCubicInchInOneWattPerCubicMeter, KilowattsPerCubicInchTolerance),
+ PowerDensityUnit.KilowattPerCubicMeter => (KilowattsPerCubicMeterInOneWattPerCubicMeter, KilowattsPerCubicMeterTolerance),
+ PowerDensityUnit.KilowattPerLiter => (KilowattsPerLiterInOneWattPerCubicMeter, KilowattsPerLiterTolerance),
+ PowerDensityUnit.MegawattPerCubicFoot => (MegawattsPerCubicFootInOneWattPerCubicMeter, MegawattsPerCubicFootTolerance),
+ PowerDensityUnit.MegawattPerCubicInch => (MegawattsPerCubicInchInOneWattPerCubicMeter, MegawattsPerCubicInchTolerance),
+ PowerDensityUnit.MegawattPerCubicMeter => (MegawattsPerCubicMeterInOneWattPerCubicMeter, MegawattsPerCubicMeterTolerance),
+ PowerDensityUnit.MegawattPerLiter => (MegawattsPerLiterInOneWattPerCubicMeter, MegawattsPerLiterTolerance),
+ PowerDensityUnit.MicrowattPerCubicFoot => (MicrowattsPerCubicFootInOneWattPerCubicMeter, MicrowattsPerCubicFootTolerance),
+ PowerDensityUnit.MicrowattPerCubicInch => (MicrowattsPerCubicInchInOneWattPerCubicMeter, MicrowattsPerCubicInchTolerance),
+ PowerDensityUnit.MicrowattPerCubicMeter => (MicrowattsPerCubicMeterInOneWattPerCubicMeter, MicrowattsPerCubicMeterTolerance),
+ PowerDensityUnit.MicrowattPerLiter => (MicrowattsPerLiterInOneWattPerCubicMeter, MicrowattsPerLiterTolerance),
+ PowerDensityUnit.MilliwattPerCubicFoot => (MilliwattsPerCubicFootInOneWattPerCubicMeter, MilliwattsPerCubicFootTolerance),
+ PowerDensityUnit.MilliwattPerCubicInch => (MilliwattsPerCubicInchInOneWattPerCubicMeter, MilliwattsPerCubicInchTolerance),
+ PowerDensityUnit.MilliwattPerCubicMeter => (MilliwattsPerCubicMeterInOneWattPerCubicMeter, MilliwattsPerCubicMeterTolerance),
+ PowerDensityUnit.MilliwattPerLiter => (MilliwattsPerLiterInOneWattPerCubicMeter, MilliwattsPerLiterTolerance),
+ PowerDensityUnit.NanowattPerCubicFoot => (NanowattsPerCubicFootInOneWattPerCubicMeter, NanowattsPerCubicFootTolerance),
+ PowerDensityUnit.NanowattPerCubicInch => (NanowattsPerCubicInchInOneWattPerCubicMeter, NanowattsPerCubicInchTolerance),
+ PowerDensityUnit.NanowattPerCubicMeter => (NanowattsPerCubicMeterInOneWattPerCubicMeter, NanowattsPerCubicMeterTolerance),
+ PowerDensityUnit.NanowattPerLiter => (NanowattsPerLiterInOneWattPerCubicMeter, NanowattsPerLiterTolerance),
+ PowerDensityUnit.PicowattPerCubicFoot => (PicowattsPerCubicFootInOneWattPerCubicMeter, PicowattsPerCubicFootTolerance),
+ PowerDensityUnit.PicowattPerCubicInch => (PicowattsPerCubicInchInOneWattPerCubicMeter, PicowattsPerCubicInchTolerance),
+ PowerDensityUnit.PicowattPerCubicMeter => (PicowattsPerCubicMeterInOneWattPerCubicMeter, PicowattsPerCubicMeterTolerance),
+ PowerDensityUnit.PicowattPerLiter => (PicowattsPerLiterInOneWattPerCubicMeter, PicowattsPerLiterTolerance),
+ PowerDensityUnit.TerawattPerCubicFoot => (TerawattsPerCubicFootInOneWattPerCubicMeter, TerawattsPerCubicFootTolerance),
+ PowerDensityUnit.TerawattPerCubicInch => (TerawattsPerCubicInchInOneWattPerCubicMeter, TerawattsPerCubicInchTolerance),
+ PowerDensityUnit.TerawattPerCubicMeter => (TerawattsPerCubicMeterInOneWattPerCubicMeter, TerawattsPerCubicMeterTolerance),
+ PowerDensityUnit.TerawattPerLiter => (TerawattsPerLiterInOneWattPerCubicMeter, TerawattsPerLiterTolerance),
+ PowerDensityUnit.WattPerCubicFoot => (WattsPerCubicFootInOneWattPerCubicMeter, WattsPerCubicFootTolerance),
+ PowerDensityUnit.WattPerCubicInch => (WattsPerCubicInchInOneWattPerCubicMeter, WattsPerCubicInchTolerance),
+ PowerDensityUnit.WattPerCubicMeter => (WattsPerCubicMeterInOneWattPerCubicMeter, WattsPerCubicMeterTolerance),
+ PowerDensityUnit.WattPerLiter => (WattsPerLiterInOneWattPerCubicMeter, WattsPerLiterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PowerDensityUnit.DecawattPerCubicFoot },
+ new object[] { PowerDensityUnit.DecawattPerCubicInch },
+ new object[] { PowerDensityUnit.DecawattPerCubicMeter },
+ new object[] { PowerDensityUnit.DecawattPerLiter },
+ new object[] { PowerDensityUnit.DeciwattPerCubicFoot },
+ new object[] { PowerDensityUnit.DeciwattPerCubicInch },
+ new object[] { PowerDensityUnit.DeciwattPerCubicMeter },
+ new object[] { PowerDensityUnit.DeciwattPerLiter },
+ new object[] { PowerDensityUnit.GigawattPerCubicFoot },
+ new object[] { PowerDensityUnit.GigawattPerCubicInch },
+ new object[] { PowerDensityUnit.GigawattPerCubicMeter },
+ new object[] { PowerDensityUnit.GigawattPerLiter },
+ new object[] { PowerDensityUnit.KilowattPerCubicFoot },
+ new object[] { PowerDensityUnit.KilowattPerCubicInch },
+ new object[] { PowerDensityUnit.KilowattPerCubicMeter },
+ new object[] { PowerDensityUnit.KilowattPerLiter },
+ new object[] { PowerDensityUnit.MegawattPerCubicFoot },
+ new object[] { PowerDensityUnit.MegawattPerCubicInch },
+ new object[] { PowerDensityUnit.MegawattPerCubicMeter },
+ new object[] { PowerDensityUnit.MegawattPerLiter },
+ new object[] { PowerDensityUnit.MicrowattPerCubicFoot },
+ new object[] { PowerDensityUnit.MicrowattPerCubicInch },
+ new object[] { PowerDensityUnit.MicrowattPerCubicMeter },
+ new object[] { PowerDensityUnit.MicrowattPerLiter },
+ new object[] { PowerDensityUnit.MilliwattPerCubicFoot },
+ new object[] { PowerDensityUnit.MilliwattPerCubicInch },
+ new object[] { PowerDensityUnit.MilliwattPerCubicMeter },
+ new object[] { PowerDensityUnit.MilliwattPerLiter },
+ new object[] { PowerDensityUnit.NanowattPerCubicFoot },
+ new object[] { PowerDensityUnit.NanowattPerCubicInch },
+ new object[] { PowerDensityUnit.NanowattPerCubicMeter },
+ new object[] { PowerDensityUnit.NanowattPerLiter },
+ new object[] { PowerDensityUnit.PicowattPerCubicFoot },
+ new object[] { PowerDensityUnit.PicowattPerCubicInch },
+ new object[] { PowerDensityUnit.PicowattPerCubicMeter },
+ new object[] { PowerDensityUnit.PicowattPerLiter },
+ new object[] { PowerDensityUnit.TerawattPerCubicFoot },
+ new object[] { PowerDensityUnit.TerawattPerCubicInch },
+ new object[] { PowerDensityUnit.TerawattPerCubicMeter },
+ new object[] { PowerDensityUnit.TerawattPerLiter },
+ new object[] { PowerDensityUnit.WattPerCubicFoot },
+ new object[] { PowerDensityUnit.WattPerCubicInch },
+ new object[] { PowerDensityUnit.WattPerCubicMeter },
+ new object[] { PowerDensityUnit.WattPerLiter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -508,193 +609,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PowerDensityUnit unit)
{
- var wattpercubicmeter = PowerDensity.FromWattsPerCubicMeter(1);
-
- var decawattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DecawattPerCubicFoot);
- AssertEx.EqualTolerance(DecawattsPerCubicFootInOneWattPerCubicMeter, (double)decawattpercubicfootQuantity.Value, DecawattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.DecawattPerCubicFoot, decawattpercubicfootQuantity.Unit);
-
- var decawattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DecawattPerCubicInch);
- AssertEx.EqualTolerance(DecawattsPerCubicInchInOneWattPerCubicMeter, (double)decawattpercubicinchQuantity.Value, DecawattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.DecawattPerCubicInch, decawattpercubicinchQuantity.Unit);
-
- var decawattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DecawattPerCubicMeter);
- AssertEx.EqualTolerance(DecawattsPerCubicMeterInOneWattPerCubicMeter, (double)decawattpercubicmeterQuantity.Value, DecawattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.DecawattPerCubicMeter, decawattpercubicmeterQuantity.Unit);
-
- var decawattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DecawattPerLiter);
- AssertEx.EqualTolerance(DecawattsPerLiterInOneWattPerCubicMeter, (double)decawattperliterQuantity.Value, DecawattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.DecawattPerLiter, decawattperliterQuantity.Unit);
-
- var deciwattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DeciwattPerCubicFoot);
- AssertEx.EqualTolerance(DeciwattsPerCubicFootInOneWattPerCubicMeter, (double)deciwattpercubicfootQuantity.Value, DeciwattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.DeciwattPerCubicFoot, deciwattpercubicfootQuantity.Unit);
-
- var deciwattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DeciwattPerCubicInch);
- AssertEx.EqualTolerance(DeciwattsPerCubicInchInOneWattPerCubicMeter, (double)deciwattpercubicinchQuantity.Value, DeciwattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.DeciwattPerCubicInch, deciwattpercubicinchQuantity.Unit);
-
- var deciwattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DeciwattPerCubicMeter);
- AssertEx.EqualTolerance(DeciwattsPerCubicMeterInOneWattPerCubicMeter, (double)deciwattpercubicmeterQuantity.Value, DeciwattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.DeciwattPerCubicMeter, deciwattpercubicmeterQuantity.Unit);
-
- var deciwattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.DeciwattPerLiter);
- AssertEx.EqualTolerance(DeciwattsPerLiterInOneWattPerCubicMeter, (double)deciwattperliterQuantity.Value, DeciwattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.DeciwattPerLiter, deciwattperliterQuantity.Unit);
-
- var gigawattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.GigawattPerCubicFoot);
- AssertEx.EqualTolerance(GigawattsPerCubicFootInOneWattPerCubicMeter, (double)gigawattpercubicfootQuantity.Value, GigawattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.GigawattPerCubicFoot, gigawattpercubicfootQuantity.Unit);
-
- var gigawattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.GigawattPerCubicInch);
- AssertEx.EqualTolerance(GigawattsPerCubicInchInOneWattPerCubicMeter, (double)gigawattpercubicinchQuantity.Value, GigawattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.GigawattPerCubicInch, gigawattpercubicinchQuantity.Unit);
-
- var gigawattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.GigawattPerCubicMeter);
- AssertEx.EqualTolerance(GigawattsPerCubicMeterInOneWattPerCubicMeter, (double)gigawattpercubicmeterQuantity.Value, GigawattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.GigawattPerCubicMeter, gigawattpercubicmeterQuantity.Unit);
-
- var gigawattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.GigawattPerLiter);
- AssertEx.EqualTolerance(GigawattsPerLiterInOneWattPerCubicMeter, (double)gigawattperliterQuantity.Value, GigawattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.GigawattPerLiter, gigawattperliterQuantity.Unit);
-
- var kilowattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.KilowattPerCubicFoot);
- AssertEx.EqualTolerance(KilowattsPerCubicFootInOneWattPerCubicMeter, (double)kilowattpercubicfootQuantity.Value, KilowattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.KilowattPerCubicFoot, kilowattpercubicfootQuantity.Unit);
-
- var kilowattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.KilowattPerCubicInch);
- AssertEx.EqualTolerance(KilowattsPerCubicInchInOneWattPerCubicMeter, (double)kilowattpercubicinchQuantity.Value, KilowattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.KilowattPerCubicInch, kilowattpercubicinchQuantity.Unit);
-
- var kilowattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.KilowattPerCubicMeter);
- AssertEx.EqualTolerance(KilowattsPerCubicMeterInOneWattPerCubicMeter, (double)kilowattpercubicmeterQuantity.Value, KilowattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.KilowattPerCubicMeter, kilowattpercubicmeterQuantity.Unit);
-
- var kilowattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.KilowattPerLiter);
- AssertEx.EqualTolerance(KilowattsPerLiterInOneWattPerCubicMeter, (double)kilowattperliterQuantity.Value, KilowattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.KilowattPerLiter, kilowattperliterQuantity.Unit);
-
- var megawattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MegawattPerCubicFoot);
- AssertEx.EqualTolerance(MegawattsPerCubicFootInOneWattPerCubicMeter, (double)megawattpercubicfootQuantity.Value, MegawattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.MegawattPerCubicFoot, megawattpercubicfootQuantity.Unit);
-
- var megawattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MegawattPerCubicInch);
- AssertEx.EqualTolerance(MegawattsPerCubicInchInOneWattPerCubicMeter, (double)megawattpercubicinchQuantity.Value, MegawattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.MegawattPerCubicInch, megawattpercubicinchQuantity.Unit);
-
- var megawattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MegawattPerCubicMeter);
- AssertEx.EqualTolerance(MegawattsPerCubicMeterInOneWattPerCubicMeter, (double)megawattpercubicmeterQuantity.Value, MegawattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.MegawattPerCubicMeter, megawattpercubicmeterQuantity.Unit);
-
- var megawattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MegawattPerLiter);
- AssertEx.EqualTolerance(MegawattsPerLiterInOneWattPerCubicMeter, (double)megawattperliterQuantity.Value, MegawattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.MegawattPerLiter, megawattperliterQuantity.Unit);
+ var inBaseUnits = PowerDensity.From(1.0, PowerDensity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var microwattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MicrowattPerCubicFoot);
- AssertEx.EqualTolerance(MicrowattsPerCubicFootInOneWattPerCubicMeter, (double)microwattpercubicfootQuantity.Value, MicrowattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.MicrowattPerCubicFoot, microwattpercubicfootQuantity.Unit);
-
- var microwattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MicrowattPerCubicInch);
- AssertEx.EqualTolerance(MicrowattsPerCubicInchInOneWattPerCubicMeter, (double)microwattpercubicinchQuantity.Value, MicrowattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.MicrowattPerCubicInch, microwattpercubicinchQuantity.Unit);
-
- var microwattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MicrowattPerCubicMeter);
- AssertEx.EqualTolerance(MicrowattsPerCubicMeterInOneWattPerCubicMeter, (double)microwattpercubicmeterQuantity.Value, MicrowattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.MicrowattPerCubicMeter, microwattpercubicmeterQuantity.Unit);
-
- var microwattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MicrowattPerLiter);
- AssertEx.EqualTolerance(MicrowattsPerLiterInOneWattPerCubicMeter, (double)microwattperliterQuantity.Value, MicrowattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.MicrowattPerLiter, microwattperliterQuantity.Unit);
-
- var milliwattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MilliwattPerCubicFoot);
- AssertEx.EqualTolerance(MilliwattsPerCubicFootInOneWattPerCubicMeter, (double)milliwattpercubicfootQuantity.Value, MilliwattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.MilliwattPerCubicFoot, milliwattpercubicfootQuantity.Unit);
-
- var milliwattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MilliwattPerCubicInch);
- AssertEx.EqualTolerance(MilliwattsPerCubicInchInOneWattPerCubicMeter, (double)milliwattpercubicinchQuantity.Value, MilliwattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.MilliwattPerCubicInch, milliwattpercubicinchQuantity.Unit);
-
- var milliwattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MilliwattPerCubicMeter);
- AssertEx.EqualTolerance(MilliwattsPerCubicMeterInOneWattPerCubicMeter, (double)milliwattpercubicmeterQuantity.Value, MilliwattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.MilliwattPerCubicMeter, milliwattpercubicmeterQuantity.Unit);
-
- var milliwattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.MilliwattPerLiter);
- AssertEx.EqualTolerance(MilliwattsPerLiterInOneWattPerCubicMeter, (double)milliwattperliterQuantity.Value, MilliwattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.MilliwattPerLiter, milliwattperliterQuantity.Unit);
-
- var nanowattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.NanowattPerCubicFoot);
- AssertEx.EqualTolerance(NanowattsPerCubicFootInOneWattPerCubicMeter, (double)nanowattpercubicfootQuantity.Value, NanowattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.NanowattPerCubicFoot, nanowattpercubicfootQuantity.Unit);
-
- var nanowattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.NanowattPerCubicInch);
- AssertEx.EqualTolerance(NanowattsPerCubicInchInOneWattPerCubicMeter, (double)nanowattpercubicinchQuantity.Value, NanowattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.NanowattPerCubicInch, nanowattpercubicinchQuantity.Unit);
-
- var nanowattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.NanowattPerCubicMeter);
- AssertEx.EqualTolerance(NanowattsPerCubicMeterInOneWattPerCubicMeter, (double)nanowattpercubicmeterQuantity.Value, NanowattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.NanowattPerCubicMeter, nanowattpercubicmeterQuantity.Unit);
-
- var nanowattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.NanowattPerLiter);
- AssertEx.EqualTolerance(NanowattsPerLiterInOneWattPerCubicMeter, (double)nanowattperliterQuantity.Value, NanowattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.NanowattPerLiter, nanowattperliterQuantity.Unit);
-
- var picowattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.PicowattPerCubicFoot);
- AssertEx.EqualTolerance(PicowattsPerCubicFootInOneWattPerCubicMeter, (double)picowattpercubicfootQuantity.Value, PicowattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.PicowattPerCubicFoot, picowattpercubicfootQuantity.Unit);
-
- var picowattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.PicowattPerCubicInch);
- AssertEx.EqualTolerance(PicowattsPerCubicInchInOneWattPerCubicMeter, (double)picowattpercubicinchQuantity.Value, PicowattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.PicowattPerCubicInch, picowattpercubicinchQuantity.Unit);
-
- var picowattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.PicowattPerCubicMeter);
- AssertEx.EqualTolerance(PicowattsPerCubicMeterInOneWattPerCubicMeter, (double)picowattpercubicmeterQuantity.Value, PicowattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.PicowattPerCubicMeter, picowattpercubicmeterQuantity.Unit);
-
- var picowattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.PicowattPerLiter);
- AssertEx.EqualTolerance(PicowattsPerLiterInOneWattPerCubicMeter, (double)picowattperliterQuantity.Value, PicowattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.PicowattPerLiter, picowattperliterQuantity.Unit);
-
- var terawattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.TerawattPerCubicFoot);
- AssertEx.EqualTolerance(TerawattsPerCubicFootInOneWattPerCubicMeter, (double)terawattpercubicfootQuantity.Value, TerawattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.TerawattPerCubicFoot, terawattpercubicfootQuantity.Unit);
-
- var terawattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.TerawattPerCubicInch);
- AssertEx.EqualTolerance(TerawattsPerCubicInchInOneWattPerCubicMeter, (double)terawattpercubicinchQuantity.Value, TerawattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.TerawattPerCubicInch, terawattpercubicinchQuantity.Unit);
-
- var terawattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.TerawattPerCubicMeter);
- AssertEx.EqualTolerance(TerawattsPerCubicMeterInOneWattPerCubicMeter, (double)terawattpercubicmeterQuantity.Value, TerawattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.TerawattPerCubicMeter, terawattpercubicmeterQuantity.Unit);
-
- var terawattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.TerawattPerLiter);
- AssertEx.EqualTolerance(TerawattsPerLiterInOneWattPerCubicMeter, (double)terawattperliterQuantity.Value, TerawattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.TerawattPerLiter, terawattperliterQuantity.Unit);
-
- var wattpercubicfootQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.WattPerCubicFoot);
- AssertEx.EqualTolerance(WattsPerCubicFootInOneWattPerCubicMeter, (double)wattpercubicfootQuantity.Value, WattsPerCubicFootTolerance);
- Assert.Equal(PowerDensityUnit.WattPerCubicFoot, wattpercubicfootQuantity.Unit);
-
- var wattpercubicinchQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.WattPerCubicInch);
- AssertEx.EqualTolerance(WattsPerCubicInchInOneWattPerCubicMeter, (double)wattpercubicinchQuantity.Value, WattsPerCubicInchTolerance);
- Assert.Equal(PowerDensityUnit.WattPerCubicInch, wattpercubicinchQuantity.Unit);
-
- var wattpercubicmeterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.WattPerCubicMeter);
- AssertEx.EqualTolerance(WattsPerCubicMeterInOneWattPerCubicMeter, (double)wattpercubicmeterQuantity.Value, WattsPerCubicMeterTolerance);
- Assert.Equal(PowerDensityUnit.WattPerCubicMeter, wattpercubicmeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattperliterQuantity = wattpercubicmeter.ToUnit(PowerDensityUnit.WattPerLiter);
- AssertEx.EqualTolerance(WattsPerLiterInOneWattPerCubicMeter, (double)wattperliterQuantity.Value, WattsPerLiterTolerance);
- Assert.Equal(PowerDensityUnit.WattPerLiter, wattperliterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PowerDensityUnit unit)
+ {
+ var quantity = PowerDensity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PowerDensityUnit unit)
{
- var quantityInBaseUnit = PowerDensity.FromWattsPerCubicMeter(1).ToBaseUnit();
- Assert.Equal(PowerDensity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = PowerDensity.Units.FirstOrDefault(u => u != PowerDensity.BaseUnit && u != PowerDensityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PowerDensityUnit.Undefined)
+ fromUnit = PowerDensity.BaseUnit;
+
+ var quantity = PowerDensity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PowerRatioTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PowerRatioTestsBase.g.cs
index 2468327188..2105344f09 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PowerRatioTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PowerRatioTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -45,6 +46,22 @@ public abstract partial class PowerRatioTestsBase : QuantityTestsBase
protected virtual double DecibelWattsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PowerRatioUnit unit)
+ {
+ return unit switch
+ {
+ PowerRatioUnit.DecibelMilliwatt => (DecibelMilliwattsInOneDecibelWatt, DecibelMilliwattsTolerance),
+ PowerRatioUnit.DecibelWatt => (DecibelWattsInOneDecibelWatt, DecibelWattsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PowerRatioUnit.DecibelMilliwatt },
+ new object[] { PowerRatioUnit.DecibelWatt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -172,25 +189,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PowerRatioUnit unit)
{
- var decibelwatt = PowerRatio.FromDecibelWatts(1);
+ var inBaseUnits = PowerRatio.From(1.0, PowerRatio.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var decibelmilliwattQuantity = decibelwatt.ToUnit(PowerRatioUnit.DecibelMilliwatt);
- AssertEx.EqualTolerance(DecibelMilliwattsInOneDecibelWatt, (double)decibelmilliwattQuantity.Value, DecibelMilliwattsTolerance);
- Assert.Equal(PowerRatioUnit.DecibelMilliwatt, decibelmilliwattQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var decibelwattQuantity = decibelwatt.ToUnit(PowerRatioUnit.DecibelWatt);
- AssertEx.EqualTolerance(DecibelWattsInOneDecibelWatt, (double)decibelwattQuantity.Value, DecibelWattsTolerance);
- Assert.Equal(PowerRatioUnit.DecibelWatt, decibelwattQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PowerRatioUnit unit)
+ {
+ var quantity = PowerRatio.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PowerRatioUnit unit)
{
- var quantityInBaseUnit = PowerRatio.FromDecibelWatts(1).ToBaseUnit();
- Assert.Equal(PowerRatio.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = PowerRatio.Units.FirstOrDefault(u => u != PowerRatio.BaseUnit && u != PowerRatioUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PowerRatioUnit.Undefined)
+ fromUnit = PowerRatio.BaseUnit;
+
+ var quantity = PowerRatio.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PowerTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PowerTestsBase.g.cs
index 234c3c9f09..7dcfd7a6a4 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PowerTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PowerTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -91,6 +92,68 @@ public abstract partial class PowerTestsBase : QuantityTestsBase
protected virtual double WattsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PowerUnit unit)
+ {
+ return unit switch
+ {
+ PowerUnit.BoilerHorsepower => (BoilerHorsepowerInOneWatt, BoilerHorsepowerTolerance),
+ PowerUnit.BritishThermalUnitPerHour => (BritishThermalUnitsPerHourInOneWatt, BritishThermalUnitsPerHourTolerance),
+ PowerUnit.Decawatt => (DecawattsInOneWatt, DecawattsTolerance),
+ PowerUnit.Deciwatt => (DeciwattsInOneWatt, DeciwattsTolerance),
+ PowerUnit.ElectricalHorsepower => (ElectricalHorsepowerInOneWatt, ElectricalHorsepowerTolerance),
+ PowerUnit.Femtowatt => (FemtowattsInOneWatt, FemtowattsTolerance),
+ PowerUnit.GigajoulePerHour => (GigajoulesPerHourInOneWatt, GigajoulesPerHourTolerance),
+ PowerUnit.Gigawatt => (GigawattsInOneWatt, GigawattsTolerance),
+ PowerUnit.HydraulicHorsepower => (HydraulicHorsepowerInOneWatt, HydraulicHorsepowerTolerance),
+ PowerUnit.JoulePerHour => (JoulesPerHourInOneWatt, JoulesPerHourTolerance),
+ PowerUnit.KilobritishThermalUnitPerHour => (KilobritishThermalUnitsPerHourInOneWatt, KilobritishThermalUnitsPerHourTolerance),
+ PowerUnit.KilojoulePerHour => (KilojoulesPerHourInOneWatt, KilojoulesPerHourTolerance),
+ PowerUnit.Kilowatt => (KilowattsInOneWatt, KilowattsTolerance),
+ PowerUnit.MechanicalHorsepower => (MechanicalHorsepowerInOneWatt, MechanicalHorsepowerTolerance),
+ PowerUnit.MegajoulePerHour => (MegajoulesPerHourInOneWatt, MegajoulesPerHourTolerance),
+ PowerUnit.Megawatt => (MegawattsInOneWatt, MegawattsTolerance),
+ PowerUnit.MetricHorsepower => (MetricHorsepowerInOneWatt, MetricHorsepowerTolerance),
+ PowerUnit.Microwatt => (MicrowattsInOneWatt, MicrowattsTolerance),
+ PowerUnit.MillijoulePerHour => (MillijoulesPerHourInOneWatt, MillijoulesPerHourTolerance),
+ PowerUnit.Milliwatt => (MilliwattsInOneWatt, MilliwattsTolerance),
+ PowerUnit.Nanowatt => (NanowattsInOneWatt, NanowattsTolerance),
+ PowerUnit.Petawatt => (PetawattsInOneWatt, PetawattsTolerance),
+ PowerUnit.Picowatt => (PicowattsInOneWatt, PicowattsTolerance),
+ PowerUnit.Terawatt => (TerawattsInOneWatt, TerawattsTolerance),
+ PowerUnit.Watt => (WattsInOneWatt, WattsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PowerUnit.BoilerHorsepower },
+ new object[] { PowerUnit.BritishThermalUnitPerHour },
+ new object[] { PowerUnit.Decawatt },
+ new object[] { PowerUnit.Deciwatt },
+ new object[] { PowerUnit.ElectricalHorsepower },
+ new object[] { PowerUnit.Femtowatt },
+ new object[] { PowerUnit.GigajoulePerHour },
+ new object[] { PowerUnit.Gigawatt },
+ new object[] { PowerUnit.HydraulicHorsepower },
+ new object[] { PowerUnit.JoulePerHour },
+ new object[] { PowerUnit.KilobritishThermalUnitPerHour },
+ new object[] { PowerUnit.KilojoulePerHour },
+ new object[] { PowerUnit.Kilowatt },
+ new object[] { PowerUnit.MechanicalHorsepower },
+ new object[] { PowerUnit.MegajoulePerHour },
+ new object[] { PowerUnit.Megawatt },
+ new object[] { PowerUnit.MetricHorsepower },
+ new object[] { PowerUnit.Microwatt },
+ new object[] { PowerUnit.MillijoulePerHour },
+ new object[] { PowerUnit.Milliwatt },
+ new object[] { PowerUnit.Nanowatt },
+ new object[] { PowerUnit.Petawatt },
+ new object[] { PowerUnit.Picowatt },
+ new object[] { PowerUnit.Terawatt },
+ new object[] { PowerUnit.Watt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -331,117 +394,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PowerUnit unit)
{
- var watt = Power.FromWatts(1);
-
- var boilerhorsepowerQuantity = watt.ToUnit(PowerUnit.BoilerHorsepower);
- AssertEx.EqualTolerance(BoilerHorsepowerInOneWatt, (double)boilerhorsepowerQuantity.Value, BoilerHorsepowerTolerance);
- Assert.Equal(PowerUnit.BoilerHorsepower, boilerhorsepowerQuantity.Unit);
-
- var britishthermalunitperhourQuantity = watt.ToUnit(PowerUnit.BritishThermalUnitPerHour);
- AssertEx.EqualTolerance(BritishThermalUnitsPerHourInOneWatt, (double)britishthermalunitperhourQuantity.Value, BritishThermalUnitsPerHourTolerance);
- Assert.Equal(PowerUnit.BritishThermalUnitPerHour, britishthermalunitperhourQuantity.Unit);
-
- var decawattQuantity = watt.ToUnit(PowerUnit.Decawatt);
- AssertEx.EqualTolerance(DecawattsInOneWatt, (double)decawattQuantity.Value, DecawattsTolerance);
- Assert.Equal(PowerUnit.Decawatt, decawattQuantity.Unit);
-
- var deciwattQuantity = watt.ToUnit(PowerUnit.Deciwatt);
- AssertEx.EqualTolerance(DeciwattsInOneWatt, (double)deciwattQuantity.Value, DeciwattsTolerance);
- Assert.Equal(PowerUnit.Deciwatt, deciwattQuantity.Unit);
-
- var electricalhorsepowerQuantity = watt.ToUnit(PowerUnit.ElectricalHorsepower);
- AssertEx.EqualTolerance(ElectricalHorsepowerInOneWatt, (double)electricalhorsepowerQuantity.Value, ElectricalHorsepowerTolerance);
- Assert.Equal(PowerUnit.ElectricalHorsepower, electricalhorsepowerQuantity.Unit);
-
- var femtowattQuantity = watt.ToUnit(PowerUnit.Femtowatt);
- AssertEx.EqualTolerance(FemtowattsInOneWatt, (double)femtowattQuantity.Value, FemtowattsTolerance);
- Assert.Equal(PowerUnit.Femtowatt, femtowattQuantity.Unit);
-
- var gigajouleperhourQuantity = watt.ToUnit(PowerUnit.GigajoulePerHour);
- AssertEx.EqualTolerance(GigajoulesPerHourInOneWatt, (double)gigajouleperhourQuantity.Value, GigajoulesPerHourTolerance);
- Assert.Equal(PowerUnit.GigajoulePerHour, gigajouleperhourQuantity.Unit);
-
- var gigawattQuantity = watt.ToUnit(PowerUnit.Gigawatt);
- AssertEx.EqualTolerance(GigawattsInOneWatt, (double)gigawattQuantity.Value, GigawattsTolerance);
- Assert.Equal(PowerUnit.Gigawatt, gigawattQuantity.Unit);
-
- var hydraulichorsepowerQuantity = watt.ToUnit(PowerUnit.HydraulicHorsepower);
- AssertEx.EqualTolerance(HydraulicHorsepowerInOneWatt, (double)hydraulichorsepowerQuantity.Value, HydraulicHorsepowerTolerance);
- Assert.Equal(PowerUnit.HydraulicHorsepower, hydraulichorsepowerQuantity.Unit);
-
- var jouleperhourQuantity = watt.ToUnit(PowerUnit.JoulePerHour);
- AssertEx.EqualTolerance(JoulesPerHourInOneWatt, (double)jouleperhourQuantity.Value, JoulesPerHourTolerance);
- Assert.Equal(PowerUnit.JoulePerHour, jouleperhourQuantity.Unit);
-
- var kilobritishthermalunitperhourQuantity = watt.ToUnit(PowerUnit.KilobritishThermalUnitPerHour);
- AssertEx.EqualTolerance(KilobritishThermalUnitsPerHourInOneWatt, (double)kilobritishthermalunitperhourQuantity.Value, KilobritishThermalUnitsPerHourTolerance);
- Assert.Equal(PowerUnit.KilobritishThermalUnitPerHour, kilobritishthermalunitperhourQuantity.Unit);
-
- var kilojouleperhourQuantity = watt.ToUnit(PowerUnit.KilojoulePerHour);
- AssertEx.EqualTolerance(KilojoulesPerHourInOneWatt, (double)kilojouleperhourQuantity.Value, KilojoulesPerHourTolerance);
- Assert.Equal(PowerUnit.KilojoulePerHour, kilojouleperhourQuantity.Unit);
+ var inBaseUnits = Power.From(1.0, Power.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilowattQuantity = watt.ToUnit(PowerUnit.Kilowatt);
- AssertEx.EqualTolerance(KilowattsInOneWatt, (double)kilowattQuantity.Value, KilowattsTolerance);
- Assert.Equal(PowerUnit.Kilowatt, kilowattQuantity.Unit);
-
- var mechanicalhorsepowerQuantity = watt.ToUnit(PowerUnit.MechanicalHorsepower);
- AssertEx.EqualTolerance(MechanicalHorsepowerInOneWatt, (double)mechanicalhorsepowerQuantity.Value, MechanicalHorsepowerTolerance);
- Assert.Equal(PowerUnit.MechanicalHorsepower, mechanicalhorsepowerQuantity.Unit);
-
- var megajouleperhourQuantity = watt.ToUnit(PowerUnit.MegajoulePerHour);
- AssertEx.EqualTolerance(MegajoulesPerHourInOneWatt, (double)megajouleperhourQuantity.Value, MegajoulesPerHourTolerance);
- Assert.Equal(PowerUnit.MegajoulePerHour, megajouleperhourQuantity.Unit);
-
- var megawattQuantity = watt.ToUnit(PowerUnit.Megawatt);
- AssertEx.EqualTolerance(MegawattsInOneWatt, (double)megawattQuantity.Value, MegawattsTolerance);
- Assert.Equal(PowerUnit.Megawatt, megawattQuantity.Unit);
-
- var metrichorsepowerQuantity = watt.ToUnit(PowerUnit.MetricHorsepower);
- AssertEx.EqualTolerance(MetricHorsepowerInOneWatt, (double)metrichorsepowerQuantity.Value, MetricHorsepowerTolerance);
- Assert.Equal(PowerUnit.MetricHorsepower, metrichorsepowerQuantity.Unit);
-
- var microwattQuantity = watt.ToUnit(PowerUnit.Microwatt);
- AssertEx.EqualTolerance(MicrowattsInOneWatt, (double)microwattQuantity.Value, MicrowattsTolerance);
- Assert.Equal(PowerUnit.Microwatt, microwattQuantity.Unit);
-
- var millijouleperhourQuantity = watt.ToUnit(PowerUnit.MillijoulePerHour);
- AssertEx.EqualTolerance(MillijoulesPerHourInOneWatt, (double)millijouleperhourQuantity.Value, MillijoulesPerHourTolerance);
- Assert.Equal(PowerUnit.MillijoulePerHour, millijouleperhourQuantity.Unit);
-
- var milliwattQuantity = watt.ToUnit(PowerUnit.Milliwatt);
- AssertEx.EqualTolerance(MilliwattsInOneWatt, (double)milliwattQuantity.Value, MilliwattsTolerance);
- Assert.Equal(PowerUnit.Milliwatt, milliwattQuantity.Unit);
-
- var nanowattQuantity = watt.ToUnit(PowerUnit.Nanowatt);
- AssertEx.EqualTolerance(NanowattsInOneWatt, (double)nanowattQuantity.Value, NanowattsTolerance);
- Assert.Equal(PowerUnit.Nanowatt, nanowattQuantity.Unit);
-
- var petawattQuantity = watt.ToUnit(PowerUnit.Petawatt);
- AssertEx.EqualTolerance(PetawattsInOneWatt, (double)petawattQuantity.Value, PetawattsTolerance);
- Assert.Equal(PowerUnit.Petawatt, petawattQuantity.Unit);
-
- var picowattQuantity = watt.ToUnit(PowerUnit.Picowatt);
- AssertEx.EqualTolerance(PicowattsInOneWatt, (double)picowattQuantity.Value, PicowattsTolerance);
- Assert.Equal(PowerUnit.Picowatt, picowattQuantity.Unit);
-
- var terawattQuantity = watt.ToUnit(PowerUnit.Terawatt);
- AssertEx.EqualTolerance(TerawattsInOneWatt, (double)terawattQuantity.Value, TerawattsTolerance);
- Assert.Equal(PowerUnit.Terawatt, terawattQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattQuantity = watt.ToUnit(PowerUnit.Watt);
- AssertEx.EqualTolerance(WattsInOneWatt, (double)wattQuantity.Value, WattsTolerance);
- Assert.Equal(PowerUnit.Watt, wattQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PowerUnit unit)
+ {
+ var quantity = Power.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PowerUnit unit)
{
- var quantityInBaseUnit = Power.FromWatts(1).ToBaseUnit();
- Assert.Equal(Power.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Power.Units.FirstOrDefault(u => u != Power.BaseUnit && u != PowerUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PowerUnit.Undefined)
+ fromUnit = Power.BaseUnit;
+
+ var quantity = Power.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PressureChangeRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PressureChangeRateTestsBase.g.cs
index cc9e185838..6a73fa3571 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PressureChangeRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PressureChangeRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -69,6 +70,46 @@ public abstract partial class PressureChangeRateTestsBase : QuantityTestsBase
protected virtual double PoundsForcePerSquareInchPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PressureChangeRateUnit unit)
+ {
+ return unit switch
+ {
+ PressureChangeRateUnit.AtmospherePerSecond => (AtmospheresPerSecondInOnePascalPerSecond, AtmospheresPerSecondTolerance),
+ PressureChangeRateUnit.KilopascalPerMinute => (KilopascalsPerMinuteInOnePascalPerSecond, KilopascalsPerMinuteTolerance),
+ PressureChangeRateUnit.KilopascalPerSecond => (KilopascalsPerSecondInOnePascalPerSecond, KilopascalsPerSecondTolerance),
+ PressureChangeRateUnit.KilopoundForcePerSquareInchPerMinute => (KilopoundsForcePerSquareInchPerMinuteInOnePascalPerSecond, KilopoundsForcePerSquareInchPerMinuteTolerance),
+ PressureChangeRateUnit.KilopoundForcePerSquareInchPerSecond => (KilopoundsForcePerSquareInchPerSecondInOnePascalPerSecond, KilopoundsForcePerSquareInchPerSecondTolerance),
+ PressureChangeRateUnit.MegapascalPerMinute => (MegapascalsPerMinuteInOnePascalPerSecond, MegapascalsPerMinuteTolerance),
+ PressureChangeRateUnit.MegapascalPerSecond => (MegapascalsPerSecondInOnePascalPerSecond, MegapascalsPerSecondTolerance),
+ PressureChangeRateUnit.MegapoundForcePerSquareInchPerMinute => (MegapoundsForcePerSquareInchPerMinuteInOnePascalPerSecond, MegapoundsForcePerSquareInchPerMinuteTolerance),
+ PressureChangeRateUnit.MegapoundForcePerSquareInchPerSecond => (MegapoundsForcePerSquareInchPerSecondInOnePascalPerSecond, MegapoundsForcePerSquareInchPerSecondTolerance),
+ PressureChangeRateUnit.MillimeterOfMercuryPerSecond => (MillimetersOfMercuryPerSecondInOnePascalPerSecond, MillimetersOfMercuryPerSecondTolerance),
+ PressureChangeRateUnit.PascalPerMinute => (PascalsPerMinuteInOnePascalPerSecond, PascalsPerMinuteTolerance),
+ PressureChangeRateUnit.PascalPerSecond => (PascalsPerSecondInOnePascalPerSecond, PascalsPerSecondTolerance),
+ PressureChangeRateUnit.PoundForcePerSquareInchPerMinute => (PoundsForcePerSquareInchPerMinuteInOnePascalPerSecond, PoundsForcePerSquareInchPerMinuteTolerance),
+ PressureChangeRateUnit.PoundForcePerSquareInchPerSecond => (PoundsForcePerSquareInchPerSecondInOnePascalPerSecond, PoundsForcePerSquareInchPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PressureChangeRateUnit.AtmospherePerSecond },
+ new object[] { PressureChangeRateUnit.KilopascalPerMinute },
+ new object[] { PressureChangeRateUnit.KilopascalPerSecond },
+ new object[] { PressureChangeRateUnit.KilopoundForcePerSquareInchPerMinute },
+ new object[] { PressureChangeRateUnit.KilopoundForcePerSquareInchPerSecond },
+ new object[] { PressureChangeRateUnit.MegapascalPerMinute },
+ new object[] { PressureChangeRateUnit.MegapascalPerSecond },
+ new object[] { PressureChangeRateUnit.MegapoundForcePerSquareInchPerMinute },
+ new object[] { PressureChangeRateUnit.MegapoundForcePerSquareInchPerSecond },
+ new object[] { PressureChangeRateUnit.MillimeterOfMercuryPerSecond },
+ new object[] { PressureChangeRateUnit.PascalPerMinute },
+ new object[] { PressureChangeRateUnit.PascalPerSecond },
+ new object[] { PressureChangeRateUnit.PoundForcePerSquareInchPerMinute },
+ new object[] { PressureChangeRateUnit.PoundForcePerSquareInchPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -268,73 +309,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PressureChangeRateUnit unit)
{
- var pascalpersecond = PressureChangeRate.FromPascalsPerSecond(1);
-
- var atmospherepersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.AtmospherePerSecond);
- AssertEx.EqualTolerance(AtmospheresPerSecondInOnePascalPerSecond, (double)atmospherepersecondQuantity.Value, AtmospheresPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.AtmospherePerSecond, atmospherepersecondQuantity.Unit);
-
- var kilopascalperminuteQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.KilopascalPerMinute);
- AssertEx.EqualTolerance(KilopascalsPerMinuteInOnePascalPerSecond, (double)kilopascalperminuteQuantity.Value, KilopascalsPerMinuteTolerance);
- Assert.Equal(PressureChangeRateUnit.KilopascalPerMinute, kilopascalperminuteQuantity.Unit);
-
- var kilopascalpersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.KilopascalPerSecond);
- AssertEx.EqualTolerance(KilopascalsPerSecondInOnePascalPerSecond, (double)kilopascalpersecondQuantity.Value, KilopascalsPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.KilopascalPerSecond, kilopascalpersecondQuantity.Unit);
-
- var kilopoundforcepersquareinchperminuteQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.KilopoundForcePerSquareInchPerMinute);
- AssertEx.EqualTolerance(KilopoundsForcePerSquareInchPerMinuteInOnePascalPerSecond, (double)kilopoundforcepersquareinchperminuteQuantity.Value, KilopoundsForcePerSquareInchPerMinuteTolerance);
- Assert.Equal(PressureChangeRateUnit.KilopoundForcePerSquareInchPerMinute, kilopoundforcepersquareinchperminuteQuantity.Unit);
-
- var kilopoundforcepersquareinchpersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.KilopoundForcePerSquareInchPerSecond);
- AssertEx.EqualTolerance(KilopoundsForcePerSquareInchPerSecondInOnePascalPerSecond, (double)kilopoundforcepersquareinchpersecondQuantity.Value, KilopoundsForcePerSquareInchPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.KilopoundForcePerSquareInchPerSecond, kilopoundforcepersquareinchpersecondQuantity.Unit);
-
- var megapascalperminuteQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.MegapascalPerMinute);
- AssertEx.EqualTolerance(MegapascalsPerMinuteInOnePascalPerSecond, (double)megapascalperminuteQuantity.Value, MegapascalsPerMinuteTolerance);
- Assert.Equal(PressureChangeRateUnit.MegapascalPerMinute, megapascalperminuteQuantity.Unit);
-
- var megapascalpersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.MegapascalPerSecond);
- AssertEx.EqualTolerance(MegapascalsPerSecondInOnePascalPerSecond, (double)megapascalpersecondQuantity.Value, MegapascalsPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.MegapascalPerSecond, megapascalpersecondQuantity.Unit);
+ var inBaseUnits = PressureChangeRate.From(1.0, PressureChangeRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var megapoundforcepersquareinchperminuteQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.MegapoundForcePerSquareInchPerMinute);
- AssertEx.EqualTolerance(MegapoundsForcePerSquareInchPerMinuteInOnePascalPerSecond, (double)megapoundforcepersquareinchperminuteQuantity.Value, MegapoundsForcePerSquareInchPerMinuteTolerance);
- Assert.Equal(PressureChangeRateUnit.MegapoundForcePerSquareInchPerMinute, megapoundforcepersquareinchperminuteQuantity.Unit);
-
- var megapoundforcepersquareinchpersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.MegapoundForcePerSquareInchPerSecond);
- AssertEx.EqualTolerance(MegapoundsForcePerSquareInchPerSecondInOnePascalPerSecond, (double)megapoundforcepersquareinchpersecondQuantity.Value, MegapoundsForcePerSquareInchPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.MegapoundForcePerSquareInchPerSecond, megapoundforcepersquareinchpersecondQuantity.Unit);
-
- var millimeterofmercurypersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.MillimeterOfMercuryPerSecond);
- AssertEx.EqualTolerance(MillimetersOfMercuryPerSecondInOnePascalPerSecond, (double)millimeterofmercurypersecondQuantity.Value, MillimetersOfMercuryPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.MillimeterOfMercuryPerSecond, millimeterofmercurypersecondQuantity.Unit);
-
- var pascalperminuteQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.PascalPerMinute);
- AssertEx.EqualTolerance(PascalsPerMinuteInOnePascalPerSecond, (double)pascalperminuteQuantity.Value, PascalsPerMinuteTolerance);
- Assert.Equal(PressureChangeRateUnit.PascalPerMinute, pascalperminuteQuantity.Unit);
-
- var pascalpersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.PascalPerSecond);
- AssertEx.EqualTolerance(PascalsPerSecondInOnePascalPerSecond, (double)pascalpersecondQuantity.Value, PascalsPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.PascalPerSecond, pascalpersecondQuantity.Unit);
-
- var poundforcepersquareinchperminuteQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.PoundForcePerSquareInchPerMinute);
- AssertEx.EqualTolerance(PoundsForcePerSquareInchPerMinuteInOnePascalPerSecond, (double)poundforcepersquareinchperminuteQuantity.Value, PoundsForcePerSquareInchPerMinuteTolerance);
- Assert.Equal(PressureChangeRateUnit.PoundForcePerSquareInchPerMinute, poundforcepersquareinchperminuteQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundforcepersquareinchpersecondQuantity = pascalpersecond.ToUnit(PressureChangeRateUnit.PoundForcePerSquareInchPerSecond);
- AssertEx.EqualTolerance(PoundsForcePerSquareInchPerSecondInOnePascalPerSecond, (double)poundforcepersquareinchpersecondQuantity.Value, PoundsForcePerSquareInchPerSecondTolerance);
- Assert.Equal(PressureChangeRateUnit.PoundForcePerSquareInchPerSecond, poundforcepersquareinchpersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PressureChangeRateUnit unit)
+ {
+ var quantity = PressureChangeRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PressureChangeRateUnit unit)
{
- var quantityInBaseUnit = PressureChangeRate.FromPascalsPerSecond(1).ToBaseUnit();
- Assert.Equal(PressureChangeRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = PressureChangeRate.Units.FirstOrDefault(u => u != PressureChangeRate.BaseUnit && u != PressureChangeRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PressureChangeRateUnit.Undefined)
+ fromUnit = PressureChangeRate.BaseUnit;
+
+ var quantity = PressureChangeRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/PressureTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/PressureTestsBase.g.cs
index 84f3bfa29b..5c78f02394 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/PressureTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/PressureTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -131,6 +132,108 @@ public abstract partial class PressureTestsBase : QuantityTestsBase
protected virtual double TorrsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(PressureUnit unit)
+ {
+ return unit switch
+ {
+ PressureUnit.Atmosphere => (AtmospheresInOnePascal, AtmospheresTolerance),
+ PressureUnit.Bar => (BarsInOnePascal, BarsTolerance),
+ PressureUnit.Centibar => (CentibarsInOnePascal, CentibarsTolerance),
+ PressureUnit.Decapascal => (DecapascalsInOnePascal, DecapascalsTolerance),
+ PressureUnit.Decibar => (DecibarsInOnePascal, DecibarsTolerance),
+ PressureUnit.DynePerSquareCentimeter => (DynesPerSquareCentimeterInOnePascal, DynesPerSquareCentimeterTolerance),
+ PressureUnit.FootOfElevation => (FeetOfElevationInOnePascal, FeetOfElevationTolerance),
+ PressureUnit.FootOfHead => (FeetOfHeadInOnePascal, FeetOfHeadTolerance),
+ PressureUnit.Gigapascal => (GigapascalsInOnePascal, GigapascalsTolerance),
+ PressureUnit.Hectopascal => (HectopascalsInOnePascal, HectopascalsTolerance),
+ PressureUnit.InchOfMercury => (InchesOfMercuryInOnePascal, InchesOfMercuryTolerance),
+ PressureUnit.InchOfWaterColumn => (InchesOfWaterColumnInOnePascal, InchesOfWaterColumnTolerance),
+ PressureUnit.Kilobar => (KilobarsInOnePascal, KilobarsTolerance),
+ PressureUnit.KilogramForcePerSquareCentimeter => (KilogramsForcePerSquareCentimeterInOnePascal, KilogramsForcePerSquareCentimeterTolerance),
+ PressureUnit.KilogramForcePerSquareMeter => (KilogramsForcePerSquareMeterInOnePascal, KilogramsForcePerSquareMeterTolerance),
+ PressureUnit.KilogramForcePerSquareMillimeter => (KilogramsForcePerSquareMillimeterInOnePascal, KilogramsForcePerSquareMillimeterTolerance),
+ PressureUnit.KilonewtonPerSquareCentimeter => (KilonewtonsPerSquareCentimeterInOnePascal, KilonewtonsPerSquareCentimeterTolerance),
+ PressureUnit.KilonewtonPerSquareMeter => (KilonewtonsPerSquareMeterInOnePascal, KilonewtonsPerSquareMeterTolerance),
+ PressureUnit.KilonewtonPerSquareMillimeter => (KilonewtonsPerSquareMillimeterInOnePascal, KilonewtonsPerSquareMillimeterTolerance),
+ PressureUnit.Kilopascal => (KilopascalsInOnePascal, KilopascalsTolerance),
+ PressureUnit.KilopoundForcePerSquareFoot => (KilopoundsForcePerSquareFootInOnePascal, KilopoundsForcePerSquareFootTolerance),
+ PressureUnit.KilopoundForcePerSquareInch => (KilopoundsForcePerSquareInchInOnePascal, KilopoundsForcePerSquareInchTolerance),
+ PressureUnit.Megabar => (MegabarsInOnePascal, MegabarsTolerance),
+ PressureUnit.MeganewtonPerSquareMeter => (MeganewtonsPerSquareMeterInOnePascal, MeganewtonsPerSquareMeterTolerance),
+ PressureUnit.Megapascal => (MegapascalsInOnePascal, MegapascalsTolerance),
+ PressureUnit.MeterOfElevation => (MetersOfElevationInOnePascal, MetersOfElevationTolerance),
+ PressureUnit.MeterOfHead => (MetersOfHeadInOnePascal, MetersOfHeadTolerance),
+ PressureUnit.Microbar => (MicrobarsInOnePascal, MicrobarsTolerance),
+ PressureUnit.Micropascal => (MicropascalsInOnePascal, MicropascalsTolerance),
+ PressureUnit.Millibar => (MillibarsInOnePascal, MillibarsTolerance),
+ PressureUnit.MillimeterOfMercury => (MillimetersOfMercuryInOnePascal, MillimetersOfMercuryTolerance),
+ PressureUnit.MillimeterOfWaterColumn => (MillimeterOfWaterColumnInOnePascal, MillimeterOfWaterColumnTolerance),
+ PressureUnit.Millipascal => (MillipascalsInOnePascal, MillipascalsTolerance),
+ PressureUnit.NewtonPerSquareCentimeter => (NewtonsPerSquareCentimeterInOnePascal, NewtonsPerSquareCentimeterTolerance),
+ PressureUnit.NewtonPerSquareMeter => (NewtonsPerSquareMeterInOnePascal, NewtonsPerSquareMeterTolerance),
+ PressureUnit.NewtonPerSquareMillimeter => (NewtonsPerSquareMillimeterInOnePascal, NewtonsPerSquareMillimeterTolerance),
+ PressureUnit.Pascal => (PascalsInOnePascal, PascalsTolerance),
+ PressureUnit.PoundForcePerSquareFoot => (PoundsForcePerSquareFootInOnePascal, PoundsForcePerSquareFootTolerance),
+ PressureUnit.PoundForcePerSquareInch => (PoundsForcePerSquareInchInOnePascal, PoundsForcePerSquareInchTolerance),
+ PressureUnit.PoundPerInchSecondSquared => (PoundsPerInchSecondSquaredInOnePascal, PoundsPerInchSecondSquaredTolerance),
+ PressureUnit.TechnicalAtmosphere => (TechnicalAtmospheresInOnePascal, TechnicalAtmospheresTolerance),
+ PressureUnit.TonneForcePerSquareCentimeter => (TonnesForcePerSquareCentimeterInOnePascal, TonnesForcePerSquareCentimeterTolerance),
+ PressureUnit.TonneForcePerSquareMeter => (TonnesForcePerSquareMeterInOnePascal, TonnesForcePerSquareMeterTolerance),
+ PressureUnit.TonneForcePerSquareMillimeter => (TonnesForcePerSquareMillimeterInOnePascal, TonnesForcePerSquareMillimeterTolerance),
+ PressureUnit.Torr => (TorrsInOnePascal, TorrsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { PressureUnit.Atmosphere },
+ new object[] { PressureUnit.Bar },
+ new object[] { PressureUnit.Centibar },
+ new object[] { PressureUnit.Decapascal },
+ new object[] { PressureUnit.Decibar },
+ new object[] { PressureUnit.DynePerSquareCentimeter },
+ new object[] { PressureUnit.FootOfElevation },
+ new object[] { PressureUnit.FootOfHead },
+ new object[] { PressureUnit.Gigapascal },
+ new object[] { PressureUnit.Hectopascal },
+ new object[] { PressureUnit.InchOfMercury },
+ new object[] { PressureUnit.InchOfWaterColumn },
+ new object[] { PressureUnit.Kilobar },
+ new object[] { PressureUnit.KilogramForcePerSquareCentimeter },
+ new object[] { PressureUnit.KilogramForcePerSquareMeter },
+ new object[] { PressureUnit.KilogramForcePerSquareMillimeter },
+ new object[] { PressureUnit.KilonewtonPerSquareCentimeter },
+ new object[] { PressureUnit.KilonewtonPerSquareMeter },
+ new object[] { PressureUnit.KilonewtonPerSquareMillimeter },
+ new object[] { PressureUnit.Kilopascal },
+ new object[] { PressureUnit.KilopoundForcePerSquareFoot },
+ new object[] { PressureUnit.KilopoundForcePerSquareInch },
+ new object[] { PressureUnit.Megabar },
+ new object[] { PressureUnit.MeganewtonPerSquareMeter },
+ new object[] { PressureUnit.Megapascal },
+ new object[] { PressureUnit.MeterOfElevation },
+ new object[] { PressureUnit.MeterOfHead },
+ new object[] { PressureUnit.Microbar },
+ new object[] { PressureUnit.Micropascal },
+ new object[] { PressureUnit.Millibar },
+ new object[] { PressureUnit.MillimeterOfMercury },
+ new object[] { PressureUnit.MillimeterOfWaterColumn },
+ new object[] { PressureUnit.Millipascal },
+ new object[] { PressureUnit.NewtonPerSquareCentimeter },
+ new object[] { PressureUnit.NewtonPerSquareMeter },
+ new object[] { PressureUnit.NewtonPerSquareMillimeter },
+ new object[] { PressureUnit.Pascal },
+ new object[] { PressureUnit.PoundForcePerSquareFoot },
+ new object[] { PressureUnit.PoundForcePerSquareInch },
+ new object[] { PressureUnit.PoundPerInchSecondSquared },
+ new object[] { PressureUnit.TechnicalAtmosphere },
+ new object[] { PressureUnit.TonneForcePerSquareCentimeter },
+ new object[] { PressureUnit.TonneForcePerSquareMeter },
+ new object[] { PressureUnit.TonneForcePerSquareMillimeter },
+ new object[] { PressureUnit.Torr },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -516,197 +619,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(PressureUnit unit)
{
- var pascal = Pressure.FromPascals(1);
-
- var atmosphereQuantity = pascal.ToUnit(PressureUnit.Atmosphere);
- AssertEx.EqualTolerance(AtmospheresInOnePascal, (double)atmosphereQuantity.Value, AtmospheresTolerance);
- Assert.Equal(PressureUnit.Atmosphere, atmosphereQuantity.Unit);
-
- var barQuantity = pascal.ToUnit(PressureUnit.Bar);
- AssertEx.EqualTolerance(BarsInOnePascal, (double)barQuantity.Value, BarsTolerance);
- Assert.Equal(PressureUnit.Bar, barQuantity.Unit);
-
- var centibarQuantity = pascal.ToUnit(PressureUnit.Centibar);
- AssertEx.EqualTolerance(CentibarsInOnePascal, (double)centibarQuantity.Value, CentibarsTolerance);
- Assert.Equal(PressureUnit.Centibar, centibarQuantity.Unit);
-
- var decapascalQuantity = pascal.ToUnit(PressureUnit.Decapascal);
- AssertEx.EqualTolerance(DecapascalsInOnePascal, (double)decapascalQuantity.Value, DecapascalsTolerance);
- Assert.Equal(PressureUnit.Decapascal, decapascalQuantity.Unit);
-
- var decibarQuantity = pascal.ToUnit(PressureUnit.Decibar);
- AssertEx.EqualTolerance(DecibarsInOnePascal, (double)decibarQuantity.Value, DecibarsTolerance);
- Assert.Equal(PressureUnit.Decibar, decibarQuantity.Unit);
-
- var dynepersquarecentimeterQuantity = pascal.ToUnit(PressureUnit.DynePerSquareCentimeter);
- AssertEx.EqualTolerance(DynesPerSquareCentimeterInOnePascal, (double)dynepersquarecentimeterQuantity.Value, DynesPerSquareCentimeterTolerance);
- Assert.Equal(PressureUnit.DynePerSquareCentimeter, dynepersquarecentimeterQuantity.Unit);
-
- var footofelevationQuantity = pascal.ToUnit(PressureUnit.FootOfElevation);
- AssertEx.EqualTolerance(FeetOfElevationInOnePascal, (double)footofelevationQuantity.Value, FeetOfElevationTolerance);
- Assert.Equal(PressureUnit.FootOfElevation, footofelevationQuantity.Unit);
-
- var footofheadQuantity = pascal.ToUnit(PressureUnit.FootOfHead);
- AssertEx.EqualTolerance(FeetOfHeadInOnePascal, (double)footofheadQuantity.Value, FeetOfHeadTolerance);
- Assert.Equal(PressureUnit.FootOfHead, footofheadQuantity.Unit);
-
- var gigapascalQuantity = pascal.ToUnit(PressureUnit.Gigapascal);
- AssertEx.EqualTolerance(GigapascalsInOnePascal, (double)gigapascalQuantity.Value, GigapascalsTolerance);
- Assert.Equal(PressureUnit.Gigapascal, gigapascalQuantity.Unit);
-
- var hectopascalQuantity = pascal.ToUnit(PressureUnit.Hectopascal);
- AssertEx.EqualTolerance(HectopascalsInOnePascal, (double)hectopascalQuantity.Value, HectopascalsTolerance);
- Assert.Equal(PressureUnit.Hectopascal, hectopascalQuantity.Unit);
-
- var inchofmercuryQuantity = pascal.ToUnit(PressureUnit.InchOfMercury);
- AssertEx.EqualTolerance(InchesOfMercuryInOnePascal, (double)inchofmercuryQuantity.Value, InchesOfMercuryTolerance);
- Assert.Equal(PressureUnit.InchOfMercury, inchofmercuryQuantity.Unit);
-
- var inchofwatercolumnQuantity = pascal.ToUnit(PressureUnit.InchOfWaterColumn);
- AssertEx.EqualTolerance(InchesOfWaterColumnInOnePascal, (double)inchofwatercolumnQuantity.Value, InchesOfWaterColumnTolerance);
- Assert.Equal(PressureUnit.InchOfWaterColumn, inchofwatercolumnQuantity.Unit);
-
- var kilobarQuantity = pascal.ToUnit(PressureUnit.Kilobar);
- AssertEx.EqualTolerance(KilobarsInOnePascal, (double)kilobarQuantity.Value, KilobarsTolerance);
- Assert.Equal(PressureUnit.Kilobar, kilobarQuantity.Unit);
-
- var kilogramforcepersquarecentimeterQuantity = pascal.ToUnit(PressureUnit.KilogramForcePerSquareCentimeter);
- AssertEx.EqualTolerance(KilogramsForcePerSquareCentimeterInOnePascal, (double)kilogramforcepersquarecentimeterQuantity.Value, KilogramsForcePerSquareCentimeterTolerance);
- Assert.Equal(PressureUnit.KilogramForcePerSquareCentimeter, kilogramforcepersquarecentimeterQuantity.Unit);
-
- var kilogramforcepersquaremeterQuantity = pascal.ToUnit(PressureUnit.KilogramForcePerSquareMeter);
- AssertEx.EqualTolerance(KilogramsForcePerSquareMeterInOnePascal, (double)kilogramforcepersquaremeterQuantity.Value, KilogramsForcePerSquareMeterTolerance);
- Assert.Equal(PressureUnit.KilogramForcePerSquareMeter, kilogramforcepersquaremeterQuantity.Unit);
-
- var kilogramforcepersquaremillimeterQuantity = pascal.ToUnit(PressureUnit.KilogramForcePerSquareMillimeter);
- AssertEx.EqualTolerance(KilogramsForcePerSquareMillimeterInOnePascal, (double)kilogramforcepersquaremillimeterQuantity.Value, KilogramsForcePerSquareMillimeterTolerance);
- Assert.Equal(PressureUnit.KilogramForcePerSquareMillimeter, kilogramforcepersquaremillimeterQuantity.Unit);
-
- var kilonewtonpersquarecentimeterQuantity = pascal.ToUnit(PressureUnit.KilonewtonPerSquareCentimeter);
- AssertEx.EqualTolerance(KilonewtonsPerSquareCentimeterInOnePascal, (double)kilonewtonpersquarecentimeterQuantity.Value, KilonewtonsPerSquareCentimeterTolerance);
- Assert.Equal(PressureUnit.KilonewtonPerSquareCentimeter, kilonewtonpersquarecentimeterQuantity.Unit);
-
- var kilonewtonpersquaremeterQuantity = pascal.ToUnit(PressureUnit.KilonewtonPerSquareMeter);
- AssertEx.EqualTolerance(KilonewtonsPerSquareMeterInOnePascal, (double)kilonewtonpersquaremeterQuantity.Value, KilonewtonsPerSquareMeterTolerance);
- Assert.Equal(PressureUnit.KilonewtonPerSquareMeter, kilonewtonpersquaremeterQuantity.Unit);
-
- var kilonewtonpersquaremillimeterQuantity = pascal.ToUnit(PressureUnit.KilonewtonPerSquareMillimeter);
- AssertEx.EqualTolerance(KilonewtonsPerSquareMillimeterInOnePascal, (double)kilonewtonpersquaremillimeterQuantity.Value, KilonewtonsPerSquareMillimeterTolerance);
- Assert.Equal(PressureUnit.KilonewtonPerSquareMillimeter, kilonewtonpersquaremillimeterQuantity.Unit);
-
- var kilopascalQuantity = pascal.ToUnit(PressureUnit.Kilopascal);
- AssertEx.EqualTolerance(KilopascalsInOnePascal, (double)kilopascalQuantity.Value, KilopascalsTolerance);
- Assert.Equal(PressureUnit.Kilopascal, kilopascalQuantity.Unit);
-
- var kilopoundforcepersquarefootQuantity = pascal.ToUnit(PressureUnit.KilopoundForcePerSquareFoot);
- AssertEx.EqualTolerance(KilopoundsForcePerSquareFootInOnePascal, (double)kilopoundforcepersquarefootQuantity.Value, KilopoundsForcePerSquareFootTolerance);
- Assert.Equal(PressureUnit.KilopoundForcePerSquareFoot, kilopoundforcepersquarefootQuantity.Unit);
+ var inBaseUnits = Pressure.From(1.0, Pressure.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilopoundforcepersquareinchQuantity = pascal.ToUnit(PressureUnit.KilopoundForcePerSquareInch);
- AssertEx.EqualTolerance(KilopoundsForcePerSquareInchInOnePascal, (double)kilopoundforcepersquareinchQuantity.Value, KilopoundsForcePerSquareInchTolerance);
- Assert.Equal(PressureUnit.KilopoundForcePerSquareInch, kilopoundforcepersquareinchQuantity.Unit);
-
- var megabarQuantity = pascal.ToUnit(PressureUnit.Megabar);
- AssertEx.EqualTolerance(MegabarsInOnePascal, (double)megabarQuantity.Value, MegabarsTolerance);
- Assert.Equal(PressureUnit.Megabar, megabarQuantity.Unit);
-
- var meganewtonpersquaremeterQuantity = pascal.ToUnit(PressureUnit.MeganewtonPerSquareMeter);
- AssertEx.EqualTolerance(MeganewtonsPerSquareMeterInOnePascal, (double)meganewtonpersquaremeterQuantity.Value, MeganewtonsPerSquareMeterTolerance);
- Assert.Equal(PressureUnit.MeganewtonPerSquareMeter, meganewtonpersquaremeterQuantity.Unit);
-
- var megapascalQuantity = pascal.ToUnit(PressureUnit.Megapascal);
- AssertEx.EqualTolerance(MegapascalsInOnePascal, (double)megapascalQuantity.Value, MegapascalsTolerance);
- Assert.Equal(PressureUnit.Megapascal, megapascalQuantity.Unit);
-
- var meterofelevationQuantity = pascal.ToUnit(PressureUnit.MeterOfElevation);
- AssertEx.EqualTolerance(MetersOfElevationInOnePascal, (double)meterofelevationQuantity.Value, MetersOfElevationTolerance);
- Assert.Equal(PressureUnit.MeterOfElevation, meterofelevationQuantity.Unit);
-
- var meterofheadQuantity = pascal.ToUnit(PressureUnit.MeterOfHead);
- AssertEx.EqualTolerance(MetersOfHeadInOnePascal, (double)meterofheadQuantity.Value, MetersOfHeadTolerance);
- Assert.Equal(PressureUnit.MeterOfHead, meterofheadQuantity.Unit);
-
- var microbarQuantity = pascal.ToUnit(PressureUnit.Microbar);
- AssertEx.EqualTolerance(MicrobarsInOnePascal, (double)microbarQuantity.Value, MicrobarsTolerance);
- Assert.Equal(PressureUnit.Microbar, microbarQuantity.Unit);
-
- var micropascalQuantity = pascal.ToUnit(PressureUnit.Micropascal);
- AssertEx.EqualTolerance(MicropascalsInOnePascal, (double)micropascalQuantity.Value, MicropascalsTolerance);
- Assert.Equal(PressureUnit.Micropascal, micropascalQuantity.Unit);
-
- var millibarQuantity = pascal.ToUnit(PressureUnit.Millibar);
- AssertEx.EqualTolerance(MillibarsInOnePascal, (double)millibarQuantity.Value, MillibarsTolerance);
- Assert.Equal(PressureUnit.Millibar, millibarQuantity.Unit);
-
- var millimeterofmercuryQuantity = pascal.ToUnit(PressureUnit.MillimeterOfMercury);
- AssertEx.EqualTolerance(MillimetersOfMercuryInOnePascal, (double)millimeterofmercuryQuantity.Value, MillimetersOfMercuryTolerance);
- Assert.Equal(PressureUnit.MillimeterOfMercury, millimeterofmercuryQuantity.Unit);
-
- var millimeterofwatercolumnQuantity = pascal.ToUnit(PressureUnit.MillimeterOfWaterColumn);
- AssertEx.EqualTolerance(MillimeterOfWaterColumnInOnePascal, (double)millimeterofwatercolumnQuantity.Value, MillimeterOfWaterColumnTolerance);
- Assert.Equal(PressureUnit.MillimeterOfWaterColumn, millimeterofwatercolumnQuantity.Unit);
-
- var millipascalQuantity = pascal.ToUnit(PressureUnit.Millipascal);
- AssertEx.EqualTolerance(MillipascalsInOnePascal, (double)millipascalQuantity.Value, MillipascalsTolerance);
- Assert.Equal(PressureUnit.Millipascal, millipascalQuantity.Unit);
-
- var newtonpersquarecentimeterQuantity = pascal.ToUnit(PressureUnit.NewtonPerSquareCentimeter);
- AssertEx.EqualTolerance(NewtonsPerSquareCentimeterInOnePascal, (double)newtonpersquarecentimeterQuantity.Value, NewtonsPerSquareCentimeterTolerance);
- Assert.Equal(PressureUnit.NewtonPerSquareCentimeter, newtonpersquarecentimeterQuantity.Unit);
-
- var newtonpersquaremeterQuantity = pascal.ToUnit(PressureUnit.NewtonPerSquareMeter);
- AssertEx.EqualTolerance(NewtonsPerSquareMeterInOnePascal, (double)newtonpersquaremeterQuantity.Value, NewtonsPerSquareMeterTolerance);
- Assert.Equal(PressureUnit.NewtonPerSquareMeter, newtonpersquaremeterQuantity.Unit);
-
- var newtonpersquaremillimeterQuantity = pascal.ToUnit(PressureUnit.NewtonPerSquareMillimeter);
- AssertEx.EqualTolerance(NewtonsPerSquareMillimeterInOnePascal, (double)newtonpersquaremillimeterQuantity.Value, NewtonsPerSquareMillimeterTolerance);
- Assert.Equal(PressureUnit.NewtonPerSquareMillimeter, newtonpersquaremillimeterQuantity.Unit);
-
- var pascalQuantity = pascal.ToUnit(PressureUnit.Pascal);
- AssertEx.EqualTolerance(PascalsInOnePascal, (double)pascalQuantity.Value, PascalsTolerance);
- Assert.Equal(PressureUnit.Pascal, pascalQuantity.Unit);
-
- var poundforcepersquarefootQuantity = pascal.ToUnit(PressureUnit.PoundForcePerSquareFoot);
- AssertEx.EqualTolerance(PoundsForcePerSquareFootInOnePascal, (double)poundforcepersquarefootQuantity.Value, PoundsForcePerSquareFootTolerance);
- Assert.Equal(PressureUnit.PoundForcePerSquareFoot, poundforcepersquarefootQuantity.Unit);
-
- var poundforcepersquareinchQuantity = pascal.ToUnit(PressureUnit.PoundForcePerSquareInch);
- AssertEx.EqualTolerance(PoundsForcePerSquareInchInOnePascal, (double)poundforcepersquareinchQuantity.Value, PoundsForcePerSquareInchTolerance);
- Assert.Equal(PressureUnit.PoundForcePerSquareInch, poundforcepersquareinchQuantity.Unit);
-
- var poundperinchsecondsquaredQuantity = pascal.ToUnit(PressureUnit.PoundPerInchSecondSquared);
- AssertEx.EqualTolerance(PoundsPerInchSecondSquaredInOnePascal, (double)poundperinchsecondsquaredQuantity.Value, PoundsPerInchSecondSquaredTolerance);
- Assert.Equal(PressureUnit.PoundPerInchSecondSquared, poundperinchsecondsquaredQuantity.Unit);
-
- var technicalatmosphereQuantity = pascal.ToUnit(PressureUnit.TechnicalAtmosphere);
- AssertEx.EqualTolerance(TechnicalAtmospheresInOnePascal, (double)technicalatmosphereQuantity.Value, TechnicalAtmospheresTolerance);
- Assert.Equal(PressureUnit.TechnicalAtmosphere, technicalatmosphereQuantity.Unit);
-
- var tonneforcepersquarecentimeterQuantity = pascal.ToUnit(PressureUnit.TonneForcePerSquareCentimeter);
- AssertEx.EqualTolerance(TonnesForcePerSquareCentimeterInOnePascal, (double)tonneforcepersquarecentimeterQuantity.Value, TonnesForcePerSquareCentimeterTolerance);
- Assert.Equal(PressureUnit.TonneForcePerSquareCentimeter, tonneforcepersquarecentimeterQuantity.Unit);
-
- var tonneforcepersquaremeterQuantity = pascal.ToUnit(PressureUnit.TonneForcePerSquareMeter);
- AssertEx.EqualTolerance(TonnesForcePerSquareMeterInOnePascal, (double)tonneforcepersquaremeterQuantity.Value, TonnesForcePerSquareMeterTolerance);
- Assert.Equal(PressureUnit.TonneForcePerSquareMeter, tonneforcepersquaremeterQuantity.Unit);
-
- var tonneforcepersquaremillimeterQuantity = pascal.ToUnit(PressureUnit.TonneForcePerSquareMillimeter);
- AssertEx.EqualTolerance(TonnesForcePerSquareMillimeterInOnePascal, (double)tonneforcepersquaremillimeterQuantity.Value, TonnesForcePerSquareMillimeterTolerance);
- Assert.Equal(PressureUnit.TonneForcePerSquareMillimeter, tonneforcepersquaremillimeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var torrQuantity = pascal.ToUnit(PressureUnit.Torr);
- AssertEx.EqualTolerance(TorrsInOnePascal, (double)torrQuantity.Value, TorrsTolerance);
- Assert.Equal(PressureUnit.Torr, torrQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(PressureUnit unit)
+ {
+ var quantity = Pressure.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(PressureUnit unit)
{
- var quantityInBaseUnit = Pressure.FromPascals(1).ToBaseUnit();
- Assert.Equal(Pressure.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Pressure.Units.FirstOrDefault(u => u != Pressure.BaseUnit && u != PressureUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == PressureUnit.Undefined)
+ fromUnit = Pressure.BaseUnit;
+
+ var quantity = Pressure.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RatioChangeRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RatioChangeRateTestsBase.g.cs
index f6e2cd8ff6..41395a85c3 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RatioChangeRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RatioChangeRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -45,6 +46,22 @@ public abstract partial class RatioChangeRateTestsBase : QuantityTestsBase
protected virtual double PercentsPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RatioChangeRateUnit unit)
+ {
+ return unit switch
+ {
+ RatioChangeRateUnit.DecimalFractionPerSecond => (DecimalFractionsPerSecondInOneDecimalFractionPerSecond, DecimalFractionsPerSecondTolerance),
+ RatioChangeRateUnit.PercentPerSecond => (PercentsPerSecondInOneDecimalFractionPerSecond, PercentsPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RatioChangeRateUnit.DecimalFractionPerSecond },
+ new object[] { RatioChangeRateUnit.PercentPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -172,25 +189,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RatioChangeRateUnit unit)
{
- var decimalfractionpersecond = RatioChangeRate.FromDecimalFractionsPerSecond(1);
+ var inBaseUnits = RatioChangeRate.From(1.0, RatioChangeRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var decimalfractionpersecondQuantity = decimalfractionpersecond.ToUnit(RatioChangeRateUnit.DecimalFractionPerSecond);
- AssertEx.EqualTolerance(DecimalFractionsPerSecondInOneDecimalFractionPerSecond, (double)decimalfractionpersecondQuantity.Value, DecimalFractionsPerSecondTolerance);
- Assert.Equal(RatioChangeRateUnit.DecimalFractionPerSecond, decimalfractionpersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var percentpersecondQuantity = decimalfractionpersecond.ToUnit(RatioChangeRateUnit.PercentPerSecond);
- AssertEx.EqualTolerance(PercentsPerSecondInOneDecimalFractionPerSecond, (double)percentpersecondQuantity.Value, PercentsPerSecondTolerance);
- Assert.Equal(RatioChangeRateUnit.PercentPerSecond, percentpersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RatioChangeRateUnit unit)
+ {
+ var quantity = RatioChangeRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RatioChangeRateUnit unit)
{
- var quantityInBaseUnit = RatioChangeRate.FromDecimalFractionsPerSecond(1).ToBaseUnit();
- Assert.Equal(RatioChangeRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = RatioChangeRate.Units.FirstOrDefault(u => u != RatioChangeRate.BaseUnit && u != RatioChangeRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RatioChangeRateUnit.Undefined)
+ fromUnit = RatioChangeRate.BaseUnit;
+
+ var quantity = RatioChangeRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RatioTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RatioTestsBase.g.cs
index 5f1c730275..58ab0bb668 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RatioTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RatioTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -53,6 +54,30 @@ public abstract partial class RatioTestsBase : QuantityTestsBase
protected virtual double PercentTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RatioUnit unit)
+ {
+ return unit switch
+ {
+ RatioUnit.DecimalFraction => (DecimalFractionsInOneDecimalFraction, DecimalFractionsTolerance),
+ RatioUnit.PartPerBillion => (PartsPerBillionInOneDecimalFraction, PartsPerBillionTolerance),
+ RatioUnit.PartPerMillion => (PartsPerMillionInOneDecimalFraction, PartsPerMillionTolerance),
+ RatioUnit.PartPerThousand => (PartsPerThousandInOneDecimalFraction, PartsPerThousandTolerance),
+ RatioUnit.PartPerTrillion => (PartsPerTrillionInOneDecimalFraction, PartsPerTrillionTolerance),
+ RatioUnit.Percent => (PercentInOneDecimalFraction, PercentTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RatioUnit.DecimalFraction },
+ new object[] { RatioUnit.PartPerBillion },
+ new object[] { RatioUnit.PartPerMillion },
+ new object[] { RatioUnit.PartPerThousand },
+ new object[] { RatioUnit.PartPerTrillion },
+ new object[] { RatioUnit.Percent },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -204,41 +229,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RatioUnit unit)
{
- var decimalfraction = Ratio.FromDecimalFractions(1);
-
- var decimalfractionQuantity = decimalfraction.ToUnit(RatioUnit.DecimalFraction);
- AssertEx.EqualTolerance(DecimalFractionsInOneDecimalFraction, (double)decimalfractionQuantity.Value, DecimalFractionsTolerance);
- Assert.Equal(RatioUnit.DecimalFraction, decimalfractionQuantity.Unit);
+ var inBaseUnits = Ratio.From(1.0, Ratio.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var partperbillionQuantity = decimalfraction.ToUnit(RatioUnit.PartPerBillion);
- AssertEx.EqualTolerance(PartsPerBillionInOneDecimalFraction, (double)partperbillionQuantity.Value, PartsPerBillionTolerance);
- Assert.Equal(RatioUnit.PartPerBillion, partperbillionQuantity.Unit);
-
- var partpermillionQuantity = decimalfraction.ToUnit(RatioUnit.PartPerMillion);
- AssertEx.EqualTolerance(PartsPerMillionInOneDecimalFraction, (double)partpermillionQuantity.Value, PartsPerMillionTolerance);
- Assert.Equal(RatioUnit.PartPerMillion, partpermillionQuantity.Unit);
-
- var partperthousandQuantity = decimalfraction.ToUnit(RatioUnit.PartPerThousand);
- AssertEx.EqualTolerance(PartsPerThousandInOneDecimalFraction, (double)partperthousandQuantity.Value, PartsPerThousandTolerance);
- Assert.Equal(RatioUnit.PartPerThousand, partperthousandQuantity.Unit);
-
- var partpertrillionQuantity = decimalfraction.ToUnit(RatioUnit.PartPerTrillion);
- AssertEx.EqualTolerance(PartsPerTrillionInOneDecimalFraction, (double)partpertrillionQuantity.Value, PartsPerTrillionTolerance);
- Assert.Equal(RatioUnit.PartPerTrillion, partpertrillionQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var percentQuantity = decimalfraction.ToUnit(RatioUnit.Percent);
- AssertEx.EqualTolerance(PercentInOneDecimalFraction, (double)percentQuantity.Value, PercentTolerance);
- Assert.Equal(RatioUnit.Percent, percentQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RatioUnit unit)
+ {
+ var quantity = Ratio.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RatioUnit unit)
{
- var quantityInBaseUnit = Ratio.FromDecimalFractions(1).ToBaseUnit();
- Assert.Equal(Ratio.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Ratio.Units.FirstOrDefault(u => u != Ratio.BaseUnit && u != RatioUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RatioUnit.Undefined)
+ fromUnit = Ratio.BaseUnit;
+
+ var quantity = Ratio.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ReactiveEnergyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ReactiveEnergyTestsBase.g.cs
index a5fb420a1b..26420e02ad 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ReactiveEnergyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ReactiveEnergyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class ReactiveEnergyTestsBase : QuantityTestsBase
protected virtual double VoltampereReactiveHoursTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReactiveEnergyUnit unit)
+ {
+ return unit switch
+ {
+ ReactiveEnergyUnit.KilovoltampereReactiveHour => (KilovoltampereReactiveHoursInOneVoltampereReactiveHour, KilovoltampereReactiveHoursTolerance),
+ ReactiveEnergyUnit.MegavoltampereReactiveHour => (MegavoltampereReactiveHoursInOneVoltampereReactiveHour, MegavoltampereReactiveHoursTolerance),
+ ReactiveEnergyUnit.VoltampereReactiveHour => (VoltampereReactiveHoursInOneVoltampereReactiveHour, VoltampereReactiveHoursTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ReactiveEnergyUnit.KilovoltampereReactiveHour },
+ new object[] { ReactiveEnergyUnit.MegavoltampereReactiveHour },
+ new object[] { ReactiveEnergyUnit.VoltampereReactiveHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ReactiveEnergyUnit unit)
{
- var voltamperereactivehour = ReactiveEnergy.FromVoltampereReactiveHours(1);
+ var inBaseUnits = ReactiveEnergy.From(1.0, ReactiveEnergy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilovoltamperereactivehourQuantity = voltamperereactivehour.ToUnit(ReactiveEnergyUnit.KilovoltampereReactiveHour);
- AssertEx.EqualTolerance(KilovoltampereReactiveHoursInOneVoltampereReactiveHour, (double)kilovoltamperereactivehourQuantity.Value, KilovoltampereReactiveHoursTolerance);
- Assert.Equal(ReactiveEnergyUnit.KilovoltampereReactiveHour, kilovoltamperereactivehourQuantity.Unit);
-
- var megavoltamperereactivehourQuantity = voltamperereactivehour.ToUnit(ReactiveEnergyUnit.MegavoltampereReactiveHour);
- AssertEx.EqualTolerance(MegavoltampereReactiveHoursInOneVoltampereReactiveHour, (double)megavoltamperereactivehourQuantity.Value, MegavoltampereReactiveHoursTolerance);
- Assert.Equal(ReactiveEnergyUnit.MegavoltampereReactiveHour, megavoltamperereactivehourQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltamperereactivehourQuantity = voltamperereactivehour.ToUnit(ReactiveEnergyUnit.VoltampereReactiveHour);
- AssertEx.EqualTolerance(VoltampereReactiveHoursInOneVoltampereReactiveHour, (double)voltamperereactivehourQuantity.Value, VoltampereReactiveHoursTolerance);
- Assert.Equal(ReactiveEnergyUnit.VoltampereReactiveHour, voltamperereactivehourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ReactiveEnergyUnit unit)
+ {
+ var quantity = ReactiveEnergy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ReactiveEnergyUnit unit)
{
- var quantityInBaseUnit = ReactiveEnergy.FromVoltampereReactiveHours(1).ToBaseUnit();
- Assert.Equal(ReactiveEnergy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ReactiveEnergy.Units.FirstOrDefault(u => u != ReactiveEnergy.BaseUnit && u != ReactiveEnergyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ReactiveEnergyUnit.Undefined)
+ fromUnit = ReactiveEnergy.BaseUnit;
+
+ var quantity = ReactiveEnergy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ReactivePowerTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ReactivePowerTestsBase.g.cs
index 7d4cb80b6a..9b122787b6 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ReactivePowerTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ReactivePowerTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class ReactivePowerTestsBase : QuantityTestsBase
protected virtual double VoltamperesReactiveTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReactivePowerUnit unit)
+ {
+ return unit switch
+ {
+ ReactivePowerUnit.GigavoltampereReactive => (GigavoltamperesReactiveInOneVoltampereReactive, GigavoltamperesReactiveTolerance),
+ ReactivePowerUnit.KilovoltampereReactive => (KilovoltamperesReactiveInOneVoltampereReactive, KilovoltamperesReactiveTolerance),
+ ReactivePowerUnit.MegavoltampereReactive => (MegavoltamperesReactiveInOneVoltampereReactive, MegavoltamperesReactiveTolerance),
+ ReactivePowerUnit.VoltampereReactive => (VoltamperesReactiveInOneVoltampereReactive, VoltamperesReactiveTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ReactivePowerUnit.GigavoltampereReactive },
+ new object[] { ReactivePowerUnit.KilovoltampereReactive },
+ new object[] { ReactivePowerUnit.MegavoltampereReactive },
+ new object[] { ReactivePowerUnit.VoltampereReactive },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ReactivePowerUnit unit)
{
- var voltamperereactive = ReactivePower.FromVoltamperesReactive(1);
+ var inBaseUnits = ReactivePower.From(1.0, ReactivePower.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var gigavoltamperereactiveQuantity = voltamperereactive.ToUnit(ReactivePowerUnit.GigavoltampereReactive);
- AssertEx.EqualTolerance(GigavoltamperesReactiveInOneVoltampereReactive, (double)gigavoltamperereactiveQuantity.Value, GigavoltamperesReactiveTolerance);
- Assert.Equal(ReactivePowerUnit.GigavoltampereReactive, gigavoltamperereactiveQuantity.Unit);
-
- var kilovoltamperereactiveQuantity = voltamperereactive.ToUnit(ReactivePowerUnit.KilovoltampereReactive);
- AssertEx.EqualTolerance(KilovoltamperesReactiveInOneVoltampereReactive, (double)kilovoltamperereactiveQuantity.Value, KilovoltamperesReactiveTolerance);
- Assert.Equal(ReactivePowerUnit.KilovoltampereReactive, kilovoltamperereactiveQuantity.Unit);
-
- var megavoltamperereactiveQuantity = voltamperereactive.ToUnit(ReactivePowerUnit.MegavoltampereReactive);
- AssertEx.EqualTolerance(MegavoltamperesReactiveInOneVoltampereReactive, (double)megavoltamperereactiveQuantity.Value, MegavoltamperesReactiveTolerance);
- Assert.Equal(ReactivePowerUnit.MegavoltampereReactive, megavoltamperereactiveQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var voltamperereactiveQuantity = voltamperereactive.ToUnit(ReactivePowerUnit.VoltampereReactive);
- AssertEx.EqualTolerance(VoltamperesReactiveInOneVoltampereReactive, (double)voltamperereactiveQuantity.Value, VoltamperesReactiveTolerance);
- Assert.Equal(ReactivePowerUnit.VoltampereReactive, voltamperereactiveQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ReactivePowerUnit unit)
+ {
+ var quantity = ReactivePower.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ReactivePowerUnit unit)
{
- var quantityInBaseUnit = ReactivePower.FromVoltamperesReactive(1).ToBaseUnit();
- Assert.Equal(ReactivePower.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ReactivePower.Units.FirstOrDefault(u => u != ReactivePower.BaseUnit && u != ReactivePowerUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ReactivePowerUnit.Undefined)
+ fromUnit = ReactivePower.BaseUnit;
+
+ var quantity = ReactivePower.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalAreaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalAreaTestsBase.g.cs
index b64695166c..639244d32b 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalAreaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalAreaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -63,6 +64,40 @@ public abstract partial class ReciprocalAreaTestsBase : QuantityTestsBase
protected virtual double InverseUsSurveySquareFeetTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReciprocalAreaUnit unit)
+ {
+ return unit switch
+ {
+ ReciprocalAreaUnit.InverseSquareCentimeter => (InverseSquareCentimetersInOneInverseSquareMeter, InverseSquareCentimetersTolerance),
+ ReciprocalAreaUnit.InverseSquareDecimeter => (InverseSquareDecimetersInOneInverseSquareMeter, InverseSquareDecimetersTolerance),
+ ReciprocalAreaUnit.InverseSquareFoot => (InverseSquareFeetInOneInverseSquareMeter, InverseSquareFeetTolerance),
+ ReciprocalAreaUnit.InverseSquareInch => (InverseSquareInchesInOneInverseSquareMeter, InverseSquareInchesTolerance),
+ ReciprocalAreaUnit.InverseSquareKilometer => (InverseSquareKilometersInOneInverseSquareMeter, InverseSquareKilometersTolerance),
+ ReciprocalAreaUnit.InverseSquareMeter => (InverseSquareMetersInOneInverseSquareMeter, InverseSquareMetersTolerance),
+ ReciprocalAreaUnit.InverseSquareMicrometer => (InverseSquareMicrometersInOneInverseSquareMeter, InverseSquareMicrometersTolerance),
+ ReciprocalAreaUnit.InverseSquareMile => (InverseSquareMilesInOneInverseSquareMeter, InverseSquareMilesTolerance),
+ ReciprocalAreaUnit.InverseSquareMillimeter => (InverseSquareMillimetersInOneInverseSquareMeter, InverseSquareMillimetersTolerance),
+ ReciprocalAreaUnit.InverseSquareYard => (InverseSquareYardsInOneInverseSquareMeter, InverseSquareYardsTolerance),
+ ReciprocalAreaUnit.InverseUsSurveySquareFoot => (InverseUsSurveySquareFeetInOneInverseSquareMeter, InverseUsSurveySquareFeetTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ReciprocalAreaUnit.InverseSquareCentimeter },
+ new object[] { ReciprocalAreaUnit.InverseSquareDecimeter },
+ new object[] { ReciprocalAreaUnit.InverseSquareFoot },
+ new object[] { ReciprocalAreaUnit.InverseSquareInch },
+ new object[] { ReciprocalAreaUnit.InverseSquareKilometer },
+ new object[] { ReciprocalAreaUnit.InverseSquareMeter },
+ new object[] { ReciprocalAreaUnit.InverseSquareMicrometer },
+ new object[] { ReciprocalAreaUnit.InverseSquareMile },
+ new object[] { ReciprocalAreaUnit.InverseSquareMillimeter },
+ new object[] { ReciprocalAreaUnit.InverseSquareYard },
+ new object[] { ReciprocalAreaUnit.InverseUsSurveySquareFoot },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -244,61 +279,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ReciprocalAreaUnit unit)
{
- var inversesquaremeter = ReciprocalArea.FromInverseSquareMeters(1);
-
- var inversesquarecentimeterQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareCentimeter);
- AssertEx.EqualTolerance(InverseSquareCentimetersInOneInverseSquareMeter, (double)inversesquarecentimeterQuantity.Value, InverseSquareCentimetersTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareCentimeter, inversesquarecentimeterQuantity.Unit);
-
- var inversesquaredecimeterQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareDecimeter);
- AssertEx.EqualTolerance(InverseSquareDecimetersInOneInverseSquareMeter, (double)inversesquaredecimeterQuantity.Value, InverseSquareDecimetersTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareDecimeter, inversesquaredecimeterQuantity.Unit);
-
- var inversesquarefootQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareFoot);
- AssertEx.EqualTolerance(InverseSquareFeetInOneInverseSquareMeter, (double)inversesquarefootQuantity.Value, InverseSquareFeetTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareFoot, inversesquarefootQuantity.Unit);
-
- var inversesquareinchQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareInch);
- AssertEx.EqualTolerance(InverseSquareInchesInOneInverseSquareMeter, (double)inversesquareinchQuantity.Value, InverseSquareInchesTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareInch, inversesquareinchQuantity.Unit);
-
- var inversesquarekilometerQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareKilometer);
- AssertEx.EqualTolerance(InverseSquareKilometersInOneInverseSquareMeter, (double)inversesquarekilometerQuantity.Value, InverseSquareKilometersTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareKilometer, inversesquarekilometerQuantity.Unit);
+ var inBaseUnits = ReciprocalArea.From(1.0, ReciprocalArea.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var inversesquaremeterQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareMeter);
- AssertEx.EqualTolerance(InverseSquareMetersInOneInverseSquareMeter, (double)inversesquaremeterQuantity.Value, InverseSquareMetersTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareMeter, inversesquaremeterQuantity.Unit);
-
- var inversesquaremicrometerQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareMicrometer);
- AssertEx.EqualTolerance(InverseSquareMicrometersInOneInverseSquareMeter, (double)inversesquaremicrometerQuantity.Value, InverseSquareMicrometersTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareMicrometer, inversesquaremicrometerQuantity.Unit);
-
- var inversesquaremileQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareMile);
- AssertEx.EqualTolerance(InverseSquareMilesInOneInverseSquareMeter, (double)inversesquaremileQuantity.Value, InverseSquareMilesTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareMile, inversesquaremileQuantity.Unit);
-
- var inversesquaremillimeterQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareMillimeter);
- AssertEx.EqualTolerance(InverseSquareMillimetersInOneInverseSquareMeter, (double)inversesquaremillimeterQuantity.Value, InverseSquareMillimetersTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareMillimeter, inversesquaremillimeterQuantity.Unit);
-
- var inversesquareyardQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseSquareYard);
- AssertEx.EqualTolerance(InverseSquareYardsInOneInverseSquareMeter, (double)inversesquareyardQuantity.Value, InverseSquareYardsTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseSquareYard, inversesquareyardQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var inverseussurveysquarefootQuantity = inversesquaremeter.ToUnit(ReciprocalAreaUnit.InverseUsSurveySquareFoot);
- AssertEx.EqualTolerance(InverseUsSurveySquareFeetInOneInverseSquareMeter, (double)inverseussurveysquarefootQuantity.Value, InverseUsSurveySquareFeetTolerance);
- Assert.Equal(ReciprocalAreaUnit.InverseUsSurveySquareFoot, inverseussurveysquarefootQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ReciprocalAreaUnit unit)
+ {
+ var quantity = ReciprocalArea.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ReciprocalAreaUnit unit)
{
- var quantityInBaseUnit = ReciprocalArea.FromInverseSquareMeters(1).ToBaseUnit();
- Assert.Equal(ReciprocalArea.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ReciprocalArea.Units.FirstOrDefault(u => u != ReciprocalArea.BaseUnit && u != ReciprocalAreaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ReciprocalAreaUnit.Undefined)
+ fromUnit = ReciprocalArea.BaseUnit;
+
+ var quantity = ReciprocalArea.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalLengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalLengthTestsBase.g.cs
index 682963c9ff..96ae80abdd 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalLengthTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ReciprocalLengthTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -61,6 +62,38 @@ public abstract partial class ReciprocalLengthTestsBase : QuantityTestsBase
protected virtual double InverseYardsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ReciprocalLengthUnit unit)
+ {
+ return unit switch
+ {
+ ReciprocalLengthUnit.InverseCentimeter => (InverseCentimetersInOneInverseMeter, InverseCentimetersTolerance),
+ ReciprocalLengthUnit.InverseFoot => (InverseFeetInOneInverseMeter, InverseFeetTolerance),
+ ReciprocalLengthUnit.InverseInch => (InverseInchesInOneInverseMeter, InverseInchesTolerance),
+ ReciprocalLengthUnit.InverseMeter => (InverseMetersInOneInverseMeter, InverseMetersTolerance),
+ ReciprocalLengthUnit.InverseMicroinch => (InverseMicroinchesInOneInverseMeter, InverseMicroinchesTolerance),
+ ReciprocalLengthUnit.InverseMil => (InverseMilsInOneInverseMeter, InverseMilsTolerance),
+ ReciprocalLengthUnit.InverseMile => (InverseMilesInOneInverseMeter, InverseMilesTolerance),
+ ReciprocalLengthUnit.InverseMillimeter => (InverseMillimetersInOneInverseMeter, InverseMillimetersTolerance),
+ ReciprocalLengthUnit.InverseUsSurveyFoot => (InverseUsSurveyFeetInOneInverseMeter, InverseUsSurveyFeetTolerance),
+ ReciprocalLengthUnit.InverseYard => (InverseYardsInOneInverseMeter, InverseYardsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ReciprocalLengthUnit.InverseCentimeter },
+ new object[] { ReciprocalLengthUnit.InverseFoot },
+ new object[] { ReciprocalLengthUnit.InverseInch },
+ new object[] { ReciprocalLengthUnit.InverseMeter },
+ new object[] { ReciprocalLengthUnit.InverseMicroinch },
+ new object[] { ReciprocalLengthUnit.InverseMil },
+ new object[] { ReciprocalLengthUnit.InverseMile },
+ new object[] { ReciprocalLengthUnit.InverseMillimeter },
+ new object[] { ReciprocalLengthUnit.InverseUsSurveyFoot },
+ new object[] { ReciprocalLengthUnit.InverseYard },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -236,57 +269,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ReciprocalLengthUnit unit)
{
- var inversemeter = ReciprocalLength.FromInverseMeters(1);
-
- var inversecentimeterQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseCentimeter);
- AssertEx.EqualTolerance(InverseCentimetersInOneInverseMeter, (double)inversecentimeterQuantity.Value, InverseCentimetersTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseCentimeter, inversecentimeterQuantity.Unit);
-
- var inversefootQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseFoot);
- AssertEx.EqualTolerance(InverseFeetInOneInverseMeter, (double)inversefootQuantity.Value, InverseFeetTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseFoot, inversefootQuantity.Unit);
-
- var inverseinchQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseInch);
- AssertEx.EqualTolerance(InverseInchesInOneInverseMeter, (double)inverseinchQuantity.Value, InverseInchesTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseInch, inverseinchQuantity.Unit);
-
- var inversemeterQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseMeter);
- AssertEx.EqualTolerance(InverseMetersInOneInverseMeter, (double)inversemeterQuantity.Value, InverseMetersTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseMeter, inversemeterQuantity.Unit);
-
- var inversemicroinchQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseMicroinch);
- AssertEx.EqualTolerance(InverseMicroinchesInOneInverseMeter, (double)inversemicroinchQuantity.Value, InverseMicroinchesTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseMicroinch, inversemicroinchQuantity.Unit);
+ var inBaseUnits = ReciprocalLength.From(1.0, ReciprocalLength.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var inversemilQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseMil);
- AssertEx.EqualTolerance(InverseMilsInOneInverseMeter, (double)inversemilQuantity.Value, InverseMilsTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseMil, inversemilQuantity.Unit);
-
- var inversemileQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseMile);
- AssertEx.EqualTolerance(InverseMilesInOneInverseMeter, (double)inversemileQuantity.Value, InverseMilesTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseMile, inversemileQuantity.Unit);
-
- var inversemillimeterQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseMillimeter);
- AssertEx.EqualTolerance(InverseMillimetersInOneInverseMeter, (double)inversemillimeterQuantity.Value, InverseMillimetersTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseMillimeter, inversemillimeterQuantity.Unit);
-
- var inverseussurveyfootQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseUsSurveyFoot);
- AssertEx.EqualTolerance(InverseUsSurveyFeetInOneInverseMeter, (double)inverseussurveyfootQuantity.Value, InverseUsSurveyFeetTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseUsSurveyFoot, inverseussurveyfootQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var inverseyardQuantity = inversemeter.ToUnit(ReciprocalLengthUnit.InverseYard);
- AssertEx.EqualTolerance(InverseYardsInOneInverseMeter, (double)inverseyardQuantity.Value, InverseYardsTolerance);
- Assert.Equal(ReciprocalLengthUnit.InverseYard, inverseyardQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ReciprocalLengthUnit unit)
+ {
+ var quantity = ReciprocalLength.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ReciprocalLengthUnit unit)
{
- var quantityInBaseUnit = ReciprocalLength.FromInverseMeters(1).ToBaseUnit();
- Assert.Equal(ReciprocalLength.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ReciprocalLength.Units.FirstOrDefault(u => u != ReciprocalLength.BaseUnit && u != ReciprocalLengthUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ReciprocalLengthUnit.Undefined)
+ fromUnit = ReciprocalLength.BaseUnit;
+
+ var quantity = ReciprocalLength.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RelativeHumidityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RelativeHumidityTestsBase.g.cs
index a7ba21d25b..736579b1a4 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RelativeHumidityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RelativeHumidityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class RelativeHumidityTestsBase : QuantityTestsBase
protected virtual double PercentTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RelativeHumidityUnit unit)
+ {
+ return unit switch
+ {
+ RelativeHumidityUnit.Percent => (PercentInOnePercent, PercentTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RelativeHumidityUnit.Percent },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RelativeHumidityUnit unit)
{
- var percent = RelativeHumidity.FromPercent(1);
+ var inBaseUnits = RelativeHumidity.From(1.0, RelativeHumidity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var percentQuantity = percent.ToUnit(RelativeHumidityUnit.Percent);
- AssertEx.EqualTolerance(PercentInOnePercent, (double)percentQuantity.Value, PercentTolerance);
- Assert.Equal(RelativeHumidityUnit.Percent, percentQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RelativeHumidityUnit unit)
+ {
+ var quantity = RelativeHumidity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RelativeHumidityUnit unit)
{
- var quantityInBaseUnit = RelativeHumidity.FromPercent(1).ToBaseUnit();
- Assert.Equal(RelativeHumidity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = RelativeHumidity.Units.FirstOrDefault(u => u != RelativeHumidity.BaseUnit && u != RelativeHumidityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RelativeHumidityUnit.Undefined)
+ fromUnit = RelativeHumidity.BaseUnit;
+
+ var quantity = RelativeHumidity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalAccelerationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalAccelerationTestsBase.g.cs
index 634a40430b..63d84cdbdc 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalAccelerationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalAccelerationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class RotationalAccelerationTestsBase : QuantityTestsBas
protected virtual double RevolutionsPerSecondSquaredTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RotationalAccelerationUnit unit)
+ {
+ return unit switch
+ {
+ RotationalAccelerationUnit.DegreePerSecondSquared => (DegreesPerSecondSquaredInOneRadianPerSecondSquared, DegreesPerSecondSquaredTolerance),
+ RotationalAccelerationUnit.RadianPerSecondSquared => (RadiansPerSecondSquaredInOneRadianPerSecondSquared, RadiansPerSecondSquaredTolerance),
+ RotationalAccelerationUnit.RevolutionPerMinutePerSecond => (RevolutionsPerMinutePerSecondInOneRadianPerSecondSquared, RevolutionsPerMinutePerSecondTolerance),
+ RotationalAccelerationUnit.RevolutionPerSecondSquared => (RevolutionsPerSecondSquaredInOneRadianPerSecondSquared, RevolutionsPerSecondSquaredTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RotationalAccelerationUnit.DegreePerSecondSquared },
+ new object[] { RotationalAccelerationUnit.RadianPerSecondSquared },
+ new object[] { RotationalAccelerationUnit.RevolutionPerMinutePerSecond },
+ new object[] { RotationalAccelerationUnit.RevolutionPerSecondSquared },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RotationalAccelerationUnit unit)
{
- var radianpersecondsquared = RotationalAcceleration.FromRadiansPerSecondSquared(1);
+ var inBaseUnits = RotationalAcceleration.From(1.0, RotationalAcceleration.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var degreepersecondsquaredQuantity = radianpersecondsquared.ToUnit(RotationalAccelerationUnit.DegreePerSecondSquared);
- AssertEx.EqualTolerance(DegreesPerSecondSquaredInOneRadianPerSecondSquared, (double)degreepersecondsquaredQuantity.Value, DegreesPerSecondSquaredTolerance);
- Assert.Equal(RotationalAccelerationUnit.DegreePerSecondSquared, degreepersecondsquaredQuantity.Unit);
-
- var radianpersecondsquaredQuantity = radianpersecondsquared.ToUnit(RotationalAccelerationUnit.RadianPerSecondSquared);
- AssertEx.EqualTolerance(RadiansPerSecondSquaredInOneRadianPerSecondSquared, (double)radianpersecondsquaredQuantity.Value, RadiansPerSecondSquaredTolerance);
- Assert.Equal(RotationalAccelerationUnit.RadianPerSecondSquared, radianpersecondsquaredQuantity.Unit);
-
- var revolutionperminutepersecondQuantity = radianpersecondsquared.ToUnit(RotationalAccelerationUnit.RevolutionPerMinutePerSecond);
- AssertEx.EqualTolerance(RevolutionsPerMinutePerSecondInOneRadianPerSecondSquared, (double)revolutionperminutepersecondQuantity.Value, RevolutionsPerMinutePerSecondTolerance);
- Assert.Equal(RotationalAccelerationUnit.RevolutionPerMinutePerSecond, revolutionperminutepersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var revolutionpersecondsquaredQuantity = radianpersecondsquared.ToUnit(RotationalAccelerationUnit.RevolutionPerSecondSquared);
- AssertEx.EqualTolerance(RevolutionsPerSecondSquaredInOneRadianPerSecondSquared, (double)revolutionpersecondsquaredQuantity.Value, RevolutionsPerSecondSquaredTolerance);
- Assert.Equal(RotationalAccelerationUnit.RevolutionPerSecondSquared, revolutionpersecondsquaredQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RotationalAccelerationUnit unit)
+ {
+ var quantity = RotationalAcceleration.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RotationalAccelerationUnit unit)
{
- var quantityInBaseUnit = RotationalAcceleration.FromRadiansPerSecondSquared(1).ToBaseUnit();
- Assert.Equal(RotationalAcceleration.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = RotationalAcceleration.Units.FirstOrDefault(u => u != RotationalAcceleration.BaseUnit && u != RotationalAccelerationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RotationalAccelerationUnit.Undefined)
+ fromUnit = RotationalAcceleration.BaseUnit;
+
+ var quantity = RotationalAcceleration.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalSpeedTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalSpeedTestsBase.g.cs
index 93cc2fea10..d6e6f12cf1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalSpeedTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalSpeedTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -67,6 +68,44 @@ public abstract partial class RotationalSpeedTestsBase : QuantityTestsBase
protected virtual double RevolutionsPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RotationalSpeedUnit unit)
+ {
+ return unit switch
+ {
+ RotationalSpeedUnit.CentiradianPerSecond => (CentiradiansPerSecondInOneRadianPerSecond, CentiradiansPerSecondTolerance),
+ RotationalSpeedUnit.DeciradianPerSecond => (DeciradiansPerSecondInOneRadianPerSecond, DeciradiansPerSecondTolerance),
+ RotationalSpeedUnit.DegreePerMinute => (DegreesPerMinuteInOneRadianPerSecond, DegreesPerMinuteTolerance),
+ RotationalSpeedUnit.DegreePerSecond => (DegreesPerSecondInOneRadianPerSecond, DegreesPerSecondTolerance),
+ RotationalSpeedUnit.MicrodegreePerSecond => (MicrodegreesPerSecondInOneRadianPerSecond, MicrodegreesPerSecondTolerance),
+ RotationalSpeedUnit.MicroradianPerSecond => (MicroradiansPerSecondInOneRadianPerSecond, MicroradiansPerSecondTolerance),
+ RotationalSpeedUnit.MillidegreePerSecond => (MillidegreesPerSecondInOneRadianPerSecond, MillidegreesPerSecondTolerance),
+ RotationalSpeedUnit.MilliradianPerSecond => (MilliradiansPerSecondInOneRadianPerSecond, MilliradiansPerSecondTolerance),
+ RotationalSpeedUnit.NanodegreePerSecond => (NanodegreesPerSecondInOneRadianPerSecond, NanodegreesPerSecondTolerance),
+ RotationalSpeedUnit.NanoradianPerSecond => (NanoradiansPerSecondInOneRadianPerSecond, NanoradiansPerSecondTolerance),
+ RotationalSpeedUnit.RadianPerSecond => (RadiansPerSecondInOneRadianPerSecond, RadiansPerSecondTolerance),
+ RotationalSpeedUnit.RevolutionPerMinute => (RevolutionsPerMinuteInOneRadianPerSecond, RevolutionsPerMinuteTolerance),
+ RotationalSpeedUnit.RevolutionPerSecond => (RevolutionsPerSecondInOneRadianPerSecond, RevolutionsPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RotationalSpeedUnit.CentiradianPerSecond },
+ new object[] { RotationalSpeedUnit.DeciradianPerSecond },
+ new object[] { RotationalSpeedUnit.DegreePerMinute },
+ new object[] { RotationalSpeedUnit.DegreePerSecond },
+ new object[] { RotationalSpeedUnit.MicrodegreePerSecond },
+ new object[] { RotationalSpeedUnit.MicroradianPerSecond },
+ new object[] { RotationalSpeedUnit.MillidegreePerSecond },
+ new object[] { RotationalSpeedUnit.MilliradianPerSecond },
+ new object[] { RotationalSpeedUnit.NanodegreePerSecond },
+ new object[] { RotationalSpeedUnit.NanoradianPerSecond },
+ new object[] { RotationalSpeedUnit.RadianPerSecond },
+ new object[] { RotationalSpeedUnit.RevolutionPerMinute },
+ new object[] { RotationalSpeedUnit.RevolutionPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -260,69 +299,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RotationalSpeedUnit unit)
{
- var radianpersecond = RotationalSpeed.FromRadiansPerSecond(1);
-
- var centiradianpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.CentiradianPerSecond);
- AssertEx.EqualTolerance(CentiradiansPerSecondInOneRadianPerSecond, (double)centiradianpersecondQuantity.Value, CentiradiansPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.CentiradianPerSecond, centiradianpersecondQuantity.Unit);
-
- var deciradianpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.DeciradianPerSecond);
- AssertEx.EqualTolerance(DeciradiansPerSecondInOneRadianPerSecond, (double)deciradianpersecondQuantity.Value, DeciradiansPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.DeciradianPerSecond, deciradianpersecondQuantity.Unit);
-
- var degreeperminuteQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.DegreePerMinute);
- AssertEx.EqualTolerance(DegreesPerMinuteInOneRadianPerSecond, (double)degreeperminuteQuantity.Value, DegreesPerMinuteTolerance);
- Assert.Equal(RotationalSpeedUnit.DegreePerMinute, degreeperminuteQuantity.Unit);
-
- var degreepersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.DegreePerSecond);
- AssertEx.EqualTolerance(DegreesPerSecondInOneRadianPerSecond, (double)degreepersecondQuantity.Value, DegreesPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.DegreePerSecond, degreepersecondQuantity.Unit);
-
- var microdegreepersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.MicrodegreePerSecond);
- AssertEx.EqualTolerance(MicrodegreesPerSecondInOneRadianPerSecond, (double)microdegreepersecondQuantity.Value, MicrodegreesPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.MicrodegreePerSecond, microdegreepersecondQuantity.Unit);
-
- var microradianpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.MicroradianPerSecond);
- AssertEx.EqualTolerance(MicroradiansPerSecondInOneRadianPerSecond, (double)microradianpersecondQuantity.Value, MicroradiansPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.MicroradianPerSecond, microradianpersecondQuantity.Unit);
+ var inBaseUnits = RotationalSpeed.From(1.0, RotationalSpeed.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var millidegreepersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.MillidegreePerSecond);
- AssertEx.EqualTolerance(MillidegreesPerSecondInOneRadianPerSecond, (double)millidegreepersecondQuantity.Value, MillidegreesPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.MillidegreePerSecond, millidegreepersecondQuantity.Unit);
-
- var milliradianpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.MilliradianPerSecond);
- AssertEx.EqualTolerance(MilliradiansPerSecondInOneRadianPerSecond, (double)milliradianpersecondQuantity.Value, MilliradiansPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.MilliradianPerSecond, milliradianpersecondQuantity.Unit);
-
- var nanodegreepersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.NanodegreePerSecond);
- AssertEx.EqualTolerance(NanodegreesPerSecondInOneRadianPerSecond, (double)nanodegreepersecondQuantity.Value, NanodegreesPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.NanodegreePerSecond, nanodegreepersecondQuantity.Unit);
-
- var nanoradianpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.NanoradianPerSecond);
- AssertEx.EqualTolerance(NanoradiansPerSecondInOneRadianPerSecond, (double)nanoradianpersecondQuantity.Value, NanoradiansPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.NanoradianPerSecond, nanoradianpersecondQuantity.Unit);
-
- var radianpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.RadianPerSecond);
- AssertEx.EqualTolerance(RadiansPerSecondInOneRadianPerSecond, (double)radianpersecondQuantity.Value, RadiansPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.RadianPerSecond, radianpersecondQuantity.Unit);
-
- var revolutionperminuteQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.RevolutionPerMinute);
- AssertEx.EqualTolerance(RevolutionsPerMinuteInOneRadianPerSecond, (double)revolutionperminuteQuantity.Value, RevolutionsPerMinuteTolerance);
- Assert.Equal(RotationalSpeedUnit.RevolutionPerMinute, revolutionperminuteQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var revolutionpersecondQuantity = radianpersecond.ToUnit(RotationalSpeedUnit.RevolutionPerSecond);
- AssertEx.EqualTolerance(RevolutionsPerSecondInOneRadianPerSecond, (double)revolutionpersecondQuantity.Value, RevolutionsPerSecondTolerance);
- Assert.Equal(RotationalSpeedUnit.RevolutionPerSecond, revolutionpersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RotationalSpeedUnit unit)
+ {
+ var quantity = RotationalSpeed.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RotationalSpeedUnit unit)
{
- var quantityInBaseUnit = RotationalSpeed.FromRadiansPerSecond(1).ToBaseUnit();
- Assert.Equal(RotationalSpeed.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = RotationalSpeed.Units.FirstOrDefault(u => u != RotationalSpeed.BaseUnit && u != RotationalSpeedUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RotationalSpeedUnit.Undefined)
+ fromUnit = RotationalSpeed.BaseUnit;
+
+ var quantity = RotationalSpeed.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessPerLengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessPerLengthTestsBase.g.cs
index aae2ddea51..763be52685 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessPerLengthTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessPerLengthTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -51,6 +52,28 @@ public abstract partial class RotationalStiffnessPerLengthTestsBase : QuantityTe
protected virtual double PoundForceFeetPerDegreesPerFeetTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RotationalStiffnessPerLengthUnit unit)
+ {
+ return unit switch
+ {
+ RotationalStiffnessPerLengthUnit.KilonewtonMeterPerRadianPerMeter => (KilonewtonMetersPerRadianPerMeterInOneNewtonMeterPerRadianPerMeter, KilonewtonMetersPerRadianPerMeterTolerance),
+ RotationalStiffnessPerLengthUnit.KilopoundForceFootPerDegreesPerFoot => (KilopoundForceFeetPerDegreesPerFeetInOneNewtonMeterPerRadianPerMeter, KilopoundForceFeetPerDegreesPerFeetTolerance),
+ RotationalStiffnessPerLengthUnit.MeganewtonMeterPerRadianPerMeter => (MeganewtonMetersPerRadianPerMeterInOneNewtonMeterPerRadianPerMeter, MeganewtonMetersPerRadianPerMeterTolerance),
+ RotationalStiffnessPerLengthUnit.NewtonMeterPerRadianPerMeter => (NewtonMetersPerRadianPerMeterInOneNewtonMeterPerRadianPerMeter, NewtonMetersPerRadianPerMeterTolerance),
+ RotationalStiffnessPerLengthUnit.PoundForceFootPerDegreesPerFoot => (PoundForceFeetPerDegreesPerFeetInOneNewtonMeterPerRadianPerMeter, PoundForceFeetPerDegreesPerFeetTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RotationalStiffnessPerLengthUnit.KilonewtonMeterPerRadianPerMeter },
+ new object[] { RotationalStiffnessPerLengthUnit.KilopoundForceFootPerDegreesPerFoot },
+ new object[] { RotationalStiffnessPerLengthUnit.MeganewtonMeterPerRadianPerMeter },
+ new object[] { RotationalStiffnessPerLengthUnit.NewtonMeterPerRadianPerMeter },
+ new object[] { RotationalStiffnessPerLengthUnit.PoundForceFootPerDegreesPerFoot },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -196,37 +219,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RotationalStiffnessPerLengthUnit unit)
{
- var newtonmeterperradianpermeter = RotationalStiffnessPerLength.FromNewtonMetersPerRadianPerMeter(1);
-
- var kilonewtonmeterperradianpermeterQuantity = newtonmeterperradianpermeter.ToUnit(RotationalStiffnessPerLengthUnit.KilonewtonMeterPerRadianPerMeter);
- AssertEx.EqualTolerance(KilonewtonMetersPerRadianPerMeterInOneNewtonMeterPerRadianPerMeter, (double)kilonewtonmeterperradianpermeterQuantity.Value, KilonewtonMetersPerRadianPerMeterTolerance);
- Assert.Equal(RotationalStiffnessPerLengthUnit.KilonewtonMeterPerRadianPerMeter, kilonewtonmeterperradianpermeterQuantity.Unit);
+ var inBaseUnits = RotationalStiffnessPerLength.From(1.0, RotationalStiffnessPerLength.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilopoundforcefootperdegreesperfootQuantity = newtonmeterperradianpermeter.ToUnit(RotationalStiffnessPerLengthUnit.KilopoundForceFootPerDegreesPerFoot);
- AssertEx.EqualTolerance(KilopoundForceFeetPerDegreesPerFeetInOneNewtonMeterPerRadianPerMeter, (double)kilopoundforcefootperdegreesperfootQuantity.Value, KilopoundForceFeetPerDegreesPerFeetTolerance);
- Assert.Equal(RotationalStiffnessPerLengthUnit.KilopoundForceFootPerDegreesPerFoot, kilopoundforcefootperdegreesperfootQuantity.Unit);
-
- var meganewtonmeterperradianpermeterQuantity = newtonmeterperradianpermeter.ToUnit(RotationalStiffnessPerLengthUnit.MeganewtonMeterPerRadianPerMeter);
- AssertEx.EqualTolerance(MeganewtonMetersPerRadianPerMeterInOneNewtonMeterPerRadianPerMeter, (double)meganewtonmeterperradianpermeterQuantity.Value, MeganewtonMetersPerRadianPerMeterTolerance);
- Assert.Equal(RotationalStiffnessPerLengthUnit.MeganewtonMeterPerRadianPerMeter, meganewtonmeterperradianpermeterQuantity.Unit);
-
- var newtonmeterperradianpermeterQuantity = newtonmeterperradianpermeter.ToUnit(RotationalStiffnessPerLengthUnit.NewtonMeterPerRadianPerMeter);
- AssertEx.EqualTolerance(NewtonMetersPerRadianPerMeterInOneNewtonMeterPerRadianPerMeter, (double)newtonmeterperradianpermeterQuantity.Value, NewtonMetersPerRadianPerMeterTolerance);
- Assert.Equal(RotationalStiffnessPerLengthUnit.NewtonMeterPerRadianPerMeter, newtonmeterperradianpermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundforcefootperdegreesperfootQuantity = newtonmeterperradianpermeter.ToUnit(RotationalStiffnessPerLengthUnit.PoundForceFootPerDegreesPerFoot);
- AssertEx.EqualTolerance(PoundForceFeetPerDegreesPerFeetInOneNewtonMeterPerRadianPerMeter, (double)poundforcefootperdegreesperfootQuantity.Value, PoundForceFeetPerDegreesPerFeetTolerance);
- Assert.Equal(RotationalStiffnessPerLengthUnit.PoundForceFootPerDegreesPerFoot, poundforcefootperdegreesperfootQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RotationalStiffnessPerLengthUnit unit)
+ {
+ var quantity = RotationalStiffnessPerLength.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RotationalStiffnessPerLengthUnit unit)
{
- var quantityInBaseUnit = RotationalStiffnessPerLength.FromNewtonMetersPerRadianPerMeter(1).ToBaseUnit();
- Assert.Equal(RotationalStiffnessPerLength.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = RotationalStiffnessPerLength.Units.FirstOrDefault(u => u != RotationalStiffnessPerLength.BaseUnit && u != RotationalStiffnessPerLengthUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RotationalStiffnessPerLengthUnit.Undefined)
+ fromUnit = RotationalStiffnessPerLength.BaseUnit;
+
+ var quantity = RotationalStiffnessPerLength.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessTestsBase.g.cs
index b5d56b1897..6129bd22c8 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RotationalStiffnessTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -107,6 +108,84 @@ public abstract partial class RotationalStiffnessTestsBase : QuantityTestsBase
protected virtual double PoundForceFeetPerDegreesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RotationalStiffnessUnit unit)
+ {
+ return unit switch
+ {
+ RotationalStiffnessUnit.CentinewtonMeterPerDegree => (CentinewtonMetersPerDegreeInOneNewtonMeterPerRadian, CentinewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.CentinewtonMillimeterPerDegree => (CentinewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, CentinewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.CentinewtonMillimeterPerRadian => (CentinewtonMillimetersPerRadianInOneNewtonMeterPerRadian, CentinewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.DecanewtonMeterPerDegree => (DecanewtonMetersPerDegreeInOneNewtonMeterPerRadian, DecanewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.DecanewtonMillimeterPerDegree => (DecanewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, DecanewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.DecanewtonMillimeterPerRadian => (DecanewtonMillimetersPerRadianInOneNewtonMeterPerRadian, DecanewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.DecinewtonMeterPerDegree => (DecinewtonMetersPerDegreeInOneNewtonMeterPerRadian, DecinewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.DecinewtonMillimeterPerDegree => (DecinewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, DecinewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.DecinewtonMillimeterPerRadian => (DecinewtonMillimetersPerRadianInOneNewtonMeterPerRadian, DecinewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.KilonewtonMeterPerDegree => (KilonewtonMetersPerDegreeInOneNewtonMeterPerRadian, KilonewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.KilonewtonMeterPerRadian => (KilonewtonMetersPerRadianInOneNewtonMeterPerRadian, KilonewtonMetersPerRadianTolerance),
+ RotationalStiffnessUnit.KilonewtonMillimeterPerDegree => (KilonewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, KilonewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.KilonewtonMillimeterPerRadian => (KilonewtonMillimetersPerRadianInOneNewtonMeterPerRadian, KilonewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.KilopoundForceFootPerDegrees => (KilopoundForceFeetPerDegreesInOneNewtonMeterPerRadian, KilopoundForceFeetPerDegreesTolerance),
+ RotationalStiffnessUnit.MeganewtonMeterPerDegree => (MeganewtonMetersPerDegreeInOneNewtonMeterPerRadian, MeganewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.MeganewtonMeterPerRadian => (MeganewtonMetersPerRadianInOneNewtonMeterPerRadian, MeganewtonMetersPerRadianTolerance),
+ RotationalStiffnessUnit.MeganewtonMillimeterPerDegree => (MeganewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, MeganewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.MeganewtonMillimeterPerRadian => (MeganewtonMillimetersPerRadianInOneNewtonMeterPerRadian, MeganewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.MicronewtonMeterPerDegree => (MicronewtonMetersPerDegreeInOneNewtonMeterPerRadian, MicronewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.MicronewtonMillimeterPerDegree => (MicronewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, MicronewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.MicronewtonMillimeterPerRadian => (MicronewtonMillimetersPerRadianInOneNewtonMeterPerRadian, MicronewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.MillinewtonMeterPerDegree => (MillinewtonMetersPerDegreeInOneNewtonMeterPerRadian, MillinewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.MillinewtonMillimeterPerDegree => (MillinewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, MillinewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.MillinewtonMillimeterPerRadian => (MillinewtonMillimetersPerRadianInOneNewtonMeterPerRadian, MillinewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.NanonewtonMeterPerDegree => (NanonewtonMetersPerDegreeInOneNewtonMeterPerRadian, NanonewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.NanonewtonMillimeterPerDegree => (NanonewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, NanonewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.NanonewtonMillimeterPerRadian => (NanonewtonMillimetersPerRadianInOneNewtonMeterPerRadian, NanonewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.NewtonMeterPerDegree => (NewtonMetersPerDegreeInOneNewtonMeterPerRadian, NewtonMetersPerDegreeTolerance),
+ RotationalStiffnessUnit.NewtonMeterPerRadian => (NewtonMetersPerRadianInOneNewtonMeterPerRadian, NewtonMetersPerRadianTolerance),
+ RotationalStiffnessUnit.NewtonMillimeterPerDegree => (NewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, NewtonMillimetersPerDegreeTolerance),
+ RotationalStiffnessUnit.NewtonMillimeterPerRadian => (NewtonMillimetersPerRadianInOneNewtonMeterPerRadian, NewtonMillimetersPerRadianTolerance),
+ RotationalStiffnessUnit.PoundForceFeetPerRadian => (PoundForceFeetPerRadianInOneNewtonMeterPerRadian, PoundForceFeetPerRadianTolerance),
+ RotationalStiffnessUnit.PoundForceFootPerDegrees => (PoundForceFeetPerDegreesInOneNewtonMeterPerRadian, PoundForceFeetPerDegreesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { RotationalStiffnessUnit.CentinewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.CentinewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.CentinewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.DecanewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.DecanewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.DecanewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.DecinewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.DecinewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.DecinewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.KilonewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.KilonewtonMeterPerRadian },
+ new object[] { RotationalStiffnessUnit.KilonewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.KilonewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.KilopoundForceFootPerDegrees },
+ new object[] { RotationalStiffnessUnit.MeganewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.MeganewtonMeterPerRadian },
+ new object[] { RotationalStiffnessUnit.MeganewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.MeganewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.MicronewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.MicronewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.MicronewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.MillinewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.MillinewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.MillinewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.NanonewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.NanonewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.NanonewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.NewtonMeterPerDegree },
+ new object[] { RotationalStiffnessUnit.NewtonMeterPerRadian },
+ new object[] { RotationalStiffnessUnit.NewtonMillimeterPerDegree },
+ new object[] { RotationalStiffnessUnit.NewtonMillimeterPerRadian },
+ new object[] { RotationalStiffnessUnit.PoundForceFeetPerRadian },
+ new object[] { RotationalStiffnessUnit.PoundForceFootPerDegrees },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -420,149 +499,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(RotationalStiffnessUnit unit)
{
- var newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
-
- var centinewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.CentinewtonMeterPerDegree);
- AssertEx.EqualTolerance(CentinewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)centinewtonmeterperdegreeQuantity.Value, CentinewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.CentinewtonMeterPerDegree, centinewtonmeterperdegreeQuantity.Unit);
-
- var centinewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.CentinewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(CentinewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)centinewtonmillimeterperdegreeQuantity.Value, CentinewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.CentinewtonMillimeterPerDegree, centinewtonmillimeterperdegreeQuantity.Unit);
-
- var centinewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.CentinewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(CentinewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)centinewtonmillimeterperradianQuantity.Value, CentinewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.CentinewtonMillimeterPerRadian, centinewtonmillimeterperradianQuantity.Unit);
-
- var decanewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.DecanewtonMeterPerDegree);
- AssertEx.EqualTolerance(DecanewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)decanewtonmeterperdegreeQuantity.Value, DecanewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.DecanewtonMeterPerDegree, decanewtonmeterperdegreeQuantity.Unit);
-
- var decanewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.DecanewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(DecanewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)decanewtonmillimeterperdegreeQuantity.Value, DecanewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.DecanewtonMillimeterPerDegree, decanewtonmillimeterperdegreeQuantity.Unit);
-
- var decanewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.DecanewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(DecanewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)decanewtonmillimeterperradianQuantity.Value, DecanewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.DecanewtonMillimeterPerRadian, decanewtonmillimeterperradianQuantity.Unit);
-
- var decinewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.DecinewtonMeterPerDegree);
- AssertEx.EqualTolerance(DecinewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)decinewtonmeterperdegreeQuantity.Value, DecinewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.DecinewtonMeterPerDegree, decinewtonmeterperdegreeQuantity.Unit);
-
- var decinewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.DecinewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(DecinewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)decinewtonmillimeterperdegreeQuantity.Value, DecinewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.DecinewtonMillimeterPerDegree, decinewtonmillimeterperdegreeQuantity.Unit);
-
- var decinewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.DecinewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(DecinewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)decinewtonmillimeterperradianQuantity.Value, DecinewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.DecinewtonMillimeterPerRadian, decinewtonmillimeterperradianQuantity.Unit);
-
- var kilonewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.KilonewtonMeterPerDegree);
- AssertEx.EqualTolerance(KilonewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)kilonewtonmeterperdegreeQuantity.Value, KilonewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.KilonewtonMeterPerDegree, kilonewtonmeterperdegreeQuantity.Unit);
-
- var kilonewtonmeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.KilonewtonMeterPerRadian);
- AssertEx.EqualTolerance(KilonewtonMetersPerRadianInOneNewtonMeterPerRadian, (double)kilonewtonmeterperradianQuantity.Value, KilonewtonMetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.KilonewtonMeterPerRadian, kilonewtonmeterperradianQuantity.Unit);
-
- var kilonewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.KilonewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(KilonewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)kilonewtonmillimeterperdegreeQuantity.Value, KilonewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.KilonewtonMillimeterPerDegree, kilonewtonmillimeterperdegreeQuantity.Unit);
-
- var kilonewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.KilonewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(KilonewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)kilonewtonmillimeterperradianQuantity.Value, KilonewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.KilonewtonMillimeterPerRadian, kilonewtonmillimeterperradianQuantity.Unit);
-
- var kilopoundforcefootperdegreesQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.KilopoundForceFootPerDegrees);
- AssertEx.EqualTolerance(KilopoundForceFeetPerDegreesInOneNewtonMeterPerRadian, (double)kilopoundforcefootperdegreesQuantity.Value, KilopoundForceFeetPerDegreesTolerance);
- Assert.Equal(RotationalStiffnessUnit.KilopoundForceFootPerDegrees, kilopoundforcefootperdegreesQuantity.Unit);
-
- var meganewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MeganewtonMeterPerDegree);
- AssertEx.EqualTolerance(MeganewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)meganewtonmeterperdegreeQuantity.Value, MeganewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.MeganewtonMeterPerDegree, meganewtonmeterperdegreeQuantity.Unit);
+ var inBaseUnits = RotationalStiffness.From(1.0, RotationalStiffness.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var meganewtonmeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MeganewtonMeterPerRadian);
- AssertEx.EqualTolerance(MeganewtonMetersPerRadianInOneNewtonMeterPerRadian, (double)meganewtonmeterperradianQuantity.Value, MeganewtonMetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.MeganewtonMeterPerRadian, meganewtonmeterperradianQuantity.Unit);
-
- var meganewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MeganewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(MeganewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)meganewtonmillimeterperdegreeQuantity.Value, MeganewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.MeganewtonMillimeterPerDegree, meganewtonmillimeterperdegreeQuantity.Unit);
-
- var meganewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MeganewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(MeganewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)meganewtonmillimeterperradianQuantity.Value, MeganewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.MeganewtonMillimeterPerRadian, meganewtonmillimeterperradianQuantity.Unit);
-
- var micronewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MicronewtonMeterPerDegree);
- AssertEx.EqualTolerance(MicronewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)micronewtonmeterperdegreeQuantity.Value, MicronewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.MicronewtonMeterPerDegree, micronewtonmeterperdegreeQuantity.Unit);
-
- var micronewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MicronewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(MicronewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)micronewtonmillimeterperdegreeQuantity.Value, MicronewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.MicronewtonMillimeterPerDegree, micronewtonmillimeterperdegreeQuantity.Unit);
-
- var micronewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MicronewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(MicronewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)micronewtonmillimeterperradianQuantity.Value, MicronewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.MicronewtonMillimeterPerRadian, micronewtonmillimeterperradianQuantity.Unit);
-
- var millinewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MillinewtonMeterPerDegree);
- AssertEx.EqualTolerance(MillinewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)millinewtonmeterperdegreeQuantity.Value, MillinewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.MillinewtonMeterPerDegree, millinewtonmeterperdegreeQuantity.Unit);
-
- var millinewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MillinewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(MillinewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)millinewtonmillimeterperdegreeQuantity.Value, MillinewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.MillinewtonMillimeterPerDegree, millinewtonmillimeterperdegreeQuantity.Unit);
-
- var millinewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.MillinewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(MillinewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)millinewtonmillimeterperradianQuantity.Value, MillinewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.MillinewtonMillimeterPerRadian, millinewtonmillimeterperradianQuantity.Unit);
-
- var nanonewtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NanonewtonMeterPerDegree);
- AssertEx.EqualTolerance(NanonewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)nanonewtonmeterperdegreeQuantity.Value, NanonewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.NanonewtonMeterPerDegree, nanonewtonmeterperdegreeQuantity.Unit);
-
- var nanonewtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NanonewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(NanonewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)nanonewtonmillimeterperdegreeQuantity.Value, NanonewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.NanonewtonMillimeterPerDegree, nanonewtonmillimeterperdegreeQuantity.Unit);
-
- var nanonewtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NanonewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(NanonewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)nanonewtonmillimeterperradianQuantity.Value, NanonewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.NanonewtonMillimeterPerRadian, nanonewtonmillimeterperradianQuantity.Unit);
-
- var newtonmeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NewtonMeterPerDegree);
- AssertEx.EqualTolerance(NewtonMetersPerDegreeInOneNewtonMeterPerRadian, (double)newtonmeterperdegreeQuantity.Value, NewtonMetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.NewtonMeterPerDegree, newtonmeterperdegreeQuantity.Unit);
-
- var newtonmeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NewtonMeterPerRadian);
- AssertEx.EqualTolerance(NewtonMetersPerRadianInOneNewtonMeterPerRadian, (double)newtonmeterperradianQuantity.Value, NewtonMetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.NewtonMeterPerRadian, newtonmeterperradianQuantity.Unit);
-
- var newtonmillimeterperdegreeQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NewtonMillimeterPerDegree);
- AssertEx.EqualTolerance(NewtonMillimetersPerDegreeInOneNewtonMeterPerRadian, (double)newtonmillimeterperdegreeQuantity.Value, NewtonMillimetersPerDegreeTolerance);
- Assert.Equal(RotationalStiffnessUnit.NewtonMillimeterPerDegree, newtonmillimeterperdegreeQuantity.Unit);
-
- var newtonmillimeterperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.NewtonMillimeterPerRadian);
- AssertEx.EqualTolerance(NewtonMillimetersPerRadianInOneNewtonMeterPerRadian, (double)newtonmillimeterperradianQuantity.Value, NewtonMillimetersPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.NewtonMillimeterPerRadian, newtonmillimeterperradianQuantity.Unit);
-
- var poundforcefeetperradianQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.PoundForceFeetPerRadian);
- AssertEx.EqualTolerance(PoundForceFeetPerRadianInOneNewtonMeterPerRadian, (double)poundforcefeetperradianQuantity.Value, PoundForceFeetPerRadianTolerance);
- Assert.Equal(RotationalStiffnessUnit.PoundForceFeetPerRadian, poundforcefeetperradianQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundforcefootperdegreesQuantity = newtonmeterperradian.ToUnit(RotationalStiffnessUnit.PoundForceFootPerDegrees);
- AssertEx.EqualTolerance(PoundForceFeetPerDegreesInOneNewtonMeterPerRadian, (double)poundforcefootperdegreesQuantity.Value, PoundForceFeetPerDegreesTolerance);
- Assert.Equal(RotationalStiffnessUnit.PoundForceFootPerDegrees, poundforcefootperdegreesQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(RotationalStiffnessUnit unit)
+ {
+ var quantity = RotationalStiffness.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RotationalStiffnessUnit unit)
{
- var quantityInBaseUnit = RotationalStiffness.FromNewtonMetersPerRadian(1).ToBaseUnit();
- Assert.Equal(RotationalStiffness.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = RotationalStiffness.Units.FirstOrDefault(u => u != RotationalStiffness.BaseUnit && u != RotationalStiffnessUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == RotationalStiffnessUnit.Undefined)
+ fromUnit = RotationalStiffness.BaseUnit;
+
+ var quantity = RotationalStiffness.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ScalarTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ScalarTestsBase.g.cs
index d01595760c..0a1f6c8422 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ScalarTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ScalarTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class ScalarTestsBase : QuantityTestsBase
protected virtual double AmountTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ScalarUnit unit)
+ {
+ return unit switch
+ {
+ ScalarUnit.Amount => (AmountInOneAmount, AmountTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ScalarUnit.Amount },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ScalarUnit unit)
{
- var amount = Scalar.FromAmount(1);
+ var inBaseUnits = Scalar.From(1.0, Scalar.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var amountQuantity = amount.ToUnit(ScalarUnit.Amount);
- AssertEx.EqualTolerance(AmountInOneAmount, (double)amountQuantity.Value, AmountTolerance);
- Assert.Equal(ScalarUnit.Amount, amountQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ScalarUnit unit)
+ {
+ var quantity = Scalar.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ScalarUnit unit)
{
- var quantityInBaseUnit = Scalar.FromAmount(1).ToBaseUnit();
- Assert.Equal(Scalar.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Scalar.Units.FirstOrDefault(u => u != Scalar.BaseUnit && u != ScalarUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ScalarUnit.Undefined)
+ fromUnit = Scalar.BaseUnit;
+
+ var quantity = Scalar.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SolidAngleTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SolidAngleTestsBase.g.cs
index 5c9b1a4ebb..505594fca5 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SolidAngleTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SolidAngleTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class SolidAngleTestsBase : QuantityTestsBase
protected virtual double SteradiansTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SolidAngleUnit unit)
+ {
+ return unit switch
+ {
+ SolidAngleUnit.Steradian => (SteradiansInOneSteradian, SteradiansTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SolidAngleUnit.Steradian },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SolidAngleUnit unit)
{
- var steradian = SolidAngle.FromSteradians(1);
+ var inBaseUnits = SolidAngle.From(1.0, SolidAngle.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var steradianQuantity = steradian.ToUnit(SolidAngleUnit.Steradian);
- AssertEx.EqualTolerance(SteradiansInOneSteradian, (double)steradianQuantity.Value, SteradiansTolerance);
- Assert.Equal(SolidAngleUnit.Steradian, steradianQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SolidAngleUnit unit)
+ {
+ var quantity = SolidAngle.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SolidAngleUnit unit)
{
- var quantityInBaseUnit = SolidAngle.FromSteradians(1).ToBaseUnit();
- Assert.Equal(SolidAngle.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = SolidAngle.Units.FirstOrDefault(u => u != SolidAngle.BaseUnit && u != SolidAngleUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SolidAngleUnit.Undefined)
+ fromUnit = SolidAngle.BaseUnit;
+
+ var quantity = SolidAngle.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEnergyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEnergyTestsBase.g.cs
index 4a59195ae3..00999677a8 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEnergyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEnergyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -91,6 +92,68 @@ public abstract partial class SpecificEnergyTestsBase : QuantityTestsBase
protected virtual double WattHoursPerKilogramTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SpecificEnergyUnit unit)
+ {
+ return unit switch
+ {
+ SpecificEnergyUnit.BtuPerPound => (BtuPerPoundInOneJoulePerKilogram, BtuPerPoundTolerance),
+ SpecificEnergyUnit.CaloriePerGram => (CaloriesPerGramInOneJoulePerKilogram, CaloriesPerGramTolerance),
+ SpecificEnergyUnit.GigawattDayPerKilogram => (GigawattDaysPerKilogramInOneJoulePerKilogram, GigawattDaysPerKilogramTolerance),
+ SpecificEnergyUnit.GigawattDayPerShortTon => (GigawattDaysPerShortTonInOneJoulePerKilogram, GigawattDaysPerShortTonTolerance),
+ SpecificEnergyUnit.GigawattDayPerTonne => (GigawattDaysPerTonneInOneJoulePerKilogram, GigawattDaysPerTonneTolerance),
+ SpecificEnergyUnit.GigawattHourPerKilogram => (GigawattHoursPerKilogramInOneJoulePerKilogram, GigawattHoursPerKilogramTolerance),
+ SpecificEnergyUnit.JoulePerKilogram => (JoulesPerKilogramInOneJoulePerKilogram, JoulesPerKilogramTolerance),
+ SpecificEnergyUnit.KilocaloriePerGram => (KilocaloriesPerGramInOneJoulePerKilogram, KilocaloriesPerGramTolerance),
+ SpecificEnergyUnit.KilojoulePerKilogram => (KilojoulesPerKilogramInOneJoulePerKilogram, KilojoulesPerKilogramTolerance),
+ SpecificEnergyUnit.KilowattDayPerKilogram => (KilowattDaysPerKilogramInOneJoulePerKilogram, KilowattDaysPerKilogramTolerance),
+ SpecificEnergyUnit.KilowattDayPerShortTon => (KilowattDaysPerShortTonInOneJoulePerKilogram, KilowattDaysPerShortTonTolerance),
+ SpecificEnergyUnit.KilowattDayPerTonne => (KilowattDaysPerTonneInOneJoulePerKilogram, KilowattDaysPerTonneTolerance),
+ SpecificEnergyUnit.KilowattHourPerKilogram => (KilowattHoursPerKilogramInOneJoulePerKilogram, KilowattHoursPerKilogramTolerance),
+ SpecificEnergyUnit.MegajoulePerKilogram => (MegajoulesPerKilogramInOneJoulePerKilogram, MegajoulesPerKilogramTolerance),
+ SpecificEnergyUnit.MegawattDayPerKilogram => (MegawattDaysPerKilogramInOneJoulePerKilogram, MegawattDaysPerKilogramTolerance),
+ SpecificEnergyUnit.MegawattDayPerShortTon => (MegawattDaysPerShortTonInOneJoulePerKilogram, MegawattDaysPerShortTonTolerance),
+ SpecificEnergyUnit.MegawattDayPerTonne => (MegawattDaysPerTonneInOneJoulePerKilogram, MegawattDaysPerTonneTolerance),
+ SpecificEnergyUnit.MegawattHourPerKilogram => (MegawattHoursPerKilogramInOneJoulePerKilogram, MegawattHoursPerKilogramTolerance),
+ SpecificEnergyUnit.TerawattDayPerKilogram => (TerawattDaysPerKilogramInOneJoulePerKilogram, TerawattDaysPerKilogramTolerance),
+ SpecificEnergyUnit.TerawattDayPerShortTon => (TerawattDaysPerShortTonInOneJoulePerKilogram, TerawattDaysPerShortTonTolerance),
+ SpecificEnergyUnit.TerawattDayPerTonne => (TerawattDaysPerTonneInOneJoulePerKilogram, TerawattDaysPerTonneTolerance),
+ SpecificEnergyUnit.WattDayPerKilogram => (WattDaysPerKilogramInOneJoulePerKilogram, WattDaysPerKilogramTolerance),
+ SpecificEnergyUnit.WattDayPerShortTon => (WattDaysPerShortTonInOneJoulePerKilogram, WattDaysPerShortTonTolerance),
+ SpecificEnergyUnit.WattDayPerTonne => (WattDaysPerTonneInOneJoulePerKilogram, WattDaysPerTonneTolerance),
+ SpecificEnergyUnit.WattHourPerKilogram => (WattHoursPerKilogramInOneJoulePerKilogram, WattHoursPerKilogramTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SpecificEnergyUnit.BtuPerPound },
+ new object[] { SpecificEnergyUnit.CaloriePerGram },
+ new object[] { SpecificEnergyUnit.GigawattDayPerKilogram },
+ new object[] { SpecificEnergyUnit.GigawattDayPerShortTon },
+ new object[] { SpecificEnergyUnit.GigawattDayPerTonne },
+ new object[] { SpecificEnergyUnit.GigawattHourPerKilogram },
+ new object[] { SpecificEnergyUnit.JoulePerKilogram },
+ new object[] { SpecificEnergyUnit.KilocaloriePerGram },
+ new object[] { SpecificEnergyUnit.KilojoulePerKilogram },
+ new object[] { SpecificEnergyUnit.KilowattDayPerKilogram },
+ new object[] { SpecificEnergyUnit.KilowattDayPerShortTon },
+ new object[] { SpecificEnergyUnit.KilowattDayPerTonne },
+ new object[] { SpecificEnergyUnit.KilowattHourPerKilogram },
+ new object[] { SpecificEnergyUnit.MegajoulePerKilogram },
+ new object[] { SpecificEnergyUnit.MegawattDayPerKilogram },
+ new object[] { SpecificEnergyUnit.MegawattDayPerShortTon },
+ new object[] { SpecificEnergyUnit.MegawattDayPerTonne },
+ new object[] { SpecificEnergyUnit.MegawattHourPerKilogram },
+ new object[] { SpecificEnergyUnit.TerawattDayPerKilogram },
+ new object[] { SpecificEnergyUnit.TerawattDayPerShortTon },
+ new object[] { SpecificEnergyUnit.TerawattDayPerTonne },
+ new object[] { SpecificEnergyUnit.WattDayPerKilogram },
+ new object[] { SpecificEnergyUnit.WattDayPerShortTon },
+ new object[] { SpecificEnergyUnit.WattDayPerTonne },
+ new object[] { SpecificEnergyUnit.WattHourPerKilogram },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -356,117 +419,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SpecificEnergyUnit unit)
{
- var jouleperkilogram = SpecificEnergy.FromJoulesPerKilogram(1);
-
- var btuperpoundQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.BtuPerPound);
- AssertEx.EqualTolerance(BtuPerPoundInOneJoulePerKilogram, (double)btuperpoundQuantity.Value, BtuPerPoundTolerance);
- Assert.Equal(SpecificEnergyUnit.BtuPerPound, btuperpoundQuantity.Unit);
-
- var caloriepergramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.CaloriePerGram);
- AssertEx.EqualTolerance(CaloriesPerGramInOneJoulePerKilogram, (double)caloriepergramQuantity.Value, CaloriesPerGramTolerance);
- Assert.Equal(SpecificEnergyUnit.CaloriePerGram, caloriepergramQuantity.Unit);
-
- var gigawattdayperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.GigawattDayPerKilogram);
- AssertEx.EqualTolerance(GigawattDaysPerKilogramInOneJoulePerKilogram, (double)gigawattdayperkilogramQuantity.Value, GigawattDaysPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.GigawattDayPerKilogram, gigawattdayperkilogramQuantity.Unit);
-
- var gigawattdaypershorttonQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.GigawattDayPerShortTon);
- AssertEx.EqualTolerance(GigawattDaysPerShortTonInOneJoulePerKilogram, (double)gigawattdaypershorttonQuantity.Value, GigawattDaysPerShortTonTolerance);
- Assert.Equal(SpecificEnergyUnit.GigawattDayPerShortTon, gigawattdaypershorttonQuantity.Unit);
-
- var gigawattdaypertonneQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.GigawattDayPerTonne);
- AssertEx.EqualTolerance(GigawattDaysPerTonneInOneJoulePerKilogram, (double)gigawattdaypertonneQuantity.Value, GigawattDaysPerTonneTolerance);
- Assert.Equal(SpecificEnergyUnit.GigawattDayPerTonne, gigawattdaypertonneQuantity.Unit);
-
- var gigawatthourperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.GigawattHourPerKilogram);
- AssertEx.EqualTolerance(GigawattHoursPerKilogramInOneJoulePerKilogram, (double)gigawatthourperkilogramQuantity.Value, GigawattHoursPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.GigawattHourPerKilogram, gigawatthourperkilogramQuantity.Unit);
-
- var jouleperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.JoulePerKilogram);
- AssertEx.EqualTolerance(JoulesPerKilogramInOneJoulePerKilogram, (double)jouleperkilogramQuantity.Value, JoulesPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.JoulePerKilogram, jouleperkilogramQuantity.Unit);
-
- var kilocaloriepergramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.KilocaloriePerGram);
- AssertEx.EqualTolerance(KilocaloriesPerGramInOneJoulePerKilogram, (double)kilocaloriepergramQuantity.Value, KilocaloriesPerGramTolerance);
- Assert.Equal(SpecificEnergyUnit.KilocaloriePerGram, kilocaloriepergramQuantity.Unit);
-
- var kilojouleperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.KilojoulePerKilogram);
- AssertEx.EqualTolerance(KilojoulesPerKilogramInOneJoulePerKilogram, (double)kilojouleperkilogramQuantity.Value, KilojoulesPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.KilojoulePerKilogram, kilojouleperkilogramQuantity.Unit);
-
- var kilowattdayperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.KilowattDayPerKilogram);
- AssertEx.EqualTolerance(KilowattDaysPerKilogramInOneJoulePerKilogram, (double)kilowattdayperkilogramQuantity.Value, KilowattDaysPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.KilowattDayPerKilogram, kilowattdayperkilogramQuantity.Unit);
-
- var kilowattdaypershorttonQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.KilowattDayPerShortTon);
- AssertEx.EqualTolerance(KilowattDaysPerShortTonInOneJoulePerKilogram, (double)kilowattdaypershorttonQuantity.Value, KilowattDaysPerShortTonTolerance);
- Assert.Equal(SpecificEnergyUnit.KilowattDayPerShortTon, kilowattdaypershorttonQuantity.Unit);
-
- var kilowattdaypertonneQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.KilowattDayPerTonne);
- AssertEx.EqualTolerance(KilowattDaysPerTonneInOneJoulePerKilogram, (double)kilowattdaypertonneQuantity.Value, KilowattDaysPerTonneTolerance);
- Assert.Equal(SpecificEnergyUnit.KilowattDayPerTonne, kilowattdaypertonneQuantity.Unit);
+ var inBaseUnits = SpecificEnergy.From(1.0, SpecificEnergy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilowatthourperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.KilowattHourPerKilogram);
- AssertEx.EqualTolerance(KilowattHoursPerKilogramInOneJoulePerKilogram, (double)kilowatthourperkilogramQuantity.Value, KilowattHoursPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.KilowattHourPerKilogram, kilowatthourperkilogramQuantity.Unit);
-
- var megajouleperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.MegajoulePerKilogram);
- AssertEx.EqualTolerance(MegajoulesPerKilogramInOneJoulePerKilogram, (double)megajouleperkilogramQuantity.Value, MegajoulesPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.MegajoulePerKilogram, megajouleperkilogramQuantity.Unit);
-
- var megawattdayperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.MegawattDayPerKilogram);
- AssertEx.EqualTolerance(MegawattDaysPerKilogramInOneJoulePerKilogram, (double)megawattdayperkilogramQuantity.Value, MegawattDaysPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.MegawattDayPerKilogram, megawattdayperkilogramQuantity.Unit);
-
- var megawattdaypershorttonQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.MegawattDayPerShortTon);
- AssertEx.EqualTolerance(MegawattDaysPerShortTonInOneJoulePerKilogram, (double)megawattdaypershorttonQuantity.Value, MegawattDaysPerShortTonTolerance);
- Assert.Equal(SpecificEnergyUnit.MegawattDayPerShortTon, megawattdaypershorttonQuantity.Unit);
-
- var megawattdaypertonneQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.MegawattDayPerTonne);
- AssertEx.EqualTolerance(MegawattDaysPerTonneInOneJoulePerKilogram, (double)megawattdaypertonneQuantity.Value, MegawattDaysPerTonneTolerance);
- Assert.Equal(SpecificEnergyUnit.MegawattDayPerTonne, megawattdaypertonneQuantity.Unit);
-
- var megawatthourperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.MegawattHourPerKilogram);
- AssertEx.EqualTolerance(MegawattHoursPerKilogramInOneJoulePerKilogram, (double)megawatthourperkilogramQuantity.Value, MegawattHoursPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.MegawattHourPerKilogram, megawatthourperkilogramQuantity.Unit);
-
- var terawattdayperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.TerawattDayPerKilogram);
- AssertEx.EqualTolerance(TerawattDaysPerKilogramInOneJoulePerKilogram, (double)terawattdayperkilogramQuantity.Value, TerawattDaysPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.TerawattDayPerKilogram, terawattdayperkilogramQuantity.Unit);
-
- var terawattdaypershorttonQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.TerawattDayPerShortTon);
- AssertEx.EqualTolerance(TerawattDaysPerShortTonInOneJoulePerKilogram, (double)terawattdaypershorttonQuantity.Value, TerawattDaysPerShortTonTolerance);
- Assert.Equal(SpecificEnergyUnit.TerawattDayPerShortTon, terawattdaypershorttonQuantity.Unit);
-
- var terawattdaypertonneQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.TerawattDayPerTonne);
- AssertEx.EqualTolerance(TerawattDaysPerTonneInOneJoulePerKilogram, (double)terawattdaypertonneQuantity.Value, TerawattDaysPerTonneTolerance);
- Assert.Equal(SpecificEnergyUnit.TerawattDayPerTonne, terawattdaypertonneQuantity.Unit);
-
- var wattdayperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.WattDayPerKilogram);
- AssertEx.EqualTolerance(WattDaysPerKilogramInOneJoulePerKilogram, (double)wattdayperkilogramQuantity.Value, WattDaysPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.WattDayPerKilogram, wattdayperkilogramQuantity.Unit);
-
- var wattdaypershorttonQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.WattDayPerShortTon);
- AssertEx.EqualTolerance(WattDaysPerShortTonInOneJoulePerKilogram, (double)wattdaypershorttonQuantity.Value, WattDaysPerShortTonTolerance);
- Assert.Equal(SpecificEnergyUnit.WattDayPerShortTon, wattdaypershorttonQuantity.Unit);
-
- var wattdaypertonneQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.WattDayPerTonne);
- AssertEx.EqualTolerance(WattDaysPerTonneInOneJoulePerKilogram, (double)wattdaypertonneQuantity.Value, WattDaysPerTonneTolerance);
- Assert.Equal(SpecificEnergyUnit.WattDayPerTonne, wattdaypertonneQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var watthourperkilogramQuantity = jouleperkilogram.ToUnit(SpecificEnergyUnit.WattHourPerKilogram);
- AssertEx.EqualTolerance(WattHoursPerKilogramInOneJoulePerKilogram, (double)watthourperkilogramQuantity.Value, WattHoursPerKilogramTolerance);
- Assert.Equal(SpecificEnergyUnit.WattHourPerKilogram, watthourperkilogramQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SpecificEnergyUnit unit)
+ {
+ var quantity = SpecificEnergy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SpecificEnergyUnit unit)
{
- var quantityInBaseUnit = SpecificEnergy.FromJoulesPerKilogram(1).ToBaseUnit();
- Assert.Equal(SpecificEnergy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = SpecificEnergy.Units.FirstOrDefault(u => u != SpecificEnergy.BaseUnit && u != SpecificEnergyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SpecificEnergyUnit.Undefined)
+ fromUnit = SpecificEnergy.BaseUnit;
+
+ var quantity = SpecificEnergy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEntropyTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEntropyTestsBase.g.cs
index d46d26509b..9c9f018be4 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEntropyTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificEntropyTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -59,6 +60,36 @@ public abstract partial class SpecificEntropyTestsBase : QuantityTestsBase
protected virtual double MegajoulesPerKilogramKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SpecificEntropyUnit unit)
+ {
+ return unit switch
+ {
+ SpecificEntropyUnit.BtuPerPoundFahrenheit => (BtusPerPoundFahrenheitInOneJoulePerKilogramKelvin, BtusPerPoundFahrenheitTolerance),
+ SpecificEntropyUnit.CaloriePerGramKelvin => (CaloriesPerGramKelvinInOneJoulePerKilogramKelvin, CaloriesPerGramKelvinTolerance),
+ SpecificEntropyUnit.JoulePerKilogramDegreeCelsius => (JoulesPerKilogramDegreeCelsiusInOneJoulePerKilogramKelvin, JoulesPerKilogramDegreeCelsiusTolerance),
+ SpecificEntropyUnit.JoulePerKilogramKelvin => (JoulesPerKilogramKelvinInOneJoulePerKilogramKelvin, JoulesPerKilogramKelvinTolerance),
+ SpecificEntropyUnit.KilocaloriePerGramKelvin => (KilocaloriesPerGramKelvinInOneJoulePerKilogramKelvin, KilocaloriesPerGramKelvinTolerance),
+ SpecificEntropyUnit.KilojoulePerKilogramDegreeCelsius => (KilojoulesPerKilogramDegreeCelsiusInOneJoulePerKilogramKelvin, KilojoulesPerKilogramDegreeCelsiusTolerance),
+ SpecificEntropyUnit.KilojoulePerKilogramKelvin => (KilojoulesPerKilogramKelvinInOneJoulePerKilogramKelvin, KilojoulesPerKilogramKelvinTolerance),
+ SpecificEntropyUnit.MegajoulePerKilogramDegreeCelsius => (MegajoulesPerKilogramDegreeCelsiusInOneJoulePerKilogramKelvin, MegajoulesPerKilogramDegreeCelsiusTolerance),
+ SpecificEntropyUnit.MegajoulePerKilogramKelvin => (MegajoulesPerKilogramKelvinInOneJoulePerKilogramKelvin, MegajoulesPerKilogramKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SpecificEntropyUnit.BtuPerPoundFahrenheit },
+ new object[] { SpecificEntropyUnit.CaloriePerGramKelvin },
+ new object[] { SpecificEntropyUnit.JoulePerKilogramDegreeCelsius },
+ new object[] { SpecificEntropyUnit.JoulePerKilogramKelvin },
+ new object[] { SpecificEntropyUnit.KilocaloriePerGramKelvin },
+ new object[] { SpecificEntropyUnit.KilojoulePerKilogramDegreeCelsius },
+ new object[] { SpecificEntropyUnit.KilojoulePerKilogramKelvin },
+ new object[] { SpecificEntropyUnit.MegajoulePerKilogramDegreeCelsius },
+ new object[] { SpecificEntropyUnit.MegajoulePerKilogramKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -228,53 +259,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SpecificEntropyUnit unit)
{
- var jouleperkilogramkelvin = SpecificEntropy.FromJoulesPerKilogramKelvin(1);
-
- var btuperpoundfahrenheitQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.BtuPerPoundFahrenheit);
- AssertEx.EqualTolerance(BtusPerPoundFahrenheitInOneJoulePerKilogramKelvin, (double)btuperpoundfahrenheitQuantity.Value, BtusPerPoundFahrenheitTolerance);
- Assert.Equal(SpecificEntropyUnit.BtuPerPoundFahrenheit, btuperpoundfahrenheitQuantity.Unit);
-
- var caloriepergramkelvinQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.CaloriePerGramKelvin);
- AssertEx.EqualTolerance(CaloriesPerGramKelvinInOneJoulePerKilogramKelvin, (double)caloriepergramkelvinQuantity.Value, CaloriesPerGramKelvinTolerance);
- Assert.Equal(SpecificEntropyUnit.CaloriePerGramKelvin, caloriepergramkelvinQuantity.Unit);
-
- var jouleperkilogramdegreecelsiusQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.JoulePerKilogramDegreeCelsius);
- AssertEx.EqualTolerance(JoulesPerKilogramDegreeCelsiusInOneJoulePerKilogramKelvin, (double)jouleperkilogramdegreecelsiusQuantity.Value, JoulesPerKilogramDegreeCelsiusTolerance);
- Assert.Equal(SpecificEntropyUnit.JoulePerKilogramDegreeCelsius, jouleperkilogramdegreecelsiusQuantity.Unit);
-
- var jouleperkilogramkelvinQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.JoulePerKilogramKelvin);
- AssertEx.EqualTolerance(JoulesPerKilogramKelvinInOneJoulePerKilogramKelvin, (double)jouleperkilogramkelvinQuantity.Value, JoulesPerKilogramKelvinTolerance);
- Assert.Equal(SpecificEntropyUnit.JoulePerKilogramKelvin, jouleperkilogramkelvinQuantity.Unit);
+ var inBaseUnits = SpecificEntropy.From(1.0, SpecificEntropy.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilocaloriepergramkelvinQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.KilocaloriePerGramKelvin);
- AssertEx.EqualTolerance(KilocaloriesPerGramKelvinInOneJoulePerKilogramKelvin, (double)kilocaloriepergramkelvinQuantity.Value, KilocaloriesPerGramKelvinTolerance);
- Assert.Equal(SpecificEntropyUnit.KilocaloriePerGramKelvin, kilocaloriepergramkelvinQuantity.Unit);
-
- var kilojouleperkilogramdegreecelsiusQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramDegreeCelsius);
- AssertEx.EqualTolerance(KilojoulesPerKilogramDegreeCelsiusInOneJoulePerKilogramKelvin, (double)kilojouleperkilogramdegreecelsiusQuantity.Value, KilojoulesPerKilogramDegreeCelsiusTolerance);
- Assert.Equal(SpecificEntropyUnit.KilojoulePerKilogramDegreeCelsius, kilojouleperkilogramdegreecelsiusQuantity.Unit);
-
- var kilojouleperkilogramkelvinQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.KilojoulePerKilogramKelvin);
- AssertEx.EqualTolerance(KilojoulesPerKilogramKelvinInOneJoulePerKilogramKelvin, (double)kilojouleperkilogramkelvinQuantity.Value, KilojoulesPerKilogramKelvinTolerance);
- Assert.Equal(SpecificEntropyUnit.KilojoulePerKilogramKelvin, kilojouleperkilogramkelvinQuantity.Unit);
-
- var megajouleperkilogramdegreecelsiusQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.MegajoulePerKilogramDegreeCelsius);
- AssertEx.EqualTolerance(MegajoulesPerKilogramDegreeCelsiusInOneJoulePerKilogramKelvin, (double)megajouleperkilogramdegreecelsiusQuantity.Value, MegajoulesPerKilogramDegreeCelsiusTolerance);
- Assert.Equal(SpecificEntropyUnit.MegajoulePerKilogramDegreeCelsius, megajouleperkilogramdegreecelsiusQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var megajouleperkilogramkelvinQuantity = jouleperkilogramkelvin.ToUnit(SpecificEntropyUnit.MegajoulePerKilogramKelvin);
- AssertEx.EqualTolerance(MegajoulesPerKilogramKelvinInOneJoulePerKilogramKelvin, (double)megajouleperkilogramkelvinQuantity.Value, MegajoulesPerKilogramKelvinTolerance);
- Assert.Equal(SpecificEntropyUnit.MegajoulePerKilogramKelvin, megajouleperkilogramkelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SpecificEntropyUnit unit)
+ {
+ var quantity = SpecificEntropy.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SpecificEntropyUnit unit)
{
- var quantityInBaseUnit = SpecificEntropy.FromJoulesPerKilogramKelvin(1).ToBaseUnit();
- Assert.Equal(SpecificEntropy.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = SpecificEntropy.Units.FirstOrDefault(u => u != SpecificEntropy.BaseUnit && u != SpecificEntropyUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SpecificEntropyUnit.Undefined)
+ fromUnit = SpecificEntropy.BaseUnit;
+
+ var quantity = SpecificEntropy.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificFuelConsumptionTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificFuelConsumptionTestsBase.g.cs
index aa43b0b7fa..99dca952d1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificFuelConsumptionTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificFuelConsumptionTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class SpecificFuelConsumptionTestsBase : QuantityTestsBa
protected virtual double PoundsMassPerPoundForceHourTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SpecificFuelConsumptionUnit unit)
+ {
+ return unit switch
+ {
+ SpecificFuelConsumptionUnit.GramPerKiloNewtonSecond => (GramsPerKiloNewtonSecondInOneGramPerKiloNewtonSecond, GramsPerKiloNewtonSecondTolerance),
+ SpecificFuelConsumptionUnit.KilogramPerKilogramForceHour => (KilogramsPerKilogramForceHourInOneGramPerKiloNewtonSecond, KilogramsPerKilogramForceHourTolerance),
+ SpecificFuelConsumptionUnit.KilogramPerKiloNewtonSecond => (KilogramsPerKiloNewtonSecondInOneGramPerKiloNewtonSecond, KilogramsPerKiloNewtonSecondTolerance),
+ SpecificFuelConsumptionUnit.PoundMassPerPoundForceHour => (PoundsMassPerPoundForceHourInOneGramPerKiloNewtonSecond, PoundsMassPerPoundForceHourTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SpecificFuelConsumptionUnit.GramPerKiloNewtonSecond },
+ new object[] { SpecificFuelConsumptionUnit.KilogramPerKilogramForceHour },
+ new object[] { SpecificFuelConsumptionUnit.KilogramPerKiloNewtonSecond },
+ new object[] { SpecificFuelConsumptionUnit.PoundMassPerPoundForceHour },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SpecificFuelConsumptionUnit unit)
{
- var gramperkilonewtonsecond = SpecificFuelConsumption.FromGramsPerKiloNewtonSecond(1);
+ var inBaseUnits = SpecificFuelConsumption.From(1.0, SpecificFuelConsumption.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var gramperkilonewtonsecondQuantity = gramperkilonewtonsecond.ToUnit(SpecificFuelConsumptionUnit.GramPerKiloNewtonSecond);
- AssertEx.EqualTolerance(GramsPerKiloNewtonSecondInOneGramPerKiloNewtonSecond, (double)gramperkilonewtonsecondQuantity.Value, GramsPerKiloNewtonSecondTolerance);
- Assert.Equal(SpecificFuelConsumptionUnit.GramPerKiloNewtonSecond, gramperkilonewtonsecondQuantity.Unit);
-
- var kilogramperkilogramforcehourQuantity = gramperkilonewtonsecond.ToUnit(SpecificFuelConsumptionUnit.KilogramPerKilogramForceHour);
- AssertEx.EqualTolerance(KilogramsPerKilogramForceHourInOneGramPerKiloNewtonSecond, (double)kilogramperkilogramforcehourQuantity.Value, KilogramsPerKilogramForceHourTolerance);
- Assert.Equal(SpecificFuelConsumptionUnit.KilogramPerKilogramForceHour, kilogramperkilogramforcehourQuantity.Unit);
-
- var kilogramperkilonewtonsecondQuantity = gramperkilonewtonsecond.ToUnit(SpecificFuelConsumptionUnit.KilogramPerKiloNewtonSecond);
- AssertEx.EqualTolerance(KilogramsPerKiloNewtonSecondInOneGramPerKiloNewtonSecond, (double)kilogramperkilonewtonsecondQuantity.Value, KilogramsPerKiloNewtonSecondTolerance);
- Assert.Equal(SpecificFuelConsumptionUnit.KilogramPerKiloNewtonSecond, kilogramperkilonewtonsecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var poundmassperpoundforcehourQuantity = gramperkilonewtonsecond.ToUnit(SpecificFuelConsumptionUnit.PoundMassPerPoundForceHour);
- AssertEx.EqualTolerance(PoundsMassPerPoundForceHourInOneGramPerKiloNewtonSecond, (double)poundmassperpoundforcehourQuantity.Value, PoundsMassPerPoundForceHourTolerance);
- Assert.Equal(SpecificFuelConsumptionUnit.PoundMassPerPoundForceHour, poundmassperpoundforcehourQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SpecificFuelConsumptionUnit unit)
+ {
+ var quantity = SpecificFuelConsumption.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SpecificFuelConsumptionUnit unit)
{
- var quantityInBaseUnit = SpecificFuelConsumption.FromGramsPerKiloNewtonSecond(1).ToBaseUnit();
- Assert.Equal(SpecificFuelConsumption.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = SpecificFuelConsumption.Units.FirstOrDefault(u => u != SpecificFuelConsumption.BaseUnit && u != SpecificFuelConsumptionUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SpecificFuelConsumptionUnit.Undefined)
+ fromUnit = SpecificFuelConsumption.BaseUnit;
+
+ var quantity = SpecificFuelConsumption.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificVolumeTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificVolumeTestsBase.g.cs
index 198913ba08..3547c01001 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificVolumeTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificVolumeTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -47,6 +48,24 @@ public abstract partial class SpecificVolumeTestsBase : QuantityTestsBase
protected virtual double MillicubicMetersPerKilogramTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SpecificVolumeUnit unit)
+ {
+ return unit switch
+ {
+ SpecificVolumeUnit.CubicFootPerPound => (CubicFeetPerPoundInOneCubicMeterPerKilogram, CubicFeetPerPoundTolerance),
+ SpecificVolumeUnit.CubicMeterPerKilogram => (CubicMetersPerKilogramInOneCubicMeterPerKilogram, CubicMetersPerKilogramTolerance),
+ SpecificVolumeUnit.MillicubicMeterPerKilogram => (MillicubicMetersPerKilogramInOneCubicMeterPerKilogram, MillicubicMetersPerKilogramTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SpecificVolumeUnit.CubicFootPerPound },
+ new object[] { SpecificVolumeUnit.CubicMeterPerKilogram },
+ new object[] { SpecificVolumeUnit.MillicubicMeterPerKilogram },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -180,29 +199,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SpecificVolumeUnit unit)
{
- var cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
+ var inBaseUnits = SpecificVolume.From(1.0, SpecificVolume.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var cubicfootperpoundQuantity = cubicmeterperkilogram.ToUnit(SpecificVolumeUnit.CubicFootPerPound);
- AssertEx.EqualTolerance(CubicFeetPerPoundInOneCubicMeterPerKilogram, (double)cubicfootperpoundQuantity.Value, CubicFeetPerPoundTolerance);
- Assert.Equal(SpecificVolumeUnit.CubicFootPerPound, cubicfootperpoundQuantity.Unit);
-
- var cubicmeterperkilogramQuantity = cubicmeterperkilogram.ToUnit(SpecificVolumeUnit.CubicMeterPerKilogram);
- AssertEx.EqualTolerance(CubicMetersPerKilogramInOneCubicMeterPerKilogram, (double)cubicmeterperkilogramQuantity.Value, CubicMetersPerKilogramTolerance);
- Assert.Equal(SpecificVolumeUnit.CubicMeterPerKilogram, cubicmeterperkilogramQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var millicubicmeterperkilogramQuantity = cubicmeterperkilogram.ToUnit(SpecificVolumeUnit.MillicubicMeterPerKilogram);
- AssertEx.EqualTolerance(MillicubicMetersPerKilogramInOneCubicMeterPerKilogram, (double)millicubicmeterperkilogramQuantity.Value, MillicubicMetersPerKilogramTolerance);
- Assert.Equal(SpecificVolumeUnit.MillicubicMeterPerKilogram, millicubicmeterperkilogramQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SpecificVolumeUnit unit)
+ {
+ var quantity = SpecificVolume.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SpecificVolumeUnit unit)
{
- var quantityInBaseUnit = SpecificVolume.FromCubicMetersPerKilogram(1).ToBaseUnit();
- Assert.Equal(SpecificVolume.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = SpecificVolume.Units.FirstOrDefault(u => u != SpecificVolume.BaseUnit && u != SpecificVolumeUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SpecificVolumeUnit.Undefined)
+ fromUnit = SpecificVolume.BaseUnit;
+
+ var quantity = SpecificVolume.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificWeightTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificWeightTestsBase.g.cs
index 6c6288dcbb..631236e82c 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificWeightTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SpecificWeightTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -75,6 +76,52 @@ public abstract partial class SpecificWeightTestsBase : QuantityTestsBase
protected virtual double TonnesForcePerCubicMillimeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SpecificWeightUnit unit)
+ {
+ return unit switch
+ {
+ SpecificWeightUnit.KilogramForcePerCubicCentimeter => (KilogramsForcePerCubicCentimeterInOneNewtonPerCubicMeter, KilogramsForcePerCubicCentimeterTolerance),
+ SpecificWeightUnit.KilogramForcePerCubicMeter => (KilogramsForcePerCubicMeterInOneNewtonPerCubicMeter, KilogramsForcePerCubicMeterTolerance),
+ SpecificWeightUnit.KilogramForcePerCubicMillimeter => (KilogramsForcePerCubicMillimeterInOneNewtonPerCubicMeter, KilogramsForcePerCubicMillimeterTolerance),
+ SpecificWeightUnit.KilonewtonPerCubicCentimeter => (KilonewtonsPerCubicCentimeterInOneNewtonPerCubicMeter, KilonewtonsPerCubicCentimeterTolerance),
+ SpecificWeightUnit.KilonewtonPerCubicMeter => (KilonewtonsPerCubicMeterInOneNewtonPerCubicMeter, KilonewtonsPerCubicMeterTolerance),
+ SpecificWeightUnit.KilonewtonPerCubicMillimeter => (KilonewtonsPerCubicMillimeterInOneNewtonPerCubicMeter, KilonewtonsPerCubicMillimeterTolerance),
+ SpecificWeightUnit.KilopoundForcePerCubicFoot => (KilopoundsForcePerCubicFootInOneNewtonPerCubicMeter, KilopoundsForcePerCubicFootTolerance),
+ SpecificWeightUnit.KilopoundForcePerCubicInch => (KilopoundsForcePerCubicInchInOneNewtonPerCubicMeter, KilopoundsForcePerCubicInchTolerance),
+ SpecificWeightUnit.MeganewtonPerCubicMeter => (MeganewtonsPerCubicMeterInOneNewtonPerCubicMeter, MeganewtonsPerCubicMeterTolerance),
+ SpecificWeightUnit.NewtonPerCubicCentimeter => (NewtonsPerCubicCentimeterInOneNewtonPerCubicMeter, NewtonsPerCubicCentimeterTolerance),
+ SpecificWeightUnit.NewtonPerCubicMeter => (NewtonsPerCubicMeterInOneNewtonPerCubicMeter, NewtonsPerCubicMeterTolerance),
+ SpecificWeightUnit.NewtonPerCubicMillimeter => (NewtonsPerCubicMillimeterInOneNewtonPerCubicMeter, NewtonsPerCubicMillimeterTolerance),
+ SpecificWeightUnit.PoundForcePerCubicFoot => (PoundsForcePerCubicFootInOneNewtonPerCubicMeter, PoundsForcePerCubicFootTolerance),
+ SpecificWeightUnit.PoundForcePerCubicInch => (PoundsForcePerCubicInchInOneNewtonPerCubicMeter, PoundsForcePerCubicInchTolerance),
+ SpecificWeightUnit.TonneForcePerCubicCentimeter => (TonnesForcePerCubicCentimeterInOneNewtonPerCubicMeter, TonnesForcePerCubicCentimeterTolerance),
+ SpecificWeightUnit.TonneForcePerCubicMeter => (TonnesForcePerCubicMeterInOneNewtonPerCubicMeter, TonnesForcePerCubicMeterTolerance),
+ SpecificWeightUnit.TonneForcePerCubicMillimeter => (TonnesForcePerCubicMillimeterInOneNewtonPerCubicMeter, TonnesForcePerCubicMillimeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SpecificWeightUnit.KilogramForcePerCubicCentimeter },
+ new object[] { SpecificWeightUnit.KilogramForcePerCubicMeter },
+ new object[] { SpecificWeightUnit.KilogramForcePerCubicMillimeter },
+ new object[] { SpecificWeightUnit.KilonewtonPerCubicCentimeter },
+ new object[] { SpecificWeightUnit.KilonewtonPerCubicMeter },
+ new object[] { SpecificWeightUnit.KilonewtonPerCubicMillimeter },
+ new object[] { SpecificWeightUnit.KilopoundForcePerCubicFoot },
+ new object[] { SpecificWeightUnit.KilopoundForcePerCubicInch },
+ new object[] { SpecificWeightUnit.MeganewtonPerCubicMeter },
+ new object[] { SpecificWeightUnit.NewtonPerCubicCentimeter },
+ new object[] { SpecificWeightUnit.NewtonPerCubicMeter },
+ new object[] { SpecificWeightUnit.NewtonPerCubicMillimeter },
+ new object[] { SpecificWeightUnit.PoundForcePerCubicFoot },
+ new object[] { SpecificWeightUnit.PoundForcePerCubicInch },
+ new object[] { SpecificWeightUnit.TonneForcePerCubicCentimeter },
+ new object[] { SpecificWeightUnit.TonneForcePerCubicMeter },
+ new object[] { SpecificWeightUnit.TonneForcePerCubicMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -292,85 +339,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SpecificWeightUnit unit)
{
- var newtonpercubicmeter = SpecificWeight.FromNewtonsPerCubicMeter(1);
-
- var kilogramforcepercubiccentimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilogramForcePerCubicCentimeter);
- AssertEx.EqualTolerance(KilogramsForcePerCubicCentimeterInOneNewtonPerCubicMeter, (double)kilogramforcepercubiccentimeterQuantity.Value, KilogramsForcePerCubicCentimeterTolerance);
- Assert.Equal(SpecificWeightUnit.KilogramForcePerCubicCentimeter, kilogramforcepercubiccentimeterQuantity.Unit);
-
- var kilogramforcepercubicmeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilogramForcePerCubicMeter);
- AssertEx.EqualTolerance(KilogramsForcePerCubicMeterInOneNewtonPerCubicMeter, (double)kilogramforcepercubicmeterQuantity.Value, KilogramsForcePerCubicMeterTolerance);
- Assert.Equal(SpecificWeightUnit.KilogramForcePerCubicMeter, kilogramforcepercubicmeterQuantity.Unit);
-
- var kilogramforcepercubicmillimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilogramForcePerCubicMillimeter);
- AssertEx.EqualTolerance(KilogramsForcePerCubicMillimeterInOneNewtonPerCubicMeter, (double)kilogramforcepercubicmillimeterQuantity.Value, KilogramsForcePerCubicMillimeterTolerance);
- Assert.Equal(SpecificWeightUnit.KilogramForcePerCubicMillimeter, kilogramforcepercubicmillimeterQuantity.Unit);
-
- var kilonewtonpercubiccentimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilonewtonPerCubicCentimeter);
- AssertEx.EqualTolerance(KilonewtonsPerCubicCentimeterInOneNewtonPerCubicMeter, (double)kilonewtonpercubiccentimeterQuantity.Value, KilonewtonsPerCubicCentimeterTolerance);
- Assert.Equal(SpecificWeightUnit.KilonewtonPerCubicCentimeter, kilonewtonpercubiccentimeterQuantity.Unit);
-
- var kilonewtonpercubicmeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilonewtonPerCubicMeter);
- AssertEx.EqualTolerance(KilonewtonsPerCubicMeterInOneNewtonPerCubicMeter, (double)kilonewtonpercubicmeterQuantity.Value, KilonewtonsPerCubicMeterTolerance);
- Assert.Equal(SpecificWeightUnit.KilonewtonPerCubicMeter, kilonewtonpercubicmeterQuantity.Unit);
-
- var kilonewtonpercubicmillimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilonewtonPerCubicMillimeter);
- AssertEx.EqualTolerance(KilonewtonsPerCubicMillimeterInOneNewtonPerCubicMeter, (double)kilonewtonpercubicmillimeterQuantity.Value, KilonewtonsPerCubicMillimeterTolerance);
- Assert.Equal(SpecificWeightUnit.KilonewtonPerCubicMillimeter, kilonewtonpercubicmillimeterQuantity.Unit);
-
- var kilopoundforcepercubicfootQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilopoundForcePerCubicFoot);
- AssertEx.EqualTolerance(KilopoundsForcePerCubicFootInOneNewtonPerCubicMeter, (double)kilopoundforcepercubicfootQuantity.Value, KilopoundsForcePerCubicFootTolerance);
- Assert.Equal(SpecificWeightUnit.KilopoundForcePerCubicFoot, kilopoundforcepercubicfootQuantity.Unit);
-
- var kilopoundforcepercubicinchQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.KilopoundForcePerCubicInch);
- AssertEx.EqualTolerance(KilopoundsForcePerCubicInchInOneNewtonPerCubicMeter, (double)kilopoundforcepercubicinchQuantity.Value, KilopoundsForcePerCubicInchTolerance);
- Assert.Equal(SpecificWeightUnit.KilopoundForcePerCubicInch, kilopoundforcepercubicinchQuantity.Unit);
+ var inBaseUnits = SpecificWeight.From(1.0, SpecificWeight.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var meganewtonpercubicmeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.MeganewtonPerCubicMeter);
- AssertEx.EqualTolerance(MeganewtonsPerCubicMeterInOneNewtonPerCubicMeter, (double)meganewtonpercubicmeterQuantity.Value, MeganewtonsPerCubicMeterTolerance);
- Assert.Equal(SpecificWeightUnit.MeganewtonPerCubicMeter, meganewtonpercubicmeterQuantity.Unit);
-
- var newtonpercubiccentimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.NewtonPerCubicCentimeter);
- AssertEx.EqualTolerance(NewtonsPerCubicCentimeterInOneNewtonPerCubicMeter, (double)newtonpercubiccentimeterQuantity.Value, NewtonsPerCubicCentimeterTolerance);
- Assert.Equal(SpecificWeightUnit.NewtonPerCubicCentimeter, newtonpercubiccentimeterQuantity.Unit);
-
- var newtonpercubicmeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.NewtonPerCubicMeter);
- AssertEx.EqualTolerance(NewtonsPerCubicMeterInOneNewtonPerCubicMeter, (double)newtonpercubicmeterQuantity.Value, NewtonsPerCubicMeterTolerance);
- Assert.Equal(SpecificWeightUnit.NewtonPerCubicMeter, newtonpercubicmeterQuantity.Unit);
-
- var newtonpercubicmillimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.NewtonPerCubicMillimeter);
- AssertEx.EqualTolerance(NewtonsPerCubicMillimeterInOneNewtonPerCubicMeter, (double)newtonpercubicmillimeterQuantity.Value, NewtonsPerCubicMillimeterTolerance);
- Assert.Equal(SpecificWeightUnit.NewtonPerCubicMillimeter, newtonpercubicmillimeterQuantity.Unit);
-
- var poundforcepercubicfootQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.PoundForcePerCubicFoot);
- AssertEx.EqualTolerance(PoundsForcePerCubicFootInOneNewtonPerCubicMeter, (double)poundforcepercubicfootQuantity.Value, PoundsForcePerCubicFootTolerance);
- Assert.Equal(SpecificWeightUnit.PoundForcePerCubicFoot, poundforcepercubicfootQuantity.Unit);
-
- var poundforcepercubicinchQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.PoundForcePerCubicInch);
- AssertEx.EqualTolerance(PoundsForcePerCubicInchInOneNewtonPerCubicMeter, (double)poundforcepercubicinchQuantity.Value, PoundsForcePerCubicInchTolerance);
- Assert.Equal(SpecificWeightUnit.PoundForcePerCubicInch, poundforcepercubicinchQuantity.Unit);
-
- var tonneforcepercubiccentimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.TonneForcePerCubicCentimeter);
- AssertEx.EqualTolerance(TonnesForcePerCubicCentimeterInOneNewtonPerCubicMeter, (double)tonneforcepercubiccentimeterQuantity.Value, TonnesForcePerCubicCentimeterTolerance);
- Assert.Equal(SpecificWeightUnit.TonneForcePerCubicCentimeter, tonneforcepercubiccentimeterQuantity.Unit);
-
- var tonneforcepercubicmeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.TonneForcePerCubicMeter);
- AssertEx.EqualTolerance(TonnesForcePerCubicMeterInOneNewtonPerCubicMeter, (double)tonneforcepercubicmeterQuantity.Value, TonnesForcePerCubicMeterTolerance);
- Assert.Equal(SpecificWeightUnit.TonneForcePerCubicMeter, tonneforcepercubicmeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneforcepercubicmillimeterQuantity = newtonpercubicmeter.ToUnit(SpecificWeightUnit.TonneForcePerCubicMillimeter);
- AssertEx.EqualTolerance(TonnesForcePerCubicMillimeterInOneNewtonPerCubicMeter, (double)tonneforcepercubicmillimeterQuantity.Value, TonnesForcePerCubicMillimeterTolerance);
- Assert.Equal(SpecificWeightUnit.TonneForcePerCubicMillimeter, tonneforcepercubicmillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SpecificWeightUnit unit)
+ {
+ var quantity = SpecificWeight.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SpecificWeightUnit unit)
{
- var quantityInBaseUnit = SpecificWeight.FromNewtonsPerCubicMeter(1).ToBaseUnit();
- Assert.Equal(SpecificWeight.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = SpecificWeight.Units.FirstOrDefault(u => u != SpecificWeight.BaseUnit && u != SpecificWeightUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SpecificWeightUnit.Undefined)
+ fromUnit = SpecificWeight.BaseUnit;
+
+ var quantity = SpecificWeight.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/SpeedTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/SpeedTestsBase.g.cs
index fb9fd1ea9d..d1ba6b077a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/SpeedTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/SpeedTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -105,6 +106,82 @@ public abstract partial class SpeedTestsBase : QuantityTestsBase
protected virtual double YardsPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(SpeedUnit unit)
+ {
+ return unit switch
+ {
+ SpeedUnit.CentimeterPerHour => (CentimetersPerHourInOneMeterPerSecond, CentimetersPerHourTolerance),
+ SpeedUnit.CentimeterPerMinute => (CentimetersPerMinutesInOneMeterPerSecond, CentimetersPerMinutesTolerance),
+ SpeedUnit.CentimeterPerSecond => (CentimetersPerSecondInOneMeterPerSecond, CentimetersPerSecondTolerance),
+ SpeedUnit.DecimeterPerMinute => (DecimetersPerMinutesInOneMeterPerSecond, DecimetersPerMinutesTolerance),
+ SpeedUnit.DecimeterPerSecond => (DecimetersPerSecondInOneMeterPerSecond, DecimetersPerSecondTolerance),
+ SpeedUnit.FootPerHour => (FeetPerHourInOneMeterPerSecond, FeetPerHourTolerance),
+ SpeedUnit.FootPerMinute => (FeetPerMinuteInOneMeterPerSecond, FeetPerMinuteTolerance),
+ SpeedUnit.FootPerSecond => (FeetPerSecondInOneMeterPerSecond, FeetPerSecondTolerance),
+ SpeedUnit.InchPerHour => (InchesPerHourInOneMeterPerSecond, InchesPerHourTolerance),
+ SpeedUnit.InchPerMinute => (InchesPerMinuteInOneMeterPerSecond, InchesPerMinuteTolerance),
+ SpeedUnit.InchPerSecond => (InchesPerSecondInOneMeterPerSecond, InchesPerSecondTolerance),
+ SpeedUnit.KilometerPerHour => (KilometersPerHourInOneMeterPerSecond, KilometersPerHourTolerance),
+ SpeedUnit.KilometerPerMinute => (KilometersPerMinutesInOneMeterPerSecond, KilometersPerMinutesTolerance),
+ SpeedUnit.KilometerPerSecond => (KilometersPerSecondInOneMeterPerSecond, KilometersPerSecondTolerance),
+ SpeedUnit.Knot => (KnotsInOneMeterPerSecond, KnotsTolerance),
+ SpeedUnit.MeterPerHour => (MetersPerHourInOneMeterPerSecond, MetersPerHourTolerance),
+ SpeedUnit.MeterPerMinute => (MetersPerMinutesInOneMeterPerSecond, MetersPerMinutesTolerance),
+ SpeedUnit.MeterPerSecond => (MetersPerSecondInOneMeterPerSecond, MetersPerSecondTolerance),
+ SpeedUnit.MicrometerPerMinute => (MicrometersPerMinutesInOneMeterPerSecond, MicrometersPerMinutesTolerance),
+ SpeedUnit.MicrometerPerSecond => (MicrometersPerSecondInOneMeterPerSecond, MicrometersPerSecondTolerance),
+ SpeedUnit.MilePerHour => (MilesPerHourInOneMeterPerSecond, MilesPerHourTolerance),
+ SpeedUnit.MillimeterPerHour => (MillimetersPerHourInOneMeterPerSecond, MillimetersPerHourTolerance),
+ SpeedUnit.MillimeterPerMinute => (MillimetersPerMinutesInOneMeterPerSecond, MillimetersPerMinutesTolerance),
+ SpeedUnit.MillimeterPerSecond => (MillimetersPerSecondInOneMeterPerSecond, MillimetersPerSecondTolerance),
+ SpeedUnit.NanometerPerMinute => (NanometersPerMinutesInOneMeterPerSecond, NanometersPerMinutesTolerance),
+ SpeedUnit.NanometerPerSecond => (NanometersPerSecondInOneMeterPerSecond, NanometersPerSecondTolerance),
+ SpeedUnit.UsSurveyFootPerHour => (UsSurveyFeetPerHourInOneMeterPerSecond, UsSurveyFeetPerHourTolerance),
+ SpeedUnit.UsSurveyFootPerMinute => (UsSurveyFeetPerMinuteInOneMeterPerSecond, UsSurveyFeetPerMinuteTolerance),
+ SpeedUnit.UsSurveyFootPerSecond => (UsSurveyFeetPerSecondInOneMeterPerSecond, UsSurveyFeetPerSecondTolerance),
+ SpeedUnit.YardPerHour => (YardsPerHourInOneMeterPerSecond, YardsPerHourTolerance),
+ SpeedUnit.YardPerMinute => (YardsPerMinuteInOneMeterPerSecond, YardsPerMinuteTolerance),
+ SpeedUnit.YardPerSecond => (YardsPerSecondInOneMeterPerSecond, YardsPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { SpeedUnit.CentimeterPerHour },
+ new object[] { SpeedUnit.CentimeterPerMinute },
+ new object[] { SpeedUnit.CentimeterPerSecond },
+ new object[] { SpeedUnit.DecimeterPerMinute },
+ new object[] { SpeedUnit.DecimeterPerSecond },
+ new object[] { SpeedUnit.FootPerHour },
+ new object[] { SpeedUnit.FootPerMinute },
+ new object[] { SpeedUnit.FootPerSecond },
+ new object[] { SpeedUnit.InchPerHour },
+ new object[] { SpeedUnit.InchPerMinute },
+ new object[] { SpeedUnit.InchPerSecond },
+ new object[] { SpeedUnit.KilometerPerHour },
+ new object[] { SpeedUnit.KilometerPerMinute },
+ new object[] { SpeedUnit.KilometerPerSecond },
+ new object[] { SpeedUnit.Knot },
+ new object[] { SpeedUnit.MeterPerHour },
+ new object[] { SpeedUnit.MeterPerMinute },
+ new object[] { SpeedUnit.MeterPerSecond },
+ new object[] { SpeedUnit.MicrometerPerMinute },
+ new object[] { SpeedUnit.MicrometerPerSecond },
+ new object[] { SpeedUnit.MilePerHour },
+ new object[] { SpeedUnit.MillimeterPerHour },
+ new object[] { SpeedUnit.MillimeterPerMinute },
+ new object[] { SpeedUnit.MillimeterPerSecond },
+ new object[] { SpeedUnit.NanometerPerMinute },
+ new object[] { SpeedUnit.NanometerPerSecond },
+ new object[] { SpeedUnit.UsSurveyFootPerHour },
+ new object[] { SpeedUnit.UsSurveyFootPerMinute },
+ new object[] { SpeedUnit.UsSurveyFootPerSecond },
+ new object[] { SpeedUnit.YardPerHour },
+ new object[] { SpeedUnit.YardPerMinute },
+ new object[] { SpeedUnit.YardPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -412,145 +489,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(SpeedUnit unit)
{
- var meterpersecond = Speed.FromMetersPerSecond(1);
-
- var centimeterperhourQuantity = meterpersecond.ToUnit(SpeedUnit.CentimeterPerHour);
- AssertEx.EqualTolerance(CentimetersPerHourInOneMeterPerSecond, (double)centimeterperhourQuantity.Value, CentimetersPerHourTolerance);
- Assert.Equal(SpeedUnit.CentimeterPerHour, centimeterperhourQuantity.Unit);
-
- var centimeterperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.CentimeterPerMinute);
- AssertEx.EqualTolerance(CentimetersPerMinutesInOneMeterPerSecond, (double)centimeterperminuteQuantity.Value, CentimetersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.CentimeterPerMinute, centimeterperminuteQuantity.Unit);
-
- var centimeterpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.CentimeterPerSecond);
- AssertEx.EqualTolerance(CentimetersPerSecondInOneMeterPerSecond, (double)centimeterpersecondQuantity.Value, CentimetersPerSecondTolerance);
- Assert.Equal(SpeedUnit.CentimeterPerSecond, centimeterpersecondQuantity.Unit);
-
- var decimeterperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.DecimeterPerMinute);
- AssertEx.EqualTolerance(DecimetersPerMinutesInOneMeterPerSecond, (double)decimeterperminuteQuantity.Value, DecimetersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.DecimeterPerMinute, decimeterperminuteQuantity.Unit);
-
- var decimeterpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.DecimeterPerSecond);
- AssertEx.EqualTolerance(DecimetersPerSecondInOneMeterPerSecond, (double)decimeterpersecondQuantity.Value, DecimetersPerSecondTolerance);
- Assert.Equal(SpeedUnit.DecimeterPerSecond, decimeterpersecondQuantity.Unit);
-
- var footperhourQuantity = meterpersecond.ToUnit(SpeedUnit.FootPerHour);
- AssertEx.EqualTolerance(FeetPerHourInOneMeterPerSecond, (double)footperhourQuantity.Value, FeetPerHourTolerance);
- Assert.Equal(SpeedUnit.FootPerHour, footperhourQuantity.Unit);
-
- var footperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.FootPerMinute);
- AssertEx.EqualTolerance(FeetPerMinuteInOneMeterPerSecond, (double)footperminuteQuantity.Value, FeetPerMinuteTolerance);
- Assert.Equal(SpeedUnit.FootPerMinute, footperminuteQuantity.Unit);
-
- var footpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.FootPerSecond);
- AssertEx.EqualTolerance(FeetPerSecondInOneMeterPerSecond, (double)footpersecondQuantity.Value, FeetPerSecondTolerance);
- Assert.Equal(SpeedUnit.FootPerSecond, footpersecondQuantity.Unit);
-
- var inchperhourQuantity = meterpersecond.ToUnit(SpeedUnit.InchPerHour);
- AssertEx.EqualTolerance(InchesPerHourInOneMeterPerSecond, (double)inchperhourQuantity.Value, InchesPerHourTolerance);
- Assert.Equal(SpeedUnit.InchPerHour, inchperhourQuantity.Unit);
-
- var inchperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.InchPerMinute);
- AssertEx.EqualTolerance(InchesPerMinuteInOneMeterPerSecond, (double)inchperminuteQuantity.Value, InchesPerMinuteTolerance);
- Assert.Equal(SpeedUnit.InchPerMinute, inchperminuteQuantity.Unit);
-
- var inchpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.InchPerSecond);
- AssertEx.EqualTolerance(InchesPerSecondInOneMeterPerSecond, (double)inchpersecondQuantity.Value, InchesPerSecondTolerance);
- Assert.Equal(SpeedUnit.InchPerSecond, inchpersecondQuantity.Unit);
-
- var kilometerperhourQuantity = meterpersecond.ToUnit(SpeedUnit.KilometerPerHour);
- AssertEx.EqualTolerance(KilometersPerHourInOneMeterPerSecond, (double)kilometerperhourQuantity.Value, KilometersPerHourTolerance);
- Assert.Equal(SpeedUnit.KilometerPerHour, kilometerperhourQuantity.Unit);
-
- var kilometerperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.KilometerPerMinute);
- AssertEx.EqualTolerance(KilometersPerMinutesInOneMeterPerSecond, (double)kilometerperminuteQuantity.Value, KilometersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.KilometerPerMinute, kilometerperminuteQuantity.Unit);
-
- var kilometerpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.KilometerPerSecond);
- AssertEx.EqualTolerance(KilometersPerSecondInOneMeterPerSecond, (double)kilometerpersecondQuantity.Value, KilometersPerSecondTolerance);
- Assert.Equal(SpeedUnit.KilometerPerSecond, kilometerpersecondQuantity.Unit);
-
- var knotQuantity = meterpersecond.ToUnit(SpeedUnit.Knot);
- AssertEx.EqualTolerance(KnotsInOneMeterPerSecond, (double)knotQuantity.Value, KnotsTolerance);
- Assert.Equal(SpeedUnit.Knot, knotQuantity.Unit);
-
- var meterperhourQuantity = meterpersecond.ToUnit(SpeedUnit.MeterPerHour);
- AssertEx.EqualTolerance(MetersPerHourInOneMeterPerSecond, (double)meterperhourQuantity.Value, MetersPerHourTolerance);
- Assert.Equal(SpeedUnit.MeterPerHour, meterperhourQuantity.Unit);
+ var inBaseUnits = Speed.From(1.0, Speed.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var meterperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.MeterPerMinute);
- AssertEx.EqualTolerance(MetersPerMinutesInOneMeterPerSecond, (double)meterperminuteQuantity.Value, MetersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.MeterPerMinute, meterperminuteQuantity.Unit);
-
- var meterpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.MeterPerSecond);
- AssertEx.EqualTolerance(MetersPerSecondInOneMeterPerSecond, (double)meterpersecondQuantity.Value, MetersPerSecondTolerance);
- Assert.Equal(SpeedUnit.MeterPerSecond, meterpersecondQuantity.Unit);
-
- var micrometerperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.MicrometerPerMinute);
- AssertEx.EqualTolerance(MicrometersPerMinutesInOneMeterPerSecond, (double)micrometerperminuteQuantity.Value, MicrometersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.MicrometerPerMinute, micrometerperminuteQuantity.Unit);
-
- var micrometerpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.MicrometerPerSecond);
- AssertEx.EqualTolerance(MicrometersPerSecondInOneMeterPerSecond, (double)micrometerpersecondQuantity.Value, MicrometersPerSecondTolerance);
- Assert.Equal(SpeedUnit.MicrometerPerSecond, micrometerpersecondQuantity.Unit);
-
- var mileperhourQuantity = meterpersecond.ToUnit(SpeedUnit.MilePerHour);
- AssertEx.EqualTolerance(MilesPerHourInOneMeterPerSecond, (double)mileperhourQuantity.Value, MilesPerHourTolerance);
- Assert.Equal(SpeedUnit.MilePerHour, mileperhourQuantity.Unit);
-
- var millimeterperhourQuantity = meterpersecond.ToUnit(SpeedUnit.MillimeterPerHour);
- AssertEx.EqualTolerance(MillimetersPerHourInOneMeterPerSecond, (double)millimeterperhourQuantity.Value, MillimetersPerHourTolerance);
- Assert.Equal(SpeedUnit.MillimeterPerHour, millimeterperhourQuantity.Unit);
-
- var millimeterperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.MillimeterPerMinute);
- AssertEx.EqualTolerance(MillimetersPerMinutesInOneMeterPerSecond, (double)millimeterperminuteQuantity.Value, MillimetersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.MillimeterPerMinute, millimeterperminuteQuantity.Unit);
-
- var millimeterpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.MillimeterPerSecond);
- AssertEx.EqualTolerance(MillimetersPerSecondInOneMeterPerSecond, (double)millimeterpersecondQuantity.Value, MillimetersPerSecondTolerance);
- Assert.Equal(SpeedUnit.MillimeterPerSecond, millimeterpersecondQuantity.Unit);
-
- var nanometerperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.NanometerPerMinute);
- AssertEx.EqualTolerance(NanometersPerMinutesInOneMeterPerSecond, (double)nanometerperminuteQuantity.Value, NanometersPerMinutesTolerance);
- Assert.Equal(SpeedUnit.NanometerPerMinute, nanometerperminuteQuantity.Unit);
-
- var nanometerpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.NanometerPerSecond);
- AssertEx.EqualTolerance(NanometersPerSecondInOneMeterPerSecond, (double)nanometerpersecondQuantity.Value, NanometersPerSecondTolerance);
- Assert.Equal(SpeedUnit.NanometerPerSecond, nanometerpersecondQuantity.Unit);
-
- var ussurveyfootperhourQuantity = meterpersecond.ToUnit(SpeedUnit.UsSurveyFootPerHour);
- AssertEx.EqualTolerance(UsSurveyFeetPerHourInOneMeterPerSecond, (double)ussurveyfootperhourQuantity.Value, UsSurveyFeetPerHourTolerance);
- Assert.Equal(SpeedUnit.UsSurveyFootPerHour, ussurveyfootperhourQuantity.Unit);
-
- var ussurveyfootperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.UsSurveyFootPerMinute);
- AssertEx.EqualTolerance(UsSurveyFeetPerMinuteInOneMeterPerSecond, (double)ussurveyfootperminuteQuantity.Value, UsSurveyFeetPerMinuteTolerance);
- Assert.Equal(SpeedUnit.UsSurveyFootPerMinute, ussurveyfootperminuteQuantity.Unit);
-
- var ussurveyfootpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.UsSurveyFootPerSecond);
- AssertEx.EqualTolerance(UsSurveyFeetPerSecondInOneMeterPerSecond, (double)ussurveyfootpersecondQuantity.Value, UsSurveyFeetPerSecondTolerance);
- Assert.Equal(SpeedUnit.UsSurveyFootPerSecond, ussurveyfootpersecondQuantity.Unit);
-
- var yardperhourQuantity = meterpersecond.ToUnit(SpeedUnit.YardPerHour);
- AssertEx.EqualTolerance(YardsPerHourInOneMeterPerSecond, (double)yardperhourQuantity.Value, YardsPerHourTolerance);
- Assert.Equal(SpeedUnit.YardPerHour, yardperhourQuantity.Unit);
-
- var yardperminuteQuantity = meterpersecond.ToUnit(SpeedUnit.YardPerMinute);
- AssertEx.EqualTolerance(YardsPerMinuteInOneMeterPerSecond, (double)yardperminuteQuantity.Value, YardsPerMinuteTolerance);
- Assert.Equal(SpeedUnit.YardPerMinute, yardperminuteQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var yardpersecondQuantity = meterpersecond.ToUnit(SpeedUnit.YardPerSecond);
- AssertEx.EqualTolerance(YardsPerSecondInOneMeterPerSecond, (double)yardpersecondQuantity.Value, YardsPerSecondTolerance);
- Assert.Equal(SpeedUnit.YardPerSecond, yardpersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(SpeedUnit unit)
+ {
+ var quantity = Speed.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(SpeedUnit unit)
{
- var quantityInBaseUnit = Speed.FromMetersPerSecond(1).ToBaseUnit();
- Assert.Equal(Speed.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Speed.Units.FirstOrDefault(u => u != Speed.BaseUnit && u != SpeedUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == SpeedUnit.Undefined)
+ fromUnit = Speed.BaseUnit;
+
+ var quantity = Speed.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/StandardVolumeFlowTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/StandardVolumeFlowTestsBase.g.cs
index db667fcf5c..eabb7fd0f9 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/StandardVolumeFlowTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/StandardVolumeFlowTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -59,6 +60,36 @@ public abstract partial class StandardVolumeFlowTestsBase : QuantityTestsBase
protected virtual double StandardLitersPerMinuteTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(StandardVolumeFlowUnit unit)
+ {
+ return unit switch
+ {
+ StandardVolumeFlowUnit.StandardCubicCentimeterPerMinute => (StandardCubicCentimetersPerMinuteInOneStandardCubicMeterPerSecond, StandardCubicCentimetersPerMinuteTolerance),
+ StandardVolumeFlowUnit.StandardCubicFootPerHour => (StandardCubicFeetPerHourInOneStandardCubicMeterPerSecond, StandardCubicFeetPerHourTolerance),
+ StandardVolumeFlowUnit.StandardCubicFootPerMinute => (StandardCubicFeetPerMinuteInOneStandardCubicMeterPerSecond, StandardCubicFeetPerMinuteTolerance),
+ StandardVolumeFlowUnit.StandardCubicFootPerSecond => (StandardCubicFeetPerSecondInOneStandardCubicMeterPerSecond, StandardCubicFeetPerSecondTolerance),
+ StandardVolumeFlowUnit.StandardCubicMeterPerDay => (StandardCubicMetersPerDayInOneStandardCubicMeterPerSecond, StandardCubicMetersPerDayTolerance),
+ StandardVolumeFlowUnit.StandardCubicMeterPerHour => (StandardCubicMetersPerHourInOneStandardCubicMeterPerSecond, StandardCubicMetersPerHourTolerance),
+ StandardVolumeFlowUnit.StandardCubicMeterPerMinute => (StandardCubicMetersPerMinuteInOneStandardCubicMeterPerSecond, StandardCubicMetersPerMinuteTolerance),
+ StandardVolumeFlowUnit.StandardCubicMeterPerSecond => (StandardCubicMetersPerSecondInOneStandardCubicMeterPerSecond, StandardCubicMetersPerSecondTolerance),
+ StandardVolumeFlowUnit.StandardLiterPerMinute => (StandardLitersPerMinuteInOneStandardCubicMeterPerSecond, StandardLitersPerMinuteTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { StandardVolumeFlowUnit.StandardCubicCentimeterPerMinute },
+ new object[] { StandardVolumeFlowUnit.StandardCubicFootPerHour },
+ new object[] { StandardVolumeFlowUnit.StandardCubicFootPerMinute },
+ new object[] { StandardVolumeFlowUnit.StandardCubicFootPerSecond },
+ new object[] { StandardVolumeFlowUnit.StandardCubicMeterPerDay },
+ new object[] { StandardVolumeFlowUnit.StandardCubicMeterPerHour },
+ new object[] { StandardVolumeFlowUnit.StandardCubicMeterPerMinute },
+ new object[] { StandardVolumeFlowUnit.StandardCubicMeterPerSecond },
+ new object[] { StandardVolumeFlowUnit.StandardLiterPerMinute },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -228,53 +259,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(StandardVolumeFlowUnit unit)
{
- var standardcubicmeterpersecond = StandardVolumeFlow.FromStandardCubicMetersPerSecond(1);
-
- var standardcubiccentimeterperminuteQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicCentimeterPerMinute);
- AssertEx.EqualTolerance(StandardCubicCentimetersPerMinuteInOneStandardCubicMeterPerSecond, (double)standardcubiccentimeterperminuteQuantity.Value, StandardCubicCentimetersPerMinuteTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicCentimeterPerMinute, standardcubiccentimeterperminuteQuantity.Unit);
-
- var standardcubicfootperhourQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicFootPerHour);
- AssertEx.EqualTolerance(StandardCubicFeetPerHourInOneStandardCubicMeterPerSecond, (double)standardcubicfootperhourQuantity.Value, StandardCubicFeetPerHourTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicFootPerHour, standardcubicfootperhourQuantity.Unit);
-
- var standardcubicfootperminuteQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicFootPerMinute);
- AssertEx.EqualTolerance(StandardCubicFeetPerMinuteInOneStandardCubicMeterPerSecond, (double)standardcubicfootperminuteQuantity.Value, StandardCubicFeetPerMinuteTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicFootPerMinute, standardcubicfootperminuteQuantity.Unit);
-
- var standardcubicfootpersecondQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicFootPerSecond);
- AssertEx.EqualTolerance(StandardCubicFeetPerSecondInOneStandardCubicMeterPerSecond, (double)standardcubicfootpersecondQuantity.Value, StandardCubicFeetPerSecondTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicFootPerSecond, standardcubicfootpersecondQuantity.Unit);
+ var inBaseUnits = StandardVolumeFlow.From(1.0, StandardVolumeFlow.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var standardcubicmeterperdayQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicMeterPerDay);
- AssertEx.EqualTolerance(StandardCubicMetersPerDayInOneStandardCubicMeterPerSecond, (double)standardcubicmeterperdayQuantity.Value, StandardCubicMetersPerDayTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicMeterPerDay, standardcubicmeterperdayQuantity.Unit);
-
- var standardcubicmeterperhourQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicMeterPerHour);
- AssertEx.EqualTolerance(StandardCubicMetersPerHourInOneStandardCubicMeterPerSecond, (double)standardcubicmeterperhourQuantity.Value, StandardCubicMetersPerHourTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicMeterPerHour, standardcubicmeterperhourQuantity.Unit);
-
- var standardcubicmeterperminuteQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicMeterPerMinute);
- AssertEx.EqualTolerance(StandardCubicMetersPerMinuteInOneStandardCubicMeterPerSecond, (double)standardcubicmeterperminuteQuantity.Value, StandardCubicMetersPerMinuteTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicMeterPerMinute, standardcubicmeterperminuteQuantity.Unit);
-
- var standardcubicmeterpersecondQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardCubicMeterPerSecond);
- AssertEx.EqualTolerance(StandardCubicMetersPerSecondInOneStandardCubicMeterPerSecond, (double)standardcubicmeterpersecondQuantity.Value, StandardCubicMetersPerSecondTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardCubicMeterPerSecond, standardcubicmeterpersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var standardliterperminuteQuantity = standardcubicmeterpersecond.ToUnit(StandardVolumeFlowUnit.StandardLiterPerMinute);
- AssertEx.EqualTolerance(StandardLitersPerMinuteInOneStandardCubicMeterPerSecond, (double)standardliterperminuteQuantity.Value, StandardLitersPerMinuteTolerance);
- Assert.Equal(StandardVolumeFlowUnit.StandardLiterPerMinute, standardliterperminuteQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(StandardVolumeFlowUnit unit)
+ {
+ var quantity = StandardVolumeFlow.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(StandardVolumeFlowUnit unit)
{
- var quantityInBaseUnit = StandardVolumeFlow.FromStandardCubicMetersPerSecond(1).ToBaseUnit();
- Assert.Equal(StandardVolumeFlow.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = StandardVolumeFlow.Units.FirstOrDefault(u => u != StandardVolumeFlow.BaseUnit && u != StandardVolumeFlowUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == StandardVolumeFlowUnit.Undefined)
+ fromUnit = StandardVolumeFlow.BaseUnit;
+
+ var quantity = StandardVolumeFlow.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureChangeRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureChangeRateTestsBase.g.cs
index 3986719461..8af86a0b20 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureChangeRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureChangeRateTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -61,6 +62,38 @@ public abstract partial class TemperatureChangeRateTestsBase : QuantityTestsBase
protected virtual double NanodegreesCelsiusPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TemperatureChangeRateUnit unit)
+ {
+ return unit switch
+ {
+ TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond => (CentidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, CentidegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond => (DecadegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, DecadegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond => (DecidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, DecidegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.DegreeCelsiusPerMinute => (DegreesCelsiusPerMinuteInOneDegreeCelsiusPerSecond, DegreesCelsiusPerMinuteTolerance),
+ TemperatureChangeRateUnit.DegreeCelsiusPerSecond => (DegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, DegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond => (HectodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, HectodegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond => (KilodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, KilodegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond => (MicrodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, MicrodegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond => (MillidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, MillidegreesCelsiusPerSecondTolerance),
+ TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond => (NanodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, NanodegreesCelsiusPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.DegreeCelsiusPerMinute },
+ new object[] { TemperatureChangeRateUnit.DegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond },
+ new object[] { TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -236,57 +269,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TemperatureChangeRateUnit unit)
{
- var degreecelsiuspersecond = TemperatureChangeRate.FromDegreesCelsiusPerSecond(1);
-
- var centidegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(CentidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)centidegreecelsiuspersecondQuantity.Value, CentidegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond, centidegreecelsiuspersecondQuantity.Unit);
-
- var decadegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(DecadegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)decadegreecelsiuspersecondQuantity.Value, DecadegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond, decadegreecelsiuspersecondQuantity.Unit);
-
- var decidegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(DecidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)decidegreecelsiuspersecondQuantity.Value, DecidegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond, decidegreecelsiuspersecondQuantity.Unit);
-
- var degreecelsiusperminuteQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.DegreeCelsiusPerMinute);
- AssertEx.EqualTolerance(DegreesCelsiusPerMinuteInOneDegreeCelsiusPerSecond, (double)degreecelsiusperminuteQuantity.Value, DegreesCelsiusPerMinuteTolerance);
- Assert.Equal(TemperatureChangeRateUnit.DegreeCelsiusPerMinute, degreecelsiusperminuteQuantity.Unit);
-
- var degreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.DegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(DegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)degreecelsiuspersecondQuantity.Value, DegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.DegreeCelsiusPerSecond, degreecelsiuspersecondQuantity.Unit);
+ var inBaseUnits = TemperatureChangeRate.From(1.0, TemperatureChangeRate.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var hectodegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(HectodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)hectodegreecelsiuspersecondQuantity.Value, HectodegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond, hectodegreecelsiuspersecondQuantity.Unit);
-
- var kilodegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(KilodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)kilodegreecelsiuspersecondQuantity.Value, KilodegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond, kilodegreecelsiuspersecondQuantity.Unit);
-
- var microdegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(MicrodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)microdegreecelsiuspersecondQuantity.Value, MicrodegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond, microdegreecelsiuspersecondQuantity.Unit);
-
- var millidegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(MillidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)millidegreecelsiuspersecondQuantity.Value, MillidegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond, millidegreecelsiuspersecondQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var nanodegreecelsiuspersecondQuantity = degreecelsiuspersecond.ToUnit(TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond);
- AssertEx.EqualTolerance(NanodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, (double)nanodegreecelsiuspersecondQuantity.Value, NanodegreesCelsiusPerSecondTolerance);
- Assert.Equal(TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond, nanodegreecelsiuspersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TemperatureChangeRateUnit unit)
+ {
+ var quantity = TemperatureChangeRate.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TemperatureChangeRateUnit unit)
{
- var quantityInBaseUnit = TemperatureChangeRate.FromDegreesCelsiusPerSecond(1).ToBaseUnit();
- Assert.Equal(TemperatureChangeRate.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = TemperatureChangeRate.Units.FirstOrDefault(u => u != TemperatureChangeRate.BaseUnit && u != TemperatureChangeRateUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TemperatureChangeRateUnit.Undefined)
+ fromUnit = TemperatureChangeRate.BaseUnit;
+
+ var quantity = TemperatureChangeRate.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureDeltaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureDeltaTestsBase.g.cs
index bd811703eb..8c51fb93b6 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureDeltaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureDeltaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -59,6 +60,36 @@ public abstract partial class TemperatureDeltaTestsBase : QuantityTestsBase
protected virtual double MillidegreesCelsiusTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TemperatureDeltaUnit unit)
+ {
+ return unit switch
+ {
+ TemperatureDeltaUnit.DegreeCelsius => (DegreesCelsiusInOneKelvin, DegreesCelsiusTolerance),
+ TemperatureDeltaUnit.DegreeDelisle => (DegreesDelisleInOneKelvin, DegreesDelisleTolerance),
+ TemperatureDeltaUnit.DegreeFahrenheit => (DegreesFahrenheitInOneKelvin, DegreesFahrenheitTolerance),
+ TemperatureDeltaUnit.DegreeNewton => (DegreesNewtonInOneKelvin, DegreesNewtonTolerance),
+ TemperatureDeltaUnit.DegreeRankine => (DegreesRankineInOneKelvin, DegreesRankineTolerance),
+ TemperatureDeltaUnit.DegreeReaumur => (DegreesReaumurInOneKelvin, DegreesReaumurTolerance),
+ TemperatureDeltaUnit.DegreeRoemer => (DegreesRoemerInOneKelvin, DegreesRoemerTolerance),
+ TemperatureDeltaUnit.Kelvin => (KelvinsInOneKelvin, KelvinsTolerance),
+ TemperatureDeltaUnit.MillidegreeCelsius => (MillidegreesCelsiusInOneKelvin, MillidegreesCelsiusTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TemperatureDeltaUnit.DegreeCelsius },
+ new object[] { TemperatureDeltaUnit.DegreeDelisle },
+ new object[] { TemperatureDeltaUnit.DegreeFahrenheit },
+ new object[] { TemperatureDeltaUnit.DegreeNewton },
+ new object[] { TemperatureDeltaUnit.DegreeRankine },
+ new object[] { TemperatureDeltaUnit.DegreeReaumur },
+ new object[] { TemperatureDeltaUnit.DegreeRoemer },
+ new object[] { TemperatureDeltaUnit.Kelvin },
+ new object[] { TemperatureDeltaUnit.MillidegreeCelsius },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -228,53 +259,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TemperatureDeltaUnit unit)
{
- var kelvin = TemperatureDelta.FromKelvins(1);
-
- var degreecelsiusQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeCelsius);
- AssertEx.EqualTolerance(DegreesCelsiusInOneKelvin, (double)degreecelsiusQuantity.Value, DegreesCelsiusTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeCelsius, degreecelsiusQuantity.Unit);
-
- var degreedelisleQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeDelisle);
- AssertEx.EqualTolerance(DegreesDelisleInOneKelvin, (double)degreedelisleQuantity.Value, DegreesDelisleTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeDelisle, degreedelisleQuantity.Unit);
-
- var degreefahrenheitQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeFahrenheit);
- AssertEx.EqualTolerance(DegreesFahrenheitInOneKelvin, (double)degreefahrenheitQuantity.Value, DegreesFahrenheitTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeFahrenheit, degreefahrenheitQuantity.Unit);
-
- var degreenewtonQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeNewton);
- AssertEx.EqualTolerance(DegreesNewtonInOneKelvin, (double)degreenewtonQuantity.Value, DegreesNewtonTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeNewton, degreenewtonQuantity.Unit);
+ var inBaseUnits = TemperatureDelta.From(1.0, TemperatureDelta.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var degreerankineQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeRankine);
- AssertEx.EqualTolerance(DegreesRankineInOneKelvin, (double)degreerankineQuantity.Value, DegreesRankineTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeRankine, degreerankineQuantity.Unit);
-
- var degreereaumurQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeReaumur);
- AssertEx.EqualTolerance(DegreesReaumurInOneKelvin, (double)degreereaumurQuantity.Value, DegreesReaumurTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeReaumur, degreereaumurQuantity.Unit);
-
- var degreeroemerQuantity = kelvin.ToUnit(TemperatureDeltaUnit.DegreeRoemer);
- AssertEx.EqualTolerance(DegreesRoemerInOneKelvin, (double)degreeroemerQuantity.Value, DegreesRoemerTolerance);
- Assert.Equal(TemperatureDeltaUnit.DegreeRoemer, degreeroemerQuantity.Unit);
-
- var kelvinQuantity = kelvin.ToUnit(TemperatureDeltaUnit.Kelvin);
- AssertEx.EqualTolerance(KelvinsInOneKelvin, (double)kelvinQuantity.Value, KelvinsTolerance);
- Assert.Equal(TemperatureDeltaUnit.Kelvin, kelvinQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var millidegreecelsiusQuantity = kelvin.ToUnit(TemperatureDeltaUnit.MillidegreeCelsius);
- AssertEx.EqualTolerance(MillidegreesCelsiusInOneKelvin, (double)millidegreecelsiusQuantity.Value, MillidegreesCelsiusTolerance);
- Assert.Equal(TemperatureDeltaUnit.MillidegreeCelsius, millidegreecelsiusQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TemperatureDeltaUnit unit)
+ {
+ var quantity = TemperatureDelta.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TemperatureDeltaUnit unit)
{
- var quantityInBaseUnit = TemperatureDelta.FromKelvins(1).ToBaseUnit();
- Assert.Equal(TemperatureDelta.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = TemperatureDelta.Units.FirstOrDefault(u => u != TemperatureDelta.BaseUnit && u != TemperatureDeltaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TemperatureDeltaUnit.Undefined)
+ fromUnit = TemperatureDelta.BaseUnit;
+
+ var quantity = TemperatureDelta.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs
index 76f37cdd67..75372fd4e2 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -49,6 +50,26 @@ public abstract partial class TemperatureGradientTestsBase : QuantityTestsBase
protected virtual double KelvinsPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TemperatureGradientUnit unit)
+ {
+ return unit switch
+ {
+ TemperatureGradientUnit.DegreeCelsiusPerKilometer => (DegreesCelciusPerKilometerInOneKelvinPerMeter, DegreesCelciusPerKilometerTolerance),
+ TemperatureGradientUnit.DegreeCelsiusPerMeter => (DegreesCelciusPerMeterInOneKelvinPerMeter, DegreesCelciusPerMeterTolerance),
+ TemperatureGradientUnit.DegreeFahrenheitPerFoot => (DegreesFahrenheitPerFootInOneKelvinPerMeter, DegreesFahrenheitPerFootTolerance),
+ TemperatureGradientUnit.KelvinPerMeter => (KelvinsPerMeterInOneKelvinPerMeter, KelvinsPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TemperatureGradientUnit.DegreeCelsiusPerKilometer },
+ new object[] { TemperatureGradientUnit.DegreeCelsiusPerMeter },
+ new object[] { TemperatureGradientUnit.DegreeFahrenheitPerFoot },
+ new object[] { TemperatureGradientUnit.KelvinPerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -188,33 +209,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TemperatureGradientUnit unit)
{
- var kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1);
+ var inBaseUnits = TemperatureGradient.From(1.0, TemperatureGradient.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var degreecelsiusperkilometerQuantity = kelvinpermeter.ToUnit(TemperatureGradientUnit.DegreeCelsiusPerKilometer);
- AssertEx.EqualTolerance(DegreesCelciusPerKilometerInOneKelvinPerMeter, (double)degreecelsiusperkilometerQuantity.Value, DegreesCelciusPerKilometerTolerance);
- Assert.Equal(TemperatureGradientUnit.DegreeCelsiusPerKilometer, degreecelsiusperkilometerQuantity.Unit);
-
- var degreecelsiuspermeterQuantity = kelvinpermeter.ToUnit(TemperatureGradientUnit.DegreeCelsiusPerMeter);
- AssertEx.EqualTolerance(DegreesCelciusPerMeterInOneKelvinPerMeter, (double)degreecelsiuspermeterQuantity.Value, DegreesCelciusPerMeterTolerance);
- Assert.Equal(TemperatureGradientUnit.DegreeCelsiusPerMeter, degreecelsiuspermeterQuantity.Unit);
-
- var degreefahrenheitperfootQuantity = kelvinpermeter.ToUnit(TemperatureGradientUnit.DegreeFahrenheitPerFoot);
- AssertEx.EqualTolerance(DegreesFahrenheitPerFootInOneKelvinPerMeter, (double)degreefahrenheitperfootQuantity.Value, DegreesFahrenheitPerFootTolerance);
- Assert.Equal(TemperatureGradientUnit.DegreeFahrenheitPerFoot, degreefahrenheitperfootQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var kelvinpermeterQuantity = kelvinpermeter.ToUnit(TemperatureGradientUnit.KelvinPerMeter);
- AssertEx.EqualTolerance(KelvinsPerMeterInOneKelvinPerMeter, (double)kelvinpermeterQuantity.Value, KelvinsPerMeterTolerance);
- Assert.Equal(TemperatureGradientUnit.KelvinPerMeter, kelvinpermeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TemperatureGradientUnit unit)
+ {
+ var quantity = TemperatureGradient.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TemperatureGradientUnit unit)
{
- var quantityInBaseUnit = TemperatureGradient.FromKelvinsPerMeter(1).ToBaseUnit();
- Assert.Equal(TemperatureGradient.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = TemperatureGradient.Units.FirstOrDefault(u => u != TemperatureGradient.BaseUnit && u != TemperatureGradientUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TemperatureGradientUnit.Undefined)
+ fromUnit = TemperatureGradient.BaseUnit;
+
+ var quantity = TemperatureGradient.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureTestsBase.g.cs
index d9cc05adff..9e0c160da4 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -61,6 +62,38 @@ public abstract partial class TemperatureTestsBase : QuantityTestsBase
protected virtual double SolarTemperaturesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TemperatureUnit unit)
+ {
+ return unit switch
+ {
+ TemperatureUnit.DegreeCelsius => (DegreesCelsiusInOneKelvin, DegreesCelsiusTolerance),
+ TemperatureUnit.DegreeDelisle => (DegreesDelisleInOneKelvin, DegreesDelisleTolerance),
+ TemperatureUnit.DegreeFahrenheit => (DegreesFahrenheitInOneKelvin, DegreesFahrenheitTolerance),
+ TemperatureUnit.DegreeNewton => (DegreesNewtonInOneKelvin, DegreesNewtonTolerance),
+ TemperatureUnit.DegreeRankine => (DegreesRankineInOneKelvin, DegreesRankineTolerance),
+ TemperatureUnit.DegreeReaumur => (DegreesReaumurInOneKelvin, DegreesReaumurTolerance),
+ TemperatureUnit.DegreeRoemer => (DegreesRoemerInOneKelvin, DegreesRoemerTolerance),
+ TemperatureUnit.Kelvin => (KelvinsInOneKelvin, KelvinsTolerance),
+ TemperatureUnit.MillidegreeCelsius => (MillidegreesCelsiusInOneKelvin, MillidegreesCelsiusTolerance),
+ TemperatureUnit.SolarTemperature => (SolarTemperaturesInOneKelvin, SolarTemperaturesTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TemperatureUnit.DegreeCelsius },
+ new object[] { TemperatureUnit.DegreeDelisle },
+ new object[] { TemperatureUnit.DegreeFahrenheit },
+ new object[] { TemperatureUnit.DegreeNewton },
+ new object[] { TemperatureUnit.DegreeRankine },
+ new object[] { TemperatureUnit.DegreeReaumur },
+ new object[] { TemperatureUnit.DegreeRoemer },
+ new object[] { TemperatureUnit.Kelvin },
+ new object[] { TemperatureUnit.MillidegreeCelsius },
+ new object[] { TemperatureUnit.SolarTemperature },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -236,57 +269,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TemperatureUnit unit)
{
- var kelvin = Temperature.FromKelvins(1);
-
- var degreecelsiusQuantity = kelvin.ToUnit(TemperatureUnit.DegreeCelsius);
- AssertEx.EqualTolerance(DegreesCelsiusInOneKelvin, (double)degreecelsiusQuantity.Value, DegreesCelsiusTolerance);
- Assert.Equal(TemperatureUnit.DegreeCelsius, degreecelsiusQuantity.Unit);
-
- var degreedelisleQuantity = kelvin.ToUnit(TemperatureUnit.DegreeDelisle);
- AssertEx.EqualTolerance(DegreesDelisleInOneKelvin, (double)degreedelisleQuantity.Value, DegreesDelisleTolerance);
- Assert.Equal(TemperatureUnit.DegreeDelisle, degreedelisleQuantity.Unit);
-
- var degreefahrenheitQuantity = kelvin.ToUnit(TemperatureUnit.DegreeFahrenheit);
- AssertEx.EqualTolerance(DegreesFahrenheitInOneKelvin, (double)degreefahrenheitQuantity.Value, DegreesFahrenheitTolerance);
- Assert.Equal(TemperatureUnit.DegreeFahrenheit, degreefahrenheitQuantity.Unit);
-
- var degreenewtonQuantity = kelvin.ToUnit(TemperatureUnit.DegreeNewton);
- AssertEx.EqualTolerance(DegreesNewtonInOneKelvin, (double)degreenewtonQuantity.Value, DegreesNewtonTolerance);
- Assert.Equal(TemperatureUnit.DegreeNewton, degreenewtonQuantity.Unit);
+ var inBaseUnits = Temperature.From(1.0, Temperature.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var degreerankineQuantity = kelvin.ToUnit(TemperatureUnit.DegreeRankine);
- AssertEx.EqualTolerance(DegreesRankineInOneKelvin, (double)degreerankineQuantity.Value, DegreesRankineTolerance);
- Assert.Equal(TemperatureUnit.DegreeRankine, degreerankineQuantity.Unit);
-
- var degreereaumurQuantity = kelvin.ToUnit(TemperatureUnit.DegreeReaumur);
- AssertEx.EqualTolerance(DegreesReaumurInOneKelvin, (double)degreereaumurQuantity.Value, DegreesReaumurTolerance);
- Assert.Equal(TemperatureUnit.DegreeReaumur, degreereaumurQuantity.Unit);
-
- var degreeroemerQuantity = kelvin.ToUnit(TemperatureUnit.DegreeRoemer);
- AssertEx.EqualTolerance(DegreesRoemerInOneKelvin, (double)degreeroemerQuantity.Value, DegreesRoemerTolerance);
- Assert.Equal(TemperatureUnit.DegreeRoemer, degreeroemerQuantity.Unit);
-
- var kelvinQuantity = kelvin.ToUnit(TemperatureUnit.Kelvin);
- AssertEx.EqualTolerance(KelvinsInOneKelvin, (double)kelvinQuantity.Value, KelvinsTolerance);
- Assert.Equal(TemperatureUnit.Kelvin, kelvinQuantity.Unit);
-
- var millidegreecelsiusQuantity = kelvin.ToUnit(TemperatureUnit.MillidegreeCelsius);
- AssertEx.EqualTolerance(MillidegreesCelsiusInOneKelvin, (double)millidegreecelsiusQuantity.Value, MillidegreesCelsiusTolerance);
- Assert.Equal(TemperatureUnit.MillidegreeCelsius, millidegreecelsiusQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var solartemperatureQuantity = kelvin.ToUnit(TemperatureUnit.SolarTemperature);
- AssertEx.EqualTolerance(SolarTemperaturesInOneKelvin, (double)solartemperatureQuantity.Value, SolarTemperaturesTolerance);
- Assert.Equal(TemperatureUnit.SolarTemperature, solartemperatureQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TemperatureUnit unit)
+ {
+ var quantity = Temperature.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TemperatureUnit unit)
{
- var quantityInBaseUnit = Temperature.FromKelvins(1).ToBaseUnit();
- Assert.Equal(Temperature.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Temperature.Units.FirstOrDefault(u => u != Temperature.BaseUnit && u != TemperatureUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TemperatureUnit.Undefined)
+ fromUnit = Temperature.BaseUnit;
+
+ var quantity = Temperature.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalConductivityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalConductivityTestsBase.g.cs
index 1d5122321a..2fd369651a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalConductivityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalConductivityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -45,6 +46,22 @@ public abstract partial class ThermalConductivityTestsBase : QuantityTestsBase
protected virtual double WattsPerMeterKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ThermalConductivityUnit unit)
+ {
+ return unit switch
+ {
+ ThermalConductivityUnit.BtuPerHourFootFahrenheit => (BtusPerHourFootFahrenheitInOneWattPerMeterKelvin, BtusPerHourFootFahrenheitTolerance),
+ ThermalConductivityUnit.WattPerMeterKelvin => (WattsPerMeterKelvinInOneWattPerMeterKelvin, WattsPerMeterKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ThermalConductivityUnit.BtuPerHourFootFahrenheit },
+ new object[] { ThermalConductivityUnit.WattPerMeterKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -172,25 +189,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ThermalConductivityUnit unit)
{
- var wattpermeterkelvin = ThermalConductivity.FromWattsPerMeterKelvin(1);
+ var inBaseUnits = ThermalConductivity.From(1.0, ThermalConductivity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var btuperhourfootfahrenheitQuantity = wattpermeterkelvin.ToUnit(ThermalConductivityUnit.BtuPerHourFootFahrenheit);
- AssertEx.EqualTolerance(BtusPerHourFootFahrenheitInOneWattPerMeterKelvin, (double)btuperhourfootfahrenheitQuantity.Value, BtusPerHourFootFahrenheitTolerance);
- Assert.Equal(ThermalConductivityUnit.BtuPerHourFootFahrenheit, btuperhourfootfahrenheitQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var wattpermeterkelvinQuantity = wattpermeterkelvin.ToUnit(ThermalConductivityUnit.WattPerMeterKelvin);
- AssertEx.EqualTolerance(WattsPerMeterKelvinInOneWattPerMeterKelvin, (double)wattpermeterkelvinQuantity.Value, WattsPerMeterKelvinTolerance);
- Assert.Equal(ThermalConductivityUnit.WattPerMeterKelvin, wattpermeterkelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ThermalConductivityUnit unit)
+ {
+ var quantity = ThermalConductivity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ThermalConductivityUnit unit)
{
- var quantityInBaseUnit = ThermalConductivity.FromWattsPerMeterKelvin(1).ToBaseUnit();
- Assert.Equal(ThermalConductivity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ThermalConductivity.Units.FirstOrDefault(u => u != ThermalConductivity.BaseUnit && u != ThermalConductivityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ThermalConductivityUnit.Undefined)
+ fromUnit = ThermalConductivity.BaseUnit;
+
+ var quantity = ThermalConductivity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs
index 2008dd27a2..213aea5006 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -53,6 +54,30 @@ public abstract partial class ThermalResistanceTestsBase : QuantityTestsBase
protected virtual double SquareMeterKelvinsPerWattTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ThermalResistanceUnit unit)
+ {
+ return unit switch
+ {
+ ThermalResistanceUnit.HourSquareFeetDegreeFahrenheitPerBtu => (HourSquareFeetDegreesFahrenheitPerBtuInOneSquareMeterKelvinPerKilowatt, HourSquareFeetDegreesFahrenheitPerBtuTolerance),
+ ThermalResistanceUnit.SquareCentimeterHourDegreeCelsiusPerKilocalorie => (SquareCentimeterHourDegreesCelsiusPerKilocalorieInOneSquareMeterKelvinPerKilowatt, SquareCentimeterHourDegreesCelsiusPerKilocalorieTolerance),
+ ThermalResistanceUnit.SquareCentimeterKelvinPerWatt => (SquareCentimeterKelvinsPerWattInOneSquareMeterKelvinPerKilowatt, SquareCentimeterKelvinsPerWattTolerance),
+ ThermalResistanceUnit.SquareMeterDegreeCelsiusPerWatt => (SquareMeterDegreesCelsiusPerWattInOneSquareMeterKelvinPerKilowatt, SquareMeterDegreesCelsiusPerWattTolerance),
+ ThermalResistanceUnit.SquareMeterKelvinPerKilowatt => (SquareMeterKelvinsPerKilowattInOneSquareMeterKelvinPerKilowatt, SquareMeterKelvinsPerKilowattTolerance),
+ ThermalResistanceUnit.SquareMeterKelvinPerWatt => (SquareMeterKelvinsPerWattInOneSquareMeterKelvinPerKilowatt, SquareMeterKelvinsPerWattTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { ThermalResistanceUnit.HourSquareFeetDegreeFahrenheitPerBtu },
+ new object[] { ThermalResistanceUnit.SquareCentimeterHourDegreeCelsiusPerKilocalorie },
+ new object[] { ThermalResistanceUnit.SquareCentimeterKelvinPerWatt },
+ new object[] { ThermalResistanceUnit.SquareMeterDegreeCelsiusPerWatt },
+ new object[] { ThermalResistanceUnit.SquareMeterKelvinPerKilowatt },
+ new object[] { ThermalResistanceUnit.SquareMeterKelvinPerWatt },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -204,41 +229,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(ThermalResistanceUnit unit)
{
- var squaremeterkelvinperkilowatt = ThermalResistance.FromSquareMeterKelvinsPerKilowatt(1);
-
- var hoursquarefeetdegreefahrenheitperbtuQuantity = squaremeterkelvinperkilowatt.ToUnit(ThermalResistanceUnit.HourSquareFeetDegreeFahrenheitPerBtu);
- AssertEx.EqualTolerance(HourSquareFeetDegreesFahrenheitPerBtuInOneSquareMeterKelvinPerKilowatt, (double)hoursquarefeetdegreefahrenheitperbtuQuantity.Value, HourSquareFeetDegreesFahrenheitPerBtuTolerance);
- Assert.Equal(ThermalResistanceUnit.HourSquareFeetDegreeFahrenheitPerBtu, hoursquarefeetdegreefahrenheitperbtuQuantity.Unit);
+ var inBaseUnits = ThermalResistance.From(1.0, ThermalResistance.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var squarecentimeterhourdegreecelsiusperkilocalorieQuantity = squaremeterkelvinperkilowatt.ToUnit(ThermalResistanceUnit.SquareCentimeterHourDegreeCelsiusPerKilocalorie);
- AssertEx.EqualTolerance(SquareCentimeterHourDegreesCelsiusPerKilocalorieInOneSquareMeterKelvinPerKilowatt, (double)squarecentimeterhourdegreecelsiusperkilocalorieQuantity.Value, SquareCentimeterHourDegreesCelsiusPerKilocalorieTolerance);
- Assert.Equal(ThermalResistanceUnit.SquareCentimeterHourDegreeCelsiusPerKilocalorie, squarecentimeterhourdegreecelsiusperkilocalorieQuantity.Unit);
-
- var squarecentimeterkelvinperwattQuantity = squaremeterkelvinperkilowatt.ToUnit(ThermalResistanceUnit.SquareCentimeterKelvinPerWatt);
- AssertEx.EqualTolerance(SquareCentimeterKelvinsPerWattInOneSquareMeterKelvinPerKilowatt, (double)squarecentimeterkelvinperwattQuantity.Value, SquareCentimeterKelvinsPerWattTolerance);
- Assert.Equal(ThermalResistanceUnit.SquareCentimeterKelvinPerWatt, squarecentimeterkelvinperwattQuantity.Unit);
-
- var squaremeterdegreecelsiusperwattQuantity = squaremeterkelvinperkilowatt.ToUnit(ThermalResistanceUnit.SquareMeterDegreeCelsiusPerWatt);
- AssertEx.EqualTolerance(SquareMeterDegreesCelsiusPerWattInOneSquareMeterKelvinPerKilowatt, (double)squaremeterdegreecelsiusperwattQuantity.Value, SquareMeterDegreesCelsiusPerWattTolerance);
- Assert.Equal(ThermalResistanceUnit.SquareMeterDegreeCelsiusPerWatt, squaremeterdegreecelsiusperwattQuantity.Unit);
-
- var squaremeterkelvinperkilowattQuantity = squaremeterkelvinperkilowatt.ToUnit(ThermalResistanceUnit.SquareMeterKelvinPerKilowatt);
- AssertEx.EqualTolerance(SquareMeterKelvinsPerKilowattInOneSquareMeterKelvinPerKilowatt, (double)squaremeterkelvinperkilowattQuantity.Value, SquareMeterKelvinsPerKilowattTolerance);
- Assert.Equal(ThermalResistanceUnit.SquareMeterKelvinPerKilowatt, squaremeterkelvinperkilowattQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var squaremeterkelvinperwattQuantity = squaremeterkelvinperkilowatt.ToUnit(ThermalResistanceUnit.SquareMeterKelvinPerWatt);
- AssertEx.EqualTolerance(SquareMeterKelvinsPerWattInOneSquareMeterKelvinPerKilowatt, (double)squaremeterkelvinperwattQuantity.Value, SquareMeterKelvinsPerWattTolerance);
- Assert.Equal(ThermalResistanceUnit.SquareMeterKelvinPerWatt, squaremeterkelvinperwattQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(ThermalResistanceUnit unit)
+ {
+ var quantity = ThermalResistance.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ThermalResistanceUnit unit)
{
- var quantityInBaseUnit = ThermalResistance.FromSquareMeterKelvinsPerKilowatt(1).ToBaseUnit();
- Assert.Equal(ThermalResistance.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = ThermalResistance.Units.FirstOrDefault(u => u != ThermalResistance.BaseUnit && u != ThermalResistanceUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == ThermalResistanceUnit.Undefined)
+ fromUnit = ThermalResistance.BaseUnit;
+
+ var quantity = ThermalResistance.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TorquePerLengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TorquePerLengthTestsBase.g.cs
index 5bf790acfe..96315802a1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TorquePerLengthTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TorquePerLengthTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -83,6 +84,60 @@ public abstract partial class TorquePerLengthTestsBase : QuantityTestsBase
protected virtual double TonneForceMillimetersPerMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TorquePerLengthUnit unit)
+ {
+ return unit switch
+ {
+ TorquePerLengthUnit.KilogramForceCentimeterPerMeter => (KilogramForceCentimetersPerMeterInOneNewtonMeterPerMeter, KilogramForceCentimetersPerMeterTolerance),
+ TorquePerLengthUnit.KilogramForceMeterPerMeter => (KilogramForceMetersPerMeterInOneNewtonMeterPerMeter, KilogramForceMetersPerMeterTolerance),
+ TorquePerLengthUnit.KilogramForceMillimeterPerMeter => (KilogramForceMillimetersPerMeterInOneNewtonMeterPerMeter, KilogramForceMillimetersPerMeterTolerance),
+ TorquePerLengthUnit.KilonewtonCentimeterPerMeter => (KilonewtonCentimetersPerMeterInOneNewtonMeterPerMeter, KilonewtonCentimetersPerMeterTolerance),
+ TorquePerLengthUnit.KilonewtonMeterPerMeter => (KilonewtonMetersPerMeterInOneNewtonMeterPerMeter, KilonewtonMetersPerMeterTolerance),
+ TorquePerLengthUnit.KilonewtonMillimeterPerMeter => (KilonewtonMillimetersPerMeterInOneNewtonMeterPerMeter, KilonewtonMillimetersPerMeterTolerance),
+ TorquePerLengthUnit.KilopoundForceFootPerFoot => (KilopoundForceFeetPerFootInOneNewtonMeterPerMeter, KilopoundForceFeetPerFootTolerance),
+ TorquePerLengthUnit.KilopoundForceInchPerFoot => (KilopoundForceInchesPerFootInOneNewtonMeterPerMeter, KilopoundForceInchesPerFootTolerance),
+ TorquePerLengthUnit.MeganewtonCentimeterPerMeter => (MeganewtonCentimetersPerMeterInOneNewtonMeterPerMeter, MeganewtonCentimetersPerMeterTolerance),
+ TorquePerLengthUnit.MeganewtonMeterPerMeter => (MeganewtonMetersPerMeterInOneNewtonMeterPerMeter, MeganewtonMetersPerMeterTolerance),
+ TorquePerLengthUnit.MeganewtonMillimeterPerMeter => (MeganewtonMillimetersPerMeterInOneNewtonMeterPerMeter, MeganewtonMillimetersPerMeterTolerance),
+ TorquePerLengthUnit.MegapoundForceFootPerFoot => (MegapoundForceFeetPerFootInOneNewtonMeterPerMeter, MegapoundForceFeetPerFootTolerance),
+ TorquePerLengthUnit.MegapoundForceInchPerFoot => (MegapoundForceInchesPerFootInOneNewtonMeterPerMeter, MegapoundForceInchesPerFootTolerance),
+ TorquePerLengthUnit.NewtonCentimeterPerMeter => (NewtonCentimetersPerMeterInOneNewtonMeterPerMeter, NewtonCentimetersPerMeterTolerance),
+ TorquePerLengthUnit.NewtonMeterPerMeter => (NewtonMetersPerMeterInOneNewtonMeterPerMeter, NewtonMetersPerMeterTolerance),
+ TorquePerLengthUnit.NewtonMillimeterPerMeter => (NewtonMillimetersPerMeterInOneNewtonMeterPerMeter, NewtonMillimetersPerMeterTolerance),
+ TorquePerLengthUnit.PoundForceFootPerFoot => (PoundForceFeetPerFootInOneNewtonMeterPerMeter, PoundForceFeetPerFootTolerance),
+ TorquePerLengthUnit.PoundForceInchPerFoot => (PoundForceInchesPerFootInOneNewtonMeterPerMeter, PoundForceInchesPerFootTolerance),
+ TorquePerLengthUnit.TonneForceCentimeterPerMeter => (TonneForceCentimetersPerMeterInOneNewtonMeterPerMeter, TonneForceCentimetersPerMeterTolerance),
+ TorquePerLengthUnit.TonneForceMeterPerMeter => (TonneForceMetersPerMeterInOneNewtonMeterPerMeter, TonneForceMetersPerMeterTolerance),
+ TorquePerLengthUnit.TonneForceMillimeterPerMeter => (TonneForceMillimetersPerMeterInOneNewtonMeterPerMeter, TonneForceMillimetersPerMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TorquePerLengthUnit.KilogramForceCentimeterPerMeter },
+ new object[] { TorquePerLengthUnit.KilogramForceMeterPerMeter },
+ new object[] { TorquePerLengthUnit.KilogramForceMillimeterPerMeter },
+ new object[] { TorquePerLengthUnit.KilonewtonCentimeterPerMeter },
+ new object[] { TorquePerLengthUnit.KilonewtonMeterPerMeter },
+ new object[] { TorquePerLengthUnit.KilonewtonMillimeterPerMeter },
+ new object[] { TorquePerLengthUnit.KilopoundForceFootPerFoot },
+ new object[] { TorquePerLengthUnit.KilopoundForceInchPerFoot },
+ new object[] { TorquePerLengthUnit.MeganewtonCentimeterPerMeter },
+ new object[] { TorquePerLengthUnit.MeganewtonMeterPerMeter },
+ new object[] { TorquePerLengthUnit.MeganewtonMillimeterPerMeter },
+ new object[] { TorquePerLengthUnit.MegapoundForceFootPerFoot },
+ new object[] { TorquePerLengthUnit.MegapoundForceInchPerFoot },
+ new object[] { TorquePerLengthUnit.NewtonCentimeterPerMeter },
+ new object[] { TorquePerLengthUnit.NewtonMeterPerMeter },
+ new object[] { TorquePerLengthUnit.NewtonMillimeterPerMeter },
+ new object[] { TorquePerLengthUnit.PoundForceFootPerFoot },
+ new object[] { TorquePerLengthUnit.PoundForceInchPerFoot },
+ new object[] { TorquePerLengthUnit.TonneForceCentimeterPerMeter },
+ new object[] { TorquePerLengthUnit.TonneForceMeterPerMeter },
+ new object[] { TorquePerLengthUnit.TonneForceMillimeterPerMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -324,101 +379,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TorquePerLengthUnit unit)
{
- var newtonmeterpermeter = TorquePerLength.FromNewtonMetersPerMeter(1);
-
- var kilogramforcecentimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilogramForceCentimeterPerMeter);
- AssertEx.EqualTolerance(KilogramForceCentimetersPerMeterInOneNewtonMeterPerMeter, (double)kilogramforcecentimeterpermeterQuantity.Value, KilogramForceCentimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.KilogramForceCentimeterPerMeter, kilogramforcecentimeterpermeterQuantity.Unit);
-
- var kilogramforcemeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilogramForceMeterPerMeter);
- AssertEx.EqualTolerance(KilogramForceMetersPerMeterInOneNewtonMeterPerMeter, (double)kilogramforcemeterpermeterQuantity.Value, KilogramForceMetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.KilogramForceMeterPerMeter, kilogramforcemeterpermeterQuantity.Unit);
-
- var kilogramforcemillimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilogramForceMillimeterPerMeter);
- AssertEx.EqualTolerance(KilogramForceMillimetersPerMeterInOneNewtonMeterPerMeter, (double)kilogramforcemillimeterpermeterQuantity.Value, KilogramForceMillimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.KilogramForceMillimeterPerMeter, kilogramforcemillimeterpermeterQuantity.Unit);
-
- var kilonewtoncentimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilonewtonCentimeterPerMeter);
- AssertEx.EqualTolerance(KilonewtonCentimetersPerMeterInOneNewtonMeterPerMeter, (double)kilonewtoncentimeterpermeterQuantity.Value, KilonewtonCentimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.KilonewtonCentimeterPerMeter, kilonewtoncentimeterpermeterQuantity.Unit);
-
- var kilonewtonmeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilonewtonMeterPerMeter);
- AssertEx.EqualTolerance(KilonewtonMetersPerMeterInOneNewtonMeterPerMeter, (double)kilonewtonmeterpermeterQuantity.Value, KilonewtonMetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.KilonewtonMeterPerMeter, kilonewtonmeterpermeterQuantity.Unit);
-
- var kilonewtonmillimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilonewtonMillimeterPerMeter);
- AssertEx.EqualTolerance(KilonewtonMillimetersPerMeterInOneNewtonMeterPerMeter, (double)kilonewtonmillimeterpermeterQuantity.Value, KilonewtonMillimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.KilonewtonMillimeterPerMeter, kilonewtonmillimeterpermeterQuantity.Unit);
-
- var kilopoundforcefootperfootQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilopoundForceFootPerFoot);
- AssertEx.EqualTolerance(KilopoundForceFeetPerFootInOneNewtonMeterPerMeter, (double)kilopoundforcefootperfootQuantity.Value, KilopoundForceFeetPerFootTolerance);
- Assert.Equal(TorquePerLengthUnit.KilopoundForceFootPerFoot, kilopoundforcefootperfootQuantity.Unit);
-
- var kilopoundforceinchperfootQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.KilopoundForceInchPerFoot);
- AssertEx.EqualTolerance(KilopoundForceInchesPerFootInOneNewtonMeterPerMeter, (double)kilopoundforceinchperfootQuantity.Value, KilopoundForceInchesPerFootTolerance);
- Assert.Equal(TorquePerLengthUnit.KilopoundForceInchPerFoot, kilopoundforceinchperfootQuantity.Unit);
-
- var meganewtoncentimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.MeganewtonCentimeterPerMeter);
- AssertEx.EqualTolerance(MeganewtonCentimetersPerMeterInOneNewtonMeterPerMeter, (double)meganewtoncentimeterpermeterQuantity.Value, MeganewtonCentimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.MeganewtonCentimeterPerMeter, meganewtoncentimeterpermeterQuantity.Unit);
-
- var meganewtonmeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.MeganewtonMeterPerMeter);
- AssertEx.EqualTolerance(MeganewtonMetersPerMeterInOneNewtonMeterPerMeter, (double)meganewtonmeterpermeterQuantity.Value, MeganewtonMetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.MeganewtonMeterPerMeter, meganewtonmeterpermeterQuantity.Unit);
+ var inBaseUnits = TorquePerLength.From(1.0, TorquePerLength.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var meganewtonmillimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.MeganewtonMillimeterPerMeter);
- AssertEx.EqualTolerance(MeganewtonMillimetersPerMeterInOneNewtonMeterPerMeter, (double)meganewtonmillimeterpermeterQuantity.Value, MeganewtonMillimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.MeganewtonMillimeterPerMeter, meganewtonmillimeterpermeterQuantity.Unit);
-
- var megapoundforcefootperfootQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.MegapoundForceFootPerFoot);
- AssertEx.EqualTolerance(MegapoundForceFeetPerFootInOneNewtonMeterPerMeter, (double)megapoundforcefootperfootQuantity.Value, MegapoundForceFeetPerFootTolerance);
- Assert.Equal(TorquePerLengthUnit.MegapoundForceFootPerFoot, megapoundforcefootperfootQuantity.Unit);
-
- var megapoundforceinchperfootQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.MegapoundForceInchPerFoot);
- AssertEx.EqualTolerance(MegapoundForceInchesPerFootInOneNewtonMeterPerMeter, (double)megapoundforceinchperfootQuantity.Value, MegapoundForceInchesPerFootTolerance);
- Assert.Equal(TorquePerLengthUnit.MegapoundForceInchPerFoot, megapoundforceinchperfootQuantity.Unit);
-
- var newtoncentimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.NewtonCentimeterPerMeter);
- AssertEx.EqualTolerance(NewtonCentimetersPerMeterInOneNewtonMeterPerMeter, (double)newtoncentimeterpermeterQuantity.Value, NewtonCentimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.NewtonCentimeterPerMeter, newtoncentimeterpermeterQuantity.Unit);
-
- var newtonmeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.NewtonMeterPerMeter);
- AssertEx.EqualTolerance(NewtonMetersPerMeterInOneNewtonMeterPerMeter, (double)newtonmeterpermeterQuantity.Value, NewtonMetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.NewtonMeterPerMeter, newtonmeterpermeterQuantity.Unit);
-
- var newtonmillimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.NewtonMillimeterPerMeter);
- AssertEx.EqualTolerance(NewtonMillimetersPerMeterInOneNewtonMeterPerMeter, (double)newtonmillimeterpermeterQuantity.Value, NewtonMillimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.NewtonMillimeterPerMeter, newtonmillimeterpermeterQuantity.Unit);
-
- var poundforcefootperfootQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.PoundForceFootPerFoot);
- AssertEx.EqualTolerance(PoundForceFeetPerFootInOneNewtonMeterPerMeter, (double)poundforcefootperfootQuantity.Value, PoundForceFeetPerFootTolerance);
- Assert.Equal(TorquePerLengthUnit.PoundForceFootPerFoot, poundforcefootperfootQuantity.Unit);
-
- var poundforceinchperfootQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.PoundForceInchPerFoot);
- AssertEx.EqualTolerance(PoundForceInchesPerFootInOneNewtonMeterPerMeter, (double)poundforceinchperfootQuantity.Value, PoundForceInchesPerFootTolerance);
- Assert.Equal(TorquePerLengthUnit.PoundForceInchPerFoot, poundforceinchperfootQuantity.Unit);
-
- var tonneforcecentimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.TonneForceCentimeterPerMeter);
- AssertEx.EqualTolerance(TonneForceCentimetersPerMeterInOneNewtonMeterPerMeter, (double)tonneforcecentimeterpermeterQuantity.Value, TonneForceCentimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.TonneForceCentimeterPerMeter, tonneforcecentimeterpermeterQuantity.Unit);
-
- var tonneforcemeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.TonneForceMeterPerMeter);
- AssertEx.EqualTolerance(TonneForceMetersPerMeterInOneNewtonMeterPerMeter, (double)tonneforcemeterpermeterQuantity.Value, TonneForceMetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.TonneForceMeterPerMeter, tonneforcemeterpermeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneforcemillimeterpermeterQuantity = newtonmeterpermeter.ToUnit(TorquePerLengthUnit.TonneForceMillimeterPerMeter);
- AssertEx.EqualTolerance(TonneForceMillimetersPerMeterInOneNewtonMeterPerMeter, (double)tonneforcemillimeterpermeterQuantity.Value, TonneForceMillimetersPerMeterTolerance);
- Assert.Equal(TorquePerLengthUnit.TonneForceMillimeterPerMeter, tonneforcemillimeterpermeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TorquePerLengthUnit unit)
+ {
+ var quantity = TorquePerLength.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TorquePerLengthUnit unit)
{
- var quantityInBaseUnit = TorquePerLength.FromNewtonMetersPerMeter(1).ToBaseUnit();
- Assert.Equal(TorquePerLength.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = TorquePerLength.Units.FirstOrDefault(u => u != TorquePerLength.BaseUnit && u != TorquePerLengthUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TorquePerLengthUnit.Undefined)
+ fromUnit = TorquePerLength.BaseUnit;
+
+ var quantity = TorquePerLength.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TorqueTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TorqueTestsBase.g.cs
index 2086b3fde6..6f91770d4b 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TorqueTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TorqueTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -91,6 +92,68 @@ public abstract partial class TorqueTestsBase : QuantityTestsBase
protected virtual double TonneForceMillimetersTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TorqueUnit unit)
+ {
+ return unit switch
+ {
+ TorqueUnit.GramForceCentimeter => (GramForceCentimetersInOneNewtonMeter, GramForceCentimetersTolerance),
+ TorqueUnit.GramForceMeter => (GramForceMetersInOneNewtonMeter, GramForceMetersTolerance),
+ TorqueUnit.GramForceMillimeter => (GramForceMillimetersInOneNewtonMeter, GramForceMillimetersTolerance),
+ TorqueUnit.KilogramForceCentimeter => (KilogramForceCentimetersInOneNewtonMeter, KilogramForceCentimetersTolerance),
+ TorqueUnit.KilogramForceMeter => (KilogramForceMetersInOneNewtonMeter, KilogramForceMetersTolerance),
+ TorqueUnit.KilogramForceMillimeter => (KilogramForceMillimetersInOneNewtonMeter, KilogramForceMillimetersTolerance),
+ TorqueUnit.KilonewtonCentimeter => (KilonewtonCentimetersInOneNewtonMeter, KilonewtonCentimetersTolerance),
+ TorqueUnit.KilonewtonMeter => (KilonewtonMetersInOneNewtonMeter, KilonewtonMetersTolerance),
+ TorqueUnit.KilonewtonMillimeter => (KilonewtonMillimetersInOneNewtonMeter, KilonewtonMillimetersTolerance),
+ TorqueUnit.KilopoundForceFoot => (KilopoundForceFeetInOneNewtonMeter, KilopoundForceFeetTolerance),
+ TorqueUnit.KilopoundForceInch => (KilopoundForceInchesInOneNewtonMeter, KilopoundForceInchesTolerance),
+ TorqueUnit.MeganewtonCentimeter => (MeganewtonCentimetersInOneNewtonMeter, MeganewtonCentimetersTolerance),
+ TorqueUnit.MeganewtonMeter => (MeganewtonMetersInOneNewtonMeter, MeganewtonMetersTolerance),
+ TorqueUnit.MeganewtonMillimeter => (MeganewtonMillimetersInOneNewtonMeter, MeganewtonMillimetersTolerance),
+ TorqueUnit.MegapoundForceFoot => (MegapoundForceFeetInOneNewtonMeter, MegapoundForceFeetTolerance),
+ TorqueUnit.MegapoundForceInch => (MegapoundForceInchesInOneNewtonMeter, MegapoundForceInchesTolerance),
+ TorqueUnit.NewtonCentimeter => (NewtonCentimetersInOneNewtonMeter, NewtonCentimetersTolerance),
+ TorqueUnit.NewtonMeter => (NewtonMetersInOneNewtonMeter, NewtonMetersTolerance),
+ TorqueUnit.NewtonMillimeter => (NewtonMillimetersInOneNewtonMeter, NewtonMillimetersTolerance),
+ TorqueUnit.PoundalFoot => (PoundalFeetInOneNewtonMeter, PoundalFeetTolerance),
+ TorqueUnit.PoundForceFoot => (PoundForceFeetInOneNewtonMeter, PoundForceFeetTolerance),
+ TorqueUnit.PoundForceInch => (PoundForceInchesInOneNewtonMeter, PoundForceInchesTolerance),
+ TorqueUnit.TonneForceCentimeter => (TonneForceCentimetersInOneNewtonMeter, TonneForceCentimetersTolerance),
+ TorqueUnit.TonneForceMeter => (TonneForceMetersInOneNewtonMeter, TonneForceMetersTolerance),
+ TorqueUnit.TonneForceMillimeter => (TonneForceMillimetersInOneNewtonMeter, TonneForceMillimetersTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TorqueUnit.GramForceCentimeter },
+ new object[] { TorqueUnit.GramForceMeter },
+ new object[] { TorqueUnit.GramForceMillimeter },
+ new object[] { TorqueUnit.KilogramForceCentimeter },
+ new object[] { TorqueUnit.KilogramForceMeter },
+ new object[] { TorqueUnit.KilogramForceMillimeter },
+ new object[] { TorqueUnit.KilonewtonCentimeter },
+ new object[] { TorqueUnit.KilonewtonMeter },
+ new object[] { TorqueUnit.KilonewtonMillimeter },
+ new object[] { TorqueUnit.KilopoundForceFoot },
+ new object[] { TorqueUnit.KilopoundForceInch },
+ new object[] { TorqueUnit.MeganewtonCentimeter },
+ new object[] { TorqueUnit.MeganewtonMeter },
+ new object[] { TorqueUnit.MeganewtonMillimeter },
+ new object[] { TorqueUnit.MegapoundForceFoot },
+ new object[] { TorqueUnit.MegapoundForceInch },
+ new object[] { TorqueUnit.NewtonCentimeter },
+ new object[] { TorqueUnit.NewtonMeter },
+ new object[] { TorqueUnit.NewtonMillimeter },
+ new object[] { TorqueUnit.PoundalFoot },
+ new object[] { TorqueUnit.PoundForceFoot },
+ new object[] { TorqueUnit.PoundForceInch },
+ new object[] { TorqueUnit.TonneForceCentimeter },
+ new object[] { TorqueUnit.TonneForceMeter },
+ new object[] { TorqueUnit.TonneForceMillimeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -356,117 +419,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TorqueUnit unit)
{
- var newtonmeter = Torque.FromNewtonMeters(1);
-
- var gramforcecentimeterQuantity = newtonmeter.ToUnit(TorqueUnit.GramForceCentimeter);
- AssertEx.EqualTolerance(GramForceCentimetersInOneNewtonMeter, (double)gramforcecentimeterQuantity.Value, GramForceCentimetersTolerance);
- Assert.Equal(TorqueUnit.GramForceCentimeter, gramforcecentimeterQuantity.Unit);
-
- var gramforcemeterQuantity = newtonmeter.ToUnit(TorqueUnit.GramForceMeter);
- AssertEx.EqualTolerance(GramForceMetersInOneNewtonMeter, (double)gramforcemeterQuantity.Value, GramForceMetersTolerance);
- Assert.Equal(TorqueUnit.GramForceMeter, gramforcemeterQuantity.Unit);
-
- var gramforcemillimeterQuantity = newtonmeter.ToUnit(TorqueUnit.GramForceMillimeter);
- AssertEx.EqualTolerance(GramForceMillimetersInOneNewtonMeter, (double)gramforcemillimeterQuantity.Value, GramForceMillimetersTolerance);
- Assert.Equal(TorqueUnit.GramForceMillimeter, gramforcemillimeterQuantity.Unit);
-
- var kilogramforcecentimeterQuantity = newtonmeter.ToUnit(TorqueUnit.KilogramForceCentimeter);
- AssertEx.EqualTolerance(KilogramForceCentimetersInOneNewtonMeter, (double)kilogramforcecentimeterQuantity.Value, KilogramForceCentimetersTolerance);
- Assert.Equal(TorqueUnit.KilogramForceCentimeter, kilogramforcecentimeterQuantity.Unit);
-
- var kilogramforcemeterQuantity = newtonmeter.ToUnit(TorqueUnit.KilogramForceMeter);
- AssertEx.EqualTolerance(KilogramForceMetersInOneNewtonMeter, (double)kilogramforcemeterQuantity.Value, KilogramForceMetersTolerance);
- Assert.Equal(TorqueUnit.KilogramForceMeter, kilogramforcemeterQuantity.Unit);
-
- var kilogramforcemillimeterQuantity = newtonmeter.ToUnit(TorqueUnit.KilogramForceMillimeter);
- AssertEx.EqualTolerance(KilogramForceMillimetersInOneNewtonMeter, (double)kilogramforcemillimeterQuantity.Value, KilogramForceMillimetersTolerance);
- Assert.Equal(TorqueUnit.KilogramForceMillimeter, kilogramforcemillimeterQuantity.Unit);
-
- var kilonewtoncentimeterQuantity = newtonmeter.ToUnit(TorqueUnit.KilonewtonCentimeter);
- AssertEx.EqualTolerance(KilonewtonCentimetersInOneNewtonMeter, (double)kilonewtoncentimeterQuantity.Value, KilonewtonCentimetersTolerance);
- Assert.Equal(TorqueUnit.KilonewtonCentimeter, kilonewtoncentimeterQuantity.Unit);
-
- var kilonewtonmeterQuantity = newtonmeter.ToUnit(TorqueUnit.KilonewtonMeter);
- AssertEx.EqualTolerance(KilonewtonMetersInOneNewtonMeter, (double)kilonewtonmeterQuantity.Value, KilonewtonMetersTolerance);
- Assert.Equal(TorqueUnit.KilonewtonMeter, kilonewtonmeterQuantity.Unit);
-
- var kilonewtonmillimeterQuantity = newtonmeter.ToUnit(TorqueUnit.KilonewtonMillimeter);
- AssertEx.EqualTolerance(KilonewtonMillimetersInOneNewtonMeter, (double)kilonewtonmillimeterQuantity.Value, KilonewtonMillimetersTolerance);
- Assert.Equal(TorqueUnit.KilonewtonMillimeter, kilonewtonmillimeterQuantity.Unit);
-
- var kilopoundforcefootQuantity = newtonmeter.ToUnit(TorqueUnit.KilopoundForceFoot);
- AssertEx.EqualTolerance(KilopoundForceFeetInOneNewtonMeter, (double)kilopoundforcefootQuantity.Value, KilopoundForceFeetTolerance);
- Assert.Equal(TorqueUnit.KilopoundForceFoot, kilopoundforcefootQuantity.Unit);
-
- var kilopoundforceinchQuantity = newtonmeter.ToUnit(TorqueUnit.KilopoundForceInch);
- AssertEx.EqualTolerance(KilopoundForceInchesInOneNewtonMeter, (double)kilopoundforceinchQuantity.Value, KilopoundForceInchesTolerance);
- Assert.Equal(TorqueUnit.KilopoundForceInch, kilopoundforceinchQuantity.Unit);
-
- var meganewtoncentimeterQuantity = newtonmeter.ToUnit(TorqueUnit.MeganewtonCentimeter);
- AssertEx.EqualTolerance(MeganewtonCentimetersInOneNewtonMeter, (double)meganewtoncentimeterQuantity.Value, MeganewtonCentimetersTolerance);
- Assert.Equal(TorqueUnit.MeganewtonCentimeter, meganewtoncentimeterQuantity.Unit);
+ var inBaseUnits = Torque.From(1.0, Torque.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var meganewtonmeterQuantity = newtonmeter.ToUnit(TorqueUnit.MeganewtonMeter);
- AssertEx.EqualTolerance(MeganewtonMetersInOneNewtonMeter, (double)meganewtonmeterQuantity.Value, MeganewtonMetersTolerance);
- Assert.Equal(TorqueUnit.MeganewtonMeter, meganewtonmeterQuantity.Unit);
-
- var meganewtonmillimeterQuantity = newtonmeter.ToUnit(TorqueUnit.MeganewtonMillimeter);
- AssertEx.EqualTolerance(MeganewtonMillimetersInOneNewtonMeter, (double)meganewtonmillimeterQuantity.Value, MeganewtonMillimetersTolerance);
- Assert.Equal(TorqueUnit.MeganewtonMillimeter, meganewtonmillimeterQuantity.Unit);
-
- var megapoundforcefootQuantity = newtonmeter.ToUnit(TorqueUnit.MegapoundForceFoot);
- AssertEx.EqualTolerance(MegapoundForceFeetInOneNewtonMeter, (double)megapoundforcefootQuantity.Value, MegapoundForceFeetTolerance);
- Assert.Equal(TorqueUnit.MegapoundForceFoot, megapoundforcefootQuantity.Unit);
-
- var megapoundforceinchQuantity = newtonmeter.ToUnit(TorqueUnit.MegapoundForceInch);
- AssertEx.EqualTolerance(MegapoundForceInchesInOneNewtonMeter, (double)megapoundforceinchQuantity.Value, MegapoundForceInchesTolerance);
- Assert.Equal(TorqueUnit.MegapoundForceInch, megapoundforceinchQuantity.Unit);
-
- var newtoncentimeterQuantity = newtonmeter.ToUnit(TorqueUnit.NewtonCentimeter);
- AssertEx.EqualTolerance(NewtonCentimetersInOneNewtonMeter, (double)newtoncentimeterQuantity.Value, NewtonCentimetersTolerance);
- Assert.Equal(TorqueUnit.NewtonCentimeter, newtoncentimeterQuantity.Unit);
-
- var newtonmeterQuantity = newtonmeter.ToUnit(TorqueUnit.NewtonMeter);
- AssertEx.EqualTolerance(NewtonMetersInOneNewtonMeter, (double)newtonmeterQuantity.Value, NewtonMetersTolerance);
- Assert.Equal(TorqueUnit.NewtonMeter, newtonmeterQuantity.Unit);
-
- var newtonmillimeterQuantity = newtonmeter.ToUnit(TorqueUnit.NewtonMillimeter);
- AssertEx.EqualTolerance(NewtonMillimetersInOneNewtonMeter, (double)newtonmillimeterQuantity.Value, NewtonMillimetersTolerance);
- Assert.Equal(TorqueUnit.NewtonMillimeter, newtonmillimeterQuantity.Unit);
-
- var poundalfootQuantity = newtonmeter.ToUnit(TorqueUnit.PoundalFoot);
- AssertEx.EqualTolerance(PoundalFeetInOneNewtonMeter, (double)poundalfootQuantity.Value, PoundalFeetTolerance);
- Assert.Equal(TorqueUnit.PoundalFoot, poundalfootQuantity.Unit);
-
- var poundforcefootQuantity = newtonmeter.ToUnit(TorqueUnit.PoundForceFoot);
- AssertEx.EqualTolerance(PoundForceFeetInOneNewtonMeter, (double)poundforcefootQuantity.Value, PoundForceFeetTolerance);
- Assert.Equal(TorqueUnit.PoundForceFoot, poundforcefootQuantity.Unit);
-
- var poundforceinchQuantity = newtonmeter.ToUnit(TorqueUnit.PoundForceInch);
- AssertEx.EqualTolerance(PoundForceInchesInOneNewtonMeter, (double)poundforceinchQuantity.Value, PoundForceInchesTolerance);
- Assert.Equal(TorqueUnit.PoundForceInch, poundforceinchQuantity.Unit);
-
- var tonneforcecentimeterQuantity = newtonmeter.ToUnit(TorqueUnit.TonneForceCentimeter);
- AssertEx.EqualTolerance(TonneForceCentimetersInOneNewtonMeter, (double)tonneforcecentimeterQuantity.Value, TonneForceCentimetersTolerance);
- Assert.Equal(TorqueUnit.TonneForceCentimeter, tonneforcecentimeterQuantity.Unit);
-
- var tonneforcemeterQuantity = newtonmeter.ToUnit(TorqueUnit.TonneForceMeter);
- AssertEx.EqualTolerance(TonneForceMetersInOneNewtonMeter, (double)tonneforcemeterQuantity.Value, TonneForceMetersTolerance);
- Assert.Equal(TorqueUnit.TonneForceMeter, tonneforcemeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var tonneforcemillimeterQuantity = newtonmeter.ToUnit(TorqueUnit.TonneForceMillimeter);
- AssertEx.EqualTolerance(TonneForceMillimetersInOneNewtonMeter, (double)tonneforcemillimeterQuantity.Value, TonneForceMillimetersTolerance);
- Assert.Equal(TorqueUnit.TonneForceMillimeter, tonneforcemillimeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TorqueUnit unit)
+ {
+ var quantity = Torque.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TorqueUnit unit)
{
- var quantityInBaseUnit = Torque.FromNewtonMeters(1).ToBaseUnit();
- Assert.Equal(Torque.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Torque.Units.FirstOrDefault(u => u != Torque.BaseUnit && u != TorqueUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TorqueUnit.Undefined)
+ fromUnit = Torque.BaseUnit;
+
+ var quantity = Torque.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TurbidityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TurbidityTestsBase.g.cs
index f4b6eac73c..808250da97 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/TurbidityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TurbidityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class TurbidityTestsBase : QuantityTestsBase
protected virtual double NTUTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TurbidityUnit unit)
+ {
+ return unit switch
+ {
+ TurbidityUnit.NTU => (NTUInOneNTU, NTUTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { TurbidityUnit.NTU },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(TurbidityUnit unit)
{
- var ntu = Turbidity.FromNTU(1);
+ var inBaseUnits = Turbidity.From(1.0, Turbidity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var ntuQuantity = ntu.ToUnit(TurbidityUnit.NTU);
- AssertEx.EqualTolerance(NTUInOneNTU, (double)ntuQuantity.Value, NTUTolerance);
- Assert.Equal(TurbidityUnit.NTU, ntuQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(TurbidityUnit unit)
+ {
+ var quantity = Turbidity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(TurbidityUnit unit)
{
- var quantityInBaseUnit = Turbidity.FromNTU(1).ToBaseUnit();
- Assert.Equal(Turbidity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Turbidity.Units.FirstOrDefault(u => u != Turbidity.BaseUnit && u != TurbidityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == TurbidityUnit.Undefined)
+ fromUnit = Turbidity.BaseUnit;
+
+ var quantity = Turbidity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VitaminATestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VitaminATestsBase.g.cs
index 8e774dac97..0858d5e41e 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VitaminATestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VitaminATestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -43,6 +44,20 @@ public abstract partial class VitaminATestsBase : QuantityTestsBase
protected virtual double InternationalUnitsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VitaminAUnit unit)
+ {
+ return unit switch
+ {
+ VitaminAUnit.InternationalUnit => (InternationalUnitsInOneInternationalUnit, InternationalUnitsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VitaminAUnit.InternationalUnit },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -164,21 +179,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VitaminAUnit unit)
{
- var internationalunit = VitaminA.FromInternationalUnits(1);
+ var inBaseUnits = VitaminA.From(1.0, VitaminA.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var internationalunitQuantity = internationalunit.ToUnit(VitaminAUnit.InternationalUnit);
- AssertEx.EqualTolerance(InternationalUnitsInOneInternationalUnit, (double)internationalunitQuantity.Value, InternationalUnitsTolerance);
- Assert.Equal(VitaminAUnit.InternationalUnit, internationalunitQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VitaminAUnit unit)
+ {
+ var quantity = VitaminA.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
+ }
+
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VitaminAUnit unit)
{
- var quantityInBaseUnit = VitaminA.FromInternationalUnits(1).ToBaseUnit();
- Assert.Equal(VitaminA.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = VitaminA.Units.FirstOrDefault(u => u != VitaminA.BaseUnit && u != VitaminAUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VitaminAUnit.Undefined)
+ fromUnit = VitaminA.BaseUnit;
+
+ var quantity = VitaminA.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeConcentrationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeConcentrationTestsBase.g.cs
index 085e791a32..9db1eea2be 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeConcentrationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeConcentrationTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -81,6 +82,58 @@ public abstract partial class VolumeConcentrationTestsBase : QuantityTestsBase
protected virtual double PicolitersPerMililiterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VolumeConcentrationUnit unit)
+ {
+ return unit switch
+ {
+ VolumeConcentrationUnit.CentilitersPerLiter => (CentilitersPerLiterInOneDecimalFraction, CentilitersPerLiterTolerance),
+ VolumeConcentrationUnit.CentilitersPerMililiter => (CentilitersPerMililiterInOneDecimalFraction, CentilitersPerMililiterTolerance),
+ VolumeConcentrationUnit.DecilitersPerLiter => (DecilitersPerLiterInOneDecimalFraction, DecilitersPerLiterTolerance),
+ VolumeConcentrationUnit.DecilitersPerMililiter => (DecilitersPerMililiterInOneDecimalFraction, DecilitersPerMililiterTolerance),
+ VolumeConcentrationUnit.DecimalFraction => (DecimalFractionsInOneDecimalFraction, DecimalFractionsTolerance),
+ VolumeConcentrationUnit.LitersPerLiter => (LitersPerLiterInOneDecimalFraction, LitersPerLiterTolerance),
+ VolumeConcentrationUnit.LitersPerMililiter => (LitersPerMililiterInOneDecimalFraction, LitersPerMililiterTolerance),
+ VolumeConcentrationUnit.MicrolitersPerLiter => (MicrolitersPerLiterInOneDecimalFraction, MicrolitersPerLiterTolerance),
+ VolumeConcentrationUnit.MicrolitersPerMililiter => (MicrolitersPerMililiterInOneDecimalFraction, MicrolitersPerMililiterTolerance),
+ VolumeConcentrationUnit.MillilitersPerLiter => (MillilitersPerLiterInOneDecimalFraction, MillilitersPerLiterTolerance),
+ VolumeConcentrationUnit.MillilitersPerMililiter => (MillilitersPerMililiterInOneDecimalFraction, MillilitersPerMililiterTolerance),
+ VolumeConcentrationUnit.NanolitersPerLiter => (NanolitersPerLiterInOneDecimalFraction, NanolitersPerLiterTolerance),
+ VolumeConcentrationUnit.NanolitersPerMililiter => (NanolitersPerMililiterInOneDecimalFraction, NanolitersPerMililiterTolerance),
+ VolumeConcentrationUnit.PartPerBillion => (PartsPerBillionInOneDecimalFraction, PartsPerBillionTolerance),
+ VolumeConcentrationUnit.PartPerMillion => (PartsPerMillionInOneDecimalFraction, PartsPerMillionTolerance),
+ VolumeConcentrationUnit.PartPerThousand => (PartsPerThousandInOneDecimalFraction, PartsPerThousandTolerance),
+ VolumeConcentrationUnit.PartPerTrillion => (PartsPerTrillionInOneDecimalFraction, PartsPerTrillionTolerance),
+ VolumeConcentrationUnit.Percent => (PercentInOneDecimalFraction, PercentTolerance),
+ VolumeConcentrationUnit.PicolitersPerLiter => (PicolitersPerLiterInOneDecimalFraction, PicolitersPerLiterTolerance),
+ VolumeConcentrationUnit.PicolitersPerMililiter => (PicolitersPerMililiterInOneDecimalFraction, PicolitersPerMililiterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VolumeConcentrationUnit.CentilitersPerLiter },
+ new object[] { VolumeConcentrationUnit.CentilitersPerMililiter },
+ new object[] { VolumeConcentrationUnit.DecilitersPerLiter },
+ new object[] { VolumeConcentrationUnit.DecilitersPerMililiter },
+ new object[] { VolumeConcentrationUnit.DecimalFraction },
+ new object[] { VolumeConcentrationUnit.LitersPerLiter },
+ new object[] { VolumeConcentrationUnit.LitersPerMililiter },
+ new object[] { VolumeConcentrationUnit.MicrolitersPerLiter },
+ new object[] { VolumeConcentrationUnit.MicrolitersPerMililiter },
+ new object[] { VolumeConcentrationUnit.MillilitersPerLiter },
+ new object[] { VolumeConcentrationUnit.MillilitersPerMililiter },
+ new object[] { VolumeConcentrationUnit.NanolitersPerLiter },
+ new object[] { VolumeConcentrationUnit.NanolitersPerMililiter },
+ new object[] { VolumeConcentrationUnit.PartPerBillion },
+ new object[] { VolumeConcentrationUnit.PartPerMillion },
+ new object[] { VolumeConcentrationUnit.PartPerThousand },
+ new object[] { VolumeConcentrationUnit.PartPerTrillion },
+ new object[] { VolumeConcentrationUnit.Percent },
+ new object[] { VolumeConcentrationUnit.PicolitersPerLiter },
+ new object[] { VolumeConcentrationUnit.PicolitersPerMililiter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -316,97 +369,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VolumeConcentrationUnit unit)
{
- var decimalfraction = VolumeConcentration.FromDecimalFractions(1);
-
- var centilitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.CentilitersPerLiter);
- AssertEx.EqualTolerance(CentilitersPerLiterInOneDecimalFraction, (double)centilitersperliterQuantity.Value, CentilitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.CentilitersPerLiter, centilitersperliterQuantity.Unit);
-
- var centiliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.CentilitersPerMililiter);
- AssertEx.EqualTolerance(CentilitersPerMililiterInOneDecimalFraction, (double)centiliterspermililiterQuantity.Value, CentilitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.CentilitersPerMililiter, centiliterspermililiterQuantity.Unit);
-
- var decilitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.DecilitersPerLiter);
- AssertEx.EqualTolerance(DecilitersPerLiterInOneDecimalFraction, (double)decilitersperliterQuantity.Value, DecilitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.DecilitersPerLiter, decilitersperliterQuantity.Unit);
-
- var deciliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.DecilitersPerMililiter);
- AssertEx.EqualTolerance(DecilitersPerMililiterInOneDecimalFraction, (double)deciliterspermililiterQuantity.Value, DecilitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.DecilitersPerMililiter, deciliterspermililiterQuantity.Unit);
-
- var decimalfractionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.DecimalFraction);
- AssertEx.EqualTolerance(DecimalFractionsInOneDecimalFraction, (double)decimalfractionQuantity.Value, DecimalFractionsTolerance);
- Assert.Equal(VolumeConcentrationUnit.DecimalFraction, decimalfractionQuantity.Unit);
-
- var litersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.LitersPerLiter);
- AssertEx.EqualTolerance(LitersPerLiterInOneDecimalFraction, (double)litersperliterQuantity.Value, LitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.LitersPerLiter, litersperliterQuantity.Unit);
-
- var literspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.LitersPerMililiter);
- AssertEx.EqualTolerance(LitersPerMililiterInOneDecimalFraction, (double)literspermililiterQuantity.Value, LitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.LitersPerMililiter, literspermililiterQuantity.Unit);
-
- var microlitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MicrolitersPerLiter);
- AssertEx.EqualTolerance(MicrolitersPerLiterInOneDecimalFraction, (double)microlitersperliterQuantity.Value, MicrolitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.MicrolitersPerLiter, microlitersperliterQuantity.Unit);
-
- var microliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MicrolitersPerMililiter);
- AssertEx.EqualTolerance(MicrolitersPerMililiterInOneDecimalFraction, (double)microliterspermililiterQuantity.Value, MicrolitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.MicrolitersPerMililiter, microliterspermililiterQuantity.Unit);
-
- var millilitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MillilitersPerLiter);
- AssertEx.EqualTolerance(MillilitersPerLiterInOneDecimalFraction, (double)millilitersperliterQuantity.Value, MillilitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.MillilitersPerLiter, millilitersperliterQuantity.Unit);
+ var inBaseUnits = VolumeConcentration.From(1.0, VolumeConcentration.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var milliliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MillilitersPerMililiter);
- AssertEx.EqualTolerance(MillilitersPerMililiterInOneDecimalFraction, (double)milliliterspermililiterQuantity.Value, MillilitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.MillilitersPerMililiter, milliliterspermililiterQuantity.Unit);
-
- var nanolitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.NanolitersPerLiter);
- AssertEx.EqualTolerance(NanolitersPerLiterInOneDecimalFraction, (double)nanolitersperliterQuantity.Value, NanolitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.NanolitersPerLiter, nanolitersperliterQuantity.Unit);
-
- var nanoliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.NanolitersPerMililiter);
- AssertEx.EqualTolerance(NanolitersPerMililiterInOneDecimalFraction, (double)nanoliterspermililiterQuantity.Value, NanolitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.NanolitersPerMililiter, nanoliterspermililiterQuantity.Unit);
-
- var partperbillionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerBillion);
- AssertEx.EqualTolerance(PartsPerBillionInOneDecimalFraction, (double)partperbillionQuantity.Value, PartsPerBillionTolerance);
- Assert.Equal(VolumeConcentrationUnit.PartPerBillion, partperbillionQuantity.Unit);
-
- var partpermillionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerMillion);
- AssertEx.EqualTolerance(PartsPerMillionInOneDecimalFraction, (double)partpermillionQuantity.Value, PartsPerMillionTolerance);
- Assert.Equal(VolumeConcentrationUnit.PartPerMillion, partpermillionQuantity.Unit);
-
- var partperthousandQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerThousand);
- AssertEx.EqualTolerance(PartsPerThousandInOneDecimalFraction, (double)partperthousandQuantity.Value, PartsPerThousandTolerance);
- Assert.Equal(VolumeConcentrationUnit.PartPerThousand, partperthousandQuantity.Unit);
-
- var partpertrillionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerTrillion);
- AssertEx.EqualTolerance(PartsPerTrillionInOneDecimalFraction, (double)partpertrillionQuantity.Value, PartsPerTrillionTolerance);
- Assert.Equal(VolumeConcentrationUnit.PartPerTrillion, partpertrillionQuantity.Unit);
-
- var percentQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.Percent);
- AssertEx.EqualTolerance(PercentInOneDecimalFraction, (double)percentQuantity.Value, PercentTolerance);
- Assert.Equal(VolumeConcentrationUnit.Percent, percentQuantity.Unit);
-
- var picolitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PicolitersPerLiter);
- AssertEx.EqualTolerance(PicolitersPerLiterInOneDecimalFraction, (double)picolitersperliterQuantity.Value, PicolitersPerLiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.PicolitersPerLiter, picolitersperliterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var picoliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PicolitersPerMililiter);
- AssertEx.EqualTolerance(PicolitersPerMililiterInOneDecimalFraction, (double)picoliterspermililiterQuantity.Value, PicolitersPerMililiterTolerance);
- Assert.Equal(VolumeConcentrationUnit.PicolitersPerMililiter, picoliterspermililiterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VolumeConcentrationUnit unit)
+ {
+ var quantity = VolumeConcentration.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VolumeConcentrationUnit unit)
{
- var quantityInBaseUnit = VolumeConcentration.FromDecimalFractions(1).ToBaseUnit();
- Assert.Equal(VolumeConcentration.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = VolumeConcentration.Units.FirstOrDefault(u => u != VolumeConcentration.BaseUnit && u != VolumeConcentrationUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VolumeConcentrationUnit.Undefined)
+ fromUnit = VolumeConcentration.BaseUnit;
+
+ var quantity = VolumeConcentration.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowPerAreaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowPerAreaTestsBase.g.cs
index 766a0a6b53..6758c1bdb2 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowPerAreaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowPerAreaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -45,6 +46,22 @@ public abstract partial class VolumeFlowPerAreaTestsBase : QuantityTestsBase
protected virtual double CubicMetersPerSecondPerSquareMeterTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VolumeFlowPerAreaUnit unit)
+ {
+ return unit switch
+ {
+ VolumeFlowPerAreaUnit.CubicFootPerMinutePerSquareFoot => (CubicFeetPerMinutePerSquareFootInOneCubicMeterPerSecondPerSquareMeter, CubicFeetPerMinutePerSquareFootTolerance),
+ VolumeFlowPerAreaUnit.CubicMeterPerSecondPerSquareMeter => (CubicMetersPerSecondPerSquareMeterInOneCubicMeterPerSecondPerSquareMeter, CubicMetersPerSecondPerSquareMeterTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VolumeFlowPerAreaUnit.CubicFootPerMinutePerSquareFoot },
+ new object[] { VolumeFlowPerAreaUnit.CubicMeterPerSecondPerSquareMeter },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -172,25 +189,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VolumeFlowPerAreaUnit unit)
{
- var cubicmeterpersecondpersquaremeter = VolumeFlowPerArea.FromCubicMetersPerSecondPerSquareMeter(1);
+ var inBaseUnits = VolumeFlowPerArea.From(1.0, VolumeFlowPerArea.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var cubicfootperminutepersquarefootQuantity = cubicmeterpersecondpersquaremeter.ToUnit(VolumeFlowPerAreaUnit.CubicFootPerMinutePerSquareFoot);
- AssertEx.EqualTolerance(CubicFeetPerMinutePerSquareFootInOneCubicMeterPerSecondPerSquareMeter, (double)cubicfootperminutepersquarefootQuantity.Value, CubicFeetPerMinutePerSquareFootTolerance);
- Assert.Equal(VolumeFlowPerAreaUnit.CubicFootPerMinutePerSquareFoot, cubicfootperminutepersquarefootQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var cubicmeterpersecondpersquaremeterQuantity = cubicmeterpersecondpersquaremeter.ToUnit(VolumeFlowPerAreaUnit.CubicMeterPerSecondPerSquareMeter);
- AssertEx.EqualTolerance(CubicMetersPerSecondPerSquareMeterInOneCubicMeterPerSecondPerSquareMeter, (double)cubicmeterpersecondpersquaremeterQuantity.Value, CubicMetersPerSecondPerSquareMeterTolerance);
- Assert.Equal(VolumeFlowPerAreaUnit.CubicMeterPerSecondPerSquareMeter, cubicmeterpersecondpersquaremeterQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VolumeFlowPerAreaUnit unit)
+ {
+ var quantity = VolumeFlowPerArea.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VolumeFlowPerAreaUnit unit)
{
- var quantityInBaseUnit = VolumeFlowPerArea.FromCubicMetersPerSecondPerSquareMeter(1).ToBaseUnit();
- Assert.Equal(VolumeFlowPerArea.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = VolumeFlowPerArea.Units.FirstOrDefault(u => u != VolumeFlowPerArea.BaseUnit && u != VolumeFlowPerAreaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VolumeFlowPerAreaUnit.Undefined)
+ fromUnit = VolumeFlowPerArea.BaseUnit;
+
+ var quantity = VolumeFlowPerArea.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowTestsBase.g.cs
index 25d807d135..bbbc2ee7a6 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeFlowTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -165,6 +166,142 @@ public abstract partial class VolumeFlowTestsBase : QuantityTestsBase
protected virtual double UsGallonsPerSecondTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VolumeFlowUnit unit)
+ {
+ return unit switch
+ {
+ VolumeFlowUnit.AcreFootPerDay => (AcreFeetPerDayInOneCubicMeterPerSecond, AcreFeetPerDayTolerance),
+ VolumeFlowUnit.AcreFootPerHour => (AcreFeetPerHourInOneCubicMeterPerSecond, AcreFeetPerHourTolerance),
+ VolumeFlowUnit.AcreFootPerMinute => (AcreFeetPerMinuteInOneCubicMeterPerSecond, AcreFeetPerMinuteTolerance),
+ VolumeFlowUnit.AcreFootPerSecond => (AcreFeetPerSecondInOneCubicMeterPerSecond, AcreFeetPerSecondTolerance),
+ VolumeFlowUnit.CentiliterPerDay => (CentilitersPerDayInOneCubicMeterPerSecond, CentilitersPerDayTolerance),
+ VolumeFlowUnit.CentiliterPerHour => (CentilitersPerHourInOneCubicMeterPerSecond, CentilitersPerHourTolerance),
+ VolumeFlowUnit.CentiliterPerMinute => (CentilitersPerMinuteInOneCubicMeterPerSecond, CentilitersPerMinuteTolerance),
+ VolumeFlowUnit.CentiliterPerSecond => (CentilitersPerSecondInOneCubicMeterPerSecond, CentilitersPerSecondTolerance),
+ VolumeFlowUnit.CubicCentimeterPerMinute => (CubicCentimetersPerMinuteInOneCubicMeterPerSecond, CubicCentimetersPerMinuteTolerance),
+ VolumeFlowUnit.CubicDecimeterPerMinute => (CubicDecimetersPerMinuteInOneCubicMeterPerSecond, CubicDecimetersPerMinuteTolerance),
+ VolumeFlowUnit.CubicFootPerHour => (CubicFeetPerHourInOneCubicMeterPerSecond, CubicFeetPerHourTolerance),
+ VolumeFlowUnit.CubicFootPerMinute => (CubicFeetPerMinuteInOneCubicMeterPerSecond, CubicFeetPerMinuteTolerance),
+ VolumeFlowUnit.CubicFootPerSecond => (CubicFeetPerSecondInOneCubicMeterPerSecond, CubicFeetPerSecondTolerance),
+ VolumeFlowUnit.CubicMeterPerDay => (CubicMetersPerDayInOneCubicMeterPerSecond, CubicMetersPerDayTolerance),
+ VolumeFlowUnit.CubicMeterPerHour => (CubicMetersPerHourInOneCubicMeterPerSecond, CubicMetersPerHourTolerance),
+ VolumeFlowUnit.CubicMeterPerMinute => (CubicMetersPerMinuteInOneCubicMeterPerSecond, CubicMetersPerMinuteTolerance),
+ VolumeFlowUnit.CubicMeterPerSecond => (CubicMetersPerSecondInOneCubicMeterPerSecond, CubicMetersPerSecondTolerance),
+ VolumeFlowUnit.CubicMillimeterPerSecond => (CubicMillimetersPerSecondInOneCubicMeterPerSecond, CubicMillimetersPerSecondTolerance),
+ VolumeFlowUnit.CubicYardPerDay => (CubicYardsPerDayInOneCubicMeterPerSecond, CubicYardsPerDayTolerance),
+ VolumeFlowUnit.CubicYardPerHour => (CubicYardsPerHourInOneCubicMeterPerSecond, CubicYardsPerHourTolerance),
+ VolumeFlowUnit.CubicYardPerMinute => (CubicYardsPerMinuteInOneCubicMeterPerSecond, CubicYardsPerMinuteTolerance),
+ VolumeFlowUnit.CubicYardPerSecond => (CubicYardsPerSecondInOneCubicMeterPerSecond, CubicYardsPerSecondTolerance),
+ VolumeFlowUnit.DeciliterPerDay => (DecilitersPerDayInOneCubicMeterPerSecond, DecilitersPerDayTolerance),
+ VolumeFlowUnit.DeciliterPerHour => (DecilitersPerHourInOneCubicMeterPerSecond, DecilitersPerHourTolerance),
+ VolumeFlowUnit.DeciliterPerMinute => (DecilitersPerMinuteInOneCubicMeterPerSecond, DecilitersPerMinuteTolerance),
+ VolumeFlowUnit.DeciliterPerSecond => (DecilitersPerSecondInOneCubicMeterPerSecond, DecilitersPerSecondTolerance),
+ VolumeFlowUnit.KiloliterPerDay => (KilolitersPerDayInOneCubicMeterPerSecond, KilolitersPerDayTolerance),
+ VolumeFlowUnit.KiloliterPerHour => (KilolitersPerHourInOneCubicMeterPerSecond, KilolitersPerHourTolerance),
+ VolumeFlowUnit.KiloliterPerMinute => (KilolitersPerMinuteInOneCubicMeterPerSecond, KilolitersPerMinuteTolerance),
+ VolumeFlowUnit.KiloliterPerSecond => (KilolitersPerSecondInOneCubicMeterPerSecond, KilolitersPerSecondTolerance),
+ VolumeFlowUnit.KilousGallonPerMinute => (KilousGallonsPerMinuteInOneCubicMeterPerSecond, KilousGallonsPerMinuteTolerance),
+ VolumeFlowUnit.LiterPerDay => (LitersPerDayInOneCubicMeterPerSecond, LitersPerDayTolerance),
+ VolumeFlowUnit.LiterPerHour => (LitersPerHourInOneCubicMeterPerSecond, LitersPerHourTolerance),
+ VolumeFlowUnit.LiterPerMinute => (LitersPerMinuteInOneCubicMeterPerSecond, LitersPerMinuteTolerance),
+ VolumeFlowUnit.LiterPerSecond => (LitersPerSecondInOneCubicMeterPerSecond, LitersPerSecondTolerance),
+ VolumeFlowUnit.MegaliterPerDay => (MegalitersPerDayInOneCubicMeterPerSecond, MegalitersPerDayTolerance),
+ VolumeFlowUnit.MegaukGallonPerSecond => (MegaukGallonsPerSecondInOneCubicMeterPerSecond, MegaukGallonsPerSecondTolerance),
+ VolumeFlowUnit.MicroliterPerDay => (MicrolitersPerDayInOneCubicMeterPerSecond, MicrolitersPerDayTolerance),
+ VolumeFlowUnit.MicroliterPerHour => (MicrolitersPerHourInOneCubicMeterPerSecond, MicrolitersPerHourTolerance),
+ VolumeFlowUnit.MicroliterPerMinute => (MicrolitersPerMinuteInOneCubicMeterPerSecond, MicrolitersPerMinuteTolerance),
+ VolumeFlowUnit.MicroliterPerSecond => (MicrolitersPerSecondInOneCubicMeterPerSecond, MicrolitersPerSecondTolerance),
+ VolumeFlowUnit.MilliliterPerDay => (MillilitersPerDayInOneCubicMeterPerSecond, MillilitersPerDayTolerance),
+ VolumeFlowUnit.MilliliterPerHour => (MillilitersPerHourInOneCubicMeterPerSecond, MillilitersPerHourTolerance),
+ VolumeFlowUnit.MilliliterPerMinute => (MillilitersPerMinuteInOneCubicMeterPerSecond, MillilitersPerMinuteTolerance),
+ VolumeFlowUnit.MilliliterPerSecond => (MillilitersPerSecondInOneCubicMeterPerSecond, MillilitersPerSecondTolerance),
+ VolumeFlowUnit.MillionUsGallonsPerDay => (MillionUsGallonsPerDayInOneCubicMeterPerSecond, MillionUsGallonsPerDayTolerance),
+ VolumeFlowUnit.NanoliterPerDay => (NanolitersPerDayInOneCubicMeterPerSecond, NanolitersPerDayTolerance),
+ VolumeFlowUnit.NanoliterPerHour => (NanolitersPerHourInOneCubicMeterPerSecond, NanolitersPerHourTolerance),
+ VolumeFlowUnit.NanoliterPerMinute => (NanolitersPerMinuteInOneCubicMeterPerSecond, NanolitersPerMinuteTolerance),
+ VolumeFlowUnit.NanoliterPerSecond => (NanolitersPerSecondInOneCubicMeterPerSecond, NanolitersPerSecondTolerance),
+ VolumeFlowUnit.OilBarrelPerDay => (OilBarrelsPerDayInOneCubicMeterPerSecond, OilBarrelsPerDayTolerance),
+ VolumeFlowUnit.OilBarrelPerHour => (OilBarrelsPerHourInOneCubicMeterPerSecond, OilBarrelsPerHourTolerance),
+ VolumeFlowUnit.OilBarrelPerMinute => (OilBarrelsPerMinuteInOneCubicMeterPerSecond, OilBarrelsPerMinuteTolerance),
+ VolumeFlowUnit.OilBarrelPerSecond => (OilBarrelsPerSecondInOneCubicMeterPerSecond, OilBarrelsPerSecondTolerance),
+ VolumeFlowUnit.UkGallonPerDay => (UkGallonsPerDayInOneCubicMeterPerSecond, UkGallonsPerDayTolerance),
+ VolumeFlowUnit.UkGallonPerHour => (UkGallonsPerHourInOneCubicMeterPerSecond, UkGallonsPerHourTolerance),
+ VolumeFlowUnit.UkGallonPerMinute => (UkGallonsPerMinuteInOneCubicMeterPerSecond, UkGallonsPerMinuteTolerance),
+ VolumeFlowUnit.UkGallonPerSecond => (UkGallonsPerSecondInOneCubicMeterPerSecond, UkGallonsPerSecondTolerance),
+ VolumeFlowUnit.UsGallonPerDay => (UsGallonsPerDayInOneCubicMeterPerSecond, UsGallonsPerDayTolerance),
+ VolumeFlowUnit.UsGallonPerHour => (UsGallonsPerHourInOneCubicMeterPerSecond, UsGallonsPerHourTolerance),
+ VolumeFlowUnit.UsGallonPerMinute => (UsGallonsPerMinuteInOneCubicMeterPerSecond, UsGallonsPerMinuteTolerance),
+ VolumeFlowUnit.UsGallonPerSecond => (UsGallonsPerSecondInOneCubicMeterPerSecond, UsGallonsPerSecondTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VolumeFlowUnit.AcreFootPerDay },
+ new object[] { VolumeFlowUnit.AcreFootPerHour },
+ new object[] { VolumeFlowUnit.AcreFootPerMinute },
+ new object[] { VolumeFlowUnit.AcreFootPerSecond },
+ new object[] { VolumeFlowUnit.CentiliterPerDay },
+ new object[] { VolumeFlowUnit.CentiliterPerHour },
+ new object[] { VolumeFlowUnit.CentiliterPerMinute },
+ new object[] { VolumeFlowUnit.CentiliterPerSecond },
+ new object[] { VolumeFlowUnit.CubicCentimeterPerMinute },
+ new object[] { VolumeFlowUnit.CubicDecimeterPerMinute },
+ new object[] { VolumeFlowUnit.CubicFootPerHour },
+ new object[] { VolumeFlowUnit.CubicFootPerMinute },
+ new object[] { VolumeFlowUnit.CubicFootPerSecond },
+ new object[] { VolumeFlowUnit.CubicMeterPerDay },
+ new object[] { VolumeFlowUnit.CubicMeterPerHour },
+ new object[] { VolumeFlowUnit.CubicMeterPerMinute },
+ new object[] { VolumeFlowUnit.CubicMeterPerSecond },
+ new object[] { VolumeFlowUnit.CubicMillimeterPerSecond },
+ new object[] { VolumeFlowUnit.CubicYardPerDay },
+ new object[] { VolumeFlowUnit.CubicYardPerHour },
+ new object[] { VolumeFlowUnit.CubicYardPerMinute },
+ new object[] { VolumeFlowUnit.CubicYardPerSecond },
+ new object[] { VolumeFlowUnit.DeciliterPerDay },
+ new object[] { VolumeFlowUnit.DeciliterPerHour },
+ new object[] { VolumeFlowUnit.DeciliterPerMinute },
+ new object[] { VolumeFlowUnit.DeciliterPerSecond },
+ new object[] { VolumeFlowUnit.KiloliterPerDay },
+ new object[] { VolumeFlowUnit.KiloliterPerHour },
+ new object[] { VolumeFlowUnit.KiloliterPerMinute },
+ new object[] { VolumeFlowUnit.KiloliterPerSecond },
+ new object[] { VolumeFlowUnit.KilousGallonPerMinute },
+ new object[] { VolumeFlowUnit.LiterPerDay },
+ new object[] { VolumeFlowUnit.LiterPerHour },
+ new object[] { VolumeFlowUnit.LiterPerMinute },
+ new object[] { VolumeFlowUnit.LiterPerSecond },
+ new object[] { VolumeFlowUnit.MegaliterPerDay },
+ new object[] { VolumeFlowUnit.MegaukGallonPerSecond },
+ new object[] { VolumeFlowUnit.MicroliterPerDay },
+ new object[] { VolumeFlowUnit.MicroliterPerHour },
+ new object[] { VolumeFlowUnit.MicroliterPerMinute },
+ new object[] { VolumeFlowUnit.MicroliterPerSecond },
+ new object[] { VolumeFlowUnit.MilliliterPerDay },
+ new object[] { VolumeFlowUnit.MilliliterPerHour },
+ new object[] { VolumeFlowUnit.MilliliterPerMinute },
+ new object[] { VolumeFlowUnit.MilliliterPerSecond },
+ new object[] { VolumeFlowUnit.MillionUsGallonsPerDay },
+ new object[] { VolumeFlowUnit.NanoliterPerDay },
+ new object[] { VolumeFlowUnit.NanoliterPerHour },
+ new object[] { VolumeFlowUnit.NanoliterPerMinute },
+ new object[] { VolumeFlowUnit.NanoliterPerSecond },
+ new object[] { VolumeFlowUnit.OilBarrelPerDay },
+ new object[] { VolumeFlowUnit.OilBarrelPerHour },
+ new object[] { VolumeFlowUnit.OilBarrelPerMinute },
+ new object[] { VolumeFlowUnit.OilBarrelPerSecond },
+ new object[] { VolumeFlowUnit.UkGallonPerDay },
+ new object[] { VolumeFlowUnit.UkGallonPerHour },
+ new object[] { VolumeFlowUnit.UkGallonPerMinute },
+ new object[] { VolumeFlowUnit.UkGallonPerSecond },
+ new object[] { VolumeFlowUnit.UsGallonPerDay },
+ new object[] { VolumeFlowUnit.UsGallonPerHour },
+ new object[] { VolumeFlowUnit.UsGallonPerMinute },
+ new object[] { VolumeFlowUnit.UsGallonPerSecond },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -652,265 +789,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VolumeFlowUnit unit)
{
- var cubicmeterpersecond = VolumeFlow.FromCubicMetersPerSecond(1);
-
- var acrefootperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.AcreFootPerDay);
- AssertEx.EqualTolerance(AcreFeetPerDayInOneCubicMeterPerSecond, (double)acrefootperdayQuantity.Value, AcreFeetPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.AcreFootPerDay, acrefootperdayQuantity.Unit);
-
- var acrefootperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.AcreFootPerHour);
- AssertEx.EqualTolerance(AcreFeetPerHourInOneCubicMeterPerSecond, (double)acrefootperhourQuantity.Value, AcreFeetPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.AcreFootPerHour, acrefootperhourQuantity.Unit);
-
- var acrefootperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.AcreFootPerMinute);
- AssertEx.EqualTolerance(AcreFeetPerMinuteInOneCubicMeterPerSecond, (double)acrefootperminuteQuantity.Value, AcreFeetPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.AcreFootPerMinute, acrefootperminuteQuantity.Unit);
-
- var acrefootpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.AcreFootPerSecond);
- AssertEx.EqualTolerance(AcreFeetPerSecondInOneCubicMeterPerSecond, (double)acrefootpersecondQuantity.Value, AcreFeetPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.AcreFootPerSecond, acrefootpersecondQuantity.Unit);
-
- var centiliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CentiliterPerDay);
- AssertEx.EqualTolerance(CentilitersPerDayInOneCubicMeterPerSecond, (double)centiliterperdayQuantity.Value, CentilitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.CentiliterPerDay, centiliterperdayQuantity.Unit);
-
- var centiliterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CentiliterPerHour);
- AssertEx.EqualTolerance(CentilitersPerHourInOneCubicMeterPerSecond, (double)centiliterperhourQuantity.Value, CentilitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.CentiliterPerHour, centiliterperhourQuantity.Unit);
-
- var centiliterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CentiliterPerMinute);
- AssertEx.EqualTolerance(CentilitersPerMinuteInOneCubicMeterPerSecond, (double)centiliterperminuteQuantity.Value, CentilitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.CentiliterPerMinute, centiliterperminuteQuantity.Unit);
-
- var centiliterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CentiliterPerSecond);
- AssertEx.EqualTolerance(CentilitersPerSecondInOneCubicMeterPerSecond, (double)centiliterpersecondQuantity.Value, CentilitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.CentiliterPerSecond, centiliterpersecondQuantity.Unit);
-
- var cubiccentimeterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicCentimeterPerMinute);
- AssertEx.EqualTolerance(CubicCentimetersPerMinuteInOneCubicMeterPerSecond, (double)cubiccentimeterperminuteQuantity.Value, CubicCentimetersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.CubicCentimeterPerMinute, cubiccentimeterperminuteQuantity.Unit);
-
- var cubicdecimeterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicDecimeterPerMinute);
- AssertEx.EqualTolerance(CubicDecimetersPerMinuteInOneCubicMeterPerSecond, (double)cubicdecimeterperminuteQuantity.Value, CubicDecimetersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.CubicDecimeterPerMinute, cubicdecimeterperminuteQuantity.Unit);
-
- var cubicfootperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicFootPerHour);
- AssertEx.EqualTolerance(CubicFeetPerHourInOneCubicMeterPerSecond, (double)cubicfootperhourQuantity.Value, CubicFeetPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.CubicFootPerHour, cubicfootperhourQuantity.Unit);
-
- var cubicfootperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicFootPerMinute);
- AssertEx.EqualTolerance(CubicFeetPerMinuteInOneCubicMeterPerSecond, (double)cubicfootperminuteQuantity.Value, CubicFeetPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.CubicFootPerMinute, cubicfootperminuteQuantity.Unit);
-
- var cubicfootpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicFootPerSecond);
- AssertEx.EqualTolerance(CubicFeetPerSecondInOneCubicMeterPerSecond, (double)cubicfootpersecondQuantity.Value, CubicFeetPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.CubicFootPerSecond, cubicfootpersecondQuantity.Unit);
-
- var cubicmeterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicMeterPerDay);
- AssertEx.EqualTolerance(CubicMetersPerDayInOneCubicMeterPerSecond, (double)cubicmeterperdayQuantity.Value, CubicMetersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.CubicMeterPerDay, cubicmeterperdayQuantity.Unit);
-
- var cubicmeterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicMeterPerHour);
- AssertEx.EqualTolerance(CubicMetersPerHourInOneCubicMeterPerSecond, (double)cubicmeterperhourQuantity.Value, CubicMetersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.CubicMeterPerHour, cubicmeterperhourQuantity.Unit);
-
- var cubicmeterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicMeterPerMinute);
- AssertEx.EqualTolerance(CubicMetersPerMinuteInOneCubicMeterPerSecond, (double)cubicmeterperminuteQuantity.Value, CubicMetersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.CubicMeterPerMinute, cubicmeterperminuteQuantity.Unit);
-
- var cubicmeterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicMeterPerSecond);
- AssertEx.EqualTolerance(CubicMetersPerSecondInOneCubicMeterPerSecond, (double)cubicmeterpersecondQuantity.Value, CubicMetersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.CubicMeterPerSecond, cubicmeterpersecondQuantity.Unit);
-
- var cubicmillimeterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicMillimeterPerSecond);
- AssertEx.EqualTolerance(CubicMillimetersPerSecondInOneCubicMeterPerSecond, (double)cubicmillimeterpersecondQuantity.Value, CubicMillimetersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.CubicMillimeterPerSecond, cubicmillimeterpersecondQuantity.Unit);
-
- var cubicyardperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicYardPerDay);
- AssertEx.EqualTolerance(CubicYardsPerDayInOneCubicMeterPerSecond, (double)cubicyardperdayQuantity.Value, CubicYardsPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.CubicYardPerDay, cubicyardperdayQuantity.Unit);
-
- var cubicyardperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicYardPerHour);
- AssertEx.EqualTolerance(CubicYardsPerHourInOneCubicMeterPerSecond, (double)cubicyardperhourQuantity.Value, CubicYardsPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.CubicYardPerHour, cubicyardperhourQuantity.Unit);
-
- var cubicyardperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicYardPerMinute);
- AssertEx.EqualTolerance(CubicYardsPerMinuteInOneCubicMeterPerSecond, (double)cubicyardperminuteQuantity.Value, CubicYardsPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.CubicYardPerMinute, cubicyardperminuteQuantity.Unit);
-
- var cubicyardpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.CubicYardPerSecond);
- AssertEx.EqualTolerance(CubicYardsPerSecondInOneCubicMeterPerSecond, (double)cubicyardpersecondQuantity.Value, CubicYardsPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.CubicYardPerSecond, cubicyardpersecondQuantity.Unit);
-
- var deciliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.DeciliterPerDay);
- AssertEx.EqualTolerance(DecilitersPerDayInOneCubicMeterPerSecond, (double)deciliterperdayQuantity.Value, DecilitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.DeciliterPerDay, deciliterperdayQuantity.Unit);
-
- var deciliterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.DeciliterPerHour);
- AssertEx.EqualTolerance(DecilitersPerHourInOneCubicMeterPerSecond, (double)deciliterperhourQuantity.Value, DecilitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.DeciliterPerHour, deciliterperhourQuantity.Unit);
-
- var deciliterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.DeciliterPerMinute);
- AssertEx.EqualTolerance(DecilitersPerMinuteInOneCubicMeterPerSecond, (double)deciliterperminuteQuantity.Value, DecilitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.DeciliterPerMinute, deciliterperminuteQuantity.Unit);
-
- var deciliterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.DeciliterPerSecond);
- AssertEx.EqualTolerance(DecilitersPerSecondInOneCubicMeterPerSecond, (double)deciliterpersecondQuantity.Value, DecilitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.DeciliterPerSecond, deciliterpersecondQuantity.Unit);
-
- var kiloliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.KiloliterPerDay);
- AssertEx.EqualTolerance(KilolitersPerDayInOneCubicMeterPerSecond, (double)kiloliterperdayQuantity.Value, KilolitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.KiloliterPerDay, kiloliterperdayQuantity.Unit);
-
- var kiloliterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.KiloliterPerHour);
- AssertEx.EqualTolerance(KilolitersPerHourInOneCubicMeterPerSecond, (double)kiloliterperhourQuantity.Value, KilolitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.KiloliterPerHour, kiloliterperhourQuantity.Unit);
-
- var kiloliterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.KiloliterPerMinute);
- AssertEx.EqualTolerance(KilolitersPerMinuteInOneCubicMeterPerSecond, (double)kiloliterperminuteQuantity.Value, KilolitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.KiloliterPerMinute, kiloliterperminuteQuantity.Unit);
+ var inBaseUnits = VolumeFlow.From(1.0, VolumeFlow.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kiloliterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.KiloliterPerSecond);
- AssertEx.EqualTolerance(KilolitersPerSecondInOneCubicMeterPerSecond, (double)kiloliterpersecondQuantity.Value, KilolitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.KiloliterPerSecond, kiloliterpersecondQuantity.Unit);
-
- var kilousgallonperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.KilousGallonPerMinute);
- AssertEx.EqualTolerance(KilousGallonsPerMinuteInOneCubicMeterPerSecond, (double)kilousgallonperminuteQuantity.Value, KilousGallonsPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.KilousGallonPerMinute, kilousgallonperminuteQuantity.Unit);
-
- var literperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.LiterPerDay);
- AssertEx.EqualTolerance(LitersPerDayInOneCubicMeterPerSecond, (double)literperdayQuantity.Value, LitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.LiterPerDay, literperdayQuantity.Unit);
-
- var literperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.LiterPerHour);
- AssertEx.EqualTolerance(LitersPerHourInOneCubicMeterPerSecond, (double)literperhourQuantity.Value, LitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.LiterPerHour, literperhourQuantity.Unit);
-
- var literperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.LiterPerMinute);
- AssertEx.EqualTolerance(LitersPerMinuteInOneCubicMeterPerSecond, (double)literperminuteQuantity.Value, LitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.LiterPerMinute, literperminuteQuantity.Unit);
-
- var literpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.LiterPerSecond);
- AssertEx.EqualTolerance(LitersPerSecondInOneCubicMeterPerSecond, (double)literpersecondQuantity.Value, LitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.LiterPerSecond, literpersecondQuantity.Unit);
-
- var megaliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MegaliterPerDay);
- AssertEx.EqualTolerance(MegalitersPerDayInOneCubicMeterPerSecond, (double)megaliterperdayQuantity.Value, MegalitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.MegaliterPerDay, megaliterperdayQuantity.Unit);
-
- var megaukgallonpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MegaukGallonPerSecond);
- AssertEx.EqualTolerance(MegaukGallonsPerSecondInOneCubicMeterPerSecond, (double)megaukgallonpersecondQuantity.Value, MegaukGallonsPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.MegaukGallonPerSecond, megaukgallonpersecondQuantity.Unit);
-
- var microliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MicroliterPerDay);
- AssertEx.EqualTolerance(MicrolitersPerDayInOneCubicMeterPerSecond, (double)microliterperdayQuantity.Value, MicrolitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.MicroliterPerDay, microliterperdayQuantity.Unit);
-
- var microliterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MicroliterPerHour);
- AssertEx.EqualTolerance(MicrolitersPerHourInOneCubicMeterPerSecond, (double)microliterperhourQuantity.Value, MicrolitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.MicroliterPerHour, microliterperhourQuantity.Unit);
-
- var microliterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MicroliterPerMinute);
- AssertEx.EqualTolerance(MicrolitersPerMinuteInOneCubicMeterPerSecond, (double)microliterperminuteQuantity.Value, MicrolitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.MicroliterPerMinute, microliterperminuteQuantity.Unit);
-
- var microliterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MicroliterPerSecond);
- AssertEx.EqualTolerance(MicrolitersPerSecondInOneCubicMeterPerSecond, (double)microliterpersecondQuantity.Value, MicrolitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.MicroliterPerSecond, microliterpersecondQuantity.Unit);
-
- var milliliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MilliliterPerDay);
- AssertEx.EqualTolerance(MillilitersPerDayInOneCubicMeterPerSecond, (double)milliliterperdayQuantity.Value, MillilitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.MilliliterPerDay, milliliterperdayQuantity.Unit);
-
- var milliliterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MilliliterPerHour);
- AssertEx.EqualTolerance(MillilitersPerHourInOneCubicMeterPerSecond, (double)milliliterperhourQuantity.Value, MillilitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.MilliliterPerHour, milliliterperhourQuantity.Unit);
-
- var milliliterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MilliliterPerMinute);
- AssertEx.EqualTolerance(MillilitersPerMinuteInOneCubicMeterPerSecond, (double)milliliterperminuteQuantity.Value, MillilitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.MilliliterPerMinute, milliliterperminuteQuantity.Unit);
-
- var milliliterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MilliliterPerSecond);
- AssertEx.EqualTolerance(MillilitersPerSecondInOneCubicMeterPerSecond, (double)milliliterpersecondQuantity.Value, MillilitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.MilliliterPerSecond, milliliterpersecondQuantity.Unit);
-
- var millionusgallonsperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.MillionUsGallonsPerDay);
- AssertEx.EqualTolerance(MillionUsGallonsPerDayInOneCubicMeterPerSecond, (double)millionusgallonsperdayQuantity.Value, MillionUsGallonsPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.MillionUsGallonsPerDay, millionusgallonsperdayQuantity.Unit);
-
- var nanoliterperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.NanoliterPerDay);
- AssertEx.EqualTolerance(NanolitersPerDayInOneCubicMeterPerSecond, (double)nanoliterperdayQuantity.Value, NanolitersPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.NanoliterPerDay, nanoliterperdayQuantity.Unit);
-
- var nanoliterperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.NanoliterPerHour);
- AssertEx.EqualTolerance(NanolitersPerHourInOneCubicMeterPerSecond, (double)nanoliterperhourQuantity.Value, NanolitersPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.NanoliterPerHour, nanoliterperhourQuantity.Unit);
-
- var nanoliterperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.NanoliterPerMinute);
- AssertEx.EqualTolerance(NanolitersPerMinuteInOneCubicMeterPerSecond, (double)nanoliterperminuteQuantity.Value, NanolitersPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.NanoliterPerMinute, nanoliterperminuteQuantity.Unit);
-
- var nanoliterpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.NanoliterPerSecond);
- AssertEx.EqualTolerance(NanolitersPerSecondInOneCubicMeterPerSecond, (double)nanoliterpersecondQuantity.Value, NanolitersPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.NanoliterPerSecond, nanoliterpersecondQuantity.Unit);
-
- var oilbarrelperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.OilBarrelPerDay);
- AssertEx.EqualTolerance(OilBarrelsPerDayInOneCubicMeterPerSecond, (double)oilbarrelperdayQuantity.Value, OilBarrelsPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.OilBarrelPerDay, oilbarrelperdayQuantity.Unit);
-
- var oilbarrelperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.OilBarrelPerHour);
- AssertEx.EqualTolerance(OilBarrelsPerHourInOneCubicMeterPerSecond, (double)oilbarrelperhourQuantity.Value, OilBarrelsPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.OilBarrelPerHour, oilbarrelperhourQuantity.Unit);
-
- var oilbarrelperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.OilBarrelPerMinute);
- AssertEx.EqualTolerance(OilBarrelsPerMinuteInOneCubicMeterPerSecond, (double)oilbarrelperminuteQuantity.Value, OilBarrelsPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.OilBarrelPerMinute, oilbarrelperminuteQuantity.Unit);
-
- var oilbarrelpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.OilBarrelPerSecond);
- AssertEx.EqualTolerance(OilBarrelsPerSecondInOneCubicMeterPerSecond, (double)oilbarrelpersecondQuantity.Value, OilBarrelsPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.OilBarrelPerSecond, oilbarrelpersecondQuantity.Unit);
-
- var ukgallonperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UkGallonPerDay);
- AssertEx.EqualTolerance(UkGallonsPerDayInOneCubicMeterPerSecond, (double)ukgallonperdayQuantity.Value, UkGallonsPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.UkGallonPerDay, ukgallonperdayQuantity.Unit);
-
- var ukgallonperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UkGallonPerHour);
- AssertEx.EqualTolerance(UkGallonsPerHourInOneCubicMeterPerSecond, (double)ukgallonperhourQuantity.Value, UkGallonsPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.UkGallonPerHour, ukgallonperhourQuantity.Unit);
-
- var ukgallonperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UkGallonPerMinute);
- AssertEx.EqualTolerance(UkGallonsPerMinuteInOneCubicMeterPerSecond, (double)ukgallonperminuteQuantity.Value, UkGallonsPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.UkGallonPerMinute, ukgallonperminuteQuantity.Unit);
-
- var ukgallonpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UkGallonPerSecond);
- AssertEx.EqualTolerance(UkGallonsPerSecondInOneCubicMeterPerSecond, (double)ukgallonpersecondQuantity.Value, UkGallonsPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.UkGallonPerSecond, ukgallonpersecondQuantity.Unit);
-
- var usgallonperdayQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UsGallonPerDay);
- AssertEx.EqualTolerance(UsGallonsPerDayInOneCubicMeterPerSecond, (double)usgallonperdayQuantity.Value, UsGallonsPerDayTolerance);
- Assert.Equal(VolumeFlowUnit.UsGallonPerDay, usgallonperdayQuantity.Unit);
-
- var usgallonperhourQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UsGallonPerHour);
- AssertEx.EqualTolerance(UsGallonsPerHourInOneCubicMeterPerSecond, (double)usgallonperhourQuantity.Value, UsGallonsPerHourTolerance);
- Assert.Equal(VolumeFlowUnit.UsGallonPerHour, usgallonperhourQuantity.Unit);
-
- var usgallonperminuteQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UsGallonPerMinute);
- AssertEx.EqualTolerance(UsGallonsPerMinuteInOneCubicMeterPerSecond, (double)usgallonperminuteQuantity.Value, UsGallonsPerMinuteTolerance);
- Assert.Equal(VolumeFlowUnit.UsGallonPerMinute, usgallonperminuteQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var usgallonpersecondQuantity = cubicmeterpersecond.ToUnit(VolumeFlowUnit.UsGallonPerSecond);
- AssertEx.EqualTolerance(UsGallonsPerSecondInOneCubicMeterPerSecond, (double)usgallonpersecondQuantity.Value, UsGallonsPerSecondTolerance);
- Assert.Equal(VolumeFlowUnit.UsGallonPerSecond, usgallonpersecondQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VolumeFlowUnit unit)
+ {
+ var quantity = VolumeFlow.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VolumeFlowUnit unit)
{
- var quantityInBaseUnit = VolumeFlow.FromCubicMetersPerSecond(1).ToBaseUnit();
- Assert.Equal(VolumeFlow.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = VolumeFlow.Units.FirstOrDefault(u => u != VolumeFlow.BaseUnit && u != VolumeFlowUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VolumeFlowUnit.Undefined)
+ fromUnit = VolumeFlow.BaseUnit;
+
+ var quantity = VolumeFlow.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumePerLengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumePerLengthTestsBase.g.cs
index ce74461e4f..99b3b19301 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumePerLengthTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumePerLengthTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -55,6 +56,32 @@ public abstract partial class VolumePerLengthTestsBase : QuantityTestsBase
protected virtual double OilBarrelsPerFootTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VolumePerLengthUnit unit)
+ {
+ return unit switch
+ {
+ VolumePerLengthUnit.CubicMeterPerMeter => (CubicMetersPerMeterInOneCubicMeterPerMeter, CubicMetersPerMeterTolerance),
+ VolumePerLengthUnit.CubicYardPerFoot => (CubicYardsPerFootInOneCubicMeterPerMeter, CubicYardsPerFootTolerance),
+ VolumePerLengthUnit.CubicYardPerUsSurveyFoot => (CubicYardsPerUsSurveyFootInOneCubicMeterPerMeter, CubicYardsPerUsSurveyFootTolerance),
+ VolumePerLengthUnit.LiterPerKilometer => (LitersPerKilometerInOneCubicMeterPerMeter, LitersPerKilometerTolerance),
+ VolumePerLengthUnit.LiterPerMeter => (LitersPerMeterInOneCubicMeterPerMeter, LitersPerMeterTolerance),
+ VolumePerLengthUnit.LiterPerMillimeter => (LitersPerMillimeterInOneCubicMeterPerMeter, LitersPerMillimeterTolerance),
+ VolumePerLengthUnit.OilBarrelPerFoot => (OilBarrelsPerFootInOneCubicMeterPerMeter, OilBarrelsPerFootTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VolumePerLengthUnit.CubicMeterPerMeter },
+ new object[] { VolumePerLengthUnit.CubicYardPerFoot },
+ new object[] { VolumePerLengthUnit.CubicYardPerUsSurveyFoot },
+ new object[] { VolumePerLengthUnit.LiterPerKilometer },
+ new object[] { VolumePerLengthUnit.LiterPerMeter },
+ new object[] { VolumePerLengthUnit.LiterPerMillimeter },
+ new object[] { VolumePerLengthUnit.OilBarrelPerFoot },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -212,45 +239,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VolumePerLengthUnit unit)
{
- var cubicmeterpermeter = VolumePerLength.FromCubicMetersPerMeter(1);
-
- var cubicmeterpermeterQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.CubicMeterPerMeter);
- AssertEx.EqualTolerance(CubicMetersPerMeterInOneCubicMeterPerMeter, (double)cubicmeterpermeterQuantity.Value, CubicMetersPerMeterTolerance);
- Assert.Equal(VolumePerLengthUnit.CubicMeterPerMeter, cubicmeterpermeterQuantity.Unit);
-
- var cubicyardperfootQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.CubicYardPerFoot);
- AssertEx.EqualTolerance(CubicYardsPerFootInOneCubicMeterPerMeter, (double)cubicyardperfootQuantity.Value, CubicYardsPerFootTolerance);
- Assert.Equal(VolumePerLengthUnit.CubicYardPerFoot, cubicyardperfootQuantity.Unit);
+ var inBaseUnits = VolumePerLength.From(1.0, VolumePerLength.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var cubicyardperussurveyfootQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.CubicYardPerUsSurveyFoot);
- AssertEx.EqualTolerance(CubicYardsPerUsSurveyFootInOneCubicMeterPerMeter, (double)cubicyardperussurveyfootQuantity.Value, CubicYardsPerUsSurveyFootTolerance);
- Assert.Equal(VolumePerLengthUnit.CubicYardPerUsSurveyFoot, cubicyardperussurveyfootQuantity.Unit);
-
- var literperkilometerQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.LiterPerKilometer);
- AssertEx.EqualTolerance(LitersPerKilometerInOneCubicMeterPerMeter, (double)literperkilometerQuantity.Value, LitersPerKilometerTolerance);
- Assert.Equal(VolumePerLengthUnit.LiterPerKilometer, literperkilometerQuantity.Unit);
-
- var literpermeterQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.LiterPerMeter);
- AssertEx.EqualTolerance(LitersPerMeterInOneCubicMeterPerMeter, (double)literpermeterQuantity.Value, LitersPerMeterTolerance);
- Assert.Equal(VolumePerLengthUnit.LiterPerMeter, literpermeterQuantity.Unit);
-
- var literpermillimeterQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.LiterPerMillimeter);
- AssertEx.EqualTolerance(LitersPerMillimeterInOneCubicMeterPerMeter, (double)literpermillimeterQuantity.Value, LitersPerMillimeterTolerance);
- Assert.Equal(VolumePerLengthUnit.LiterPerMillimeter, literpermillimeterQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var oilbarrelperfootQuantity = cubicmeterpermeter.ToUnit(VolumePerLengthUnit.OilBarrelPerFoot);
- AssertEx.EqualTolerance(OilBarrelsPerFootInOneCubicMeterPerMeter, (double)oilbarrelperfootQuantity.Value, OilBarrelsPerFootTolerance);
- Assert.Equal(VolumePerLengthUnit.OilBarrelPerFoot, oilbarrelperfootQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VolumePerLengthUnit unit)
+ {
+ var quantity = VolumePerLength.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VolumePerLengthUnit unit)
{
- var quantityInBaseUnit = VolumePerLength.FromCubicMetersPerMeter(1).ToBaseUnit();
- Assert.Equal(VolumePerLength.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = VolumePerLength.Units.FirstOrDefault(u => u != VolumePerLength.BaseUnit && u != VolumePerLengthUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VolumePerLengthUnit.Undefined)
+ fromUnit = VolumePerLength.BaseUnit;
+
+ var quantity = VolumePerLength.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeTestsBase.g.cs
index 7012708187..53acb7dd64 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumeTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -143,6 +144,120 @@ public abstract partial class VolumeTestsBase : QuantityTestsBase
protected virtual double UsTeaspoonsTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VolumeUnit unit)
+ {
+ return unit switch
+ {
+ VolumeUnit.AcreFoot => (AcreFeetInOneCubicMeter, AcreFeetTolerance),
+ VolumeUnit.AuTablespoon => (AuTablespoonsInOneCubicMeter, AuTablespoonsTolerance),
+ VolumeUnit.BoardFoot => (BoardFeetInOneCubicMeter, BoardFeetTolerance),
+ VolumeUnit.Centiliter => (CentilitersInOneCubicMeter, CentilitersTolerance),
+ VolumeUnit.CubicCentimeter => (CubicCentimetersInOneCubicMeter, CubicCentimetersTolerance),
+ VolumeUnit.CubicDecimeter => (CubicDecimetersInOneCubicMeter, CubicDecimetersTolerance),
+ VolumeUnit.CubicFoot => (CubicFeetInOneCubicMeter, CubicFeetTolerance),
+ VolumeUnit.CubicHectometer => (CubicHectometersInOneCubicMeter, CubicHectometersTolerance),
+ VolumeUnit.CubicInch => (CubicInchesInOneCubicMeter, CubicInchesTolerance),
+ VolumeUnit.CubicKilometer => (CubicKilometersInOneCubicMeter, CubicKilometersTolerance),
+ VolumeUnit.CubicMeter => (CubicMetersInOneCubicMeter, CubicMetersTolerance),
+ VolumeUnit.CubicMicrometer => (CubicMicrometersInOneCubicMeter, CubicMicrometersTolerance),
+ VolumeUnit.CubicMile => (CubicMilesInOneCubicMeter, CubicMilesTolerance),
+ VolumeUnit.CubicMillimeter => (CubicMillimetersInOneCubicMeter, CubicMillimetersTolerance),
+ VolumeUnit.CubicYard => (CubicYardsInOneCubicMeter, CubicYardsTolerance),
+ VolumeUnit.DecausGallon => (DecausGallonsInOneCubicMeter, DecausGallonsTolerance),
+ VolumeUnit.Deciliter => (DecilitersInOneCubicMeter, DecilitersTolerance),
+ VolumeUnit.DeciusGallon => (DeciusGallonsInOneCubicMeter, DeciusGallonsTolerance),
+ VolumeUnit.HectocubicFoot => (HectocubicFeetInOneCubicMeter, HectocubicFeetTolerance),
+ VolumeUnit.HectocubicMeter => (HectocubicMetersInOneCubicMeter, HectocubicMetersTolerance),
+ VolumeUnit.Hectoliter => (HectolitersInOneCubicMeter, HectolitersTolerance),
+ VolumeUnit.HectousGallon => (HectousGallonsInOneCubicMeter, HectousGallonsTolerance),
+ VolumeUnit.ImperialBeerBarrel => (ImperialBeerBarrelsInOneCubicMeter, ImperialBeerBarrelsTolerance),
+ VolumeUnit.ImperialGallon => (ImperialGallonsInOneCubicMeter, ImperialGallonsTolerance),
+ VolumeUnit.ImperialOunce => (ImperialOuncesInOneCubicMeter, ImperialOuncesTolerance),
+ VolumeUnit.ImperialPint => (ImperialPintsInOneCubicMeter, ImperialPintsTolerance),
+ VolumeUnit.KilocubicFoot => (KilocubicFeetInOneCubicMeter, KilocubicFeetTolerance),
+ VolumeUnit.KilocubicMeter => (KilocubicMetersInOneCubicMeter, KilocubicMetersTolerance),
+ VolumeUnit.KiloimperialGallon => (KiloimperialGallonsInOneCubicMeter, KiloimperialGallonsTolerance),
+ VolumeUnit.Kiloliter => (KilolitersInOneCubicMeter, KilolitersTolerance),
+ VolumeUnit.KilousGallon => (KilousGallonsInOneCubicMeter, KilousGallonsTolerance),
+ VolumeUnit.Liter => (LitersInOneCubicMeter, LitersTolerance),
+ VolumeUnit.MegacubicFoot => (MegacubicFeetInOneCubicMeter, MegacubicFeetTolerance),
+ VolumeUnit.MegaimperialGallon => (MegaimperialGallonsInOneCubicMeter, MegaimperialGallonsTolerance),
+ VolumeUnit.Megaliter => (MegalitersInOneCubicMeter, MegalitersTolerance),
+ VolumeUnit.MegausGallon => (MegausGallonsInOneCubicMeter, MegausGallonsTolerance),
+ VolumeUnit.MetricCup => (MetricCupsInOneCubicMeter, MetricCupsTolerance),
+ VolumeUnit.MetricTeaspoon => (MetricTeaspoonsInOneCubicMeter, MetricTeaspoonsTolerance),
+ VolumeUnit.Microliter => (MicrolitersInOneCubicMeter, MicrolitersTolerance),
+ VolumeUnit.Milliliter => (MillilitersInOneCubicMeter, MillilitersTolerance),
+ VolumeUnit.OilBarrel => (OilBarrelsInOneCubicMeter, OilBarrelsTolerance),
+ VolumeUnit.UkTablespoon => (UkTablespoonsInOneCubicMeter, UkTablespoonsTolerance),
+ VolumeUnit.UsBeerBarrel => (UsBeerBarrelsInOneCubicMeter, UsBeerBarrelsTolerance),
+ VolumeUnit.UsCustomaryCup => (UsCustomaryCupsInOneCubicMeter, UsCustomaryCupsTolerance),
+ VolumeUnit.UsGallon => (UsGallonsInOneCubicMeter, UsGallonsTolerance),
+ VolumeUnit.UsLegalCup => (UsLegalCupsInOneCubicMeter, UsLegalCupsTolerance),
+ VolumeUnit.UsOunce => (UsOuncesInOneCubicMeter, UsOuncesTolerance),
+ VolumeUnit.UsPint => (UsPintsInOneCubicMeter, UsPintsTolerance),
+ VolumeUnit.UsQuart => (UsQuartsInOneCubicMeter, UsQuartsTolerance),
+ VolumeUnit.UsTablespoon => (UsTablespoonsInOneCubicMeter, UsTablespoonsTolerance),
+ VolumeUnit.UsTeaspoon => (UsTeaspoonsInOneCubicMeter, UsTeaspoonsTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VolumeUnit.AcreFoot },
+ new object[] { VolumeUnit.AuTablespoon },
+ new object[] { VolumeUnit.BoardFoot },
+ new object[] { VolumeUnit.Centiliter },
+ new object[] { VolumeUnit.CubicCentimeter },
+ new object[] { VolumeUnit.CubicDecimeter },
+ new object[] { VolumeUnit.CubicFoot },
+ new object[] { VolumeUnit.CubicHectometer },
+ new object[] { VolumeUnit.CubicInch },
+ new object[] { VolumeUnit.CubicKilometer },
+ new object[] { VolumeUnit.CubicMeter },
+ new object[] { VolumeUnit.CubicMicrometer },
+ new object[] { VolumeUnit.CubicMile },
+ new object[] { VolumeUnit.CubicMillimeter },
+ new object[] { VolumeUnit.CubicYard },
+ new object[] { VolumeUnit.DecausGallon },
+ new object[] { VolumeUnit.Deciliter },
+ new object[] { VolumeUnit.DeciusGallon },
+ new object[] { VolumeUnit.HectocubicFoot },
+ new object[] { VolumeUnit.HectocubicMeter },
+ new object[] { VolumeUnit.Hectoliter },
+ new object[] { VolumeUnit.HectousGallon },
+ new object[] { VolumeUnit.ImperialBeerBarrel },
+ new object[] { VolumeUnit.ImperialGallon },
+ new object[] { VolumeUnit.ImperialOunce },
+ new object[] { VolumeUnit.ImperialPint },
+ new object[] { VolumeUnit.KilocubicFoot },
+ new object[] { VolumeUnit.KilocubicMeter },
+ new object[] { VolumeUnit.KiloimperialGallon },
+ new object[] { VolumeUnit.Kiloliter },
+ new object[] { VolumeUnit.KilousGallon },
+ new object[] { VolumeUnit.Liter },
+ new object[] { VolumeUnit.MegacubicFoot },
+ new object[] { VolumeUnit.MegaimperialGallon },
+ new object[] { VolumeUnit.Megaliter },
+ new object[] { VolumeUnit.MegausGallon },
+ new object[] { VolumeUnit.MetricCup },
+ new object[] { VolumeUnit.MetricTeaspoon },
+ new object[] { VolumeUnit.Microliter },
+ new object[] { VolumeUnit.Milliliter },
+ new object[] { VolumeUnit.OilBarrel },
+ new object[] { VolumeUnit.UkTablespoon },
+ new object[] { VolumeUnit.UsBeerBarrel },
+ new object[] { VolumeUnit.UsCustomaryCup },
+ new object[] { VolumeUnit.UsGallon },
+ new object[] { VolumeUnit.UsLegalCup },
+ new object[] { VolumeUnit.UsOunce },
+ new object[] { VolumeUnit.UsPint },
+ new object[] { VolumeUnit.UsQuart },
+ new object[] { VolumeUnit.UsTablespoon },
+ new object[] { VolumeUnit.UsTeaspoon },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -564,221 +679,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VolumeUnit unit)
{
- var cubicmeter = Volume.FromCubicMeters(1);
-
- var acrefootQuantity = cubicmeter.ToUnit(VolumeUnit.AcreFoot);
- AssertEx.EqualTolerance(AcreFeetInOneCubicMeter, (double)acrefootQuantity.Value, AcreFeetTolerance);
- Assert.Equal(VolumeUnit.AcreFoot, acrefootQuantity.Unit);
-
- var autablespoonQuantity = cubicmeter.ToUnit(VolumeUnit.AuTablespoon);
- AssertEx.EqualTolerance(AuTablespoonsInOneCubicMeter, (double)autablespoonQuantity.Value, AuTablespoonsTolerance);
- Assert.Equal(VolumeUnit.AuTablespoon, autablespoonQuantity.Unit);
-
- var boardfootQuantity = cubicmeter.ToUnit(VolumeUnit.BoardFoot);
- AssertEx.EqualTolerance(BoardFeetInOneCubicMeter, (double)boardfootQuantity.Value, BoardFeetTolerance);
- Assert.Equal(VolumeUnit.BoardFoot, boardfootQuantity.Unit);
-
- var centiliterQuantity = cubicmeter.ToUnit(VolumeUnit.Centiliter);
- AssertEx.EqualTolerance(CentilitersInOneCubicMeter, (double)centiliterQuantity.Value, CentilitersTolerance);
- Assert.Equal(VolumeUnit.Centiliter, centiliterQuantity.Unit);
-
- var cubiccentimeterQuantity = cubicmeter.ToUnit(VolumeUnit.CubicCentimeter);
- AssertEx.EqualTolerance(CubicCentimetersInOneCubicMeter, (double)cubiccentimeterQuantity.Value, CubicCentimetersTolerance);
- Assert.Equal(VolumeUnit.CubicCentimeter, cubiccentimeterQuantity.Unit);
-
- var cubicdecimeterQuantity = cubicmeter.ToUnit(VolumeUnit.CubicDecimeter);
- AssertEx.EqualTolerance(CubicDecimetersInOneCubicMeter, (double)cubicdecimeterQuantity.Value, CubicDecimetersTolerance);
- Assert.Equal(VolumeUnit.CubicDecimeter, cubicdecimeterQuantity.Unit);
-
- var cubicfootQuantity = cubicmeter.ToUnit(VolumeUnit.CubicFoot);
- AssertEx.EqualTolerance(CubicFeetInOneCubicMeter, (double)cubicfootQuantity.Value, CubicFeetTolerance);
- Assert.Equal(VolumeUnit.CubicFoot, cubicfootQuantity.Unit);
-
- var cubichectometerQuantity = cubicmeter.ToUnit(VolumeUnit.CubicHectometer);
- AssertEx.EqualTolerance(CubicHectometersInOneCubicMeter, (double)cubichectometerQuantity.Value, CubicHectometersTolerance);
- Assert.Equal(VolumeUnit.CubicHectometer, cubichectometerQuantity.Unit);
-
- var cubicinchQuantity = cubicmeter.ToUnit(VolumeUnit.CubicInch);
- AssertEx.EqualTolerance(CubicInchesInOneCubicMeter, (double)cubicinchQuantity.Value, CubicInchesTolerance);
- Assert.Equal(VolumeUnit.CubicInch, cubicinchQuantity.Unit);
-
- var cubickilometerQuantity = cubicmeter.ToUnit(VolumeUnit.CubicKilometer);
- AssertEx.EqualTolerance(CubicKilometersInOneCubicMeter, (double)cubickilometerQuantity.Value, CubicKilometersTolerance);
- Assert.Equal(VolumeUnit.CubicKilometer, cubickilometerQuantity.Unit);
-
- var cubicmeterQuantity = cubicmeter.ToUnit(VolumeUnit.CubicMeter);
- AssertEx.EqualTolerance(CubicMetersInOneCubicMeter, (double)cubicmeterQuantity.Value, CubicMetersTolerance);
- Assert.Equal(VolumeUnit.CubicMeter, cubicmeterQuantity.Unit);
-
- var cubicmicrometerQuantity = cubicmeter.ToUnit(VolumeUnit.CubicMicrometer);
- AssertEx.EqualTolerance(CubicMicrometersInOneCubicMeter, (double)cubicmicrometerQuantity.Value, CubicMicrometersTolerance);
- Assert.Equal(VolumeUnit.CubicMicrometer, cubicmicrometerQuantity.Unit);
-
- var cubicmileQuantity = cubicmeter.ToUnit(VolumeUnit.CubicMile);
- AssertEx.EqualTolerance(CubicMilesInOneCubicMeter, (double)cubicmileQuantity.Value, CubicMilesTolerance);
- Assert.Equal(VolumeUnit.CubicMile, cubicmileQuantity.Unit);
-
- var cubicmillimeterQuantity = cubicmeter.ToUnit(VolumeUnit.CubicMillimeter);
- AssertEx.EqualTolerance(CubicMillimetersInOneCubicMeter, (double)cubicmillimeterQuantity.Value, CubicMillimetersTolerance);
- Assert.Equal(VolumeUnit.CubicMillimeter, cubicmillimeterQuantity.Unit);
-
- var cubicyardQuantity = cubicmeter.ToUnit(VolumeUnit.CubicYard);
- AssertEx.EqualTolerance(CubicYardsInOneCubicMeter, (double)cubicyardQuantity.Value, CubicYardsTolerance);
- Assert.Equal(VolumeUnit.CubicYard, cubicyardQuantity.Unit);
-
- var decausgallonQuantity = cubicmeter.ToUnit(VolumeUnit.DecausGallon);
- AssertEx.EqualTolerance(DecausGallonsInOneCubicMeter, (double)decausgallonQuantity.Value, DecausGallonsTolerance);
- Assert.Equal(VolumeUnit.DecausGallon, decausgallonQuantity.Unit);
-
- var deciliterQuantity = cubicmeter.ToUnit(VolumeUnit.Deciliter);
- AssertEx.EqualTolerance(DecilitersInOneCubicMeter, (double)deciliterQuantity.Value, DecilitersTolerance);
- Assert.Equal(VolumeUnit.Deciliter, deciliterQuantity.Unit);
-
- var deciusgallonQuantity = cubicmeter.ToUnit(VolumeUnit.DeciusGallon);
- AssertEx.EqualTolerance(DeciusGallonsInOneCubicMeter, (double)deciusgallonQuantity.Value, DeciusGallonsTolerance);
- Assert.Equal(VolumeUnit.DeciusGallon, deciusgallonQuantity.Unit);
-
- var hectocubicfootQuantity = cubicmeter.ToUnit(VolumeUnit.HectocubicFoot);
- AssertEx.EqualTolerance(HectocubicFeetInOneCubicMeter, (double)hectocubicfootQuantity.Value, HectocubicFeetTolerance);
- Assert.Equal(VolumeUnit.HectocubicFoot, hectocubicfootQuantity.Unit);
-
- var hectocubicmeterQuantity = cubicmeter.ToUnit(VolumeUnit.HectocubicMeter);
- AssertEx.EqualTolerance(HectocubicMetersInOneCubicMeter, (double)hectocubicmeterQuantity.Value, HectocubicMetersTolerance);
- Assert.Equal(VolumeUnit.HectocubicMeter, hectocubicmeterQuantity.Unit);
-
- var hectoliterQuantity = cubicmeter.ToUnit(VolumeUnit.Hectoliter);
- AssertEx.EqualTolerance(HectolitersInOneCubicMeter, (double)hectoliterQuantity.Value, HectolitersTolerance);
- Assert.Equal(VolumeUnit.Hectoliter, hectoliterQuantity.Unit);
-
- var hectousgallonQuantity = cubicmeter.ToUnit(VolumeUnit.HectousGallon);
- AssertEx.EqualTolerance(HectousGallonsInOneCubicMeter, (double)hectousgallonQuantity.Value, HectousGallonsTolerance);
- Assert.Equal(VolumeUnit.HectousGallon, hectousgallonQuantity.Unit);
-
- var imperialbeerbarrelQuantity = cubicmeter.ToUnit(VolumeUnit.ImperialBeerBarrel);
- AssertEx.EqualTolerance(ImperialBeerBarrelsInOneCubicMeter, (double)imperialbeerbarrelQuantity.Value, ImperialBeerBarrelsTolerance);
- Assert.Equal(VolumeUnit.ImperialBeerBarrel, imperialbeerbarrelQuantity.Unit);
-
- var imperialgallonQuantity = cubicmeter.ToUnit(VolumeUnit.ImperialGallon);
- AssertEx.EqualTolerance(ImperialGallonsInOneCubicMeter, (double)imperialgallonQuantity.Value, ImperialGallonsTolerance);
- Assert.Equal(VolumeUnit.ImperialGallon, imperialgallonQuantity.Unit);
+ var inBaseUnits = Volume.From(1.0, Volume.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var imperialounceQuantity = cubicmeter.ToUnit(VolumeUnit.ImperialOunce);
- AssertEx.EqualTolerance(ImperialOuncesInOneCubicMeter, (double)imperialounceQuantity.Value, ImperialOuncesTolerance);
- Assert.Equal(VolumeUnit.ImperialOunce, imperialounceQuantity.Unit);
-
- var imperialpintQuantity = cubicmeter.ToUnit(VolumeUnit.ImperialPint);
- AssertEx.EqualTolerance(ImperialPintsInOneCubicMeter, (double)imperialpintQuantity.Value, ImperialPintsTolerance);
- Assert.Equal(VolumeUnit.ImperialPint, imperialpintQuantity.Unit);
-
- var kilocubicfootQuantity = cubicmeter.ToUnit(VolumeUnit.KilocubicFoot);
- AssertEx.EqualTolerance(KilocubicFeetInOneCubicMeter, (double)kilocubicfootQuantity.Value, KilocubicFeetTolerance);
- Assert.Equal(VolumeUnit.KilocubicFoot, kilocubicfootQuantity.Unit);
-
- var kilocubicmeterQuantity = cubicmeter.ToUnit(VolumeUnit.KilocubicMeter);
- AssertEx.EqualTolerance(KilocubicMetersInOneCubicMeter, (double)kilocubicmeterQuantity.Value, KilocubicMetersTolerance);
- Assert.Equal(VolumeUnit.KilocubicMeter, kilocubicmeterQuantity.Unit);
-
- var kiloimperialgallonQuantity = cubicmeter.ToUnit(VolumeUnit.KiloimperialGallon);
- AssertEx.EqualTolerance(KiloimperialGallonsInOneCubicMeter, (double)kiloimperialgallonQuantity.Value, KiloimperialGallonsTolerance);
- Assert.Equal(VolumeUnit.KiloimperialGallon, kiloimperialgallonQuantity.Unit);
-
- var kiloliterQuantity = cubicmeter.ToUnit(VolumeUnit.Kiloliter);
- AssertEx.EqualTolerance(KilolitersInOneCubicMeter, (double)kiloliterQuantity.Value, KilolitersTolerance);
- Assert.Equal(VolumeUnit.Kiloliter, kiloliterQuantity.Unit);
-
- var kilousgallonQuantity = cubicmeter.ToUnit(VolumeUnit.KilousGallon);
- AssertEx.EqualTolerance(KilousGallonsInOneCubicMeter, (double)kilousgallonQuantity.Value, KilousGallonsTolerance);
- Assert.Equal(VolumeUnit.KilousGallon, kilousgallonQuantity.Unit);
-
- var literQuantity = cubicmeter.ToUnit(VolumeUnit.Liter);
- AssertEx.EqualTolerance(LitersInOneCubicMeter, (double)literQuantity.Value, LitersTolerance);
- Assert.Equal(VolumeUnit.Liter, literQuantity.Unit);
-
- var megacubicfootQuantity = cubicmeter.ToUnit(VolumeUnit.MegacubicFoot);
- AssertEx.EqualTolerance(MegacubicFeetInOneCubicMeter, (double)megacubicfootQuantity.Value, MegacubicFeetTolerance);
- Assert.Equal(VolumeUnit.MegacubicFoot, megacubicfootQuantity.Unit);
-
- var megaimperialgallonQuantity = cubicmeter.ToUnit(VolumeUnit.MegaimperialGallon);
- AssertEx.EqualTolerance(MegaimperialGallonsInOneCubicMeter, (double)megaimperialgallonQuantity.Value, MegaimperialGallonsTolerance);
- Assert.Equal(VolumeUnit.MegaimperialGallon, megaimperialgallonQuantity.Unit);
-
- var megaliterQuantity = cubicmeter.ToUnit(VolumeUnit.Megaliter);
- AssertEx.EqualTolerance(MegalitersInOneCubicMeter, (double)megaliterQuantity.Value, MegalitersTolerance);
- Assert.Equal(VolumeUnit.Megaliter, megaliterQuantity.Unit);
-
- var megausgallonQuantity = cubicmeter.ToUnit(VolumeUnit.MegausGallon);
- AssertEx.EqualTolerance(MegausGallonsInOneCubicMeter, (double)megausgallonQuantity.Value, MegausGallonsTolerance);
- Assert.Equal(VolumeUnit.MegausGallon, megausgallonQuantity.Unit);
-
- var metriccupQuantity = cubicmeter.ToUnit(VolumeUnit.MetricCup);
- AssertEx.EqualTolerance(MetricCupsInOneCubicMeter, (double)metriccupQuantity.Value, MetricCupsTolerance);
- Assert.Equal(VolumeUnit.MetricCup, metriccupQuantity.Unit);
-
- var metricteaspoonQuantity = cubicmeter.ToUnit(VolumeUnit.MetricTeaspoon);
- AssertEx.EqualTolerance(MetricTeaspoonsInOneCubicMeter, (double)metricteaspoonQuantity.Value, MetricTeaspoonsTolerance);
- Assert.Equal(VolumeUnit.MetricTeaspoon, metricteaspoonQuantity.Unit);
-
- var microliterQuantity = cubicmeter.ToUnit(VolumeUnit.Microliter);
- AssertEx.EqualTolerance(MicrolitersInOneCubicMeter, (double)microliterQuantity.Value, MicrolitersTolerance);
- Assert.Equal(VolumeUnit.Microliter, microliterQuantity.Unit);
-
- var milliliterQuantity = cubicmeter.ToUnit(VolumeUnit.Milliliter);
- AssertEx.EqualTolerance(MillilitersInOneCubicMeter, (double)milliliterQuantity.Value, MillilitersTolerance);
- Assert.Equal(VolumeUnit.Milliliter, milliliterQuantity.Unit);
-
- var oilbarrelQuantity = cubicmeter.ToUnit(VolumeUnit.OilBarrel);
- AssertEx.EqualTolerance(OilBarrelsInOneCubicMeter, (double)oilbarrelQuantity.Value, OilBarrelsTolerance);
- Assert.Equal(VolumeUnit.OilBarrel, oilbarrelQuantity.Unit);
-
- var uktablespoonQuantity = cubicmeter.ToUnit(VolumeUnit.UkTablespoon);
- AssertEx.EqualTolerance(UkTablespoonsInOneCubicMeter, (double)uktablespoonQuantity.Value, UkTablespoonsTolerance);
- Assert.Equal(VolumeUnit.UkTablespoon, uktablespoonQuantity.Unit);
-
- var usbeerbarrelQuantity = cubicmeter.ToUnit(VolumeUnit.UsBeerBarrel);
- AssertEx.EqualTolerance(UsBeerBarrelsInOneCubicMeter, (double)usbeerbarrelQuantity.Value, UsBeerBarrelsTolerance);
- Assert.Equal(VolumeUnit.UsBeerBarrel, usbeerbarrelQuantity.Unit);
-
- var uscustomarycupQuantity = cubicmeter.ToUnit(VolumeUnit.UsCustomaryCup);
- AssertEx.EqualTolerance(UsCustomaryCupsInOneCubicMeter, (double)uscustomarycupQuantity.Value, UsCustomaryCupsTolerance);
- Assert.Equal(VolumeUnit.UsCustomaryCup, uscustomarycupQuantity.Unit);
-
- var usgallonQuantity = cubicmeter.ToUnit(VolumeUnit.UsGallon);
- AssertEx.EqualTolerance(UsGallonsInOneCubicMeter, (double)usgallonQuantity.Value, UsGallonsTolerance);
- Assert.Equal(VolumeUnit.UsGallon, usgallonQuantity.Unit);
-
- var uslegalcupQuantity = cubicmeter.ToUnit(VolumeUnit.UsLegalCup);
- AssertEx.EqualTolerance(UsLegalCupsInOneCubicMeter, (double)uslegalcupQuantity.Value, UsLegalCupsTolerance);
- Assert.Equal(VolumeUnit.UsLegalCup, uslegalcupQuantity.Unit);
-
- var usounceQuantity = cubicmeter.ToUnit(VolumeUnit.UsOunce);
- AssertEx.EqualTolerance(UsOuncesInOneCubicMeter, (double)usounceQuantity.Value, UsOuncesTolerance);
- Assert.Equal(VolumeUnit.UsOunce, usounceQuantity.Unit);
-
- var uspintQuantity = cubicmeter.ToUnit(VolumeUnit.UsPint);
- AssertEx.EqualTolerance(UsPintsInOneCubicMeter, (double)uspintQuantity.Value, UsPintsTolerance);
- Assert.Equal(VolumeUnit.UsPint, uspintQuantity.Unit);
-
- var usquartQuantity = cubicmeter.ToUnit(VolumeUnit.UsQuart);
- AssertEx.EqualTolerance(UsQuartsInOneCubicMeter, (double)usquartQuantity.Value, UsQuartsTolerance);
- Assert.Equal(VolumeUnit.UsQuart, usquartQuantity.Unit);
-
- var ustablespoonQuantity = cubicmeter.ToUnit(VolumeUnit.UsTablespoon);
- AssertEx.EqualTolerance(UsTablespoonsInOneCubicMeter, (double)ustablespoonQuantity.Value, UsTablespoonsTolerance);
- Assert.Equal(VolumeUnit.UsTablespoon, ustablespoonQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var usteaspoonQuantity = cubicmeter.ToUnit(VolumeUnit.UsTeaspoon);
- AssertEx.EqualTolerance(UsTeaspoonsInOneCubicMeter, (double)usteaspoonQuantity.Value, UsTeaspoonsTolerance);
- Assert.Equal(VolumeUnit.UsTeaspoon, usteaspoonQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VolumeUnit unit)
+ {
+ var quantity = Volume.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VolumeUnit unit)
{
- var quantityInBaseUnit = Volume.FromCubicMeters(1).ToBaseUnit();
- Assert.Equal(Volume.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = Volume.Units.FirstOrDefault(u => u != Volume.BaseUnit && u != VolumeUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VolumeUnit.Undefined)
+ fromUnit = Volume.BaseUnit;
+
+ var quantity = Volume.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumetricHeatCapacityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumetricHeatCapacityTestsBase.g.cs
index 8abd390f15..68f191f66c 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/VolumetricHeatCapacityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/VolumetricHeatCapacityTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -59,6 +60,36 @@ public abstract partial class VolumetricHeatCapacityTestsBase : QuantityTestsBas
protected virtual double MegajoulesPerCubicMeterKelvinTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(VolumetricHeatCapacityUnit unit)
+ {
+ return unit switch
+ {
+ VolumetricHeatCapacityUnit.BtuPerCubicFootDegreeFahrenheit => (BtusPerCubicFootDegreeFahrenheitInOneJoulePerCubicMeterKelvin, BtusPerCubicFootDegreeFahrenheitTolerance),
+ VolumetricHeatCapacityUnit.CaloriePerCubicCentimeterDegreeCelsius => (CaloriesPerCubicCentimeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, CaloriesPerCubicCentimeterDegreeCelsiusTolerance),
+ VolumetricHeatCapacityUnit.JoulePerCubicMeterDegreeCelsius => (JoulesPerCubicMeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, JoulesPerCubicMeterDegreeCelsiusTolerance),
+ VolumetricHeatCapacityUnit.JoulePerCubicMeterKelvin => (JoulesPerCubicMeterKelvinInOneJoulePerCubicMeterKelvin, JoulesPerCubicMeterKelvinTolerance),
+ VolumetricHeatCapacityUnit.KilocaloriePerCubicCentimeterDegreeCelsius => (KilocaloriesPerCubicCentimeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, KilocaloriesPerCubicCentimeterDegreeCelsiusTolerance),
+ VolumetricHeatCapacityUnit.KilojoulePerCubicMeterDegreeCelsius => (KilojoulesPerCubicMeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, KilojoulesPerCubicMeterDegreeCelsiusTolerance),
+ VolumetricHeatCapacityUnit.KilojoulePerCubicMeterKelvin => (KilojoulesPerCubicMeterKelvinInOneJoulePerCubicMeterKelvin, KilojoulesPerCubicMeterKelvinTolerance),
+ VolumetricHeatCapacityUnit.MegajoulePerCubicMeterDegreeCelsius => (MegajoulesPerCubicMeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, MegajoulesPerCubicMeterDegreeCelsiusTolerance),
+ VolumetricHeatCapacityUnit.MegajoulePerCubicMeterKelvin => (MegajoulesPerCubicMeterKelvinInOneJoulePerCubicMeterKelvin, MegajoulesPerCubicMeterKelvinTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { VolumetricHeatCapacityUnit.BtuPerCubicFootDegreeFahrenheit },
+ new object[] { VolumetricHeatCapacityUnit.CaloriePerCubicCentimeterDegreeCelsius },
+ new object[] { VolumetricHeatCapacityUnit.JoulePerCubicMeterDegreeCelsius },
+ new object[] { VolumetricHeatCapacityUnit.JoulePerCubicMeterKelvin },
+ new object[] { VolumetricHeatCapacityUnit.KilocaloriePerCubicCentimeterDegreeCelsius },
+ new object[] { VolumetricHeatCapacityUnit.KilojoulePerCubicMeterDegreeCelsius },
+ new object[] { VolumetricHeatCapacityUnit.KilojoulePerCubicMeterKelvin },
+ new object[] { VolumetricHeatCapacityUnit.MegajoulePerCubicMeterDegreeCelsius },
+ new object[] { VolumetricHeatCapacityUnit.MegajoulePerCubicMeterKelvin },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -228,53 +259,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(VolumetricHeatCapacityUnit unit)
{
- var joulepercubicmeterkelvin = VolumetricHeatCapacity.FromJoulesPerCubicMeterKelvin(1);
-
- var btupercubicfootdegreefahrenheitQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.BtuPerCubicFootDegreeFahrenheit);
- AssertEx.EqualTolerance(BtusPerCubicFootDegreeFahrenheitInOneJoulePerCubicMeterKelvin, (double)btupercubicfootdegreefahrenheitQuantity.Value, BtusPerCubicFootDegreeFahrenheitTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.BtuPerCubicFootDegreeFahrenheit, btupercubicfootdegreefahrenheitQuantity.Unit);
-
- var caloriepercubiccentimeterdegreecelsiusQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.CaloriePerCubicCentimeterDegreeCelsius);
- AssertEx.EqualTolerance(CaloriesPerCubicCentimeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, (double)caloriepercubiccentimeterdegreecelsiusQuantity.Value, CaloriesPerCubicCentimeterDegreeCelsiusTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.CaloriePerCubicCentimeterDegreeCelsius, caloriepercubiccentimeterdegreecelsiusQuantity.Unit);
-
- var joulepercubicmeterdegreecelsiusQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.JoulePerCubicMeterDegreeCelsius);
- AssertEx.EqualTolerance(JoulesPerCubicMeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, (double)joulepercubicmeterdegreecelsiusQuantity.Value, JoulesPerCubicMeterDegreeCelsiusTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.JoulePerCubicMeterDegreeCelsius, joulepercubicmeterdegreecelsiusQuantity.Unit);
-
- var joulepercubicmeterkelvinQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.JoulePerCubicMeterKelvin);
- AssertEx.EqualTolerance(JoulesPerCubicMeterKelvinInOneJoulePerCubicMeterKelvin, (double)joulepercubicmeterkelvinQuantity.Value, JoulesPerCubicMeterKelvinTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.JoulePerCubicMeterKelvin, joulepercubicmeterkelvinQuantity.Unit);
+ var inBaseUnits = VolumetricHeatCapacity.From(1.0, VolumetricHeatCapacity.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var kilocaloriepercubiccentimeterdegreecelsiusQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.KilocaloriePerCubicCentimeterDegreeCelsius);
- AssertEx.EqualTolerance(KilocaloriesPerCubicCentimeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, (double)kilocaloriepercubiccentimeterdegreecelsiusQuantity.Value, KilocaloriesPerCubicCentimeterDegreeCelsiusTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.KilocaloriePerCubicCentimeterDegreeCelsius, kilocaloriepercubiccentimeterdegreecelsiusQuantity.Unit);
-
- var kilojoulepercubicmeterdegreecelsiusQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.KilojoulePerCubicMeterDegreeCelsius);
- AssertEx.EqualTolerance(KilojoulesPerCubicMeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, (double)kilojoulepercubicmeterdegreecelsiusQuantity.Value, KilojoulesPerCubicMeterDegreeCelsiusTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.KilojoulePerCubicMeterDegreeCelsius, kilojoulepercubicmeterdegreecelsiusQuantity.Unit);
-
- var kilojoulepercubicmeterkelvinQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.KilojoulePerCubicMeterKelvin);
- AssertEx.EqualTolerance(KilojoulesPerCubicMeterKelvinInOneJoulePerCubicMeterKelvin, (double)kilojoulepercubicmeterkelvinQuantity.Value, KilojoulesPerCubicMeterKelvinTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.KilojoulePerCubicMeterKelvin, kilojoulepercubicmeterkelvinQuantity.Unit);
-
- var megajoulepercubicmeterdegreecelsiusQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.MegajoulePerCubicMeterDegreeCelsius);
- AssertEx.EqualTolerance(MegajoulesPerCubicMeterDegreeCelsiusInOneJoulePerCubicMeterKelvin, (double)megajoulepercubicmeterdegreecelsiusQuantity.Value, MegajoulesPerCubicMeterDegreeCelsiusTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.MegajoulePerCubicMeterDegreeCelsius, megajoulepercubicmeterdegreecelsiusQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var megajoulepercubicmeterkelvinQuantity = joulepercubicmeterkelvin.ToUnit(VolumetricHeatCapacityUnit.MegajoulePerCubicMeterKelvin);
- AssertEx.EqualTolerance(MegajoulesPerCubicMeterKelvinInOneJoulePerCubicMeterKelvin, (double)megajoulepercubicmeterkelvinQuantity.Value, MegajoulesPerCubicMeterKelvinTolerance);
- Assert.Equal(VolumetricHeatCapacityUnit.MegajoulePerCubicMeterKelvin, megajoulepercubicmeterkelvinQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(VolumetricHeatCapacityUnit unit)
+ {
+ var quantity = VolumetricHeatCapacity.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(VolumetricHeatCapacityUnit unit)
{
- var quantityInBaseUnit = VolumetricHeatCapacity.FromJoulesPerCubicMeterKelvin(1).ToBaseUnit();
- Assert.Equal(VolumetricHeatCapacity.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = VolumetricHeatCapacity.Units.FirstOrDefault(u => u != VolumetricHeatCapacity.BaseUnit && u != VolumetricHeatCapacityUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == VolumetricHeatCapacityUnit.Undefined)
+ fromUnit = VolumetricHeatCapacity.BaseUnit;
+
+ var quantity = VolumetricHeatCapacity.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/WarpingMomentOfInertiaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/WarpingMomentOfInertiaTestsBase.g.cs
index 6890a5115c..6f1677cfb9 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/WarpingMomentOfInertiaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/WarpingMomentOfInertiaTestsBase.g.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -53,6 +54,30 @@ public abstract partial class WarpingMomentOfInertiaTestsBase : QuantityTestsBas
protected virtual double MillimetersToTheSixthTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global
+ protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(WarpingMomentOfInertiaUnit unit)
+ {
+ return unit switch
+ {
+ WarpingMomentOfInertiaUnit.CentimeterToTheSixth => (CentimetersToTheSixthInOneMeterToTheSixth, CentimetersToTheSixthTolerance),
+ WarpingMomentOfInertiaUnit.DecimeterToTheSixth => (DecimetersToTheSixthInOneMeterToTheSixth, DecimetersToTheSixthTolerance),
+ WarpingMomentOfInertiaUnit.FootToTheSixth => (FeetToTheSixthInOneMeterToTheSixth, FeetToTheSixthTolerance),
+ WarpingMomentOfInertiaUnit.InchToTheSixth => (InchesToTheSixthInOneMeterToTheSixth, InchesToTheSixthTolerance),
+ WarpingMomentOfInertiaUnit.MeterToTheSixth => (MetersToTheSixthInOneMeterToTheSixth, MetersToTheSixthTolerance),
+ WarpingMomentOfInertiaUnit.MillimeterToTheSixth => (MillimetersToTheSixthInOneMeterToTheSixth, MillimetersToTheSixthTolerance),
+ _ => throw new NotSupportedException()
+ };
+ }
+
+ public static IEnumerable UnitTypes = new List
+ {
+ new object[] { WarpingMomentOfInertiaUnit.CentimeterToTheSixth },
+ new object[] { WarpingMomentOfInertiaUnit.DecimeterToTheSixth },
+ new object[] { WarpingMomentOfInertiaUnit.FootToTheSixth },
+ new object[] { WarpingMomentOfInertiaUnit.InchToTheSixth },
+ new object[] { WarpingMomentOfInertiaUnit.MeterToTheSixth },
+ new object[] { WarpingMomentOfInertiaUnit.MillimeterToTheSixth },
+ };
+
[Fact]
public void Ctor_WithUndefinedUnit_ThrowsArgumentException()
{
@@ -204,41 +229,41 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
}
}
- [Fact]
- public void ToUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit(WarpingMomentOfInertiaUnit unit)
{
- var metertothesixth = WarpingMomentOfInertia.FromMetersToTheSixth(1);
-
- var centimetertothesixthQuantity = metertothesixth.ToUnit(WarpingMomentOfInertiaUnit.CentimeterToTheSixth);
- AssertEx.EqualTolerance(CentimetersToTheSixthInOneMeterToTheSixth, (double)centimetertothesixthQuantity.Value, CentimetersToTheSixthTolerance);
- Assert.Equal(WarpingMomentOfInertiaUnit.CentimeterToTheSixth, centimetertothesixthQuantity.Unit);
+ var inBaseUnits = WarpingMomentOfInertia.From(1.0, WarpingMomentOfInertia.BaseUnit);
+ var converted = inBaseUnits.ToUnit(unit);
- var decimetertothesixthQuantity = metertothesixth.ToUnit(WarpingMomentOfInertiaUnit.DecimeterToTheSixth);
- AssertEx.EqualTolerance(DecimetersToTheSixthInOneMeterToTheSixth, (double)decimetertothesixthQuantity.Value, DecimetersToTheSixthTolerance);
- Assert.Equal(WarpingMomentOfInertiaUnit.DecimeterToTheSixth, decimetertothesixthQuantity.Unit);
-
- var foottothesixthQuantity = metertothesixth.ToUnit(WarpingMomentOfInertiaUnit.FootToTheSixth);
- AssertEx.EqualTolerance(FeetToTheSixthInOneMeterToTheSixth, (double)foottothesixthQuantity.Value, FeetToTheSixthTolerance);
- Assert.Equal(WarpingMomentOfInertiaUnit.FootToTheSixth, foottothesixthQuantity.Unit);
-
- var inchtothesixthQuantity = metertothesixth.ToUnit(WarpingMomentOfInertiaUnit.InchToTheSixth);
- AssertEx.EqualTolerance(InchesToTheSixthInOneMeterToTheSixth, (double)inchtothesixthQuantity.Value, InchesToTheSixthTolerance);
- Assert.Equal(WarpingMomentOfInertiaUnit.InchToTheSixth, inchtothesixthQuantity.Unit);
-
- var metertothesixthQuantity = metertothesixth.ToUnit(WarpingMomentOfInertiaUnit.MeterToTheSixth);
- AssertEx.EqualTolerance(MetersToTheSixthInOneMeterToTheSixth, (double)metertothesixthQuantity.Value, MetersToTheSixthTolerance);
- Assert.Equal(WarpingMomentOfInertiaUnit.MeterToTheSixth, metertothesixthQuantity.Unit);
+ var conversionFactor = GetConversionFactor(unit);
+ AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, (double)converted.Value, conversionFactor.Tolerence);
+ Assert.Equal(unit, converted.Unit);
+ }
- var millimetertothesixthQuantity = metertothesixth.ToUnit(WarpingMomentOfInertiaUnit.MillimeterToTheSixth);
- AssertEx.EqualTolerance(MillimetersToTheSixthInOneMeterToTheSixth, (double)millimetertothesixthQuantity.Value, MillimetersToTheSixthTolerance);
- Assert.Equal(WarpingMomentOfInertiaUnit.MillimeterToTheSixth, millimetertothesixthQuantity.Unit);
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_WithSameUnits_AreEqual(WarpingMomentOfInertiaUnit unit)
+ {
+ var quantity = WarpingMomentOfInertia.From(3.0, unit);
+ var toUnitWithSameUnit = quantity.ToUnit(unit);
+ Assert.Equal(quantity, toUnitWithSameUnit);
}
- [Fact]
- public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
+ [Theory]
+ [MemberData(nameof(UnitTypes))]
+ public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(WarpingMomentOfInertiaUnit unit)
{
- var quantityInBaseUnit = WarpingMomentOfInertia.FromMetersToTheSixth(1).ToBaseUnit();
- Assert.Equal(WarpingMomentOfInertia.BaseUnit, quantityInBaseUnit.Unit);
+ // See if there is a unit available that is not the base unit.
+ var fromUnit = WarpingMomentOfInertia.Units.FirstOrDefault(u => u != WarpingMomentOfInertia.BaseUnit && u != WarpingMomentOfInertiaUnit.Undefined);
+
+ // If there is only one unit for the quantity, we must use the base unit.
+ if(fromUnit == WarpingMomentOfInertiaUnit.Undefined)
+ fromUnit = WarpingMomentOfInertia.BaseUnit;
+
+ var quantity = WarpingMomentOfInertia.From(3.0, fromUnit);
+ var converted = quantity.ToUnit(unit);
+ Assert.Equal(converted.Unit, unit);
}
[Fact]
diff --git a/UnitsNet.Tests/UnitConverterTest.cs b/UnitsNet.Tests/UnitConverterTest.cs
index 9de3a67e94..421d7fef23 100644
--- a/UnitsNet.Tests/UnitConverterTest.cs
+++ b/UnitsNet.Tests/UnitConverterTest.cs
@@ -9,6 +9,19 @@ namespace UnitsNet.Tests
{
public class UnitConverterTest
{
+ [Fact]
+ public void CopyConstructorCopiesCoversionFunctions()
+ {
+ Length ConversionFunction(Length from) => Length.FromInches(18);
+
+ var unitConverter = new UnitConverter();
+ unitConverter.SetConversionFunction(LengthUnit.Meter, LengthUnit.Inch, ConversionFunction);
+
+ var copiedUnitConverter = new UnitConverter(unitConverter);
+ var foundConversionFunction = copiedUnitConverter.GetConversionFunction(LengthUnit.Meter, LengthUnit.Inch);
+ Assert.NotNull(foundConversionFunction);
+ }
+
[Fact]
public void CustomConversionWithSameQuantityType()
{
diff --git a/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs b/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs
index 13cfef0974..6463a39af7 100644
--- a/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs
+++ b/UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs
@@ -78,6 +78,8 @@ static Acceleration()
new UnitInfo(AccelerationUnit.StandardGravity, "StandardGravity", new BaseUnits(length: LengthUnit.Meter, time: DurationUnit.Second)),
},
BaseUnit, Zero, BaseDimensions, QuantityType.Acceleration);
+
+ RegisterDefaultConversions(DefaultConversionFunctions);
}
///
@@ -116,6 +118,11 @@ public Acceleration(double value, UnitSystem unitSystem)
#region Static Properties
+ ///
+ /// The containing the default generated conversion functions for instances.
+ ///
+ public static UnitConverter DefaultConversionFunctions { get; } = new UnitConverter();
+
///
public static QuantityInfo Info { get; }
@@ -273,37 +280,37 @@ public Acceleration(double value, UnitSystem unitSystem)
internal static void RegisterDefaultConversions(UnitConverter unitConverter)
{
// Register in unit converter: BaseUnit -> AccelerationUnit
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.CentimeterPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.CentimeterPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.DecimeterPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.DecimeterPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.FootPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.FootPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.InchPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.InchPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KilometerPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.KilometerPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KnotPerHour, quantity => quantity.ToUnit(AccelerationUnit.KnotPerHour));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KnotPerMinute, quantity => quantity.ToUnit(AccelerationUnit.KnotPerMinute));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KnotPerSecond, quantity => quantity.ToUnit(AccelerationUnit.KnotPerSecond));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.MicrometerPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.MicrometerPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.MillimeterPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.MillimeterPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.MillistandardGravity, quantity => quantity.ToUnit(AccelerationUnit.MillistandardGravity));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.NanometerPerSecondSquared, quantity => quantity.ToUnit(AccelerationUnit.NanometerPerSecondSquared));
- unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.StandardGravity, quantity => quantity.ToUnit(AccelerationUnit.StandardGravity));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.CentimeterPerSecondSquared, quantity => new Acceleration((quantity.Value) / 1e-2d, AccelerationUnit.CentimeterPerSecondSquared));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.DecimeterPerSecondSquared, quantity => new Acceleration((quantity.Value) / 1e-1d, AccelerationUnit.DecimeterPerSecondSquared));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.FootPerSecondSquared, quantity => new Acceleration(quantity.Value/0.304800, AccelerationUnit.FootPerSecondSquared));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.InchPerSecondSquared, quantity => new Acceleration(quantity.Value/0.0254, AccelerationUnit.InchPerSecondSquared));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KilometerPerSecondSquared, quantity => new Acceleration((quantity.Value) / 1e3d, AccelerationUnit.KilometerPerSecondSquared));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KnotPerHour, quantity => new Acceleration(quantity.Value/0.5144444444444*3600, AccelerationUnit.KnotPerHour));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KnotPerMinute, quantity => new Acceleration(quantity.Value/0.5144444444444*60, AccelerationUnit.KnotPerMinute));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.KnotPerSecond, quantity => new Acceleration(quantity.Value/0.5144444444444, AccelerationUnit.KnotPerSecond));
+ unitConverter.SetConversionFunction(AccelerationUnit.MeterPerSecondSquared, AccelerationUnit.MicrometerPerSecondSquared, quantity => new Acceleration((quantity.Value) / 1e-6d, AccelerationUnit.MicrometerPerSecondSquared));
+ unitConverter.SetConversionFunction