From 3d6eb99052cae70f27c3dd5c25d463d2a1c5d993 Mon Sep 17 00:00:00 2001 From: Andrew Meservy Date: Mon, 19 May 2025 15:35:05 +0200 Subject: [PATCH] Add Thermal Resistance (KelvinPerWatt and DegreesCelsiusPerWatt) --- Common/UnitDefinitions/ThermalResistance.json | 49 ++ Common/UnitEnumValues.g.json | 4 +- .../Quantities/SpecificWeight.g.cs | 2 +- .../Quantities/ThermalResistance.g.cs | 169 ++++ .../ThermalResistance.nfproj | 42 + ...Net.NanoFramework.ThermalResistance.nuspec | 26 + .../ThermalResistance/packages.config | 4 + .../Units/ThermalResistanceUnit.g.cs | 33 + .../GeneratedCode/UnitsNet.nanoFramework.sln | 8 + ...mberToThermalResistanceExtensionsTest.g.cs | 36 + .../NumberToThermalResistanceExtensions.g.cs | 58 ++ .../CustomCode/ThermalResistanceTests.cs | 30 + .../GeneratedCode/IQuantityTests.g.cs | 3 + .../TestsBase/ThermalResistanceTestsBase.g.cs | 684 +++++++++++++++ UnitsNet/GeneratedCode/Quantities/Length.g.cs | 2 +- .../Quantities/SpecificWeight.g.cs | 2 +- .../Quantities/ThermalResistance.g.cs | 816 ++++++++++++++++++ UnitsNet/GeneratedCode/Quantity.g.cs | 5 + .../Resources/ThermalResistance.restext | 2 + .../Units/ThermalResistanceUnit.g.cs | 33 + 20 files changed, 2004 insertions(+), 4 deletions(-) create mode 100644 Common/UnitDefinitions/ThermalResistance.json create mode 100644 UnitsNet.NanoFramework/GeneratedCode/Quantities/ThermalResistance.g.cs create mode 100644 UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/ThermalResistance.nfproj create mode 100644 UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/UnitsNet.NanoFramework.ThermalResistance.nuspec create mode 100644 UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/packages.config create mode 100644 UnitsNet.NanoFramework/GeneratedCode/Units/ThermalResistanceUnit.g.cs create mode 100644 UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToThermalResistanceExtensionsTest.g.cs create mode 100644 UnitsNet.NumberExtensions/GeneratedCode/NumberToThermalResistanceExtensions.g.cs create mode 100644 UnitsNet.Tests/CustomCode/ThermalResistanceTests.cs create mode 100644 UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs create mode 100644 UnitsNet/GeneratedCode/Quantities/ThermalResistance.g.cs create mode 100644 UnitsNet/GeneratedCode/Resources/ThermalResistance.restext create mode 100644 UnitsNet/GeneratedCode/Units/ThermalResistanceUnit.g.cs diff --git a/Common/UnitDefinitions/ThermalResistance.json b/Common/UnitDefinitions/ThermalResistance.json new file mode 100644 index 0000000000..287d6c4d7c --- /dev/null +++ b/Common/UnitDefinitions/ThermalResistance.json @@ -0,0 +1,49 @@ +{ + "Name": "ThermalResistance", + "BaseUnit": "KelvinPerWatt", + "XmlDocSummary": "Thermal resistance (R) measures the opposition to the heat current in a material or system. It is measured in units of kelvins per watt (K/W) and indicates how much temperature difference (in kelvins) is required to transfer a unit of heat current (in watts) through the material or object. It is essential to optimize the building insulation, evaluate the efficiency of electronic devices, and enhance the performance of heat sinks in various applications.", + "BaseDimensions": { + "L": -2, + "M": -1, + "T": 3, + "Θ": 1 + }, + "Units": [ + { + "SingularName": "KelvinPerWatt", + "PluralName": "KelvinsPerWatt", + "BaseUnits": { + "L": "Meter", + "M": "Kilogram", + "T": "Second", + "Θ": "Kelvin" + }, + "FromUnitToBaseFunc": "{x}", + "FromBaseToUnitFunc": "{x}", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "K/W" ] + } + ] + }, + { + "SingularName": "DegreeCelsiusPerWatt", + "PluralName": "DegreesCelsiusPerWatt", + "BaseUnits": { + "L": "Meter", + "M": "Kilogram", + "T": "Second", + "Θ": "DegreeCelsius" + }, + "FromUnitToBaseFunc": "{x}", + "FromBaseToUnitFunc": "{x}", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "°C/W" ] + } + ] + } + ] +} diff --git a/Common/UnitEnumValues.g.json b/Common/UnitEnumValues.g.json index f50485b3d7..f3b5b35019 100644 --- a/Common/UnitEnumValues.g.json +++ b/Common/UnitEnumValues.g.json @@ -1490,7 +1490,9 @@ "SquareCentimeterKelvinPerWatt": 3, "SquareMeterDegreeCelsiusPerWatt": 4, "SquareMeterKelvinPerKilowatt": 5, - "SquareMeterKelvinPerWatt": 6 + "SquareMeterKelvinPerWatt": 6, + "KelvinPerWatt": 12, + "DegreeCelsiusPerWatt": 8 }, "Torque": { "GramForceCentimeter": 1, diff --git a/UnitsNet.NanoFramework/GeneratedCode/Quantities/SpecificWeight.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Quantities/SpecificWeight.g.cs index 9d23e9bbb7..8a2ab6a43c 100644 --- a/UnitsNet.NanoFramework/GeneratedCode/Quantities/SpecificWeight.g.cs +++ b/UnitsNet.NanoFramework/GeneratedCode/Quantities/SpecificWeight.g.cs @@ -27,7 +27,7 @@ namespace UnitsNet /// The SpecificWeight, or more precisely, the volumetric weight density, of a substance is its weight per unit volume. /// /// - /// http://en.wikipedia.org/wiki/Specificweight + /// https://en.wikipedia.org/wiki/Specific_weight /// public struct SpecificWeight { diff --git a/UnitsNet.NanoFramework/GeneratedCode/Quantities/ThermalResistance.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Quantities/ThermalResistance.g.cs new file mode 100644 index 0000000000..a082ec587b --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/Quantities/ThermalResistance.g.cs @@ -0,0 +1,169 @@ +//------------------------------------------------------------------------------ +// +// 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 UnitsNet.Units; + +namespace UnitsNet +{ + /// + /// + /// Thermal resistance (R) measures the opposition to the heat current in a material or system. It is measured in units of kelvins per watt (K/W) and indicates how much temperature difference (in kelvins) is required to transfer a unit of heat current (in watts) through the material or object. It is essential to optimize the building insulation, evaluate the efficiency of electronic devices, and enhance the performance of heat sinks in various applications. + /// + public struct ThermalResistance + { + /// + /// The numeric value this quantity was constructed with. + /// + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + private readonly ThermalResistanceUnit _unit; + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + /// + public ThermalResistanceUnit Unit => _unit; + + /// + /// 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. + public ThermalResistance(double value, ThermalResistanceUnit unit) + { + _value = value; + _unit = unit; + } + + /// + /// The base unit of ThermalResistance, which is Second. All conversions go via this value. + /// + public static ThermalResistanceUnit BaseUnit { get; } = ThermalResistanceUnit.KelvinPerWatt; + + /// + /// Represents the largest possible value of ThermalResistance. + /// + public static ThermalResistance MaxValue { get; } = new ThermalResistance(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of ThermalResistance. + /// + public static ThermalResistance MinValue { get; } = new ThermalResistance(double.MinValue, BaseUnit); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit Second. + /// + public static ThermalResistance Zero { get; } = new ThermalResistance(0, BaseUnit); + #region Conversion Properties + + /// + /// Gets a value of this quantity converted into + /// + public double DegreesCelsiusPerWatt => As(ThermalResistanceUnit.DegreeCelsiusPerWatt); + + /// + /// Gets a value of this quantity converted into + /// + public double KelvinsPerWatt => As(ThermalResistanceUnit.KelvinPerWatt); + + #endregion + + #region Static Factory Methods + + /// + /// Creates a from . + /// + public static ThermalResistance FromDegreesCelsiusPerWatt(double degreescelsiusperwatt) => new ThermalResistance(degreescelsiusperwatt, ThermalResistanceUnit.DegreeCelsiusPerWatt); + + /// + /// Creates a from . + /// + public static ThermalResistance FromKelvinsPerWatt(double kelvinsperwatt) => new ThermalResistance(kelvinsperwatt, ThermalResistanceUnit.KelvinPerWatt); + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// ThermalResistance unit value. + public static ThermalResistance From(double value, ThermalResistanceUnit fromUnit) + { + return new ThermalResistance(value, fromUnit); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(ThermalResistanceUnit unit) => GetValueAs(unit); + + /// + /// Converts this ThermalResistance to another ThermalResistance with the unit representation . + /// + /// A ThermalResistance with the specified unit. + public ThermalResistance ToUnit(ThermalResistanceUnit unit) + { + var convertedValue = GetValueAs(unit); + return new ThermalResistance(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 GetValueInBaseUnit() + { + return Unit switch + { + ThermalResistanceUnit.DegreeCelsiusPerWatt => _value, + ThermalResistanceUnit.KelvinPerWatt => _value, + _ => throw new NotImplementedException($"Can not convert {Unit} to base units.") + }; + } + + private double GetValueAs(ThermalResistanceUnit unit) + { + if (Unit == unit) + return _value; + + var baseUnitValue = GetValueInBaseUnit(); + + return unit switch + { + ThermalResistanceUnit.DegreeCelsiusPerWatt => baseUnitValue, + ThermalResistanceUnit.KelvinPerWatt => baseUnitValue, + _ => throw new NotImplementedException($"Can not convert {Unit} to {unit}.") + }; + } + + #endregion + } +} + diff --git a/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/ThermalResistance.nfproj b/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/ThermalResistance.nfproj new file mode 100644 index 0000000000..62823feeef --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/ThermalResistance.nfproj @@ -0,0 +1,42 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + {c69bdd4d-3ebd-4b8e-cffb-650903da3872} + Library + Properties + 512 + UnitsNet + UnitsNet.ThermalResistance + v1.0 + bin\$(Configuration)\$(AssemblyName).xml + + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll + True + True + + + + + + + + + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/UnitsNet.NanoFramework.ThermalResistance.nuspec b/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/UnitsNet.NanoFramework.ThermalResistance.nuspec new file mode 100644 index 0000000000..acebef95d7 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/UnitsNet.NanoFramework.ThermalResistance.nuspec @@ -0,0 +1,26 @@ + + + + UnitsNet.nanoFramework.ThermalResistance + 6.0.0-pre014 + Units.NET ThermalResistance - nanoFramework + Andreas Gullberg Larsen,nanoframework + UnitsNet + MIT-0 + https://github.com/angularsen/UnitsNet + false + Adds ThermalResistance units for Units.NET on .NET nanoFramework. For .NET or .NET Core, use UnitsNet instead. + https://raw.githubusercontent.com/angularsen/UnitsNet/ce85185429be345d77eb2ce09c99d59cc9ab8aed/Docs/Images/logo-32.png + + + Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). + en-US + nanoframework thermalresistance unit units quantity quantities measurement si metric imperial abbreviation abbreviations convert conversion parse immutable + + + + + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/packages.config b/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/packages.config new file mode 100644 index 0000000000..313a8dccdf --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/ThermalResistance/packages.config @@ -0,0 +1,4 @@ + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/Units/ThermalResistanceUnit.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Units/ThermalResistanceUnit.g.cs new file mode 100644 index 0000000000..edb63c8a69 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/Units/ThermalResistanceUnit.g.cs @@ -0,0 +1,33 @@ +//------------------------------------------------------------------------------ +// +// 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 ThermalResistanceUnit + { + DegreeCelsiusPerWatt = 8, + KelvinPerWatt = 12, + } + + #pragma warning restore 1591 +} diff --git a/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln b/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln index 15c28b6604..f2d98d63c4 100644 --- a/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln +++ b/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln @@ -236,6 +236,8 @@ Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "ThermalConductivity", "Ther EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "ThermalInsulance", "ThermalInsulance\ThermalInsulance.nfproj", "{0194c08d-ffcd-78bd-9e14-66634fe6d4ea}" EndProject +Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "ThermalResistance", "ThermalResistance\ThermalResistance.nfproj", "{c69bdd4d-3ebd-4b8e-cffb-650903da3872}" +EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "Torque", "Torque\Torque.nfproj", "{3b6bbc8a-1b22-deff-2980-53b77b6f3e5f}" EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "Turbidity", "Turbidity\Turbidity.nfproj", "{05ac8aed-49a4-5c9f-d8c2-8d2debf64791}" @@ -964,6 +966,12 @@ Global {0194c08d-ffcd-78bd-9e14-66634fe6d4ea}.Release|Any CPU.ActiveCfg = Release|Any CPU {0194c08d-ffcd-78bd-9e14-66634fe6d4ea}.Release|Any CPU.Build.0 = Release|Any CPU {0194c08d-ffcd-78bd-9e14-66634fe6d4ea}.Release|Any CPU.Deploy.0 = Release|Any CPU +{c69bdd4d-3ebd-4b8e-cffb-650903da3872}.Debug|Any CPU.ActiveCfg = Debug|Any CPU +{c69bdd4d-3ebd-4b8e-cffb-650903da3872}.Debug|Any CPU.Build.0 = Debug|Any CPU +{c69bdd4d-3ebd-4b8e-cffb-650903da3872}.Debug|Any CPU.Deploy.0 = Debug|Any CPU +{c69bdd4d-3ebd-4b8e-cffb-650903da3872}.Release|Any CPU.ActiveCfg = Release|Any CPU +{c69bdd4d-3ebd-4b8e-cffb-650903da3872}.Release|Any CPU.Build.0 = Release|Any CPU +{c69bdd4d-3ebd-4b8e-cffb-650903da3872}.Release|Any CPU.Deploy.0 = Release|Any CPU {3b6bbc8a-1b22-deff-2980-53b77b6f3e5f}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3b6bbc8a-1b22-deff-2980-53b77b6f3e5f}.Debug|Any CPU.Build.0 = Debug|Any CPU {3b6bbc8a-1b22-deff-2980-53b77b6f3e5f}.Debug|Any CPU.Deploy.0 = Debug|Any CPU diff --git a/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToThermalResistanceExtensionsTest.g.cs b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToThermalResistanceExtensionsTest.g.cs new file mode 100644 index 0000000000..0d90afb10c --- /dev/null +++ b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToThermalResistanceExtensionsTest.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. + +using UnitsNet.NumberExtensions.NumberToThermalResistance; +using Xunit; + +namespace UnitsNet.Tests +{ + public class NumberToThermalResistanceExtensionsTests + { + [Fact] + public void NumberToDegreesCelsiusPerWattTest() => + Assert.Equal(ThermalResistance.FromDegreesCelsiusPerWatt(2), 2.DegreesCelsiusPerWatt()); + + [Fact] + public void NumberToKelvinsPerWattTest() => + Assert.Equal(ThermalResistance.FromKelvinsPerWatt(2), 2.KelvinsPerWatt()); + + } +} diff --git a/UnitsNet.NumberExtensions/GeneratedCode/NumberToThermalResistanceExtensions.g.cs b/UnitsNet.NumberExtensions/GeneratedCode/NumberToThermalResistanceExtensions.g.cs new file mode 100644 index 0000000000..b2c18e4c12 --- /dev/null +++ b/UnitsNet.NumberExtensions/GeneratedCode/NumberToThermalResistanceExtensions.g.cs @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// 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; + +#if NET7_0_OR_GREATER +using System.Numerics; +#endif + +#nullable enable + +namespace UnitsNet.NumberExtensions.NumberToThermalResistance +{ + /// + /// A number to ThermalResistance Extensions + /// + public static class NumberToThermalResistanceExtensions + { + /// + public static ThermalResistance DegreesCelsiusPerWatt(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => ThermalResistance.FromDegreesCelsiusPerWatt(double.CreateChecked(value)); +#else + , IConvertible + => ThermalResistance.FromDegreesCelsiusPerWatt(value.ToDouble(null)); +#endif + + /// + public static ThermalResistance KelvinsPerWatt(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => ThermalResistance.FromKelvinsPerWatt(double.CreateChecked(value)); +#else + , IConvertible + => ThermalResistance.FromKelvinsPerWatt(value.ToDouble(null)); +#endif + + } +} diff --git a/UnitsNet.Tests/CustomCode/ThermalResistanceTests.cs b/UnitsNet.Tests/CustomCode/ThermalResistanceTests.cs new file mode 100644 index 0000000000..edc5bcf4d1 --- /dev/null +++ b/UnitsNet.Tests/CustomCode/ThermalResistanceTests.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// 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; + +namespace UnitsNet.Tests.CustomCode +{ + public class ThermalResistanceTests : ThermalResistanceTestsBase + { + protected override double KelvinsPerWattInOneKelvinPerWatt => 1; + + protected override double DegreesCelsiusPerWattInOneKelvinPerWatt => 1; + } +} diff --git a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs index d7ba519990..4d2443c7b6 100644 --- a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs +++ b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs @@ -151,6 +151,7 @@ void Assertion(int expectedValue, Enum expectedUnit, IQuantity quantity) Assertion(3, TemperatureGradientUnit.KelvinPerMeter, Quantity.From(3, TemperatureGradientUnit.KelvinPerMeter)); Assertion(3, ThermalConductivityUnit.WattPerMeterKelvin, Quantity.From(3, ThermalConductivityUnit.WattPerMeterKelvin)); Assertion(3, ThermalInsulanceUnit.SquareMeterKelvinPerWatt, Quantity.From(3, ThermalInsulanceUnit.SquareMeterKelvinPerWatt)); + Assertion(3, ThermalResistanceUnit.KelvinPerWatt, Quantity.From(3, ThermalResistanceUnit.KelvinPerWatt)); Assertion(3, TorqueUnit.TonneForceMillimeter, Quantity.From(3, TorqueUnit.TonneForceMillimeter)); Assertion(3, TurbidityUnit.NTU, Quantity.From(3, TurbidityUnit.NTU)); Assertion(3, VitaminAUnit.InternationalUnit, Quantity.From(3, VitaminAUnit.InternationalUnit)); @@ -285,6 +286,7 @@ public void QuantityInfo_IsSameAsStaticInfoProperty() Assertion(TemperatureGradient.Info, TemperatureGradient.Zero); Assertion(ThermalConductivity.Info, ThermalConductivity.Zero); Assertion(ThermalInsulance.Info, ThermalInsulance.Zero); + Assertion(ThermalResistance.Info, ThermalResistance.Zero); Assertion(Torque.Info, Torque.Zero); Assertion(Turbidity.Info, Turbidity.Zero); Assertion(VitaminA.Info, VitaminA.Zero); @@ -419,6 +421,7 @@ public void Dimensions_IsSameAsStaticBaseDimensions() Assertion(TemperatureGradient.BaseDimensions, TemperatureGradient.Zero); Assertion(ThermalConductivity.BaseDimensions, ThermalConductivity.Zero); Assertion(ThermalInsulance.BaseDimensions, ThermalInsulance.Zero); + Assertion(ThermalResistance.BaseDimensions, ThermalResistance.Zero); Assertion(Torque.BaseDimensions, Torque.Zero); Assertion(Turbidity.BaseDimensions, Turbidity.Zero); Assertion(VitaminA.BaseDimensions, VitaminA.Zero); diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs new file mode 100644 index 0000000000..07f992e0c7 --- /dev/null +++ b/UnitsNet.Tests/GeneratedCode/TestsBase/ThermalResistanceTestsBase.g.cs @@ -0,0 +1,684 @@ +//------------------------------------------------------------------------------ +// +// 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.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using UnitsNet.Tests.Helpers; +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 ThermalResistance. + /// +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class ThermalResistanceTestsBase : QuantityTestsBase + { + protected abstract double DegreesCelsiusPerWattInOneKelvinPerWatt { get; } + protected abstract double KelvinsPerWattInOneKelvinPerWatt { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double DegreesCelsiusPerWattTolerance { get { return 1e-5; } } + protected virtual double KelvinsPerWattTolerance { get { return 1e-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(ThermalResistanceUnit unit) + { + return unit switch + { + ThermalResistanceUnit.DegreeCelsiusPerWatt => (DegreesCelsiusPerWattInOneKelvinPerWatt, DegreesCelsiusPerWattTolerance), + ThermalResistanceUnit.KelvinPerWatt => (KelvinsPerWattInOneKelvinPerWatt, KelvinsPerWattTolerance), + _ => throw new NotSupportedException() + }; + } + + public static IEnumerable UnitTypes = new List + { + new object[] { ThermalResistanceUnit.DegreeCelsiusPerWatt }, + new object[] { ThermalResistanceUnit.KelvinPerWatt }, + }; + + [Fact] + public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit() + { + var quantity = new ThermalResistance(); + Assert.Equal(0, quantity.Value); + Assert.Equal(ThermalResistanceUnit.KelvinPerWatt, quantity.Unit); + } + + [Fact] + public void Ctor_WithInfinityValue_DoNotThrowsArgumentException() + { + var exception1 = Record.Exception(() => new ThermalResistance(double.PositiveInfinity, ThermalResistanceUnit.KelvinPerWatt)); + var exception2 = Record.Exception(() => new ThermalResistance(double.NegativeInfinity, ThermalResistanceUnit.KelvinPerWatt)); + + Assert.Null(exception1); + Assert.Null(exception2); + } + + [Fact] + public void Ctor_WithNaNValue_DoNotThrowsArgumentException() + { + var exception = Record.Exception(() => new ThermalResistance(double.NaN, ThermalResistanceUnit.KelvinPerWatt)); + + Assert.Null(exception); + } + + [Fact] + public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException() + { + Assert.Throws(() => new ThermalResistance(value: 1, unitSystem: null)); + } + + [Fact] + public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits() + { + var quantity = new ThermalResistance(value: 1, unitSystem: UnitSystem.SI); + Assert.Equal(1, quantity.Value); + Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits)); + } + + [Fact] + public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits); + Assert.Throws(() => new ThermalResistance(value: 1, unitSystem: unsupportedUnitSystem)); + } + + [Fact] + public void ThermalResistance_QuantityInfo_ReturnsQuantityInfoDescribingQuantity() + { + var quantity = new ThermalResistance(1, ThermalResistanceUnit.KelvinPerWatt); + + QuantityInfo quantityInfo = quantity.QuantityInfo; + + Assert.Equal(ThermalResistance.Zero, quantityInfo.Zero); + Assert.Equal("ThermalResistance", quantityInfo.Name); + + var units = EnumUtils.GetEnumValues().OrderBy(x => x.ToString()).ToArray(); + var unitNames = units.Select(x => x.ToString()); + } + + [Fact] + public void KelvinPerWattToThermalResistanceUnits() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + AssertEx.EqualTolerance(DegreesCelsiusPerWattInOneKelvinPerWatt, kelvinperwatt.DegreesCelsiusPerWatt, DegreesCelsiusPerWattTolerance); + AssertEx.EqualTolerance(KelvinsPerWattInOneKelvinPerWatt, kelvinperwatt.KelvinsPerWatt, KelvinsPerWattTolerance); + } + + [Fact] + public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit() + { + var quantity00 = ThermalResistance.From(1, ThermalResistanceUnit.DegreeCelsiusPerWatt); + AssertEx.EqualTolerance(1, quantity00.DegreesCelsiusPerWatt, DegreesCelsiusPerWattTolerance); + Assert.Equal(ThermalResistanceUnit.DegreeCelsiusPerWatt, quantity00.Unit); + + var quantity01 = ThermalResistance.From(1, ThermalResistanceUnit.KelvinPerWatt); + AssertEx.EqualTolerance(1, quantity01.KelvinsPerWatt, KelvinsPerWattTolerance); + Assert.Equal(ThermalResistanceUnit.KelvinPerWatt, quantity01.Unit); + + } + + [Fact] + public void FromKelvinsPerWatt_WithInfinityValue_DoNotThrowsArgumentException() + { + var exception1 = Record.Exception(() => ThermalResistance.FromKelvinsPerWatt(double.PositiveInfinity)); + var exception2 = Record.Exception(() => ThermalResistance.FromKelvinsPerWatt(double.NegativeInfinity)); + + Assert.Null(exception1); + Assert.Null(exception2); + } + + [Fact] + public void FromKelvinsPerWatt_WithNanValue_DoNotThrowsArgumentException() + { + var exception = Record.Exception(() => ThermalResistance.FromKelvinsPerWatt(double.NaN)); + + Assert.Null(exception); + } + + [Fact] + public void As() + { + var kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + AssertEx.EqualTolerance(DegreesCelsiusPerWattInOneKelvinPerWatt, kelvinperwatt.As(ThermalResistanceUnit.DegreeCelsiusPerWatt), DegreesCelsiusPerWattTolerance); + AssertEx.EqualTolerance(KelvinsPerWattInOneKelvinPerWatt, kelvinperwatt.As(ThermalResistanceUnit.KelvinPerWatt), KelvinsPerWattTolerance); + } + + [Fact] + public virtual void BaseUnit_HasSIBase() + { + var baseUnitInfo = ThermalResistance.Info.BaseUnitInfo; + Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits)); + } + + [Fact] + public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits() + { + var quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + var expectedValue = quantity.As(ThermalResistance.Info.GetDefaultUnit(UnitSystem.SI)); + + var convertedValue = quantity.As(UnitSystem.SI); + + Assert.Equal(expectedValue, convertedValue); + } + + [Fact] + public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull() + { + var quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + UnitSystem nullUnitSystem = null!; + Assert.Throws(() => quantity.As(nullUnitSystem)); + } + + [Fact] + public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits); + Assert.Throws(() => quantity.As(unsupportedUnitSystem)); + } + + [Fact] + public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits() + { + var quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + var expectedUnit = ThermalResistance.Info.GetDefaultUnit(UnitSystem.SI); + var expectedValue = quantity.As(expectedUnit); + + Assert.Multiple(() => + { + ThermalResistance quantityToConvert = quantity; + + ThermalResistance convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI); + + Assert.Equal(expectedUnit, convertedQuantity.Unit); + Assert.Equal(expectedValue, convertedQuantity.Value); + }, () => + { + IQuantity quantityToConvert = quantity; + + IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI); + + Assert.Equal(expectedUnit, convertedQuantity.Unit); + Assert.Equal(expectedValue, convertedQuantity.Value); + }, () => + { + IQuantity quantityToConvert = quantity; + + IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI); + + Assert.Equal(expectedUnit, convertedQuantity.Unit); + Assert.Equal(expectedValue, convertedQuantity.Value); + }); + } + + [Fact] + public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull() + { + UnitSystem nullUnitSystem = null!; + Assert.Multiple(() => + { + var quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + Assert.Throws(() => quantity.ToUnit(nullUnitSystem)); + }, () => + { + IQuantity quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + Assert.Throws(() => quantity.ToUnit(nullUnitSystem)); + }, () => + { + IQuantity quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + Assert.Throws(() => quantity.ToUnit(nullUnitSystem)); + }); + } + + [Fact] + public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits); + Assert.Multiple(() => + { + var quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem)); + }, () => + { + IQuantity quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem)); + }, () => + { + IQuantity quantity = new ThermalResistance(value: 1, unit: ThermalResistance.BaseUnit); + Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem)); + }); + } + + [Fact] + public void Parse() + { + try + { + var parsed = ThermalResistance.Parse("1 °C/W", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.DegreesCelsiusPerWatt, DegreesCelsiusPerWattTolerance); + Assert.Equal(ThermalResistanceUnit.DegreeCelsiusPerWatt, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = ThermalResistance.Parse("1 K/W", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.KelvinsPerWatt, KelvinsPerWattTolerance); + Assert.Equal(ThermalResistanceUnit.KelvinPerWatt, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + } + + [Fact] + public void TryParse() + { + { + Assert.True(ThermalResistance.TryParse("1 °C/W", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.DegreesCelsiusPerWatt, DegreesCelsiusPerWattTolerance); + Assert.Equal(ThermalResistanceUnit.DegreeCelsiusPerWatt, parsed.Unit); + } + + { + Assert.True(ThermalResistance.TryParse("1 K/W", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.KelvinsPerWatt, KelvinsPerWattTolerance); + Assert.Equal(ThermalResistanceUnit.KelvinPerWatt, parsed.Unit); + } + + } + + [Theory] + [InlineData("°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void ParseUnit_WithUsEnglishCurrentCulture(string abbreviation, ThermalResistanceUnit expectedUnit) + { + // Fallback culture "en-US" is always localized + using var _ = new CultureScope("en-US"); + ThermalResistanceUnit parsedUnit = ThermalResistance.ParseUnit(abbreviation); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void ParseUnit_WithUnsupportedCurrentCulture_FallsBackToUsEnglish(string abbreviation, ThermalResistanceUnit expectedUnit) + { + // Currently, no abbreviations are localized for Icelandic, so it should fall back to "en-US" when parsing. + using var _ = new CultureScope("is-IS"); + ThermalResistanceUnit parsedUnit = ThermalResistance.ParseUnit(abbreviation); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("en-US", "K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void ParseUnit_WithCurrentCulture(string culture, string abbreviation, ThermalResistanceUnit expectedUnit) + { + using var _ = new CultureScope(culture); + ThermalResistanceUnit parsedUnit = ThermalResistance.ParseUnit(abbreviation); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("en-US", "K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void ParseUnit_WithCulture(string culture, string abbreviation, ThermalResistanceUnit expectedUnit) + { + ThermalResistanceUnit parsedUnit = ThermalResistance.ParseUnit(abbreviation, CultureInfo.GetCultureInfo(culture)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void TryParseUnit_WithUsEnglishCurrentCulture(string abbreviation, ThermalResistanceUnit expectedUnit) + { + // Fallback culture "en-US" is always localized + using var _ = new CultureScope("en-US"); + Assert.True(ThermalResistance.TryParseUnit(abbreviation, out ThermalResistanceUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void TryParseUnit_WithUnsupportedCurrentCulture_FallsBackToUsEnglish(string abbreviation, ThermalResistanceUnit expectedUnit) + { + // Currently, no abbreviations are localized for Icelandic, so it should fall back to "en-US" when parsing. + using var _ = new CultureScope("is-IS"); + Assert.True(ThermalResistance.TryParseUnit(abbreviation, out ThermalResistanceUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("en-US", "K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void TryParseUnit_WithCurrentCulture(string culture, string abbreviation, ThermalResistanceUnit expectedUnit) + { + using var _ = new CultureScope(culture); + Assert.True(ThermalResistance.TryParseUnit(abbreviation, out ThermalResistanceUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "°C/W", ThermalResistanceUnit.DegreeCelsiusPerWatt)] + [InlineData("en-US", "K/W", ThermalResistanceUnit.KelvinPerWatt)] + public void TryParseUnit_WithCulture(string culture, string abbreviation, ThermalResistanceUnit expectedUnit) + { + Assert.True(ThermalResistance.TryParseUnit(abbreviation, CultureInfo.GetCultureInfo(culture), out ThermalResistanceUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit(ThermalResistanceUnit unit) + { + var inBaseUnits = ThermalResistance.From(1.0, ThermalResistance.BaseUnit); + var converted = inBaseUnits.ToUnit(unit); + + var conversionFactor = GetConversionFactor(unit); + AssertEx.EqualTolerance(conversionFactor.UnitsInBaseUnit, converted.Value, conversionFactor.Tolerence); + Assert.Equal(unit, converted.Unit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit_WithSameUnits_AreEqual(ThermalResistanceUnit unit) + { + var quantity = ThermalResistance.From(3.0, unit); + var toUnitWithSameUnit = quantity.ToUnit(unit); + Assert.Equal(quantity, toUnitWithSameUnit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(ThermalResistanceUnit unit) + { + Assert.All(ThermalResistance.Units.Where(u => u != ThermalResistance.BaseUnit), fromUnit => + { + var quantity = ThermalResistance.From(3.0, fromUnit); + var converted = quantity.ToUnit(unit); + Assert.Equal(converted.Unit, unit); + }); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public virtual void ToUnit_FromDefaultQuantity_ReturnsQuantityWithGivenUnit(ThermalResistanceUnit unit) + { + var quantity = default(ThermalResistance); + var converted = quantity.ToUnit(unit); + Assert.Equal(converted.Unit, unit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit_FromIQuantity_ReturnsTheExpectedIQuantity(ThermalResistanceUnit unit) + { + var quantity = ThermalResistance.From(3, ThermalResistance.BaseUnit); + ThermalResistance expectedQuantity = quantity.ToUnit(unit); + Assert.Multiple(() => + { + IQuantity quantityToConvert = quantity; + IQuantity convertedQuantity = quantityToConvert.ToUnit(unit); + Assert.Equal(unit, convertedQuantity.Unit); + }, () => + { + IQuantity quantityToConvert = quantity; + IQuantity convertedQuantity = quantityToConvert.ToUnit(unit); + Assert.Equal(unit, convertedQuantity.Unit); + }); + } + + [Fact] + public void ConversionRoundTrip() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + AssertEx.EqualTolerance(1, ThermalResistance.FromDegreesCelsiusPerWatt(kelvinperwatt.DegreesCelsiusPerWatt).KelvinsPerWatt, DegreesCelsiusPerWattTolerance); + AssertEx.EqualTolerance(1, ThermalResistance.FromKelvinsPerWatt(kelvinperwatt.KelvinsPerWatt).KelvinsPerWatt, KelvinsPerWattTolerance); + } + + [Fact] + public void ArithmeticOperators() + { + ThermalResistance v = ThermalResistance.FromKelvinsPerWatt(1); + AssertEx.EqualTolerance(-1, -v.KelvinsPerWatt, KelvinsPerWattTolerance); + AssertEx.EqualTolerance(2, (ThermalResistance.FromKelvinsPerWatt(3)-v).KelvinsPerWatt, KelvinsPerWattTolerance); + AssertEx.EqualTolerance(2, (v + v).KelvinsPerWatt, KelvinsPerWattTolerance); + AssertEx.EqualTolerance(10, (v*10).KelvinsPerWatt, KelvinsPerWattTolerance); + AssertEx.EqualTolerance(10, (10*v).KelvinsPerWatt, KelvinsPerWattTolerance); + AssertEx.EqualTolerance(2, (ThermalResistance.FromKelvinsPerWatt(10)/5).KelvinsPerWatt, KelvinsPerWattTolerance); + AssertEx.EqualTolerance(2, ThermalResistance.FromKelvinsPerWatt(10)/ThermalResistance.FromKelvinsPerWatt(5), KelvinsPerWattTolerance); + } + + [Fact] + public void ComparisonOperators() + { + ThermalResistance oneKelvinPerWatt = ThermalResistance.FromKelvinsPerWatt(1); + ThermalResistance twoKelvinsPerWatt = ThermalResistance.FromKelvinsPerWatt(2); + + Assert.True(oneKelvinPerWatt < twoKelvinsPerWatt); + Assert.True(oneKelvinPerWatt <= twoKelvinsPerWatt); + Assert.True(twoKelvinsPerWatt > oneKelvinPerWatt); + Assert.True(twoKelvinsPerWatt >= oneKelvinPerWatt); + + Assert.False(oneKelvinPerWatt > twoKelvinsPerWatt); + Assert.False(oneKelvinPerWatt >= twoKelvinsPerWatt); + Assert.False(twoKelvinsPerWatt < oneKelvinPerWatt); + Assert.False(twoKelvinsPerWatt <= oneKelvinPerWatt); + } + + [Fact] + public void CompareToIsImplemented() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + Assert.Equal(0, kelvinperwatt.CompareTo(kelvinperwatt)); + Assert.True(kelvinperwatt.CompareTo(ThermalResistance.Zero) > 0); + Assert.True(ThermalResistance.Zero.CompareTo(kelvinperwatt) < 0); + } + + [Fact] + public void CompareToThrowsOnTypeMismatch() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + Assert.Throws(() => kelvinperwatt.CompareTo(new object())); + } + + [Fact] + public void CompareToThrowsOnNull() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + Assert.Throws(() => kelvinperwatt.CompareTo(null)); + } + + [Theory] + [InlineData(1, ThermalResistanceUnit.KelvinPerWatt, 1, ThermalResistanceUnit.KelvinPerWatt, true)] // Same value and unit. + [InlineData(1, ThermalResistanceUnit.KelvinPerWatt, 2, ThermalResistanceUnit.KelvinPerWatt, false)] // Different value. + [InlineData(2, ThermalResistanceUnit.KelvinPerWatt, 1, ThermalResistanceUnit.DegreeCelsiusPerWatt, false)] // Different value and unit. + [InlineData(1, ThermalResistanceUnit.KelvinPerWatt, 1, ThermalResistanceUnit.DegreeCelsiusPerWatt, false)] // Different unit. + public void Equals_ReturnsTrue_IfValueAndUnitAreEqual(double valueA, ThermalResistanceUnit unitA, double valueB, ThermalResistanceUnit unitB, bool expectEqual) + { + var a = new ThermalResistance(valueA, unitA); + var b = new ThermalResistance(valueB, unitB); + + // Operator overloads. + Assert.Equal(expectEqual, a == b); + Assert.Equal(expectEqual, b == a); + Assert.Equal(!expectEqual, a != b); + Assert.Equal(!expectEqual, b != a); + + // IEquatable + Assert.Equal(expectEqual, a.Equals(b)); + Assert.Equal(expectEqual, b.Equals(a)); + + // IEquatable + Assert.Equal(expectEqual, a.Equals((object)b)); + Assert.Equal(expectEqual, b.Equals((object)a)); + } + + [Fact] + public void Equals_Null_ReturnsFalse() + { + var a = ThermalResistance.Zero; + + Assert.False(a.Equals((object)null)); + + // "The result of the expression is always 'false'..." + #pragma warning disable CS8073 + Assert.False(a == null); + Assert.False(null == a); + Assert.True(a != null); + Assert.True(null != a); + #pragma warning restore CS8073 + } + + [Fact] + public void Equals_RelativeTolerance_IsImplemented() + { + var v = ThermalResistance.FromKelvinsPerWatt(1); + Assert.True(v.Equals(ThermalResistance.FromKelvinsPerWatt(1), KelvinsPerWattTolerance, ComparisonType.Relative)); + Assert.False(v.Equals(ThermalResistance.Zero, KelvinsPerWattTolerance, ComparisonType.Relative)); + Assert.True(ThermalResistance.FromKelvinsPerWatt(100).Equals(ThermalResistance.FromKelvinsPerWatt(120), 0.3, ComparisonType.Relative)); + Assert.False(ThermalResistance.FromKelvinsPerWatt(100).Equals(ThermalResistance.FromKelvinsPerWatt(120), 0.1, ComparisonType.Relative)); + } + + [Fact] + public void Equals_NegativeRelativeTolerance_ThrowsArgumentOutOfRangeException() + { + var v = ThermalResistance.FromKelvinsPerWatt(1); + Assert.Throws(() => v.Equals(ThermalResistance.FromKelvinsPerWatt(1), -1, ComparisonType.Relative)); + } + + [Fact] + public void EqualsReturnsFalseOnTypeMismatch() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + Assert.False(kelvinperwatt.Equals(new object())); + } + + [Fact] + public void EqualsReturnsFalseOnNull() + { + ThermalResistance kelvinperwatt = ThermalResistance.FromKelvinsPerWatt(1); + Assert.False(kelvinperwatt.Equals(null)); + } + + [Fact] + public void HasAtLeastOneAbbreviationSpecified() + { + var units = Enum.GetValues(typeof(ThermalResistanceUnit)).Cast(); + foreach (var unit in units) + { + var defaultAbbreviation = UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit); + } + } + + [Fact] + public void BaseDimensionsShouldNeverBeNull() + { + Assert.False(ThermalResistance.BaseDimensions is null); + } + + [Fact] + public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture() + { + using var _ = new CultureScope("en-US"); + Assert.Equal("1 °C/W", new ThermalResistance(1, ThermalResistanceUnit.DegreeCelsiusPerWatt).ToString()); + Assert.Equal("1 K/W", new ThermalResistance(1, ThermalResistanceUnit.KelvinPerWatt).ToString()); + } + + [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/W", new ThermalResistance(1, ThermalResistanceUnit.DegreeCelsiusPerWatt).ToString(swedishCulture)); + Assert.Equal("1 K/W", new ThermalResistance(1, ThermalResistanceUnit.KelvinPerWatt).ToString(swedishCulture)); + } + + [Fact] + public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture() + { + var _ = new CultureScope(CultureInfo.InvariantCulture); + Assert.Equal("0.1 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s1")); + Assert.Equal("0.12 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s2")); + Assert.Equal("0.123 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s3")); + Assert.Equal("0.1235 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s4")); + } + + [Fact] + public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture() + { + var culture = CultureInfo.InvariantCulture; + Assert.Equal("0.1 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s1", culture)); + Assert.Equal("0.12 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s2", culture)); + Assert.Equal("0.123 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s3", culture)); + Assert.Equal("0.1235 K/W", new ThermalResistance(0.123456, ThermalResistanceUnit.KelvinPerWatt).ToString("s4", culture)); + } + + [Theory] + [InlineData(null)] + [InlineData("en-US")] + public void ToString_NullFormat_DefaultsToGeneralFormat(string cultureName) + { + var quantity = ThermalResistance.FromKelvinsPerWatt(1.0); + CultureInfo formatProvider = cultureName == null + ? null + : CultureInfo.GetCultureInfo(cultureName); + + Assert.Equal(quantity.ToString("G", formatProvider), quantity.ToString(null, formatProvider)); + } + + [Theory] + [InlineData(null)] + [InlineData("g")] + public void ToString_NullProvider_EqualsCurrentCulture(string format) + { + var quantity = ThermalResistance.FromKelvinsPerWatt(1.0); + Assert.Equal(quantity.ToString(format, CultureInfo.CurrentCulture), quantity.ToString(format, null)); + } + + [Fact] + public void GetHashCode_Equals() + { + var quantity = ThermalResistance.FromKelvinsPerWatt(1.0); + Assert.Equal(new {ThermalResistance.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 = ThermalResistance.FromKelvinsPerWatt(value); + Assert.Equal(ThermalResistance.FromKelvinsPerWatt(-value), -quantity); + } + } +} diff --git a/UnitsNet/GeneratedCode/Quantities/Length.g.cs b/UnitsNet/GeneratedCode/Quantities/Length.g.cs index c60350df15..059814fa3e 100644 --- a/UnitsNet/GeneratedCode/Quantities/Length.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Length.g.cs @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by \generate-code.bat. // diff --git a/UnitsNet/GeneratedCode/Quantities/SpecificWeight.g.cs b/UnitsNet/GeneratedCode/Quantities/SpecificWeight.g.cs index d8551976b7..f9e53dcc67 100644 --- a/UnitsNet/GeneratedCode/Quantities/SpecificWeight.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/SpecificWeight.g.cs @@ -39,7 +39,7 @@ namespace UnitsNet /// The SpecificWeight, or more precisely, the volumetric weight density, of a substance is its weight per unit volume. /// /// - /// http://en.wikipedia.org/wiki/Specificweight + /// https://en.wikipedia.org/wiki/Specific_weight /// [DataContract] [DebuggerTypeProxy(typeof(QuantityDisplay))] diff --git a/UnitsNet/GeneratedCode/Quantities/ThermalResistance.g.cs b/UnitsNet/GeneratedCode/Quantities/ThermalResistance.g.cs new file mode 100644 index 0000000000..050f966346 --- /dev/null +++ b/UnitsNet/GeneratedCode/Quantities/ThermalResistance.g.cs @@ -0,0 +1,816 @@ +//------------------------------------------------------------------------------ +// +// 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.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Linq; +using System.Runtime.Serialization; +using UnitsNet.Units; +#if NET +using System.Numerics; +#endif + +#nullable enable + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// + /// Thermal resistance (R) measures the opposition to the heat current in a material or system. It is measured in units of kelvins per watt (K/W) and indicates how much temperature difference (in kelvins) is required to transfer a unit of heat current (in watts) through the material or object. It is essential to optimize the building insulation, evaluate the efficiency of electronic devices, and enhance the performance of heat sinks in various applications. + /// + [DataContract] + [DebuggerTypeProxy(typeof(QuantityDisplay))] + public readonly partial struct ThermalResistance : + IArithmeticQuantity, +#if NET7_0_OR_GREATER + IComparisonOperators, + IParsable, +#endif + IComparable, + IComparable, + IEquatable, + IFormattable + { + /// + /// The numeric value this quantity was constructed with. + /// + [DataMember(Name = "Value", Order = 1)] + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + [DataMember(Name = "Unit", Order = 2)] + private readonly ThermalResistanceUnit? _unit; + + static ThermalResistance() + { + BaseDimensions = new BaseDimensions(-2, -1, 3, 0, 1, 0, 0); + BaseUnit = ThermalResistanceUnit.KelvinPerWatt; + Units = Enum.GetValues(typeof(ThermalResistanceUnit)).Cast().ToArray(); + Zero = new ThermalResistance(0, BaseUnit); + Info = new QuantityInfo("ThermalResistance", + new UnitInfo[] + { + new UnitInfo(ThermalResistanceUnit.DegreeCelsiusPerWatt, "DegreesCelsiusPerWatt", new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second, temperature: TemperatureUnit.DegreeCelsius), "ThermalResistance"), + new UnitInfo(ThermalResistanceUnit.KelvinPerWatt, "KelvinsPerWatt", new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second, temperature: TemperatureUnit.Kelvin), "ThermalResistance"), + }, + BaseUnit, Zero, BaseDimensions); + + DefaultConversionFunctions = new UnitConverter(); + RegisterDefaultConversions(DefaultConversionFunctions); + } + + /// + /// 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. + public ThermalResistance(double value, ThermalResistanceUnit unit) + { + _value = 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 ThermalResistance(double value, UnitSystem unitSystem) + { + _value = value; + _unit = Info.GetDefaultUnit(unitSystem); + } + + #region Static Properties + + /// + /// The containing the default generated conversion functions for instances. + /// + public static UnitConverter DefaultConversionFunctions { get; } + + /// + public static QuantityInfo Info { get; } + + /// + /// The of this quantity. + /// + public static BaseDimensions BaseDimensions { get; } + + /// + /// The base unit of ThermalResistance, which is KelvinPerWatt. All conversions go via this value. + /// + public static ThermalResistanceUnit BaseUnit { get; } + + /// + /// All units of measurement for the ThermalResistance quantity. + /// + public static ThermalResistanceUnit[] Units { get; } + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit KelvinPerWatt. + /// + public static ThermalResistance Zero { get; } + + /// + public static ThermalResistance AdditiveIdentity => Zero; + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + /// + public ThermalResistanceUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + /// + public QuantityInfo QuantityInfo => Info; + + /// + /// The of this quantity. + /// + public BaseDimensions Dimensions => ThermalResistance.BaseDimensions; + + #region Explicit implementations + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + Enum IQuantity.Unit => Unit; + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + UnitKey IQuantity.UnitKey => UnitKey.ForUnit(Unit); + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + QuantityInfo IQuantity.QuantityInfo => Info; + + #endregion + + #endregion + + #region Conversion Properties + + /// + /// Gets a value of this quantity converted into + /// + public double DegreesCelsiusPerWatt => As(ThermalResistanceUnit.DegreeCelsiusPerWatt); + + /// + /// Gets a value of this quantity converted into + /// + public double KelvinsPerWatt => As(ThermalResistanceUnit.KelvinPerWatt); + + #endregion + + #region Static Methods + + /// + /// Registers the default conversion functions in the given instance. + /// + /// The to register the default conversion functions in. + internal static void RegisterDefaultConversions(UnitConverter unitConverter) + { + // Register in unit converter: ThermalResistanceUnit -> BaseUnit + unitConverter.SetConversionFunction(ThermalResistanceUnit.DegreeCelsiusPerWatt, ThermalResistanceUnit.KelvinPerWatt, quantity => quantity.ToUnit(ThermalResistanceUnit.KelvinPerWatt)); + + // Register in unit converter: BaseUnit <-> BaseUnit + unitConverter.SetConversionFunction(ThermalResistanceUnit.KelvinPerWatt, ThermalResistanceUnit.KelvinPerWatt, quantity => quantity); + + // Register in unit converter: BaseUnit -> ThermalResistanceUnit + unitConverter.SetConversionFunction(ThermalResistanceUnit.KelvinPerWatt, ThermalResistanceUnit.DegreeCelsiusPerWatt, quantity => quantity.ToUnit(ThermalResistanceUnit.DegreeCelsiusPerWatt)); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(ThermalResistanceUnit 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(ThermalResistanceUnit unit, IFormatProvider? provider) + { + return UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Creates a from . + /// + public static ThermalResistance FromDegreesCelsiusPerWatt(double value) + { + return new ThermalResistance(value, ThermalResistanceUnit.DegreeCelsiusPerWatt); + } + + /// + /// Creates a from . + /// + public static ThermalResistance FromKelvinsPerWatt(double value) + { + return new ThermalResistance(value, ThermalResistanceUnit.KelvinPerWatt); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// ThermalResistance unit value. + public static ThermalResistance From(double value, ThermalResistanceUnit fromUnit) + { + return new ThermalResistance(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", CultureInfo.GetCultureInfo("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 ThermalResistance 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", CultureInfo.GetCultureInfo("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 ThermalResistance Parse(string str, IFormatProvider? provider) + { + return UnitsNetSetup.Default.QuantityParser.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", CultureInfo.GetCultureInfo("en-US")); + /// + public static bool TryParse([NotNullWhen(true)]string? str, out ThermalResistance 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", CultureInfo.GetCultureInfo("en-US")); + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static bool TryParse([NotNullWhen(true)]string? str, IFormatProvider? provider, out ThermalResistance result) + { + return UnitsNetSetup.Default.QuantityParser.TryParse( + str, + provider, + From, + out result); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", CultureInfo.GetCultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static ThermalResistanceUnit 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", CultureInfo.GetCultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static ThermalResistanceUnit ParseUnit(string str, IFormatProvider? provider) + { + return UnitParser.Default.Parse(str, Info.UnitInfos, provider).Value; + } + + /// + public static bool TryParseUnit([NotNullWhen(true)]string? str, out ThermalResistanceUnit 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", CultureInfo.GetCultureInfo("en-US")); + /// + /// Format to use when parsing number and unit. Defaults to if null. + public static bool TryParseUnit([NotNullWhen(true)]string? str, IFormatProvider? provider, out ThermalResistanceUnit unit) + { + return UnitParser.Default.TryParse(str, Info, provider, out unit); + } + + #endregion + + #region Arithmetic Operators + + /// Negate the value. + public static ThermalResistance operator -(ThermalResistance right) + { + return new ThermalResistance(-right.Value, right.Unit); + } + + /// Get from adding two . + public static ThermalResistance operator +(ThermalResistance left, ThermalResistance right) + { + return new ThermalResistance(left.Value + right.ToUnit(left.Unit).Value, left.Unit); + } + + /// Get from subtracting two . + public static ThermalResistance operator -(ThermalResistance left, ThermalResistance right) + { + return new ThermalResistance(left.Value - right.ToUnit(left.Unit).Value, left.Unit); + } + + /// Get from multiplying value and . + public static ThermalResistance operator *(double left, ThermalResistance right) + { + return new ThermalResistance(left * right.Value, right.Unit); + } + + /// Get from multiplying value and . + public static ThermalResistance operator *(ThermalResistance left, double right) + { + return new ThermalResistance(left.Value * right, left.Unit); + } + + /// Get from dividing by value. + public static ThermalResistance operator /(ThermalResistance left, double right) + { + return new ThermalResistance(left.Value / right, left.Unit); + } + + /// Get ratio value from dividing by . + public static double operator /(ThermalResistance left, ThermalResistance right) + { + return left.KelvinsPerWatt / right.KelvinsPerWatt; + } + + #endregion + + #region Equality / IComparable + + /// Returns true if less or equal to. + public static bool operator <=(ThermalResistance left, ThermalResistance right) + { + return left.Value <= right.ToUnit(left.Unit).Value; + } + + /// Returns true if greater than or equal to. + public static bool operator >=(ThermalResistance left, ThermalResistance right) + { + return left.Value >= right.ToUnit(left.Unit).Value; + } + + /// Returns true if less than. + public static bool operator <(ThermalResistance left, ThermalResistance right) + { + return left.Value < right.ToUnit(left.Unit).Value; + } + + /// Returns true if greater than. + public static bool operator >(ThermalResistance left, ThermalResistance right) + { + return left.Value > right.ToUnit(left.Unit).Value; + } + + // We use obsolete attribute to communicate the preferred equality members to use. + // CS0809: Obsolete member 'memberA' overrides non-obsolete member 'memberB'. + #pragma warning disable CS0809 + + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("For null checks, use `x is null` syntax to not invoke overloads. For equality checks, use Equals(ThermalResistance other, ThermalResistance tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")] + public static bool operator ==(ThermalResistance left, ThermalResistance right) + { + return left.Equals(right); + } + + /// Indicates strict inequality of two quantities, where both and are exactly equal. + [Obsolete("For null checks, use `x is null` syntax to not invoke overloads. For equality checks, use Equals(ThermalResistance other, ThermalResistance tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")] + public static bool operator !=(ThermalResistance left, ThermalResistance right) + { + return !(left == right); + } + + /// + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("Use Equals(ThermalResistance other, ThermalResistance tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")] + public override bool Equals(object? obj) + { + if (obj is null || !(obj is ThermalResistance otherQuantity)) + return false; + + return Equals(otherQuantity); + } + + /// + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("Use Equals(ThermalResistance other, ThermalResistance tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")] + public bool Equals(ThermalResistance other) + { + return new { Value, Unit }.Equals(new { other.Value, other.Unit }); + } + + #pragma warning restore CS0809 + + /// Compares the current with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit. + /// An object to compare with this instance. + /// + /// is not the same type as this instance. + /// + /// A value that indicates the relative order of the quantities being compared. The return value has these meanings: + /// + /// Value Meaning + /// Less than zero This instance precedes in the sort order. + /// Zero This instance occurs in the same position in the sort order as . + /// Greater than zero This instance follows in the sort order. + /// + /// + public int CompareTo(object? obj) + { + if (obj is null) throw new ArgumentNullException(nameof(obj)); + if (!(obj is ThermalResistance otherQuantity)) throw new ArgumentException("Expected type ThermalResistance.", nameof(obj)); + + return CompareTo(otherQuantity); + } + + /// Compares the current with another and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit. + /// A quantity to compare with this instance. + /// A value that indicates the relative order of the quantities being compared. The return value has these meanings: + /// + /// Value Meaning + /// Less than zero This instance precedes in the sort order. + /// Zero This instance occurs in the same position in the sort order as . + /// Greater than zero This instance follows in the sort order. + /// + /// + public int CompareTo(ThermalResistance other) + { + return _value.CompareTo(other.ToUnit(this.Unit).Value); + } + + /// + /// + /// Compare equality to another ThermalResistance 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 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. + [Obsolete("Use Equals(ThermalResistance other, ThermalResistance tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units.")] + public bool Equals(ThermalResistance other, double tolerance, ComparisonType comparisonType) + { + if (tolerance < 0) + throw new ArgumentOutOfRangeException(nameof(tolerance), "Tolerance must be greater than or equal to 0."); + + return UnitsNet.Comparison.Equals( + referenceValue: this.Value, + otherValue: other.As(this.Unit), + tolerance: tolerance, + comparisonType: comparisonType); + } + + /// + public bool Equals(IQuantity? other, IQuantity tolerance) + { + return other is ThermalResistance otherTyped + && (tolerance is ThermalResistance toleranceTyped + ? true + : throw new ArgumentException($"Tolerance quantity ({tolerance.QuantityInfo.Name}) did not match the other quantities of type 'ThermalResistance'.", nameof(tolerance))) + && Equals(otherTyped, toleranceTyped); + } + + /// + public bool Equals(ThermalResistance other, ThermalResistance tolerance) + { + return UnitsNet.Comparison.Equals( + referenceValue: this.Value, + otherValue: other.As(this.Unit), + tolerance: tolerance.As(this.Unit), + comparisonType: ComparisonType.Absolute); + } + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current ThermalResistance. + 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(ThermalResistanceUnit unit) + { + if (Unit == unit) + return Value; + + return ToUnit(unit).Value; + } + + /// + public double As(UnitSystem unitSystem) + { + return As(Info.GetDefaultUnit(unitSystem)); + } + + /// + /// Converts this ThermalResistance to another ThermalResistance with the unit representation . + /// + /// The unit to convert to. + /// A ThermalResistance with the specified unit. + public ThermalResistance ToUnit(ThermalResistanceUnit unit) + { + return ToUnit(unit, DefaultConversionFunctions); + } + + /// + /// Converts this to another using the given with the unit representation . + /// + /// The unit to convert to. + /// The to use for the conversion. + /// A ThermalResistance with the specified unit. + public ThermalResistance ToUnit(ThermalResistanceUnit unit, UnitConverter unitConverter) + { + if (TryToUnit(unit, out var converted)) + { + // Try to convert using the auto-generated conversion methods. + return converted!.Value; + } + else if (unitConverter.TryGetConversionFunction((typeof(ThermalResistance), Unit, typeof(ThermalResistance), unit), out var conversionFunction)) + { + // See if the unit converter has an extensibility conversion registered. + return (ThermalResistance)conversionFunction(this); + } + else if (Unit != BaseUnit) + { + // Conversion to requested unit NOT found. Try to convert to BaseUnit, and then from BaseUnit to requested unit. + var inBaseUnits = ToUnit(BaseUnit); + return inBaseUnits.ToUnit(unit); + } + else + { + // No possible conversion + throw new NotImplementedException($"Can not convert {Unit} to {unit}."); + } + } + + /// + /// Attempts to convert this to another with the unit representation . + /// + /// The unit to convert to. + /// The converted in , if successful. + /// True if successful, otherwise false. + private bool TryToUnit(ThermalResistanceUnit unit, [NotNullWhen(true)] out ThermalResistance? converted) + { + if (Unit == unit) + { + converted = this; + return true; + } + + ThermalResistance? convertedOrNull = (Unit, unit) switch + { + // ThermalResistanceUnit -> BaseUnit + (ThermalResistanceUnit.DegreeCelsiusPerWatt, ThermalResistanceUnit.KelvinPerWatt) => new ThermalResistance(_value, ThermalResistanceUnit.KelvinPerWatt), + + // BaseUnit -> ThermalResistanceUnit + (ThermalResistanceUnit.KelvinPerWatt, ThermalResistanceUnit.DegreeCelsiusPerWatt) => new ThermalResistance(_value, ThermalResistanceUnit.DegreeCelsiusPerWatt), + + _ => null + }; + + if (convertedOrNull is null) + { + converted = default; + return false; + } + + converted = convertedOrNull.Value; + return true; + } + + /// + public ThermalResistance ToUnit(UnitSystem unitSystem) + { + return ToUnit(Info.GetDefaultUnit(unitSystem)); + } + + #region Explicit implementations + + double IQuantity.As(Enum unit) + { + if (unit is not ThermalResistanceUnit typedUnit) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(ThermalResistanceUnit)} is supported.", nameof(unit)); + + return As(typedUnit); + } + + /// + IQuantity IQuantity.ToUnit(Enum unit) + { + if (!(unit is ThermalResistanceUnit typedUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(ThermalResistanceUnit)} is supported.", nameof(unit)); + + return ToUnit(typedUnit, DefaultConversionFunctions); + } + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + IQuantity IQuantity.ToUnit(ThermalResistanceUnit unit) => ToUnit(unit); + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + #endregion + + #endregion + + #region ToString Methods + + /// + /// Gets the default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString(null, null); + } + + /// + /// 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(null, provider); + } + + /// + /// + /// 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, null); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using the specified format provider, or if null. + /// + public string ToString(string? format, IFormatProvider? provider) + { + return QuantityFormatter.Default.Format(this, format, provider); + } + + #endregion + + } +} diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index bbd4d73463..8a95178ced 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -155,6 +155,7 @@ public partial class Quantity { "TemperatureGradient", TemperatureGradient.Info }, { "ThermalConductivity", ThermalConductivity.Info }, { "ThermalInsulance", ThermalInsulance.Info }, + { "ThermalResistance", ThermalResistance.Info }, { "Torque", Torque.Info }, { "Turbidity", Turbidity.Info }, { "VitaminA", VitaminA.Info }, @@ -294,6 +295,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, double value "TemperatureGradient" => TemperatureGradient.From(value, TemperatureGradient.BaseUnit), "ThermalConductivity" => ThermalConductivity.From(value, ThermalConductivity.BaseUnit), "ThermalInsulance" => ThermalInsulance.From(value, ThermalInsulance.BaseUnit), + "ThermalResistance" => ThermalResistance.From(value, ThermalResistance.BaseUnit), "Torque" => Torque.From(value, Torque.BaseUnit), "Turbidity" => Turbidity.From(value, Turbidity.BaseUnit), "VitaminA" => VitaminA.From(value, VitaminA.BaseUnit), @@ -436,6 +438,7 @@ public static bool TryFrom(double value, Enum? unit, [NotNullWhen(true)] out IQu TemperatureGradientUnit temperatureGradientUnit => TemperatureGradient.From(value, temperatureGradientUnit), ThermalConductivityUnit thermalConductivityUnit => ThermalConductivity.From(value, thermalConductivityUnit), ThermalInsulanceUnit thermalInsulanceUnit => ThermalInsulance.From(value, thermalInsulanceUnit), + ThermalResistanceUnit thermalResistanceUnit => ThermalResistance.From(value, thermalResistanceUnit), TorqueUnit torqueUnit => Torque.From(value, torqueUnit), TurbidityUnit turbidityUnit => Turbidity.From(value, turbidityUnit), VitaminAUnit vitaminAUnit => VitaminA.From(value, vitaminAUnit), @@ -588,6 +591,7 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, Type _ when quantityType == typeof(TemperatureGradient) => parser.TryParse(quantityString, formatProvider, TemperatureGradient.From, out quantity), Type _ when quantityType == typeof(ThermalConductivity) => parser.TryParse(quantityString, formatProvider, ThermalConductivity.From, out quantity), Type _ when quantityType == typeof(ThermalInsulance) => parser.TryParse(quantityString, formatProvider, ThermalInsulance.From, out quantity), + Type _ when quantityType == typeof(ThermalResistance) => parser.TryParse(quantityString, formatProvider, ThermalResistance.From, out quantity), Type _ when quantityType == typeof(Torque) => parser.TryParse(quantityString, formatProvider, Torque.From, out quantity), Type _ when quantityType == typeof(Turbidity) => parser.TryParse(quantityString, formatProvider, Turbidity.From, out quantity), Type _ when quantityType == typeof(VitaminA) => parser.TryParse(quantityString, formatProvider, VitaminA.From, out quantity), @@ -721,6 +725,7 @@ internal static IEnumerable GetQuantityTypes() yield return typeof(TemperatureGradient); yield return typeof(ThermalConductivity); yield return typeof(ThermalInsulance); + yield return typeof(ThermalResistance); yield return typeof(Torque); yield return typeof(Turbidity); yield return typeof(VitaminA); diff --git a/UnitsNet/GeneratedCode/Resources/ThermalResistance.restext b/UnitsNet/GeneratedCode/Resources/ThermalResistance.restext new file mode 100644 index 0000000000..78dd2ef664 --- /dev/null +++ b/UnitsNet/GeneratedCode/Resources/ThermalResistance.restext @@ -0,0 +1,2 @@ +DegreesCelsiusPerWatt=°C/W +KelvinsPerWatt=K/W diff --git a/UnitsNet/GeneratedCode/Units/ThermalResistanceUnit.g.cs b/UnitsNet/GeneratedCode/Units/ThermalResistanceUnit.g.cs new file mode 100644 index 0000000000..edb63c8a69 --- /dev/null +++ b/UnitsNet/GeneratedCode/Units/ThermalResistanceUnit.g.cs @@ -0,0 +1,33 @@ +//------------------------------------------------------------------------------ +// +// 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 ThermalResistanceUnit + { + DegreeCelsiusPerWatt = 8, + KelvinPerWatt = 12, + } + + #pragma warning restore 1591 +}