From 3dfe9a4913d25466671355f09ed478cd5d2ae599 Mon Sep 17 00:00:00 2001 From: Diego Garcia Lozano Date: Tue, 23 Nov 2021 22:13:03 +0100 Subject: [PATCH 1/6] Order the quantity json files alphabetically before running CodeGen Directory.GetFiles doesn't guarantee the order of the file names. In windows it seems to be retrieving them alphabetically sorted, but that's not the case for Mac. Have taken the liberty to rename `jsonFile` to `jsonFileName`, since the return of Directory.GetFiles is an array of file names. The content of the file itself is read at a later point. --- CodeGen/Generators/QuantityJsonFilesParser.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/CodeGen/Generators/QuantityJsonFilesParser.cs b/CodeGen/Generators/QuantityJsonFilesParser.cs index bf0578c593..ac3ca86be6 100644 --- a/CodeGen/Generators/QuantityJsonFilesParser.cs +++ b/CodeGen/Generators/QuantityJsonFilesParser.cs @@ -33,16 +33,19 @@ internal static class QuantityJsonFilesParser public static Quantity[] ParseQuantities(string rootDir) { var jsonDir = Path.Combine(rootDir, "Common/UnitDefinitions"); - var jsonFiles = Directory.GetFiles(jsonDir, "*.json"); - return jsonFiles.Select(ParseQuantityFile).ToArray(); + var jsonFileNames = Directory.GetFiles(jsonDir, "*.json"); + return jsonFileNames + .OrderBy(fn => fn, StringComparer.InvariantCultureIgnoreCase) + .Select(ParseQuantityFile) + .ToArray(); } - private static Quantity ParseQuantityFile(string jsonFile) + private static Quantity ParseQuantityFile(string jsonFileName) { try { - var quantity = JsonConvert.DeserializeObject(File.ReadAllText(jsonFile), JsonSerializerSettings) - ?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFile}"); + var quantity = JsonConvert.DeserializeObject(File.ReadAllText(jsonFileName), JsonSerializerSettings) + ?? throw new UnitsNetCodeGenException($"Unable to parse quantity from JSON file: {jsonFileName}"); AddPrefixUnits(quantity); FixConversionFunctionsForDecimalValueTypes(quantity); @@ -51,7 +54,7 @@ private static Quantity ParseQuantityFile(string jsonFile) } catch (Exception e) { - throw new Exception($"Error parsing quantity JSON file: {jsonFile}", e); + throw new Exception($"Error parsing quantity JSON file: {jsonFileName}", e); } } From c1aca3c60dd2457fc8c64513703407901815c149 Mon Sep 17 00:00:00 2001 From: Diego Garcia Lozano Date: Sun, 21 Nov 2021 21:20:58 +0100 Subject: [PATCH 2/6] Move Obsolete attribute in the generator below the XML documentation The [Obsolete] attribute was being generated on top of the XML documentation, which is an invalid location, so I have moved it right after it and before the [DataContract] attribute. Related: - #474 --- CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs | 2 +- CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs index 4d95b67f21..77ef517896 100644 --- a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs +++ b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs @@ -58,7 +58,6 @@ public override string Generate() namespace UnitsNet {"); - Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity)); Writer.WL($@" /// /// @@ -70,6 +69,7 @@ namespace UnitsNet /// {_quantity.XmlDocRemarks} /// "); + Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity)); Writer.W(@$" [DataContract] public partial struct {_quantity.Name} : IQuantity<{_unitEnumName}>, "); diff --git a/CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs b/CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs index 24c3d5eb34..337b14719c 100644 --- a/CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs +++ b/CodeGen/Generators/UnitsNetWrcGen/QuantityGenerator.cs @@ -55,7 +55,6 @@ public override string Generate() namespace UnitsNet {"); - Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity)); Writer.WL($@" /// /// {_quantity.XmlDoc} @@ -66,6 +65,7 @@ namespace UnitsNet /// {_quantity.XmlDocRemarks} /// "); + Writer.WLIfText(1, GetObsoleteAttributeOrNull(_quantity)); Writer.WL($@" // Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components // Public structures can't have any members other than public fields, and those fields must be value types or strings. From 456547ed054270b1a689d301d5a9447d79bc3eae Mon Sep 17 00:00:00 2001 From: Diego Garcia Lozano Date: Tue, 23 Nov 2021 22:31:53 +0100 Subject: [PATCH 3/6] Mark LapseRate as Obsolete It will be replaced by TemperatureGradient --- Common/UnitDefinitions/LapseRate.json | 1 + .../GeneratedCode/Quantities/LapseRate.g.cs | 1 + UnitsNet/GeneratedCode/Quantities/LapseRate.g.cs | 1 + 3 files changed, 3 insertions(+) diff --git a/Common/UnitDefinitions/LapseRate.json b/Common/UnitDefinitions/LapseRate.json index c43b48b49c..c443b5e2bd 100644 --- a/Common/UnitDefinitions/LapseRate.json +++ b/Common/UnitDefinitions/LapseRate.json @@ -1,6 +1,7 @@ { "Name": "LapseRate", "BaseUnit": "DegreeCelsiusPerKilometer", + "ObsoleteText": "Use TemperatureGradient instead.", "XmlDoc": "Lapse rate is the rate at which Earth's atmospheric temperature decreases with an increase in altitude, or increases with the decrease in altitude.", "BaseDimensions": { "L": -1, diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.g.cs index ae4b7fe44c..cf49b9649f 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/LapseRate.g.cs @@ -31,6 +31,7 @@ namespace UnitsNet /// /// Lapse rate is the rate at which Earth's atmospheric temperature decreases with an increase in altitude, or increases with the decrease in altitude. /// + [System.Obsolete("Use TemperatureGradient instead.")] // Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components // Public structures can't have any members other than public fields, and those fields must be value types or strings. // Public classes must be sealed (NotInheritable in Visual Basic). If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic. diff --git a/UnitsNet/GeneratedCode/Quantities/LapseRate.g.cs b/UnitsNet/GeneratedCode/Quantities/LapseRate.g.cs index 56a9bebf13..259f53fa1e 100644 --- a/UnitsNet/GeneratedCode/Quantities/LapseRate.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/LapseRate.g.cs @@ -35,6 +35,7 @@ namespace UnitsNet /// /// Lapse rate is the rate at which Earth's atmospheric temperature decreases with an increase in altitude, or increases with the decrease in altitude. /// + [System.Obsolete("Use TemperatureGradient instead.")] [DataContract] public partial struct LapseRate : IQuantity, IEquatable, IComparable, IComparable, IConvertible, IFormattable { From 04a3d21a086ae4756fc24adbcec11405a264116e Mon Sep 17 00:00:00 2001 From: Diego Garcia Lozano Date: Tue, 23 Nov 2021 22:39:53 +0100 Subject: [PATCH 4/6] Add TemperatureGradient Quantity --- .../UnitDefinitions/TemperatureGradient.json | 75 ++ ...erToTemperatureGradientExtensionsTest.g.cs | 44 + ...NumberToTemperatureGradientExtensions.g.cs | 48 + .../CustomCode/TemperatureGradientTests.cs | 37 + .../GeneratedCode/IQuantityTests.g.cs | 4 + .../TemperatureGradientTestsBase.g.cs | 623 ++++++++++++ .../Quantities/TemperatureGradient.g.cs | 631 ++++++++++++ .../GeneratedCode/Quantity.g.cs | 6 + .../GeneratedCode/QuantityType.g.cs | 1 + .../GeneratedCode/UnitAbbreviationsCache.g.cs | 4 + .../Units/TemperatureGradientUnit.g.cs | 36 + .../Quantities/TemperatureGradient.g.cs | 912 ++++++++++++++++++ UnitsNet/GeneratedCode/Quantity.g.cs | 11 + UnitsNet/GeneratedCode/QuantityType.g.cs | 1 + .../GeneratedCode/UnitAbbreviationsCache.g.cs | 4 + UnitsNet/GeneratedCode/UnitConverter.g.cs | 7 + .../Units/TemperatureGradientUnit.g.cs | 36 + UnitsNet/UnitsNet.csproj | 3 + 18 files changed, 2483 insertions(+) create mode 100644 Common/UnitDefinitions/TemperatureGradient.json create mode 100644 UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToTemperatureGradientExtensionsTest.g.cs create mode 100644 UnitsNet.NumberExtensions/GeneratedCode/NumberToTemperatureGradientExtensions.g.cs create mode 100644 UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs create mode 100644 UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs create mode 100644 UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureGradient.g.cs create mode 100644 UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/TemperatureGradientUnit.g.cs create mode 100644 UnitsNet/GeneratedCode/Quantities/TemperatureGradient.g.cs create mode 100644 UnitsNet/GeneratedCode/Units/TemperatureGradientUnit.g.cs diff --git a/Common/UnitDefinitions/TemperatureGradient.json b/Common/UnitDefinitions/TemperatureGradient.json new file mode 100644 index 0000000000..fd3f89d3ee --- /dev/null +++ b/Common/UnitDefinitions/TemperatureGradient.json @@ -0,0 +1,75 @@ +{ + "Name": "TemperatureGradient", + "BaseUnit": "KelvinPerMeter", + "XmlDoc": "The rate of change of temperature with displacement in a given direction (as with increase of height)", + "BaseDimensions": { + "L": -1, + "Θ": 1 + }, + "Units": [ + { + "SingularName": "KelvinPerMeter", + "PluralName": "KelvinsPerMeter", + "BaseUnits": { + "L": "Meter", + "Θ": "Kelvin" + }, + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "∆°K/m" ] + } + ] + }, + { + "SingularName": "DegreeCelsiusPerMeter", + "PluralName": "DegreesCelciusPerMeter", + "BaseUnits": { + "L": "Meter", + "Θ": "DegreeCelsius" + }, + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "∆°C/m" ] + } + ] + }, + { + "SingularName": "DegreeFahrenheitPerFoot", + "PluralName": "DegreesFahrenheitPerFoot", + "BaseUnits": { + "L": "Foot", + "Θ": "DegreeFahrenheit" + }, + "FromUnitToBaseFunc": "(x / 0.3048) * 5/9", + "FromBaseToUnitFunc": "(x * 0.3048) * 9/5", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "∆°F/ft" ] + } + ] + }, + { + "SingularName": "DegreeCelsiusPerKilometer", + "PluralName": "DegreesCelciusPerKilometer", + "BaseUnits": { + "L": "Kilometer", + "Θ": "DegreeCelsius" + }, + "FromUnitToBaseFunc": "x / 1e3", + "FromBaseToUnitFunc": "x * 1e3", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "∆°C/km" ] + } + ] + } + ] +} diff --git a/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToTemperatureGradientExtensionsTest.g.cs b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToTemperatureGradientExtensionsTest.g.cs new file mode 100644 index 0000000000..4062ea3754 --- /dev/null +++ b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToTemperatureGradientExtensionsTest.g.cs @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using UnitsNet.NumberExtensions.NumberToTemperatureGradient; +using Xunit; + +namespace UnitsNet.Tests +{ + public class NumberToTemperatureGradientExtensionsTests + { + [Fact] + public void NumberToDegreesCelciusPerKilometerTest() => + Assert.Equal(TemperatureGradient.FromDegreesCelciusPerKilometer(2), 2.DegreesCelciusPerKilometer()); + + [Fact] + public void NumberToDegreesCelciusPerMeterTest() => + Assert.Equal(TemperatureGradient.FromDegreesCelciusPerMeter(2), 2.DegreesCelciusPerMeter()); + + [Fact] + public void NumberToDegreesFahrenheitPerFootTest() => + Assert.Equal(TemperatureGradient.FromDegreesFahrenheitPerFoot(2), 2.DegreesFahrenheitPerFoot()); + + [Fact] + public void NumberToKelvinsPerMeterTest() => + Assert.Equal(TemperatureGradient.FromKelvinsPerMeter(2), 2.KelvinsPerMeter()); + + } +} diff --git a/UnitsNet.NumberExtensions/GeneratedCode/NumberToTemperatureGradientExtensions.g.cs b/UnitsNet.NumberExtensions/GeneratedCode/NumberToTemperatureGradientExtensions.g.cs new file mode 100644 index 0000000000..d160b22f07 --- /dev/null +++ b/UnitsNet.NumberExtensions/GeneratedCode/NumberToTemperatureGradientExtensions.g.cs @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; + +#nullable enable + +namespace UnitsNet.NumberExtensions.NumberToTemperatureGradient +{ + /// + /// A number to TemperatureGradient Extensions + /// + public static class NumberToTemperatureGradientExtensions + { + /// + public static TemperatureGradient DegreesCelciusPerKilometer(this T value) => + TemperatureGradient.FromDegreesCelciusPerKilometer(Convert.ToDouble(value)); + + /// + public static TemperatureGradient DegreesCelciusPerMeter(this T value) => + TemperatureGradient.FromDegreesCelciusPerMeter(Convert.ToDouble(value)); + + /// + public static TemperatureGradient DegreesFahrenheitPerFoot(this T value) => + TemperatureGradient.FromDegreesFahrenheitPerFoot(Convert.ToDouble(value)); + + /// + public static TemperatureGradient KelvinsPerMeter(this T value) => + TemperatureGradient.FromKelvinsPerMeter(Convert.ToDouble(value)); + + } +} diff --git a/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs b/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs new file mode 100644 index 0000000000..05ec40cbc4 --- /dev/null +++ b/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using Xunit; + +namespace UnitsNet.Tests.CustomCode +{ + public class TemperatureGradientTests : TemperatureGradientTestsBase + { + protected override bool SupportsSIUnitSystem => true; + + protected override double DegreesCelciusPerKilometerInOneKelvinPerMeter => 1000; + + protected override double DegreesCelciusPerMeterInOneKelvinPerMeter => 1; + + protected override double DegreesFahrenheitPerFootInOneKelvinPerMeter => 0.54864; + + protected override double KelvinsPerMeterInOneKelvinPerMeter => 1; + } +} diff --git a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs index a135be39b0..c3a8619726 100644 --- a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs +++ b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs @@ -132,6 +132,7 @@ void Assertion(int expectedValue, Enum expectedUnit, IQuantity quantity) Assertion(3, TemperatureUnit.SolarTemperature, Quantity.From(3, TemperatureUnit.SolarTemperature)); Assertion(3, TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond, Quantity.From(3, TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond)); Assertion(3, TemperatureDeltaUnit.MillidegreeCelsius, Quantity.From(3, TemperatureDeltaUnit.MillidegreeCelsius)); + Assertion(3, TemperatureGradientUnit.KelvinPerMeter, Quantity.From(3, TemperatureGradientUnit.KelvinPerMeter)); Assertion(3, ThermalConductivityUnit.WattPerMeterKelvin, Quantity.From(3, ThermalConductivityUnit.WattPerMeterKelvin)); Assertion(3, ThermalResistanceUnit.SquareMeterKelvinPerKilowatt, Quantity.From(3, ThermalResistanceUnit.SquareMeterKelvinPerKilowatt)); Assertion(3, TorqueUnit.TonneForceMillimeter, Quantity.From(3, TorqueUnit.TonneForceMillimeter)); @@ -249,6 +250,7 @@ public void QuantityInfo_IsSameAsStaticInfoProperty() Assertion(Temperature.Info, Temperature.Zero); Assertion(TemperatureChangeRate.Info, TemperatureChangeRate.Zero); Assertion(TemperatureDelta.Info, TemperatureDelta.Zero); + Assertion(TemperatureGradient.Info, TemperatureGradient.Zero); Assertion(ThermalConductivity.Info, ThermalConductivity.Zero); Assertion(ThermalResistance.Info, ThermalResistance.Zero); Assertion(Torque.Info, Torque.Zero); @@ -366,6 +368,7 @@ public void Type_EqualsStaticQuantityTypeProperty() Assertion(Temperature.QuantityType, Temperature.Zero); Assertion(TemperatureChangeRate.QuantityType, TemperatureChangeRate.Zero); Assertion(TemperatureDelta.QuantityType, TemperatureDelta.Zero); + Assertion(TemperatureGradient.QuantityType, TemperatureGradient.Zero); Assertion(ThermalConductivity.QuantityType, ThermalConductivity.Zero); Assertion(ThermalResistance.QuantityType, ThermalResistance.Zero); Assertion(Torque.QuantityType, Torque.Zero); @@ -483,6 +486,7 @@ public void Dimensions_IsSameAsStaticBaseDimensions() Assertion(Temperature.BaseDimensions, Temperature.Zero); Assertion(TemperatureChangeRate.BaseDimensions, TemperatureChangeRate.Zero); Assertion(TemperatureDelta.BaseDimensions, TemperatureDelta.Zero); + Assertion(TemperatureGradient.BaseDimensions, TemperatureGradient.Zero); Assertion(ThermalConductivity.BaseDimensions, ThermalConductivity.Zero); Assertion(ThermalResistance.BaseDimensions, ThermalResistance.Zero); Assertion(Torque.BaseDimensions, Torque.Zero); diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs new file mode 100644 index 0000000000..76f37cdd67 --- /dev/null +++ b/UnitsNet.Tests/GeneratedCode/TestsBase/TemperatureGradientTestsBase.g.cs @@ -0,0 +1,623 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Globalization; +using System.Linq; +using System.Threading; +using UnitsNet.Tests.TestsBase; +using UnitsNet.Units; +using Xunit; + +// Disable build warning CS1718: Comparison made to same variable; did you mean to compare something else? +#pragma warning disable 1718 + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Tests +{ + /// + /// Test of TemperatureGradient. + /// +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class TemperatureGradientTestsBase : QuantityTestsBase + { + protected abstract double DegreesCelciusPerKilometerInOneKelvinPerMeter { get; } + protected abstract double DegreesCelciusPerMeterInOneKelvinPerMeter { get; } + protected abstract double DegreesFahrenheitPerFootInOneKelvinPerMeter { get; } + protected abstract double KelvinsPerMeterInOneKelvinPerMeter { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double DegreesCelciusPerKilometerTolerance { get { return 1e-5; } } + protected virtual double DegreesCelciusPerMeterTolerance { get { return 1e-5; } } + protected virtual double DegreesFahrenheitPerFootTolerance { get { return 1e-5; } } + protected virtual double KelvinsPerMeterTolerance { get { return 1e-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + [Fact] + public void Ctor_WithUndefinedUnit_ThrowsArgumentException() + { + Assert.Throws(() => new TemperatureGradient((double)0.0, TemperatureGradientUnit.Undefined)); + } + + [Fact] + public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit() + { + var quantity = new TemperatureGradient(); + Assert.Equal(0, quantity.Value); + Assert.Equal(TemperatureGradientUnit.KelvinPerMeter, quantity.Unit); + } + + + [Fact] + public void Ctor_WithInfinityValue_ThrowsArgumentException() + { + Assert.Throws(() => new TemperatureGradient(double.PositiveInfinity, TemperatureGradientUnit.KelvinPerMeter)); + Assert.Throws(() => new TemperatureGradient(double.NegativeInfinity, TemperatureGradientUnit.KelvinPerMeter)); + } + + [Fact] + public void Ctor_WithNaNValue_ThrowsArgumentException() + { + Assert.Throws(() => new TemperatureGradient(double.NaN, TemperatureGradientUnit.KelvinPerMeter)); + } + + [Fact] + public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException() + { + Assert.Throws(() => new TemperatureGradient(value: 1, unitSystem: null)); + } + + [Fact] + public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + Func TestCode = () => new TemperatureGradient(value: 1, unitSystem: UnitSystem.SI); + if (SupportsSIUnitSystem) + { + var quantity = (TemperatureGradient) TestCode(); + Assert.Equal(1, quantity.Value); + } + else + { + Assert.Throws(TestCode); + } + } + + [Fact] + public void TemperatureGradient_QuantityInfo_ReturnsQuantityInfoDescribingQuantity() + { + var quantity = new TemperatureGradient(1, TemperatureGradientUnit.KelvinPerMeter); + + QuantityInfo quantityInfo = quantity.QuantityInfo; + + Assert.Equal(TemperatureGradient.Zero, quantityInfo.Zero); + Assert.Equal("TemperatureGradient", quantityInfo.Name); + Assert.Equal(QuantityType.TemperatureGradient, quantityInfo.QuantityType); + + var units = EnumUtils.GetEnumValues().Except(new[] {TemperatureGradientUnit.Undefined}).ToArray(); + var unitNames = units.Select(x => x.ToString()); + + // Obsolete members + Assert.Equal(units, quantityInfo.Units); + Assert.Equal(unitNames, quantityInfo.UnitNames); + } + + [Fact] + public void KelvinPerMeterToTemperatureGradientUnits() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + AssertEx.EqualTolerance(DegreesCelciusPerKilometerInOneKelvinPerMeter, kelvinpermeter.DegreesCelciusPerKilometer, DegreesCelciusPerKilometerTolerance); + AssertEx.EqualTolerance(DegreesCelciusPerMeterInOneKelvinPerMeter, kelvinpermeter.DegreesCelciusPerMeter, DegreesCelciusPerMeterTolerance); + AssertEx.EqualTolerance(DegreesFahrenheitPerFootInOneKelvinPerMeter, kelvinpermeter.DegreesFahrenheitPerFoot, DegreesFahrenheitPerFootTolerance); + AssertEx.EqualTolerance(KelvinsPerMeterInOneKelvinPerMeter, kelvinpermeter.KelvinsPerMeter, KelvinsPerMeterTolerance); + } + + [Fact] + public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit() + { + var quantity00 = TemperatureGradient.From(1, TemperatureGradientUnit.DegreeCelsiusPerKilometer); + AssertEx.EqualTolerance(1, quantity00.DegreesCelciusPerKilometer, DegreesCelciusPerKilometerTolerance); + Assert.Equal(TemperatureGradientUnit.DegreeCelsiusPerKilometer, quantity00.Unit); + + var quantity01 = TemperatureGradient.From(1, TemperatureGradientUnit.DegreeCelsiusPerMeter); + AssertEx.EqualTolerance(1, quantity01.DegreesCelciusPerMeter, DegreesCelciusPerMeterTolerance); + Assert.Equal(TemperatureGradientUnit.DegreeCelsiusPerMeter, quantity01.Unit); + + var quantity02 = TemperatureGradient.From(1, TemperatureGradientUnit.DegreeFahrenheitPerFoot); + AssertEx.EqualTolerance(1, quantity02.DegreesFahrenheitPerFoot, DegreesFahrenheitPerFootTolerance); + Assert.Equal(TemperatureGradientUnit.DegreeFahrenheitPerFoot, quantity02.Unit); + + var quantity03 = TemperatureGradient.From(1, TemperatureGradientUnit.KelvinPerMeter); + AssertEx.EqualTolerance(1, quantity03.KelvinsPerMeter, KelvinsPerMeterTolerance); + Assert.Equal(TemperatureGradientUnit.KelvinPerMeter, quantity03.Unit); + + } + + [Fact] + public void FromKelvinsPerMeter_WithInfinityValue_ThrowsArgumentException() + { + Assert.Throws(() => TemperatureGradient.FromKelvinsPerMeter(double.PositiveInfinity)); + Assert.Throws(() => TemperatureGradient.FromKelvinsPerMeter(double.NegativeInfinity)); + } + + [Fact] + public void FromKelvinsPerMeter_WithNanValue_ThrowsArgumentException() + { + Assert.Throws(() => TemperatureGradient.FromKelvinsPerMeter(double.NaN)); + } + + [Fact] + public void As() + { + var kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + AssertEx.EqualTolerance(DegreesCelciusPerKilometerInOneKelvinPerMeter, kelvinpermeter.As(TemperatureGradientUnit.DegreeCelsiusPerKilometer), DegreesCelciusPerKilometerTolerance); + AssertEx.EqualTolerance(DegreesCelciusPerMeterInOneKelvinPerMeter, kelvinpermeter.As(TemperatureGradientUnit.DegreeCelsiusPerMeter), DegreesCelciusPerMeterTolerance); + AssertEx.EqualTolerance(DegreesFahrenheitPerFootInOneKelvinPerMeter, kelvinpermeter.As(TemperatureGradientUnit.DegreeFahrenheitPerFoot), DegreesFahrenheitPerFootTolerance); + AssertEx.EqualTolerance(KelvinsPerMeterInOneKelvinPerMeter, kelvinpermeter.As(TemperatureGradientUnit.KelvinPerMeter), KelvinsPerMeterTolerance); + } + + [Fact] + public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var quantity = new TemperatureGradient(value: 1, unit: TemperatureGradient.BaseUnit); + Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI); + + if (SupportsSIUnitSystem) + { + var value = (double) AsWithSIUnitSystem(); + Assert.Equal(1, value); + } + else + { + Assert.Throws(AsWithSIUnitSystem); + } + } + + [Fact] + public void ToUnit() + { + var kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + + 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 kelvinpermeterQuantity = kelvinpermeter.ToUnit(TemperatureGradientUnit.KelvinPerMeter); + AssertEx.EqualTolerance(KelvinsPerMeterInOneKelvinPerMeter, (double)kelvinpermeterQuantity.Value, KelvinsPerMeterTolerance); + Assert.Equal(TemperatureGradientUnit.KelvinPerMeter, kelvinpermeterQuantity.Unit); + } + + [Fact] + public void ToBaseUnit_ReturnsQuantityWithBaseUnit() + { + var quantityInBaseUnit = TemperatureGradient.FromKelvinsPerMeter(1).ToBaseUnit(); + Assert.Equal(TemperatureGradient.BaseUnit, quantityInBaseUnit.Unit); + } + + [Fact] + public void ConversionRoundTrip() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + AssertEx.EqualTolerance(1, TemperatureGradient.FromDegreesCelciusPerKilometer(kelvinpermeter.DegreesCelciusPerKilometer).KelvinsPerMeter, DegreesCelciusPerKilometerTolerance); + AssertEx.EqualTolerance(1, TemperatureGradient.FromDegreesCelciusPerMeter(kelvinpermeter.DegreesCelciusPerMeter).KelvinsPerMeter, DegreesCelciusPerMeterTolerance); + AssertEx.EqualTolerance(1, TemperatureGradient.FromDegreesFahrenheitPerFoot(kelvinpermeter.DegreesFahrenheitPerFoot).KelvinsPerMeter, DegreesFahrenheitPerFootTolerance); + AssertEx.EqualTolerance(1, TemperatureGradient.FromKelvinsPerMeter(kelvinpermeter.KelvinsPerMeter).KelvinsPerMeter, KelvinsPerMeterTolerance); + } + + [Fact] + public void ArithmeticOperators() + { + TemperatureGradient v = TemperatureGradient.FromKelvinsPerMeter(1); + AssertEx.EqualTolerance(-1, -v.KelvinsPerMeter, KelvinsPerMeterTolerance); + AssertEx.EqualTolerance(2, (TemperatureGradient.FromKelvinsPerMeter(3)-v).KelvinsPerMeter, KelvinsPerMeterTolerance); + AssertEx.EqualTolerance(2, (v + v).KelvinsPerMeter, KelvinsPerMeterTolerance); + AssertEx.EqualTolerance(10, (v*10).KelvinsPerMeter, KelvinsPerMeterTolerance); + AssertEx.EqualTolerance(10, (10*v).KelvinsPerMeter, KelvinsPerMeterTolerance); + AssertEx.EqualTolerance(2, (TemperatureGradient.FromKelvinsPerMeter(10)/5).KelvinsPerMeter, KelvinsPerMeterTolerance); + AssertEx.EqualTolerance(2, TemperatureGradient.FromKelvinsPerMeter(10)/TemperatureGradient.FromKelvinsPerMeter(5), KelvinsPerMeterTolerance); + } + + [Fact] + public void ComparisonOperators() + { + TemperatureGradient oneKelvinPerMeter = TemperatureGradient.FromKelvinsPerMeter(1); + TemperatureGradient twoKelvinsPerMeter = TemperatureGradient.FromKelvinsPerMeter(2); + + Assert.True(oneKelvinPerMeter < twoKelvinsPerMeter); + Assert.True(oneKelvinPerMeter <= twoKelvinsPerMeter); + Assert.True(twoKelvinsPerMeter > oneKelvinPerMeter); + Assert.True(twoKelvinsPerMeter >= oneKelvinPerMeter); + + Assert.False(oneKelvinPerMeter > twoKelvinsPerMeter); + Assert.False(oneKelvinPerMeter >= twoKelvinsPerMeter); + Assert.False(twoKelvinsPerMeter < oneKelvinPerMeter); + Assert.False(twoKelvinsPerMeter <= oneKelvinPerMeter); + } + + [Fact] + public void CompareToIsImplemented() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.Equal(0, kelvinpermeter.CompareTo(kelvinpermeter)); + Assert.True(kelvinpermeter.CompareTo(TemperatureGradient.Zero) > 0); + Assert.True(TemperatureGradient.Zero.CompareTo(kelvinpermeter) < 0); + } + + [Fact] + public void CompareToThrowsOnTypeMismatch() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.Throws(() => kelvinpermeter.CompareTo(new object())); + } + + [Fact] + public void CompareToThrowsOnNull() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.Throws(() => kelvinpermeter.CompareTo(null)); + } + + [Fact] + public void EqualityOperators() + { + var a = TemperatureGradient.FromKelvinsPerMeter(1); + var b = TemperatureGradient.FromKelvinsPerMeter(2); + +#pragma warning disable CS8073 +// ReSharper disable EqualExpressionComparison + + Assert.True(a == a); + Assert.False(a != a); + + Assert.True(a != b); + Assert.False(a == b); + + Assert.False(a == null); + Assert.False(null == a); + +// ReSharper restore EqualExpressionComparison +#pragma warning restore CS8073 + } + + [Fact] + public void Equals_SameType_IsImplemented() + { + var a = TemperatureGradient.FromKelvinsPerMeter(1); + var b = TemperatureGradient.FromKelvinsPerMeter(2); + + Assert.True(a.Equals(a)); + Assert.False(a.Equals(b)); + } + + [Fact] + public void Equals_QuantityAsObject_IsImplemented() + { + object a = TemperatureGradient.FromKelvinsPerMeter(1); + object b = TemperatureGradient.FromKelvinsPerMeter(2); + + Assert.True(a.Equals(a)); + Assert.False(a.Equals(b)); + Assert.False(a.Equals((object)null)); + } + + [Fact] + public void Equals_RelativeTolerance_IsImplemented() + { + var v = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.True(v.Equals(TemperatureGradient.FromKelvinsPerMeter(1), KelvinsPerMeterTolerance, ComparisonType.Relative)); + Assert.False(v.Equals(TemperatureGradient.Zero, KelvinsPerMeterTolerance, ComparisonType.Relative)); + } + + [Fact] + public void Equals_NegativeRelativeTolerance_ThrowsArgumentOutOfRangeException() + { + var v = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.Throws(() => v.Equals(TemperatureGradient.FromKelvinsPerMeter(1), -1, ComparisonType.Relative)); + } + + [Fact] + public void EqualsReturnsFalseOnTypeMismatch() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.False(kelvinpermeter.Equals(new object())); + } + + [Fact] + public void EqualsReturnsFalseOnNull() + { + TemperatureGradient kelvinpermeter = TemperatureGradient.FromKelvinsPerMeter(1); + Assert.False(kelvinpermeter.Equals(null)); + } + + [Fact] + public void UnitsDoesNotContainUndefined() + { + Assert.DoesNotContain(TemperatureGradientUnit.Undefined, TemperatureGradient.Units); + } + + [Fact] + public void HasAtLeastOneAbbreviationSpecified() + { + var units = Enum.GetValues(typeof(TemperatureGradientUnit)).Cast(); + foreach(var unit in units) + { + if(unit == TemperatureGradientUnit.Undefined) + continue; + + var defaultAbbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit); + } + } + + [Fact] + public void BaseDimensionsShouldNeverBeNull() + { + Assert.False(TemperatureGradient.BaseDimensions is null); + } + + [Fact] + public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture() + { + var prevCulture = Thread.CurrentThread.CurrentUICulture; + Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); + try { + Assert.Equal("1 ∆°C/km", new TemperatureGradient(1, TemperatureGradientUnit.DegreeCelsiusPerKilometer).ToString()); + Assert.Equal("1 ∆°C/m", new TemperatureGradient(1, TemperatureGradientUnit.DegreeCelsiusPerMeter).ToString()); + Assert.Equal("1 ∆°F/ft", new TemperatureGradient(1, TemperatureGradientUnit.DegreeFahrenheitPerFoot).ToString()); + Assert.Equal("1 ∆°K/m", new TemperatureGradient(1, TemperatureGradientUnit.KelvinPerMeter).ToString()); + } + finally + { + Thread.CurrentThread.CurrentUICulture = prevCulture; + } + } + + [Fact] + public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCultureSinceThereAreNoMappings() + { + // Chose this culture, because we don't currently have any abbreviations mapped for that culture and we expect the en-US to be used as fallback. + var swedishCulture = CultureInfo.GetCultureInfo("sv-SE"); + + Assert.Equal("1 ∆°C/km", new TemperatureGradient(1, TemperatureGradientUnit.DegreeCelsiusPerKilometer).ToString(swedishCulture)); + Assert.Equal("1 ∆°C/m", new TemperatureGradient(1, TemperatureGradientUnit.DegreeCelsiusPerMeter).ToString(swedishCulture)); + Assert.Equal("1 ∆°F/ft", new TemperatureGradient(1, TemperatureGradientUnit.DegreeFahrenheitPerFoot).ToString(swedishCulture)); + Assert.Equal("1 ∆°K/m", new TemperatureGradient(1, TemperatureGradientUnit.KelvinPerMeter).ToString(swedishCulture)); + } + + [Fact] + public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture() + { + var oldCulture = CultureInfo.CurrentUICulture; + try + { + CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture; + Assert.Equal("0.1 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s1")); + Assert.Equal("0.12 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s2")); + Assert.Equal("0.123 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s3")); + Assert.Equal("0.1235 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s4")); + } + finally + { + CultureInfo.CurrentUICulture = oldCulture; + } + } + + [Fact] + public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture() + { + var culture = CultureInfo.InvariantCulture; + Assert.Equal("0.1 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s1", culture)); + Assert.Equal("0.12 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s2", culture)); + Assert.Equal("0.123 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s3", culture)); + Assert.Equal("0.1235 ∆°K/m", new TemperatureGradient(0.123456, TemperatureGradientUnit.KelvinPerMeter).ToString("s4", culture)); + } + + + [Fact] + public void ToString_NullFormat_ThrowsArgumentNullException() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Throws(() => quantity.ToString(null, null, null)); + } + + [Fact] + public void ToString_NullArgs_ThrowsArgumentNullException() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Throws(() => quantity.ToString(null, "g", null)); + } + + [Fact] + public void ToString_NullProvider_EqualsCurrentUICulture() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(quantity.ToString(CultureInfo.CurrentUICulture, "g"), quantity.ToString(null, "g")); + } + + + [Fact] + public void Convert_ToBool_ThrowsInvalidCastException() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Throws(() => Convert.ToBoolean(quantity)); + } + + [Fact] + public void Convert_ToByte_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((byte)quantity.Value, Convert.ToByte(quantity)); + } + + [Fact] + public void Convert_ToChar_ThrowsInvalidCastException() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Throws(() => Convert.ToChar(quantity)); + } + + [Fact] + public void Convert_ToDateTime_ThrowsInvalidCastException() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Throws(() => Convert.ToDateTime(quantity)); + } + + [Fact] + public void Convert_ToDecimal_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((decimal)quantity.Value, Convert.ToDecimal(quantity)); + } + + [Fact] + public void Convert_ToDouble_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((double)quantity.Value, Convert.ToDouble(quantity)); + } + + [Fact] + public void Convert_ToInt16_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((short)quantity.Value, Convert.ToInt16(quantity)); + } + + [Fact] + public void Convert_ToInt32_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((int)quantity.Value, Convert.ToInt32(quantity)); + } + + [Fact] + public void Convert_ToInt64_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((long)quantity.Value, Convert.ToInt64(quantity)); + } + + [Fact] + public void Convert_ToSByte_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((sbyte)quantity.Value, Convert.ToSByte(quantity)); + } + + [Fact] + public void Convert_ToSingle_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((float)quantity.Value, Convert.ToSingle(quantity)); + } + + [Fact] + public void Convert_ToString_EqualsToString() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(quantity.ToString(), Convert.ToString(quantity)); + } + + [Fact] + public void Convert_ToUInt16_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((ushort)quantity.Value, Convert.ToUInt16(quantity)); + } + + [Fact] + public void Convert_ToUInt32_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((uint)quantity.Value, Convert.ToUInt32(quantity)); + } + + [Fact] + public void Convert_ToUInt64_EqualsValueAsSameType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal((ulong)quantity.Value, Convert.ToUInt64(quantity)); + } + + [Fact] + public void Convert_ChangeType_SelfType_EqualsSelf() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(quantity, Convert.ChangeType(quantity, typeof(TemperatureGradient))); + } + + [Fact] + public void Convert_ChangeType_UnitType_EqualsUnit() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(quantity.Unit, Convert.ChangeType(quantity, typeof(TemperatureGradientUnit))); + } + + [Fact] + public void Convert_ChangeType_QuantityType_EqualsQuantityType() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(QuantityType.TemperatureGradient, Convert.ChangeType(quantity, typeof(QuantityType))); + } + + [Fact] + public void Convert_ChangeType_QuantityInfo_EqualsQuantityInfo() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(TemperatureGradient.Info, Convert.ChangeType(quantity, typeof(QuantityInfo))); + } + + [Fact] + public void Convert_ChangeType_BaseDimensions_EqualsBaseDimensions() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(TemperatureGradient.BaseDimensions, Convert.ChangeType(quantity, typeof(BaseDimensions))); + } + + [Fact] + public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Throws(() => Convert.ChangeType(quantity, typeof(QuantityFormatter))); + } + + [Fact] + public void GetHashCode_Equals() + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(1.0); + Assert.Equal(new {TemperatureGradient.Info.Name, quantity.Value, quantity.Unit}.GetHashCode(), quantity.GetHashCode()); + } + + [Theory] + [InlineData(1.0)] + [InlineData(-1.0)] + public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value) + { + var quantity = TemperatureGradient.FromKelvinsPerMeter(value); + Assert.Equal(TemperatureGradient.FromKelvinsPerMeter(-value), -quantity); + } + } +} diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureGradient.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureGradient.g.cs new file mode 100644 index 0000000000..00c2b50ddc --- /dev/null +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/TemperatureGradient.g.cs @@ -0,0 +1,631 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Globalization; +using System.Linq; +using JetBrains.Annotations; +using UnitsNet.Units; +using UnitsNet.InternalHelpers; + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// The rate of change of temperature with displacement in a given direction (as with increase of height) + /// + // Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components + // Public structures can't have any members other than public fields, and those fields must be value types or strings. + // Public classes must be sealed (NotInheritable in Visual Basic). If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic. + public sealed partial class TemperatureGradient : IQuantity + { + /// + /// The numeric value this quantity was constructed with. + /// + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + private readonly TemperatureGradientUnit? _unit; + + static TemperatureGradient() + { + BaseDimensions = new BaseDimensions(-1, 0, 0, 0, 1, 0, 0); + Info = new QuantityInfo(QuantityType.TemperatureGradient, Units.Cast().ToArray(), BaseUnit, Zero, BaseDimensions); + } + + /// + /// Creates the quantity with a value of 0 in the base unit KelvinPerMeter. + /// + /// + /// Windows Runtime Component requires a default constructor. + /// + public TemperatureGradient() + { + _value = 0; + _unit = BaseUnit; + } + + /// + /// Creates the quantity with the given numeric value and unit. + /// + /// The numeric value to construct this quantity with. + /// The unit representation to construct this quantity with. + /// Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component. + /// If value is NaN or Infinity. + private TemperatureGradient(double value, TemperatureGradientUnit unit) + { + if(unit == TemperatureGradientUnit.Undefined) + throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); + + _value = Guard.EnsureValidNumber(value, nameof(value)); + _unit = unit; + } + + #region Static Properties + + /// + /// Information about the quantity type, such as unit values and names. + /// + internal static QuantityInfo Info { get; } + + /// + /// The of this quantity. + /// + public static BaseDimensions BaseDimensions { get; } + + /// + /// The base unit of TemperatureGradient, which is KelvinPerMeter. All conversions go via this value. + /// + public static TemperatureGradientUnit BaseUnit { get; } = TemperatureGradientUnit.KelvinPerMeter; + + /// + /// Represents the largest possible value of TemperatureGradient + /// + public static TemperatureGradient MaxValue { get; } = new TemperatureGradient(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of TemperatureGradient + /// + public static TemperatureGradient MinValue { get; } = new TemperatureGradient(double.MinValue, BaseUnit); + + /// + /// The of this quantity. + /// + public static QuantityType QuantityType { get; } = QuantityType.TemperatureGradient; + + /// + /// All units of measurement for the TemperatureGradient quantity. + /// + public static TemperatureGradientUnit[] Units { get; } = Enum.GetValues(typeof(TemperatureGradientUnit)).Cast().Except(new TemperatureGradientUnit[]{ TemperatureGradientUnit.Undefined }).ToArray(); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit KelvinPerMeter. + /// + public static TemperatureGradient Zero { get; } = new TemperatureGradient(0, BaseUnit); + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => Convert.ToDouble(_value); + + /// + object IQuantity.Unit => Unit; + + /// + /// The unit this quantity was constructed with -or- if default ctor was used. + /// + public TemperatureGradientUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + internal QuantityInfo QuantityInfo => Info; + + /// + /// The of this quantity. + /// + public QuantityType Type => TemperatureGradient.QuantityType; + + /// + /// The of this quantity. + /// + public BaseDimensions Dimensions => TemperatureGradient.BaseDimensions; + + #endregion + + #region Conversion Properties + + /// + /// Get TemperatureGradient in DegreesCelciusPerKilometer. + /// + public double DegreesCelciusPerKilometer => As(TemperatureGradientUnit.DegreeCelsiusPerKilometer); + + /// + /// Get TemperatureGradient in DegreesCelciusPerMeter. + /// + public double DegreesCelciusPerMeter => As(TemperatureGradientUnit.DegreeCelsiusPerMeter); + + /// + /// Get TemperatureGradient in DegreesFahrenheitPerFoot. + /// + public double DegreesFahrenheitPerFoot => As(TemperatureGradientUnit.DegreeFahrenheitPerFoot); + + /// + /// Get TemperatureGradient in KelvinsPerMeter. + /// + public double KelvinsPerMeter => As(TemperatureGradientUnit.KelvinPerMeter); + + #endregion + + #region Static Methods + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(TemperatureGradientUnit unit) + { + return GetAbbreviation(unit, null); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static string GetAbbreviation(TemperatureGradientUnit unit, [CanBeNull] string cultureName) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Get TemperatureGradient from DegreesCelciusPerKilometer. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static TemperatureGradient FromDegreesCelciusPerKilometer(double degreescelciusperkilometer) + { + double value = (double) degreescelciusperkilometer; + return new TemperatureGradient(value, TemperatureGradientUnit.DegreeCelsiusPerKilometer); + } + /// + /// Get TemperatureGradient from DegreesCelciusPerMeter. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static TemperatureGradient FromDegreesCelciusPerMeter(double degreescelciuspermeter) + { + double value = (double) degreescelciuspermeter; + return new TemperatureGradient(value, TemperatureGradientUnit.DegreeCelsiusPerMeter); + } + /// + /// Get TemperatureGradient from DegreesFahrenheitPerFoot. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static TemperatureGradient FromDegreesFahrenheitPerFoot(double degreesfahrenheitperfoot) + { + double value = (double) degreesfahrenheitperfoot; + return new TemperatureGradient(value, TemperatureGradientUnit.DegreeFahrenheitPerFoot); + } + /// + /// Get TemperatureGradient from KelvinsPerMeter. + /// + /// If value is NaN or Infinity. + [Windows.Foundation.Metadata.DefaultOverload] + public static TemperatureGradient FromKelvinsPerMeter(double kelvinspermeter) + { + double value = (double) kelvinspermeter; + return new TemperatureGradient(value, TemperatureGradientUnit.KelvinPerMeter); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// TemperatureGradient unit value. + // Fix name conflict with parameter "value" + [return: System.Runtime.InteropServices.WindowsRuntime.ReturnValueName("returnValue")] + public static TemperatureGradient From(double value, TemperatureGradientUnit fromUnit) + { + return new TemperatureGradient((double)value, fromUnit); + } + + #endregion + + #region Static Parse Methods + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + public static TemperatureGradient Parse(string str) + { + return Parse(str, null); + } + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static TemperatureGradient Parse(string str, [CanBeNull] string cultureName) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return QuantityParser.Default.Parse( + str, + provider, + From); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + public static bool TryParse([CanBeNull] string str, out TemperatureGradient result) + { + return TryParse(str, null, out result); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// True if successful, otherwise false. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static bool TryParse([CanBeNull] string str, [CanBeNull] string cultureName, out TemperatureGradient result) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static TemperatureGradientUnit ParseUnit(string str) + { + return ParseUnit(str, null); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static TemperatureGradientUnit ParseUnit(string str, [CanBeNull] string cultureName) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return UnitParser.Default.Parse(str, provider); + } + + public static bool TryParseUnit(string str, out TemperatureGradientUnit unit) + { + return TryParseUnit(str, null, out unit); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// The parsed unit if successful. + /// True if successful, otherwise false. + /// + /// Length.TryParseUnit("m", new CultureInfo("en-US")); + /// + /// Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to if null. + public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out TemperatureGradientUnit unit) + { + IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + return UnitParser.Default.TryParse(str, provider, out unit); + } + + #endregion + + #region Equality / IComparable + + public int CompareTo(object obj) + { + if(obj is null) throw new ArgumentNullException(nameof(obj)); + if(!(obj is TemperatureGradient objTemperatureGradient)) throw new ArgumentException("Expected type TemperatureGradient.", nameof(obj)); + + return CompareTo(objTemperatureGradient); + } + + // Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods + internal int CompareTo(TemperatureGradient other) + { + return _value.CompareTo(other.AsBaseNumericType(this.Unit)); + } + + [Windows.Foundation.Metadata.DefaultOverload] + public override bool Equals(object obj) + { + if(obj is null || !(obj is TemperatureGradient objTemperatureGradient)) + return false; + + return Equals(objTemperatureGradient); + } + + public bool Equals(TemperatureGradient other) + { + return _value.Equals(other.AsBaseNumericType(this.Unit)); + } + + /// + /// + /// Compare equality to another TemperatureGradient within the given absolute or relative tolerance. + /// + /// + /// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a percentage of this quantity's value. will be converted into + /// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of + /// this quantity's value to be considered equal. + /// + /// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Relative); + /// + /// + /// + /// + /// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a fixed number in this quantity's unit. will be converted into + /// this quantity's unit for comparison. + /// + /// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Absolute); + /// + /// + /// + /// + /// Note that it is advised against specifying zero difference, due to the nature + /// of floating point operations and using System.Double internally. + /// + /// + /// The other quantity to compare to. + /// The absolute or relative tolerance value. Must be greater than or equal to 0. + /// The comparison type: either relative or absolute. + /// True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance. + public bool Equals(TemperatureGradient other, double tolerance, ComparisonType comparisonType) + { + if(tolerance < 0) + throw new ArgumentOutOfRangeException("tolerance", "Tolerance must be greater than or equal to 0."); + + double thisValue = (double)this.Value; + double otherValueInThisUnits = other.As(this.Unit); + + return UnitsNet.Comparison.Equals(thisValue, otherValueInThisUnits, tolerance, comparisonType); + } + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current TemperatureGradient. + public override int GetHashCode() + { + return new { QuantityType, Value, Unit }.GetHashCode(); + } + + #endregion + + #region Conversion Methods + + double IQuantity.As(object unit) => As((TemperatureGradientUnit)unit); + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(TemperatureGradientUnit unit) + { + if(Unit == unit) + return Convert.ToDouble(Value); + + var converted = AsBaseNumericType(unit); + return Convert.ToDouble(converted); + } + + /// + /// Converts this TemperatureGradient to another TemperatureGradient with the unit representation . + /// + /// A TemperatureGradient with the specified unit. + public TemperatureGradient ToUnit(TemperatureGradientUnit unit) + { + var convertedValue = AsBaseNumericType(unit); + return new TemperatureGradient(convertedValue, unit); + } + + /// + /// 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 double AsBaseUnit() + { + switch(Unit) + { + case TemperatureGradientUnit.DegreeCelsiusPerKilometer: return _value / 1e3; + case TemperatureGradientUnit.DegreeCelsiusPerMeter: return _value; + case TemperatureGradientUnit.DegreeFahrenheitPerFoot: return (_value / 0.3048) * 5/9; + case TemperatureGradientUnit.KelvinPerMeter: return _value; + default: + throw new NotImplementedException($"Can not convert {Unit} to base units."); + } + } + + private double AsBaseNumericType(TemperatureGradientUnit unit) + { + if(Unit == unit) + return _value; + + var baseUnitValue = AsBaseUnit(); + + switch(unit) + { + case TemperatureGradientUnit.DegreeCelsiusPerKilometer: return baseUnitValue * 1e3; + case TemperatureGradientUnit.DegreeCelsiusPerMeter: return baseUnitValue; + case TemperatureGradientUnit.DegreeFahrenheitPerFoot: return (baseUnitValue * 0.3048) * 9/5; + case TemperatureGradientUnit.KelvinPerMeter: return baseUnitValue; + default: + throw new NotImplementedException($"Can not convert {Unit} to {unit}."); + } + } + + #endregion + + #region ToString Methods + + /// + /// Get default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString(null); + } + + /// + /// Get string representation of value and unit. Using two significant digits after radix. + /// + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + public string ToString([CanBeNull] string cultureName) + { + var provider = cultureName; + return ToString(provider, 2); + } + + /// + /// Get string representation of value and unit. + /// + /// The number of significant digits after the radix point. + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + public string ToString(string cultureName, int significantDigitsAfterRadix) + { + var provider = cultureName; + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); + } + + /// + /// Get string representation of value and unit. + /// + /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." + /// Arguments for string format. Value and unit are implicitly included as arguments 0 and 1. + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + public string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) + { + var provider = GetFormatProviderFromCultureName(cultureName); + if (format == null) throw new ArgumentNullException(nameof(format)); + if (args == null) throw new ArgumentNullException(nameof(args)); + + provider = provider ?? GlobalConfiguration.DefaultCulture; + + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); + return string.Format(provider, format, formatArgs); + } + + #endregion + + private static IFormatProvider GetFormatProviderFromCultureName([CanBeNull] string cultureName) + { + return cultureName != null ? new CultureInfo(cultureName) : (IFormatProvider)null; + } + } +} diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs index 77af9b86c2..804ca59569 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantity.g.cs @@ -335,6 +335,9 @@ internal static bool TryFrom(double value, Enum unit, out IQuantity quantity) case TemperatureDeltaUnit temperatureDeltaUnit: quantity = TemperatureDelta.From(value, temperatureDeltaUnit); return true; + case TemperatureGradientUnit temperatureGradientUnit: + quantity = TemperatureGradient.From(value, temperatureGradientUnit); + return true; case ThermalConductivityUnit thermalConductivityUnit: quantity = ThermalConductivity.From(value, thermalConductivityUnit); return true; @@ -715,6 +718,9 @@ internal static bool TryParse([CanBeNull] IFormatProvider formatProvider, Type q if (quantityType == typeof(TemperatureDelta)) return parser.TryParse(quantityString, formatProvider, TemperatureDelta.From, out quantity); + if (quantityType == typeof(TemperatureGradient)) + return parser.TryParse(quantityString, formatProvider, TemperatureGradient.From, out quantity); + if (quantityType == typeof(ThermalConductivity)) return parser.TryParse(quantityString, formatProvider, ThermalConductivity.From, out quantity); diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs index 1ec94a2e45..a8c7540e90 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/QuantityType.g.cs @@ -127,6 +127,7 @@ public enum QuantityType Temperature, TemperatureChangeRate, TemperatureDelta, + TemperatureGradient, ThermalConductivity, ThermalResistance, Torque, diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs index dde9ff522c..92cdcd6242 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs @@ -1444,6 +1444,10 @@ private static readonly (string CultureName, Type UnitType, int UnitValue, strin ("en-US", typeof(TemperatureDeltaUnit), (int)TemperatureDeltaUnit.DegreeRoemer, new string[]{"∆°Rø"}), ("en-US", typeof(TemperatureDeltaUnit), (int)TemperatureDeltaUnit.Kelvin, new string[]{"∆K"}), ("en-US", typeof(TemperatureDeltaUnit), (int)TemperatureDeltaUnit.MillidegreeCelsius, new string[]{"∆m°C"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.DegreeCelsiusPerKilometer, new string[]{"∆°C/km"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.DegreeCelsiusPerMeter, new string[]{"∆°C/m"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.DegreeFahrenheitPerFoot, new string[]{"∆°F/ft"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.KelvinPerMeter, new string[]{"∆°K/m"}), ("en-US", typeof(ThermalConductivityUnit), (int)ThermalConductivityUnit.BtuPerHourFootFahrenheit, new string[]{"BTU/h·ft·°F"}), ("en-US", typeof(ThermalConductivityUnit), (int)ThermalConductivityUnit.WattPerMeterKelvin, new string[]{"W/m·K"}), ("en-US", typeof(ThermalResistanceUnit), (int)ThermalResistanceUnit.HourSquareFeetDegreeFahrenheitPerBtu, new string[]{"Hrft²°F/Btu"}), diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/TemperatureGradientUnit.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/TemperatureGradientUnit.g.cs new file mode 100644 index 0000000000..96522d6572 --- /dev/null +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Units/TemperatureGradientUnit.g.cs @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum TemperatureGradientUnit + { + Undefined = 0, + DegreeCelsiusPerKilometer, + DegreeCelsiusPerMeter, + DegreeFahrenheitPerFoot, + KelvinPerMeter, + } + + #pragma warning restore 1591 +} diff --git a/UnitsNet/GeneratedCode/Quantities/TemperatureGradient.g.cs b/UnitsNet/GeneratedCode/Quantities/TemperatureGradient.g.cs new file mode 100644 index 0000000000..e5a03e343e --- /dev/null +++ b/UnitsNet/GeneratedCode/Quantities/TemperatureGradient.g.cs @@ -0,0 +1,912 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Globalization; +using System.Linq; +using System.Runtime.Serialization; +using JetBrains.Annotations; +using UnitsNet.InternalHelpers; +using UnitsNet.Units; + +#nullable enable + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// + /// The rate of change of temperature with displacement in a given direction (as with increase of height) + /// + [DataContract] + public partial struct TemperatureGradient : IQuantity, IEquatable, IComparable, IComparable, IConvertible, IFormattable + { + /// + /// The numeric value this quantity was constructed with. + /// + [DataMember(Name = "Value", Order = 0)] + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + [DataMember(Name = "Unit", Order = 1)] + private readonly TemperatureGradientUnit? _unit; + + static TemperatureGradient() + { + BaseDimensions = new BaseDimensions(-1, 0, 0, 0, 1, 0, 0); + + Info = new QuantityInfo("TemperatureGradient", + new UnitInfo[] { + new UnitInfo(TemperatureGradientUnit.DegreeCelsiusPerKilometer, "DegreesCelciusPerKilometer", new BaseUnits(length: LengthUnit.Kilometer, temperature: TemperatureUnit.DegreeCelsius)), + new UnitInfo(TemperatureGradientUnit.DegreeCelsiusPerMeter, "DegreesCelciusPerMeter", new BaseUnits(length: LengthUnit.Meter, temperature: TemperatureUnit.DegreeCelsius)), + new UnitInfo(TemperatureGradientUnit.DegreeFahrenheitPerFoot, "DegreesFahrenheitPerFoot", new BaseUnits(length: LengthUnit.Foot, temperature: TemperatureUnit.DegreeFahrenheit)), + new UnitInfo(TemperatureGradientUnit.KelvinPerMeter, "KelvinsPerMeter", new BaseUnits(length: LengthUnit.Meter, temperature: TemperatureUnit.Kelvin)), + }, + BaseUnit, Zero, BaseDimensions, QuantityType.TemperatureGradient); + } + + /// + /// Creates the quantity with the given numeric value and unit. + /// + /// The numeric value to construct this quantity with. + /// The unit representation to construct this quantity with. + /// If value is NaN or Infinity. + public TemperatureGradient(double value, TemperatureGradientUnit unit) + { + if(unit == TemperatureGradientUnit.Undefined) + throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); + + _value = Guard.EnsureValidNumber(value, nameof(value)); + _unit = unit; + } + + /// + /// Creates an instance of the quantity with the given numeric value in units compatible with the given . + /// If multiple compatible units were found, the first match is used. + /// + /// The numeric value to construct this quantity with. + /// The unit system to create the quantity with. + /// The given is null. + /// No unit was found for the given . + public TemperatureGradient(double value, UnitSystem unitSystem) + { + if(unitSystem is null) throw new ArgumentNullException(nameof(unitSystem)); + + var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits); + var firstUnitInfo = unitInfos.FirstOrDefault(); + + _value = Guard.EnsureValidNumber(value, nameof(value)); + _unit = firstUnitInfo?.Value ?? throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem)); + } + + #region Static Properties + + /// + public static QuantityInfo Info { get; } + + /// + /// The of this quantity. + /// + public static BaseDimensions BaseDimensions { get; } + + /// + /// The base unit of TemperatureGradient, which is KelvinPerMeter. All conversions go via this value. + /// + public static TemperatureGradientUnit BaseUnit { get; } = TemperatureGradientUnit.KelvinPerMeter; + + /// + /// Represents the largest possible value of TemperatureGradient + /// + [Obsolete("MaxValue and MinValue will be removed. Choose your own value or use nullability for unbounded lower/upper range checks. See discussion in https://github.com/angularsen/UnitsNet/issues/848.")] + public static TemperatureGradient MaxValue { get; } = new TemperatureGradient(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of TemperatureGradient + /// + [Obsolete("MaxValue and MinValue will be removed. Choose your own value or use nullability for unbounded lower/upper range checks. See discussion in https://github.com/angularsen/UnitsNet/issues/848.")] + public static TemperatureGradient MinValue { get; } = new TemperatureGradient(double.MinValue, BaseUnit); + + /// + /// The of this quantity. + /// + [Obsolete("QuantityType will be removed in the future. Use Info property instead.")] + public static QuantityType QuantityType { get; } = QuantityType.TemperatureGradient; + + /// + /// All units of measurement for the TemperatureGradient quantity. + /// + public static TemperatureGradientUnit[] Units { get; } = Enum.GetValues(typeof(TemperatureGradientUnit)).Cast().Except(new TemperatureGradientUnit[]{ TemperatureGradientUnit.Undefined }).ToArray(); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit KelvinPerMeter. + /// + public static TemperatureGradient Zero { get; } = new TemperatureGradient(0, BaseUnit); + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + Enum IQuantity.Unit => Unit; + + /// + public TemperatureGradientUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + /// + public QuantityInfo QuantityInfo => Info; + + /// + QuantityInfo IQuantity.QuantityInfo => Info; + + /// + /// The of this quantity. + /// + public QuantityType Type => TemperatureGradient.QuantityType; + + /// + /// The of this quantity. + /// + public BaseDimensions Dimensions => TemperatureGradient.BaseDimensions; + + #endregion + + #region Conversion Properties + + /// + /// Get TemperatureGradient in DegreesCelciusPerKilometer. + /// + public double DegreesCelciusPerKilometer => As(TemperatureGradientUnit.DegreeCelsiusPerKilometer); + + /// + /// Get TemperatureGradient in DegreesCelciusPerMeter. + /// + public double DegreesCelciusPerMeter => As(TemperatureGradientUnit.DegreeCelsiusPerMeter); + + /// + /// Get TemperatureGradient in DegreesFahrenheitPerFoot. + /// + public double DegreesFahrenheitPerFoot => As(TemperatureGradientUnit.DegreeFahrenheitPerFoot); + + /// + /// Get TemperatureGradient in KelvinsPerMeter. + /// + public double KelvinsPerMeter => As(TemperatureGradientUnit.KelvinPerMeter); + + #endregion + + #region Static Methods + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(TemperatureGradientUnit unit) + { + return GetAbbreviation(unit, null); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + /// Format to use for localization. Defaults to if null. + public static string GetAbbreviation(TemperatureGradientUnit unit, IFormatProvider? provider) + { + return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Get TemperatureGradient from DegreesCelciusPerKilometer. + /// + /// If value is NaN or Infinity. + public static TemperatureGradient FromDegreesCelciusPerKilometer(QuantityValue degreescelciusperkilometer) + { + double value = (double) degreescelciusperkilometer; + return new TemperatureGradient(value, TemperatureGradientUnit.DegreeCelsiusPerKilometer); + } + /// + /// Get TemperatureGradient from DegreesCelciusPerMeter. + /// + /// If value is NaN or Infinity. + public static TemperatureGradient FromDegreesCelciusPerMeter(QuantityValue degreescelciuspermeter) + { + double value = (double) degreescelciuspermeter; + return new TemperatureGradient(value, TemperatureGradientUnit.DegreeCelsiusPerMeter); + } + /// + /// Get TemperatureGradient from DegreesFahrenheitPerFoot. + /// + /// If value is NaN or Infinity. + public static TemperatureGradient FromDegreesFahrenheitPerFoot(QuantityValue degreesfahrenheitperfoot) + { + double value = (double) degreesfahrenheitperfoot; + return new TemperatureGradient(value, TemperatureGradientUnit.DegreeFahrenheitPerFoot); + } + /// + /// Get TemperatureGradient from KelvinsPerMeter. + /// + /// If value is NaN or Infinity. + public static TemperatureGradient FromKelvinsPerMeter(QuantityValue kelvinspermeter) + { + double value = (double) kelvinspermeter; + return new TemperatureGradient(value, TemperatureGradientUnit.KelvinPerMeter); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// TemperatureGradient unit value. + public static TemperatureGradient From(QuantityValue value, TemperatureGradientUnit fromUnit) + { + return new TemperatureGradient((double)value, fromUnit); + } + + #endregion + + #region Static Parse Methods + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + public static TemperatureGradient Parse(string str) + { + return Parse(str, null); + } + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// + /// Expected string to have one or two pairs of quantity and unit in the format + /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" + /// + /// + /// More than one unit is represented by the specified unit abbreviation. + /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of + /// , and . + /// + /// + /// If anything else goes wrong, typically due to a bug or unhandled case. + /// We wrap exceptions in to allow you to distinguish + /// Units.NET exceptions from other exceptions. + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static TemperatureGradient Parse(string str, IFormatProvider? provider) + { + return QuantityParser.Default.Parse( + str, + provider, + From); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + public static bool TryParse(string? str, out TemperatureGradient result) + { + return TryParse(str, null, out result); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// True if successful, otherwise false. + /// + /// Length.Parse("5.5 m", new CultureInfo("en-US")); + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static bool TryParse(string? str, IFormatProvider? provider, out TemperatureGradient result) + { + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static TemperatureGradientUnit ParseUnit(string str) + { + return ParseUnit(str, null); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// Format to use when parsing number and unit. Defaults to if null. + /// + /// Length.ParseUnit("m", new CultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static TemperatureGradientUnit ParseUnit(string str, IFormatProvider? provider) + { + return UnitParser.Default.Parse(str, provider); + } + + /// + public static bool TryParseUnit(string str, out TemperatureGradientUnit unit) + { + return TryParseUnit(str, null, out unit); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// The parsed unit if successful. + /// True if successful, otherwise false. + /// + /// Length.TryParseUnit("m", new CultureInfo("en-US")); + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static bool TryParseUnit(string str, IFormatProvider? provider, out TemperatureGradientUnit unit) + { + return UnitParser.Default.TryParse(str, provider, out unit); + } + + #endregion + + #region Arithmetic Operators + + /// Negate the value. + public static TemperatureGradient operator -(TemperatureGradient right) + { + return new TemperatureGradient(-right.Value, right.Unit); + } + + /// Get from adding two . + public static TemperatureGradient operator +(TemperatureGradient left, TemperatureGradient right) + { + return new TemperatureGradient(left.Value + right.GetValueAs(left.Unit), left.Unit); + } + + /// Get from subtracting two . + public static TemperatureGradient operator -(TemperatureGradient left, TemperatureGradient right) + { + return new TemperatureGradient(left.Value - right.GetValueAs(left.Unit), left.Unit); + } + + /// Get from multiplying value and . + public static TemperatureGradient operator *(double left, TemperatureGradient right) + { + return new TemperatureGradient(left * right.Value, right.Unit); + } + + /// Get from multiplying value and . + public static TemperatureGradient operator *(TemperatureGradient left, double right) + { + return new TemperatureGradient(left.Value * right, left.Unit); + } + + /// Get from dividing by value. + public static TemperatureGradient operator /(TemperatureGradient left, double right) + { + return new TemperatureGradient(left.Value / right, left.Unit); + } + + /// Get ratio value from dividing by . + public static double operator /(TemperatureGradient left, TemperatureGradient right) + { + return left.KelvinsPerMeter / right.KelvinsPerMeter; + } + + #endregion + + #region Equality / IComparable + + /// Returns true if less or equal to. + public static bool operator <=(TemperatureGradient left, TemperatureGradient right) + { + return left.Value <= right.GetValueAs(left.Unit); + } + + /// Returns true if greater than or equal to. + public static bool operator >=(TemperatureGradient left, TemperatureGradient right) + { + return left.Value >= right.GetValueAs(left.Unit); + } + + /// Returns true if less than. + public static bool operator <(TemperatureGradient left, TemperatureGradient right) + { + return left.Value < right.GetValueAs(left.Unit); + } + + /// Returns true if greater than. + public static bool operator >(TemperatureGradient left, TemperatureGradient right) + { + return left.Value > right.GetValueAs(left.Unit); + } + + /// Returns true if exactly equal. + /// Consider using for safely comparing floating point values. + public static bool operator ==(TemperatureGradient left, TemperatureGradient right) + { + return left.Equals(right); + } + + /// Returns true if not exactly equal. + /// Consider using for safely comparing floating point values. + public static bool operator !=(TemperatureGradient left, TemperatureGradient right) + { + return !(left == right); + } + + /// + public int CompareTo(object obj) + { + if(obj is null) throw new ArgumentNullException(nameof(obj)); + if(!(obj is TemperatureGradient objTemperatureGradient)) throw new ArgumentException("Expected type TemperatureGradient.", nameof(obj)); + + return CompareTo(objTemperatureGradient); + } + + /// + public int CompareTo(TemperatureGradient other) + { + return _value.CompareTo(other.GetValueAs(this.Unit)); + } + + /// + /// Consider using for safely comparing floating point values. + public override bool Equals(object obj) + { + if(obj is null || !(obj is TemperatureGradient objTemperatureGradient)) + return false; + + return Equals(objTemperatureGradient); + } + + /// + /// Consider using for safely comparing floating point values. + public bool Equals(TemperatureGradient other) + { + return _value.Equals(other.GetValueAs(this.Unit)); + } + + /// + /// + /// Compare equality to another TemperatureGradient within the given absolute or relative tolerance. + /// + /// + /// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a percentage of this quantity's value. will be converted into + /// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of + /// this quantity's value to be considered equal. + /// + /// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Relative); + /// + /// + /// + /// + /// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a fixed number in this quantity's unit. will be converted into + /// this quantity's unit for comparison. + /// + /// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Absolute); + /// + /// + /// + /// + /// Note that it is advised against specifying zero difference, due to the nature + /// of floating point operations and using System.Double internally. + /// + /// + /// The other quantity to compare to. + /// The absolute or relative tolerance value. Must be greater than or equal to 0. + /// The comparison type: either relative or absolute. + /// True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance. + public bool Equals(TemperatureGradient other, double tolerance, ComparisonType comparisonType) + { + if(tolerance < 0) + throw new ArgumentOutOfRangeException("tolerance", "Tolerance must be greater than or equal to 0."); + + double thisValue = (double)this.Value; + double otherValueInThisUnits = other.As(this.Unit); + + return UnitsNet.Comparison.Equals(thisValue, otherValueInThisUnits, tolerance, comparisonType); + } + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current TemperatureGradient. + public override int GetHashCode() + { + return new { Info.Name, Value, Unit }.GetHashCode(); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(TemperatureGradientUnit unit) + { + if(Unit == unit) + return Convert.ToDouble(Value); + + var converted = GetValueAs(unit); + return Convert.ToDouble(converted); + } + + /// + public double As(UnitSystem unitSystem) + { + if(unitSystem is null) + throw new ArgumentNullException(nameof(unitSystem)); + + var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits); + + var firstUnitInfo = unitInfos.FirstOrDefault(); + if(firstUnitInfo == null) + throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem)); + + return As(firstUnitInfo.Value); + } + + /// + double IQuantity.As(Enum unit) + { + if(!(unit is TemperatureGradientUnit unitAsTemperatureGradientUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(TemperatureGradientUnit)} is supported.", nameof(unit)); + + return As(unitAsTemperatureGradientUnit); + } + + /// + /// Converts this TemperatureGradient to another TemperatureGradient with the unit representation . + /// + /// A TemperatureGradient with the specified unit. + public TemperatureGradient ToUnit(TemperatureGradientUnit unit) + { + var convertedValue = GetValueAs(unit); + return new TemperatureGradient(convertedValue, unit); + } + + /// + IQuantity IQuantity.ToUnit(Enum unit) + { + if(!(unit is TemperatureGradientUnit unitAsTemperatureGradientUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(TemperatureGradientUnit)} is supported.", nameof(unit)); + + return ToUnit(unitAsTemperatureGradientUnit); + } + + /// + public TemperatureGradient ToUnit(UnitSystem unitSystem) + { + if(unitSystem is null) + throw new ArgumentNullException(nameof(unitSystem)); + + var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits); + + var firstUnitInfo = unitInfos.FirstOrDefault(); + if(firstUnitInfo == null) + throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem)); + + return ToUnit(firstUnitInfo.Value); + } + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + IQuantity IQuantity.ToUnit(TemperatureGradientUnit unit) => ToUnit(unit); + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + /// 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 double GetValueInBaseUnit() + { + switch(Unit) + { + case TemperatureGradientUnit.DegreeCelsiusPerKilometer: return _value / 1e3; + case TemperatureGradientUnit.DegreeCelsiusPerMeter: return _value; + case TemperatureGradientUnit.DegreeFahrenheitPerFoot: return (_value / 0.3048) * 5/9; + case TemperatureGradientUnit.KelvinPerMeter: return _value; + 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 TemperatureGradient ToBaseUnit() + { + var baseUnitValue = GetValueInBaseUnit(); + return new TemperatureGradient(baseUnitValue, BaseUnit); + } + + private double GetValueAs(TemperatureGradientUnit unit) + { + if(Unit == unit) + return _value; + + var baseUnitValue = GetValueInBaseUnit(); + + switch(unit) + { + case TemperatureGradientUnit.DegreeCelsiusPerKilometer: return baseUnitValue * 1e3; + case TemperatureGradientUnit.DegreeCelsiusPerMeter: return baseUnitValue; + case TemperatureGradientUnit.DegreeFahrenheitPerFoot: return (baseUnitValue * 0.3048) * 9/5; + case TemperatureGradientUnit.KelvinPerMeter: return baseUnitValue; + default: + throw new NotImplementedException($"Can not convert {Unit} to {unit}."); + } + } + + #endregion + + #region ToString Methods + + /// + /// Gets the default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString("g"); + } + + /// + /// Gets the default string representation of value and unit using the given format provider. + /// + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + public string ToString(IFormatProvider? provider) + { + return ToString("g", provider); + } + + /// + /// Get string representation of value and unit. + /// + /// The number of significant digits after the radix point. + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + [Obsolete(@"This method is deprecated and will be removed at a future release. Please use ToString(""s2"") or ToString(""s2"", provider) where 2 is an example of the number passed to significantDigitsAfterRadix.")] + public string ToString(IFormatProvider? provider, int significantDigitsAfterRadix) + { + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); + } + + /// + /// Get string representation of value and unit. + /// + /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." + /// Arguments for string format. Value and unit are implicitly included as arguments 0 and 1. + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + [Obsolete("This method is deprecated and will be removed at a future release. Please use string.Format().")] + public string ToString(IFormatProvider? provider, [NotNull] string format, [NotNull] params object[] args) + { + if (format == null) throw new ArgumentNullException(nameof(format)); + if (args == null) throw new ArgumentNullException(nameof(args)); + + provider = provider ?? CultureInfo.CurrentUICulture; + + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); + return string.Format(provider, format, formatArgs); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using . + /// + /// The format string. + /// The string representation. + public string ToString(string format) + { + return ToString(format, CultureInfo.CurrentUICulture); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using the specified format provider, or if null. + /// + /// The format string. + /// Format to use for localization and number formatting. Defaults to if null. + /// The string representation. + public string ToString(string format, IFormatProvider? provider) + { + return QuantityFormatter.Format(this, format, provider); + } + + #endregion + + #region IConvertible Methods + + TypeCode IConvertible.GetTypeCode() + { + return TypeCode.Object; + } + + bool IConvertible.ToBoolean(IFormatProvider provider) + { + throw new InvalidCastException($"Converting {typeof(TemperatureGradient)} to bool is not supported."); + } + + byte IConvertible.ToByte(IFormatProvider provider) + { + return Convert.ToByte(_value); + } + + char IConvertible.ToChar(IFormatProvider provider) + { + throw new InvalidCastException($"Converting {typeof(TemperatureGradient)} to char is not supported."); + } + + DateTime IConvertible.ToDateTime(IFormatProvider provider) + { + throw new InvalidCastException($"Converting {typeof(TemperatureGradient)} to DateTime is not supported."); + } + + decimal IConvertible.ToDecimal(IFormatProvider provider) + { + return Convert.ToDecimal(_value); + } + + double IConvertible.ToDouble(IFormatProvider provider) + { + return Convert.ToDouble(_value); + } + + short IConvertible.ToInt16(IFormatProvider provider) + { + return Convert.ToInt16(_value); + } + + int IConvertible.ToInt32(IFormatProvider provider) + { + return Convert.ToInt32(_value); + } + + long IConvertible.ToInt64(IFormatProvider provider) + { + return Convert.ToInt64(_value); + } + + sbyte IConvertible.ToSByte(IFormatProvider provider) + { + return Convert.ToSByte(_value); + } + + float IConvertible.ToSingle(IFormatProvider provider) + { + return Convert.ToSingle(_value); + } + + string IConvertible.ToString(IFormatProvider provider) + { + return ToString("g", provider); + } + + object IConvertible.ToType(Type conversionType, IFormatProvider provider) + { + if(conversionType == typeof(TemperatureGradient)) + return this; + else if(conversionType == typeof(TemperatureGradientUnit)) + return Unit; + else if(conversionType == typeof(QuantityType)) + return TemperatureGradient.QuantityType; + else if(conversionType == typeof(QuantityInfo)) + return TemperatureGradient.Info; + else if(conversionType == typeof(BaseDimensions)) + return TemperatureGradient.BaseDimensions; + else + throw new InvalidCastException($"Converting {typeof(TemperatureGradient)} to {conversionType} is not supported."); + } + + ushort IConvertible.ToUInt16(IFormatProvider provider) + { + return Convert.ToUInt16(_value); + } + + uint IConvertible.ToUInt32(IFormatProvider provider) + { + return Convert.ToUInt32(_value); + } + + ulong IConvertible.ToUInt64(IFormatProvider provider) + { + return Convert.ToUInt64(_value); + } + + #endregion + } +} diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index 44f07e2aa5..9eacfe820d 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -136,6 +136,7 @@ public static partial class Quantity { "Temperature", Temperature.Info }, { "TemperatureChangeRate", TemperatureChangeRate.Info }, { "TemperatureDelta", TemperatureDelta.Info }, + { "TemperatureGradient", TemperatureGradient.Info }, { "ThermalConductivity", ThermalConductivity.Info }, { "ThermalResistance", ThermalResistance.Info }, { "Torque", Torque.Info }, @@ -252,6 +253,7 @@ public static partial class Quantity { "Temperature", QuantityType.Temperature }, { "TemperatureChangeRate", QuantityType.TemperatureChangeRate }, { "TemperatureDelta", QuantityType.TemperatureDelta }, + { "TemperatureGradient", QuantityType.TemperatureGradient }, { "ThermalConductivity", QuantityType.ThermalConductivity }, { "ThermalResistance", QuantityType.ThermalResistance }, { "Torque", QuantityType.Torque }, @@ -473,6 +475,8 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu return TemperatureChangeRate.From(value, TemperatureChangeRate.BaseUnit); case QuantityType.TemperatureDelta: return TemperatureDelta.From(value, TemperatureDelta.BaseUnit); + case QuantityType.TemperatureGradient: + return TemperatureGradient.From(value, TemperatureGradient.BaseUnit); case QuantityType.ThermalConductivity: return ThermalConductivity.From(value, ThermalConductivity.BaseUnit); case QuantityType.ThermalResistance: @@ -708,6 +712,8 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu return TemperatureChangeRate.From(value, TemperatureChangeRate.BaseUnit); case "TemperatureDelta": return TemperatureDelta.From(value, TemperatureDelta.BaseUnit); + case "TemperatureGradient": + return TemperatureGradient.From(value, TemperatureGradient.BaseUnit); case "ThermalConductivity": return ThermalConductivity.From(value, ThermalConductivity.BaseUnit); case "ThermalResistance": @@ -1042,6 +1048,9 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quanti case TemperatureDeltaUnit temperatureDeltaUnit: quantity = TemperatureDelta.From(value, temperatureDeltaUnit); return true; + case TemperatureGradientUnit temperatureGradientUnit: + quantity = TemperatureGradient.From(value, temperatureGradientUnit); + return true; case ThermalConductivityUnit thermalConductivityUnit: quantity = ThermalConductivity.From(value, thermalConductivityUnit); return true; @@ -1301,6 +1310,8 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, return parser.TryParse(quantityString, formatProvider, TemperatureChangeRate.From, out quantity); case Type _ when quantityType == typeof(TemperatureDelta): return parser.TryParse(quantityString, formatProvider, TemperatureDelta.From, out quantity); + case Type _ when quantityType == typeof(TemperatureGradient): + return parser.TryParse(quantityString, formatProvider, TemperatureGradient.From, out quantity); case Type _ when quantityType == typeof(ThermalConductivity): return parser.TryParse(quantityString, formatProvider, ThermalConductivity.From, out quantity); case Type _ when quantityType == typeof(ThermalResistance): diff --git a/UnitsNet/GeneratedCode/QuantityType.g.cs b/UnitsNet/GeneratedCode/QuantityType.g.cs index f9f36fdc40..ebccab0465 100644 --- a/UnitsNet/GeneratedCode/QuantityType.g.cs +++ b/UnitsNet/GeneratedCode/QuantityType.g.cs @@ -129,6 +129,7 @@ public enum QuantityType Temperature, TemperatureChangeRate, TemperatureDelta, + TemperatureGradient, ThermalConductivity, ThermalResistance, Torque, diff --git a/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs b/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs index 0b76e8daf3..84c86f5bce 100644 --- a/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs +++ b/UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs @@ -1444,6 +1444,10 @@ private static readonly (string CultureName, Type UnitType, int UnitValue, strin ("en-US", typeof(TemperatureDeltaUnit), (int)TemperatureDeltaUnit.DegreeRoemer, new string[]{"∆°Rø"}), ("en-US", typeof(TemperatureDeltaUnit), (int)TemperatureDeltaUnit.Kelvin, new string[]{"∆K"}), ("en-US", typeof(TemperatureDeltaUnit), (int)TemperatureDeltaUnit.MillidegreeCelsius, new string[]{"∆m°C"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.DegreeCelsiusPerKilometer, new string[]{"∆°C/km"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.DegreeCelsiusPerMeter, new string[]{"∆°C/m"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.DegreeFahrenheitPerFoot, new string[]{"∆°F/ft"}), + ("en-US", typeof(TemperatureGradientUnit), (int)TemperatureGradientUnit.KelvinPerMeter, new string[]{"∆°K/m"}), ("en-US", typeof(ThermalConductivityUnit), (int)ThermalConductivityUnit.BtuPerHourFootFahrenheit, new string[]{"BTU/h·ft·°F"}), ("en-US", typeof(ThermalConductivityUnit), (int)ThermalConductivityUnit.WattPerMeterKelvin, new string[]{"W/m·K"}), ("en-US", typeof(ThermalResistanceUnit), (int)ThermalResistanceUnit.HourSquareFeetDegreeFahrenheitPerBtu, new string[]{"Hrft²°F/Btu"}), diff --git a/UnitsNet/GeneratedCode/UnitConverter.g.cs b/UnitsNet/GeneratedCode/UnitConverter.g.cs index ce923e6ffd..cd692967f6 100644 --- a/UnitsNet/GeneratedCode/UnitConverter.g.cs +++ b/UnitsNet/GeneratedCode/UnitConverter.g.cs @@ -2186,6 +2186,13 @@ public static void RegisterDefaultConversions(UnitConverter unitConverter) unitConverter.SetConversionFunction(TemperatureDelta.BaseUnit, TemperatureDelta.BaseUnit, q => q); unitConverter.SetConversionFunction(TemperatureDelta.BaseUnit, TemperatureDeltaUnit.MillidegreeCelsius, q => q.ToUnit(TemperatureDeltaUnit.MillidegreeCelsius)); unitConverter.SetConversionFunction(TemperatureDeltaUnit.MillidegreeCelsius, TemperatureDelta.BaseUnit, q => q.ToBaseUnit()); + unitConverter.SetConversionFunction(TemperatureGradient.BaseUnit, TemperatureGradientUnit.DegreeCelsiusPerKilometer, q => q.ToUnit(TemperatureGradientUnit.DegreeCelsiusPerKilometer)); + unitConverter.SetConversionFunction(TemperatureGradientUnit.DegreeCelsiusPerKilometer, TemperatureGradient.BaseUnit, q => q.ToBaseUnit()); + unitConverter.SetConversionFunction(TemperatureGradient.BaseUnit, TemperatureGradientUnit.DegreeCelsiusPerMeter, q => q.ToUnit(TemperatureGradientUnit.DegreeCelsiusPerMeter)); + unitConverter.SetConversionFunction(TemperatureGradientUnit.DegreeCelsiusPerMeter, TemperatureGradient.BaseUnit, q => q.ToBaseUnit()); + unitConverter.SetConversionFunction(TemperatureGradient.BaseUnit, TemperatureGradientUnit.DegreeFahrenheitPerFoot, q => q.ToUnit(TemperatureGradientUnit.DegreeFahrenheitPerFoot)); + unitConverter.SetConversionFunction(TemperatureGradientUnit.DegreeFahrenheitPerFoot, TemperatureGradient.BaseUnit, q => q.ToBaseUnit()); + unitConverter.SetConversionFunction(TemperatureGradient.BaseUnit, TemperatureGradient.BaseUnit, q => q); unitConverter.SetConversionFunction(ThermalConductivity.BaseUnit, ThermalConductivityUnit.BtuPerHourFootFahrenheit, q => q.ToUnit(ThermalConductivityUnit.BtuPerHourFootFahrenheit)); unitConverter.SetConversionFunction(ThermalConductivityUnit.BtuPerHourFootFahrenheit, ThermalConductivity.BaseUnit, q => q.ToBaseUnit()); unitConverter.SetConversionFunction(ThermalConductivity.BaseUnit, ThermalConductivity.BaseUnit, q => q); diff --git a/UnitsNet/GeneratedCode/Units/TemperatureGradientUnit.g.cs b/UnitsNet/GeneratedCode/Units/TemperatureGradientUnit.g.cs new file mode 100644 index 0000000000..96522d6572 --- /dev/null +++ b/UnitsNet/GeneratedCode/Units/TemperatureGradientUnit.g.cs @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum TemperatureGradientUnit + { + Undefined = 0, + DegreeCelsiusPerKilometer, + DegreeCelsiusPerMeter, + DegreeFahrenheitPerFoot, + KelvinPerMeter, + } + + #pragma warning restore 1591 +} diff --git a/UnitsNet/UnitsNet.csproj b/UnitsNet/UnitsNet.csproj index 4e0a560b5e..1803af8cfc 100644 --- a/UnitsNet/UnitsNet.csproj +++ b/UnitsNet/UnitsNet.csproj @@ -60,6 +60,9 @@ + + Common\UnitDefinitions\TemperatureGradient.json + From 99b8a6e49b368c7823cb098aeef16b0275a0bae7 Mon Sep 17 00:00:00 2001 From: Diego Garcia Lozano Date: Tue, 23 Nov 2021 22:43:08 +0100 Subject: [PATCH 5/6] Add operators extensions to TemperatureGradient and remove duplicates from LapseRate I have removed the TemperatureDelta/Length = LapseRate operator since it would collide with the same overload that equals TemperatureGradient. This is a breaking change, but it was decided that we can do it anyways since it's easy to replace and you will get a compiler error. --- UnitsNet.Tests/CustomCode/LapseRateTests.cs | 7 ----- .../CustomCode/TemperatureGradientTests.cs | 28 +++++++++++++++++++ .../Quantities/TemperatureDelta.extra.cs | 6 ++-- .../Quantities/TemperatureGradient.extra.cs | 23 +++++++++++++++ 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 UnitsNet/CustomCode/Quantities/TemperatureGradient.extra.cs diff --git a/UnitsNet.Tests/CustomCode/LapseRateTests.cs b/UnitsNet.Tests/CustomCode/LapseRateTests.cs index 7701f7309b..a1b04f9b13 100644 --- a/UnitsNet.Tests/CustomCode/LapseRateTests.cs +++ b/UnitsNet.Tests/CustomCode/LapseRateTests.cs @@ -38,13 +38,6 @@ public void TemperatureDeltaDividedByLapseRateEqualsLength() Assert.Equal(length, Length.FromKilometers(10)); } - [Fact] - public void TemperatureDeltaDividedByLengthEqualsLapseRate() - { - LapseRate lapseRate = TemperatureDelta.FromDegreesCelsius(50) / Length.FromKilometers(10); - Assert.Equal(lapseRate, LapseRate.FromDegreesCelciusPerKilometer(5)); - } - [Fact] public void LengthMultipliedByLapseRateEqualsTemperatureDelta() { diff --git a/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs b/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs index 05ec40cbc4..81c3825492 100644 --- a/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs +++ b/UnitsNet.Tests/CustomCode/TemperatureGradientTests.cs @@ -33,5 +33,33 @@ public class TemperatureGradientTests : TemperatureGradientTestsBase protected override double DegreesFahrenheitPerFootInOneKelvinPerMeter => 0.54864; protected override double KelvinsPerMeterInOneKelvinPerMeter => 1; + + [Fact] + public void TemperatureDeltaDividedByTemperatureGradientEqualsLength() + { + Length length = TemperatureDelta.FromDegreesCelsius(50) / TemperatureGradient.FromDegreesCelciusPerKilometer(5); + Assert.Equal(length, Length.FromKilometers(10)); + } + + [Fact] + public void TemperatureDeltaDividedByLengthEqualsTemperatureGradient() + { + TemperatureGradient lapseRate = TemperatureDelta.FromDegreesCelsius(50) / Length.FromKilometers(10); + Assert.Equal(lapseRate, TemperatureGradient.FromDegreesCelciusPerKilometer(5)); + } + + [Fact] + public void LengthMultipliedByTemperatureGradientEqualsTemperatureDelta() + { + TemperatureDelta temperatureDelta = Length.FromKilometers(10) * TemperatureGradient.FromDegreesCelciusPerKilometer(5); + Assert.Equal(temperatureDelta, TemperatureDelta.FromDegreesCelsius(50)); + } + + [Fact] + public void TemperatureGradientMultipliedByLengthEqualsTemperatureDelta() + { + TemperatureDelta temperatureDelta = TemperatureGradient.FromDegreesCelciusPerKilometer(5) * Length.FromKilometers(10); + Assert.Equal(temperatureDelta, TemperatureDelta.FromDegreesCelsius(50)); + } } } diff --git a/UnitsNet/CustomCode/Quantities/TemperatureDelta.extra.cs b/UnitsNet/CustomCode/Quantities/TemperatureDelta.extra.cs index b3b3878cbb..f45ca28bc4 100644 --- a/UnitsNet/CustomCode/Quantities/TemperatureDelta.extra.cs +++ b/UnitsNet/CustomCode/Quantities/TemperatureDelta.extra.cs @@ -5,10 +5,10 @@ namespace UnitsNet { public partial struct TemperatureDelta { - /// Get from divided by . - public static LapseRate operator /(TemperatureDelta left, Length right) + /// Get from divided by . + public static TemperatureGradient operator /(TemperatureDelta left, Length right) { - return LapseRate.FromDegreesCelciusPerKilometer(left.DegreesCelsius / right.Kilometers); + return TemperatureGradient.FromKelvinsPerMeter(left.Kelvins / right.Meters); } /// Get from times . diff --git a/UnitsNet/CustomCode/Quantities/TemperatureGradient.extra.cs b/UnitsNet/CustomCode/Quantities/TemperatureGradient.extra.cs new file mode 100644 index 0000000000..c6d5d087c7 --- /dev/null +++ b/UnitsNet/CustomCode/Quantities/TemperatureGradient.extra.cs @@ -0,0 +1,23 @@ +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +namespace UnitsNet +{ + public partial struct TemperatureGradient + { + /// Get from divided by . + public static Length operator /(TemperatureDelta left, TemperatureGradient right) + { + return Length.FromKilometers(left.Kelvins / right.DegreesCelciusPerKilometer); + } + + /// Get from times . + public static TemperatureDelta operator *(Length left, TemperatureGradient right) => right * left; + + /// Get from times . + public static TemperatureDelta operator *(TemperatureGradient left, Length right) + { + return TemperatureDelta.FromDegreesCelsius(left.DegreesCelciusPerKilometer * right.Kilometers); + } + } +} From 68e85164fb0ab660c382dd85ac1baa3cb58ff58b Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Tue, 23 Nov 2021 23:15:12 +0100 Subject: [PATCH 6/6] Remove None Include="" --- UnitsNet/UnitsNet.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/UnitsNet/UnitsNet.csproj b/UnitsNet/UnitsNet.csproj index 1803af8cfc..4e0a560b5e 100644 --- a/UnitsNet/UnitsNet.csproj +++ b/UnitsNet/UnitsNet.csproj @@ -60,9 +60,6 @@ - - Common\UnitDefinitions\TemperatureGradient.json -