From 094dba11fe56c865ff279813e4ebc91a7e23a3cd Mon Sep 17 00:00:00 2001 From: rbabol Date: Fri, 29 May 2026 04:21:40 -0400 Subject: [PATCH] Add AreaPerLength MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add new quantity for area per unit length (m²/m), commonly used in structural engineering for distributed reinforcement. Includes metric (cm²/m, mm²/m) and imperial (in²/ft, in²/in, ft²/ft) units with verified conversion factors. --- Common/UnitDefinitions/AreaPerLength.json | 85 ++ Common/UnitEnumValues.g.json | 8 + .../AreaPerLength/AreaPerLength.nfproj | 42 + ...nitsNet.NanoFramework.AreaPerLength.nuspec | 26 + .../AreaPerLength/packages.config | 4 + .../Quantities/AreaPerLength.g.cs | 217 +++++ .../Units/AreaPerLengthUnit.g.cs | 37 + .../GeneratedCode/UnitsNet.nanoFramework.sln | 8 + .../NumberToAreaPerLengthExtensionsTest.g.cs | 52 ++ .../NumberToAreaPerLengthExtensions.g.cs | 95 ++ .../NumberToAreaPerLengthExtensionsTest.g.cs | 52 ++ .../NumberToAreaPerLengthExtensions.g.cs | 102 +++ .../CustomCode/AreaPerLengthTests.cs | 22 + .../GeneratedCode/IQuantityTests.g.cs | 2 + .../TestsBase/AreaPerLengthTestsBase.g.cs | 741 ++++++++++++++++ .../Quantities/AreaPerLength.g.cs | 830 ++++++++++++++++++ UnitsNet/GeneratedCode/Quantity.g.cs | 2 + .../Resources/AreaPerLength.restext | 6 + .../Units/AreaPerLengthUnit.g.cs | 37 + 19 files changed, 2368 insertions(+) create mode 100644 Common/UnitDefinitions/AreaPerLength.json create mode 100644 UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/AreaPerLength.nfproj create mode 100644 UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/UnitsNet.NanoFramework.AreaPerLength.nuspec create mode 100644 UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/packages.config create mode 100644 UnitsNet.NanoFramework/GeneratedCode/Quantities/AreaPerLength.g.cs create mode 100644 UnitsNet.NanoFramework/GeneratedCode/Units/AreaPerLengthUnit.g.cs create mode 100644 UnitsNet.NumberExtensions.CS14.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs create mode 100644 UnitsNet.NumberExtensions.CS14/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs create mode 100644 UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs create mode 100644 UnitsNet.NumberExtensions/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs create mode 100644 UnitsNet.Tests/CustomCode/AreaPerLengthTests.cs create mode 100644 UnitsNet.Tests/GeneratedCode/TestsBase/AreaPerLengthTestsBase.g.cs create mode 100644 UnitsNet/GeneratedCode/Quantities/AreaPerLength.g.cs create mode 100644 UnitsNet/GeneratedCode/Resources/AreaPerLength.restext create mode 100644 UnitsNet/GeneratedCode/Units/AreaPerLengthUnit.g.cs diff --git a/Common/UnitDefinitions/AreaPerLength.json b/Common/UnitDefinitions/AreaPerLength.json new file mode 100644 index 0000000000..25960b59c6 --- /dev/null +++ b/Common/UnitDefinitions/AreaPerLength.json @@ -0,0 +1,85 @@ +{ + "Name": "AreaPerLength", + "BaseUnit": "SquareMeterPerMeter", + "XmlDocSummary": "The magnitude of area per unit length, typically used in structural engineering to specify distributed reinforcement.", + "BaseDimensions": { + "L": 1 + }, + "Units": [ + { + "SingularName": "SquareMeterPerMeter", + "PluralName": "SquareMetersPerMeter", + "BaseUnits": { + "L": "Meter" + }, + "FromUnitToBaseFunc": "{x}", + "FromBaseToUnitFunc": "{x}", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "m²/m" ] + } + ] + }, + { + "SingularName": "SquareCentimeterPerMeter", + "PluralName": "SquareCentimetersPerMeter", + "FromUnitToBaseFunc": "{x} * 1e-4", + "FromBaseToUnitFunc": "{x} / 1e-4", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "cm²/m" ] + } + ] + }, + { + "SingularName": "SquareMillimeterPerMeter", + "PluralName": "SquareMillimetersPerMeter", + "FromUnitToBaseFunc": "{x} * 1e-6", + "FromBaseToUnitFunc": "{x} / 1e-6", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "mm²/m" ] + } + ] + }, + { + "SingularName": "SquareInchPerFoot", + "PluralName": "SquareInchesPerFoot", + "FromUnitToBaseFunc": "{x} * 0.00064516 / 0.3048", + "FromBaseToUnitFunc": "{x} * 0.3048 / 0.00064516", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "in²/ft" ] + } + ] + }, + { + "SingularName": "SquareInchPerInch", + "PluralName": "SquareInchesPerInch", + "FromUnitToBaseFunc": "{x} * 0.00064516 / 0.0254", + "FromBaseToUnitFunc": "{x} * 0.0254 / 0.00064516", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "in²/in" ] + } + ] + }, + { + "SingularName": "SquareFootPerFoot", + "PluralName": "SquareFeetPerFoot", + "FromUnitToBaseFunc": "{x} * 0.09290304 / 0.3048", + "FromBaseToUnitFunc": "{x} * 0.3048 / 0.09290304", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "ft²/ft" ] + } + ] + } + ] +} \ No newline at end of file diff --git a/Common/UnitEnumValues.g.json b/Common/UnitEnumValues.g.json index 3ebdc3d87d..da948a3edf 100644 --- a/Common/UnitEnumValues.g.json +++ b/Common/UnitEnumValues.g.json @@ -2018,5 +2018,13 @@ "SquareMeterKelvinPerKilowatt": 5, "SquareMeterKelvinPerWatt": 4, "SquareMillimeterKelvinPerWatt": 13 + }, + "AreaPerLength": { + "SquareCentimeterPerMeter": 10, + "SquareFootPerFoot": 4, + "SquareInchPerFoot": 3, + "SquareInchPerInch": 7, + "SquareMeterPerMeter": 1, + "SquareMillimeterPerMeter": 5 } } diff --git a/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/AreaPerLength.nfproj b/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/AreaPerLength.nfproj new file mode 100644 index 0000000000..70445a6766 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/AreaPerLength.nfproj @@ -0,0 +1,42 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + {99bce9bf-d162-d7b5-a3f8-ea77e4f194c3} + Library + Properties + 512 + UnitsNet + UnitsNet.AreaPerLength + v1.0 + bin\$(Configuration)\$(AssemblyName).xml + + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll + True + True + + + + + + + + + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/UnitsNet.NanoFramework.AreaPerLength.nuspec b/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/UnitsNet.NanoFramework.AreaPerLength.nuspec new file mode 100644 index 0000000000..186c41f8c0 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/UnitsNet.NanoFramework.AreaPerLength.nuspec @@ -0,0 +1,26 @@ + + + + UnitsNet.nanoFramework.AreaPerLength + 6.0.0-pre019 + Units.NET AreaPerLength - nanoFramework + Andreas Gullberg Larsen,nanoframework + UnitsNet + MIT-0 + https://github.com/angularsen/UnitsNet + false + Adds AreaPerLength 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 areaperlength unit units quantity quantities measurement si metric imperial abbreviation abbreviations convert conversion parse immutable + + + + + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/packages.config b/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/packages.config new file mode 100644 index 0000000000..313a8dccdf --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/AreaPerLength/packages.config @@ -0,0 +1,4 @@ + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/Quantities/AreaPerLength.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Quantities/AreaPerLength.g.cs new file mode 100644 index 0000000000..4d3d95c0a7 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/Quantities/AreaPerLength.g.cs @@ -0,0 +1,217 @@ +//------------------------------------------------------------------------------ +// +// 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 +{ + /// + /// + /// The magnitude of area per unit length, typically used in structural engineering to specify distributed reinforcement. + /// + public struct AreaPerLength + { + /// + /// The numeric value this quantity was constructed with. + /// + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + private readonly AreaPerLengthUnit _unit; + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + /// + public AreaPerLengthUnit 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 AreaPerLength(double value, AreaPerLengthUnit unit) + { + _value = value; + _unit = unit; + } + + /// + /// The base unit of AreaPerLength, which is Second. All conversions go via this value. + /// + public static AreaPerLengthUnit BaseUnit { get; } = AreaPerLengthUnit.SquareMeterPerMeter; + + /// + /// Represents the largest possible value of AreaPerLength. + /// + public static AreaPerLength MaxValue { get; } = new AreaPerLength(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of AreaPerLength. + /// + public static AreaPerLength MinValue { get; } = new AreaPerLength(double.MinValue, BaseUnit); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit Second. + /// + public static AreaPerLength Zero { get; } = new AreaPerLength(0, BaseUnit); + #region Conversion Properties + + /// + /// Gets a value of this quantity converted into + /// + public double SquareCentimetersPerMeter => As(AreaPerLengthUnit.SquareCentimeterPerMeter); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareFeetPerFoot => As(AreaPerLengthUnit.SquareFootPerFoot); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareInchesPerFoot => As(AreaPerLengthUnit.SquareInchPerFoot); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareInchesPerInch => As(AreaPerLengthUnit.SquareInchPerInch); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareMetersPerMeter => As(AreaPerLengthUnit.SquareMeterPerMeter); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareMillimetersPerMeter => As(AreaPerLengthUnit.SquareMillimeterPerMeter); + + #endregion + + #region Static Factory Methods + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareCentimetersPerMeter(double squarecentimeterspermeter) => new AreaPerLength(squarecentimeterspermeter, AreaPerLengthUnit.SquareCentimeterPerMeter); + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareFeetPerFoot(double squarefeetperfoot) => new AreaPerLength(squarefeetperfoot, AreaPerLengthUnit.SquareFootPerFoot); + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareInchesPerFoot(double squareinchesperfoot) => new AreaPerLength(squareinchesperfoot, AreaPerLengthUnit.SquareInchPerFoot); + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareInchesPerInch(double squareinchesperinch) => new AreaPerLength(squareinchesperinch, AreaPerLengthUnit.SquareInchPerInch); + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareMetersPerMeter(double squaremeterspermeter) => new AreaPerLength(squaremeterspermeter, AreaPerLengthUnit.SquareMeterPerMeter); + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareMillimetersPerMeter(double squaremillimeterspermeter) => new AreaPerLength(squaremillimeterspermeter, AreaPerLengthUnit.SquareMillimeterPerMeter); + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// AreaPerLength unit value. + public static AreaPerLength From(double value, AreaPerLengthUnit fromUnit) + { + return new AreaPerLength(value, fromUnit); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(AreaPerLengthUnit unit) => GetValueAs(unit); + + /// + /// Converts this AreaPerLength to another AreaPerLength with the unit representation . + /// + /// A AreaPerLength with the specified unit. + public AreaPerLength ToUnit(AreaPerLengthUnit unit) + { + var convertedValue = GetValueAs(unit); + return new AreaPerLength(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 + { + AreaPerLengthUnit.SquareCentimeterPerMeter => _value * 1e-4, + AreaPerLengthUnit.SquareFootPerFoot => _value * 0.09290304 / 0.3048, + AreaPerLengthUnit.SquareInchPerFoot => _value * 0.00064516 / 0.3048, + AreaPerLengthUnit.SquareInchPerInch => _value * 0.00064516 / 0.0254, + AreaPerLengthUnit.SquareMeterPerMeter => _value, + AreaPerLengthUnit.SquareMillimeterPerMeter => _value * 1e-6, + _ => throw new NotImplementedException($"Can't convert {Unit} to base units.") + }; + } + + private double GetValueAs(AreaPerLengthUnit unit) + { + if (Unit == unit) + return _value; + + var baseUnitValue = GetValueInBaseUnit(); + + return unit switch + { + AreaPerLengthUnit.SquareCentimeterPerMeter => baseUnitValue / 1e-4, + AreaPerLengthUnit.SquareFootPerFoot => baseUnitValue * 0.3048 / 0.09290304, + AreaPerLengthUnit.SquareInchPerFoot => baseUnitValue * 0.3048 / 0.00064516, + AreaPerLengthUnit.SquareInchPerInch => baseUnitValue * 0.0254 / 0.00064516, + AreaPerLengthUnit.SquareMeterPerMeter => baseUnitValue, + AreaPerLengthUnit.SquareMillimeterPerMeter => baseUnitValue / 1e-6, + _ => throw new NotImplementedException($"Can't convert {Unit} to {unit}.") + }; + } + + #endregion + } +} + diff --git a/UnitsNet.NanoFramework/GeneratedCode/Units/AreaPerLengthUnit.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Units/AreaPerLengthUnit.g.cs new file mode 100644 index 0000000000..7dad5d4754 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/Units/AreaPerLengthUnit.g.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. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum AreaPerLengthUnit + { + SquareCentimeterPerMeter = 10, + SquareFootPerFoot = 4, + SquareInchPerFoot = 3, + SquareInchPerInch = 7, + SquareMeterPerMeter = 1, + SquareMillimeterPerMeter = 5, + } + + #pragma warning restore 1591 +} diff --git a/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln b/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln index f2d98d63c4..4409409884 100644 --- a/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln +++ b/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln @@ -18,6 +18,8 @@ Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "AreaDensity", "AreaDensity\ EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "AreaMomentOfInertia", "AreaMomentOfInertia\AreaMomentOfInertia.nfproj", "{566ef13c-0d17-e465-d35f-1bd9c65559bf}" EndProject +Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "AreaPerLength", "AreaPerLength\AreaPerLength.nfproj", "{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}" +EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "BitRate", "BitRate\BitRate.nfproj", "{b5fdf997-829f-5281-e624-ee4eee5aa26c}" EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "BrakeSpecificFuelConsumption", "BrakeSpecificFuelConsumption\BrakeSpecificFuelConsumption.nfproj", "{085145f2-2b8f-4d09-5290-c14cdcd452bf}" @@ -312,6 +314,12 @@ Global {566ef13c-0d17-e465-d35f-1bd9c65559bf}.Release|Any CPU.ActiveCfg = Release|Any CPU {566ef13c-0d17-e465-d35f-1bd9c65559bf}.Release|Any CPU.Build.0 = Release|Any CPU {566ef13c-0d17-e465-d35f-1bd9c65559bf}.Release|Any CPU.Deploy.0 = Release|Any CPU +{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU +{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}.Debug|Any CPU.Build.0 = Debug|Any CPU +{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}.Debug|Any CPU.Deploy.0 = Debug|Any CPU +{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}.Release|Any CPU.ActiveCfg = Release|Any CPU +{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}.Release|Any CPU.Build.0 = Release|Any CPU +{99bce9bf-d162-d7b5-a3f8-ea77e4f194c3}.Release|Any CPU.Deploy.0 = Release|Any CPU {b5fdf997-829f-5281-e624-ee4eee5aa26c}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {b5fdf997-829f-5281-e624-ee4eee5aa26c}.Debug|Any CPU.Build.0 = Debug|Any CPU {b5fdf997-829f-5281-e624-ee4eee5aa26c}.Debug|Any CPU.Deploy.0 = Debug|Any CPU diff --git a/UnitsNet.NumberExtensions.CS14.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs b/UnitsNet.NumberExtensions.CS14.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs new file mode 100644 index 0000000000..bedb3ca1fc --- /dev/null +++ b/UnitsNet.NumberExtensions.CS14.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// 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.NumberToAreaPerLength; +using Xunit; + +namespace UnitsNet.Tests +{ + public class NumberToAreaPerLengthExtensionsTests + { + [Fact] + public void NumberToSquareCentimetersPerMeterTest() => + Assert.Equal(AreaPerLength.FromSquareCentimetersPerMeter(2), 2.SquareCentimetersPerMeter); + + [Fact] + public void NumberToSquareFeetPerFootTest() => + Assert.Equal(AreaPerLength.FromSquareFeetPerFoot(2), 2.SquareFeetPerFoot); + + [Fact] + public void NumberToSquareInchesPerFootTest() => + Assert.Equal(AreaPerLength.FromSquareInchesPerFoot(2), 2.SquareInchesPerFoot); + + [Fact] + public void NumberToSquareInchesPerInchTest() => + Assert.Equal(AreaPerLength.FromSquareInchesPerInch(2), 2.SquareInchesPerInch); + + [Fact] + public void NumberToSquareMetersPerMeterTest() => + Assert.Equal(AreaPerLength.FromSquareMetersPerMeter(2), 2.SquareMetersPerMeter); + + [Fact] + public void NumberToSquareMillimetersPerMeterTest() => + Assert.Equal(AreaPerLength.FromSquareMillimetersPerMeter(2), 2.SquareMillimetersPerMeter); + + } +} diff --git a/UnitsNet.NumberExtensions.CS14/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs b/UnitsNet.NumberExtensions.CS14/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs new file mode 100644 index 0000000000..ce3e24ba13 --- /dev/null +++ b/UnitsNet.NumberExtensions.CS14/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// 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.NumberToAreaPerLength +{ + /// + /// A number to AreaPerLength Extensions + /// + public static class NumberToAreaPerLengthExtensions + { +#pragma warning disable CS1591 + extension(T value) +#pragma warning restore CS1591 + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#else + , IConvertible +#endif + { + /// + public AreaPerLength SquareCentimetersPerMeter +#if NET7_0_OR_GREATER + => AreaPerLength.FromSquareCentimetersPerMeter(double.CreateChecked(value)); +#else + => AreaPerLength.FromSquareCentimetersPerMeter(value.ToDouble(null)); +#endif + + /// + public AreaPerLength SquareFeetPerFoot +#if NET7_0_OR_GREATER + => AreaPerLength.FromSquareFeetPerFoot(double.CreateChecked(value)); +#else + => AreaPerLength.FromSquareFeetPerFoot(value.ToDouble(null)); +#endif + + /// + public AreaPerLength SquareInchesPerFoot +#if NET7_0_OR_GREATER + => AreaPerLength.FromSquareInchesPerFoot(double.CreateChecked(value)); +#else + => AreaPerLength.FromSquareInchesPerFoot(value.ToDouble(null)); +#endif + + /// + public AreaPerLength SquareInchesPerInch +#if NET7_0_OR_GREATER + => AreaPerLength.FromSquareInchesPerInch(double.CreateChecked(value)); +#else + => AreaPerLength.FromSquareInchesPerInch(value.ToDouble(null)); +#endif + + /// + public AreaPerLength SquareMetersPerMeter +#if NET7_0_OR_GREATER + => AreaPerLength.FromSquareMetersPerMeter(double.CreateChecked(value)); +#else + => AreaPerLength.FromSquareMetersPerMeter(value.ToDouble(null)); +#endif + + /// + public AreaPerLength SquareMillimetersPerMeter +#if NET7_0_OR_GREATER + => AreaPerLength.FromSquareMillimetersPerMeter(double.CreateChecked(value)); +#else + => AreaPerLength.FromSquareMillimetersPerMeter(value.ToDouble(null)); +#endif + + } + } +} diff --git a/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs new file mode 100644 index 0000000000..801cd9f5f5 --- /dev/null +++ b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToAreaPerLengthExtensionsTest.g.cs @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// 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.NumberToAreaPerLength; +using Xunit; + +namespace UnitsNet.Tests +{ + public class NumberToAreaPerLengthExtensionsTests + { + [Fact] + public void NumberToSquareCentimetersPerMeterTest() => + Assert.Equal(AreaPerLength.FromSquareCentimetersPerMeter(2), 2.SquareCentimetersPerMeter()); + + [Fact] + public void NumberToSquareFeetPerFootTest() => + Assert.Equal(AreaPerLength.FromSquareFeetPerFoot(2), 2.SquareFeetPerFoot()); + + [Fact] + public void NumberToSquareInchesPerFootTest() => + Assert.Equal(AreaPerLength.FromSquareInchesPerFoot(2), 2.SquareInchesPerFoot()); + + [Fact] + public void NumberToSquareInchesPerInchTest() => + Assert.Equal(AreaPerLength.FromSquareInchesPerInch(2), 2.SquareInchesPerInch()); + + [Fact] + public void NumberToSquareMetersPerMeterTest() => + Assert.Equal(AreaPerLength.FromSquareMetersPerMeter(2), 2.SquareMetersPerMeter()); + + [Fact] + public void NumberToSquareMillimetersPerMeterTest() => + Assert.Equal(AreaPerLength.FromSquareMillimetersPerMeter(2), 2.SquareMillimetersPerMeter()); + + } +} diff --git a/UnitsNet.NumberExtensions/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs b/UnitsNet.NumberExtensions/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs new file mode 100644 index 0000000000..777ead587f --- /dev/null +++ b/UnitsNet.NumberExtensions/GeneratedCode/NumberToAreaPerLengthExtensions.g.cs @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// 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.NumberToAreaPerLength +{ + /// + /// A number to AreaPerLength Extensions + /// + public static class NumberToAreaPerLengthExtensions + { + /// + public static AreaPerLength SquareCentimetersPerMeter(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => AreaPerLength.FromSquareCentimetersPerMeter(double.CreateChecked(value)); +#else + , IConvertible + => AreaPerLength.FromSquareCentimetersPerMeter(value.ToDouble(null)); +#endif + + /// + public static AreaPerLength SquareFeetPerFoot(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => AreaPerLength.FromSquareFeetPerFoot(double.CreateChecked(value)); +#else + , IConvertible + => AreaPerLength.FromSquareFeetPerFoot(value.ToDouble(null)); +#endif + + /// + public static AreaPerLength SquareInchesPerFoot(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => AreaPerLength.FromSquareInchesPerFoot(double.CreateChecked(value)); +#else + , IConvertible + => AreaPerLength.FromSquareInchesPerFoot(value.ToDouble(null)); +#endif + + /// + public static AreaPerLength SquareInchesPerInch(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => AreaPerLength.FromSquareInchesPerInch(double.CreateChecked(value)); +#else + , IConvertible + => AreaPerLength.FromSquareInchesPerInch(value.ToDouble(null)); +#endif + + /// + public static AreaPerLength SquareMetersPerMeter(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => AreaPerLength.FromSquareMetersPerMeter(double.CreateChecked(value)); +#else + , IConvertible + => AreaPerLength.FromSquareMetersPerMeter(value.ToDouble(null)); +#endif + + /// + public static AreaPerLength SquareMillimetersPerMeter(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber + => AreaPerLength.FromSquareMillimetersPerMeter(double.CreateChecked(value)); +#else + , IConvertible + => AreaPerLength.FromSquareMillimetersPerMeter(value.ToDouble(null)); +#endif + + } +} diff --git a/UnitsNet.Tests/CustomCode/AreaPerLengthTests.cs b/UnitsNet.Tests/CustomCode/AreaPerLengthTests.cs new file mode 100644 index 0000000000..c7ae2284bd --- /dev/null +++ b/UnitsNet.Tests/CustomCode/AreaPerLengthTests.cs @@ -0,0 +1,22 @@ +// 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 AreaPerLengthTests : AreaPerLengthTestsBase + { + protected override double SquareMetersPerMeterInOneSquareMeterPerMeter => 1; + + protected override double SquareCentimetersPerMeterInOneSquareMeterPerMeter => 1E4; + + protected override double SquareMillimetersPerMeterInOneSquareMeterPerMeter => 1E6; + + protected override double SquareInchesPerFootInOneSquareMeterPerMeter => 472.44094488188976; + + protected override double SquareInchesPerInchInOneSquareMeterPerMeter => 39.370078740157481; + + protected override double SquareFeetPerFootInOneSquareMeterPerMeter => 3.2808398950131234; + } +} diff --git a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs index 9dfabd182a..3af19e212b 100644 --- a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs +++ b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs @@ -42,6 +42,7 @@ void Assertion(int expectedValue, Enum expectedUnit, IQuantity quantity) Assertion(3, AreaUnit.UsSurveySquareFoot, Quantity.From(3, AreaUnit.UsSurveySquareFoot)); Assertion(3, AreaDensityUnit.MilligramPerSquareMeter, Quantity.From(3, AreaDensityUnit.MilligramPerSquareMeter)); Assertion(3, AreaMomentOfInertiaUnit.MillimeterToTheFourth, Quantity.From(3, AreaMomentOfInertiaUnit.MillimeterToTheFourth)); + Assertion(3, AreaPerLengthUnit.SquareMillimeterPerMeter, Quantity.From(3, AreaPerLengthUnit.SquareMillimeterPerMeter)); Assertion(3, BitRateUnit.TeraoctetPerSecond, Quantity.From(3, BitRateUnit.TeraoctetPerSecond)); Assertion(3, BrakeSpecificFuelConsumptionUnit.PoundPerMechanicalHorsepowerHour, Quantity.From(3, BrakeSpecificFuelConsumptionUnit.PoundPerMechanicalHorsepowerHour)); Assertion(3, CoefficientOfThermalExpansionUnit.PpmPerKelvin, Quantity.From(3, CoefficientOfThermalExpansionUnit.PpmPerKelvin)); @@ -177,6 +178,7 @@ public void QuantityInfo_IsSameAsStaticInfoProperty() Assertion(Area.Info, Area.Zero); Assertion(AreaDensity.Info, AreaDensity.Zero); Assertion(AreaMomentOfInertia.Info, AreaMomentOfInertia.Zero); + Assertion(AreaPerLength.Info, AreaPerLength.Zero); Assertion(BitRate.Info, BitRate.Zero); Assertion(BrakeSpecificFuelConsumption.Info, BrakeSpecificFuelConsumption.Zero); Assertion(CoefficientOfThermalExpansion.Info, CoefficientOfThermalExpansion.Zero); diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaPerLengthTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaPerLengthTestsBase.g.cs new file mode 100644 index 0000000000..12258cdfc1 --- /dev/null +++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaPerLengthTestsBase.g.cs @@ -0,0 +1,741 @@ +//------------------------------------------------------------------------------ +// +// 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.InternalHelpers; +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 AreaPerLength. + /// +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class AreaPerLengthTestsBase : QuantityTestsBase + { + protected abstract double SquareCentimetersPerMeterInOneSquareMeterPerMeter { get; } + protected abstract double SquareFeetPerFootInOneSquareMeterPerMeter { get; } + protected abstract double SquareInchesPerFootInOneSquareMeterPerMeter { get; } + protected abstract double SquareInchesPerInchInOneSquareMeterPerMeter { get; } + protected abstract double SquareMetersPerMeterInOneSquareMeterPerMeter { get; } + protected abstract double SquareMillimetersPerMeterInOneSquareMeterPerMeter { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double SquareCentimetersPerMeterTolerance { get { return 1e-5; } } + protected virtual double SquareFeetPerFootTolerance { get { return 1e-5; } } + protected virtual double SquareInchesPerFootTolerance { get { return 1e-5; } } + protected virtual double SquareInchesPerInchTolerance { get { return 1e-5; } } + protected virtual double SquareMetersPerMeterTolerance { get { return 1e-5; } } + protected virtual double SquareMillimetersPerMeterTolerance { get { return 1e-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(AreaPerLengthUnit unit) + { + return unit switch + { + AreaPerLengthUnit.SquareCentimeterPerMeter => (SquareCentimetersPerMeterInOneSquareMeterPerMeter, SquareCentimetersPerMeterTolerance), + AreaPerLengthUnit.SquareFootPerFoot => (SquareFeetPerFootInOneSquareMeterPerMeter, SquareFeetPerFootTolerance), + AreaPerLengthUnit.SquareInchPerFoot => (SquareInchesPerFootInOneSquareMeterPerMeter, SquareInchesPerFootTolerance), + AreaPerLengthUnit.SquareInchPerInch => (SquareInchesPerInchInOneSquareMeterPerMeter, SquareInchesPerInchTolerance), + AreaPerLengthUnit.SquareMeterPerMeter => (SquareMetersPerMeterInOneSquareMeterPerMeter, SquareMetersPerMeterTolerance), + AreaPerLengthUnit.SquareMillimeterPerMeter => (SquareMillimetersPerMeterInOneSquareMeterPerMeter, SquareMillimetersPerMeterTolerance), + _ => throw new NotSupportedException() + }; + } + + public static IEnumerable UnitTypes = new List + { + new object[] { AreaPerLengthUnit.SquareCentimeterPerMeter }, + new object[] { AreaPerLengthUnit.SquareFootPerFoot }, + new object[] { AreaPerLengthUnit.SquareInchPerFoot }, + new object[] { AreaPerLengthUnit.SquareInchPerInch }, + new object[] { AreaPerLengthUnit.SquareMeterPerMeter }, + new object[] { AreaPerLengthUnit.SquareMillimeterPerMeter }, + }; + + [Fact] + public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit() + { + var quantity = new AreaPerLength(); + Assert.Equal(0, quantity.Value); + Assert.Equal(AreaPerLengthUnit.SquareMeterPerMeter, quantity.Unit); + } + + [Fact] + public void Ctor_WithInfinityValue_DoNotThrowsArgumentException() + { + var exception1 = Record.Exception(() => new AreaPerLength(double.PositiveInfinity, AreaPerLengthUnit.SquareMeterPerMeter)); + var exception2 = Record.Exception(() => new AreaPerLength(double.NegativeInfinity, AreaPerLengthUnit.SquareMeterPerMeter)); + + Assert.Null(exception1); + Assert.Null(exception2); + } + + [Fact] + public void Ctor_WithNaNValue_DoNotThrowsArgumentException() + { + var exception = Record.Exception(() => new AreaPerLength(double.NaN, AreaPerLengthUnit.SquareMeterPerMeter)); + + Assert.Null(exception); + } + + [Fact] + public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException() + { + Assert.Throws(() => new AreaPerLength(value: 1, unitSystem: null)); + } + + [Fact] + public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits() + { + var quantity = new AreaPerLength(value: 1, unitSystem: UnitSystem.SI); + Assert.Equal(1, quantity.Value); + Assert.True(quantity.QuantityInfo[quantity.Unit].BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits)); + } + + [Fact] + public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits); + Assert.Throws(() => new AreaPerLength(value: 1, unitSystem: unsupportedUnitSystem)); + } + + [Fact] + public void AreaPerLength_QuantityInfo_ReturnsQuantityInfoDescribingQuantity() + { + AreaPerLengthUnit[] unitsOrderedByName = EnumHelper.GetValues().OrderBy(x => x.ToString(), StringComparer.OrdinalIgnoreCase).ToArray(); + var quantity = new AreaPerLength(1, AreaPerLengthUnit.SquareMeterPerMeter); + + QuantityInfo quantityInfo = quantity.QuantityInfo; + + Assert.Equal("AreaPerLength", quantityInfo.Name); + Assert.Equal(AreaPerLength.Zero, quantityInfo.Zero); + Assert.Equal(AreaPerLength.BaseUnit, quantityInfo.BaseUnitInfo.Value); + Assert.Equal(unitsOrderedByName, quantityInfo.Units); + Assert.Equal(unitsOrderedByName, quantityInfo.UnitInfos.Select(x => x.Value)); + Assert.Equal(AreaPerLength.Info, quantityInfo); + Assert.Equal(quantityInfo, ((IQuantity)quantity).QuantityInfo); + Assert.Equal(quantityInfo, ((IQuantity)quantity).QuantityInfo); + } + + [Fact] + public void SquareMeterPerMeterToAreaPerLengthUnits() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + AssertEx.EqualTolerance(SquareCentimetersPerMeterInOneSquareMeterPerMeter, squaremeterpermeter.SquareCentimetersPerMeter, SquareCentimetersPerMeterTolerance); + AssertEx.EqualTolerance(SquareFeetPerFootInOneSquareMeterPerMeter, squaremeterpermeter.SquareFeetPerFoot, SquareFeetPerFootTolerance); + AssertEx.EqualTolerance(SquareInchesPerFootInOneSquareMeterPerMeter, squaremeterpermeter.SquareInchesPerFoot, SquareInchesPerFootTolerance); + AssertEx.EqualTolerance(SquareInchesPerInchInOneSquareMeterPerMeter, squaremeterpermeter.SquareInchesPerInch, SquareInchesPerInchTolerance); + AssertEx.EqualTolerance(SquareMetersPerMeterInOneSquareMeterPerMeter, squaremeterpermeter.SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(SquareMillimetersPerMeterInOneSquareMeterPerMeter, squaremeterpermeter.SquareMillimetersPerMeter, SquareMillimetersPerMeterTolerance); + } + + [Fact] + public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit() + { + Assert.All(EnumHelper.GetValues(), unit => + { + var quantity = AreaPerLength.From(1, unit); + Assert.Equal(1, quantity.Value); + Assert.Equal(unit, quantity.Unit); + }); + } + + [Fact] + public void FromSquareMetersPerMeter_WithInfinityValue_DoNotThrowsArgumentException() + { + var exception1 = Record.Exception(() => AreaPerLength.FromSquareMetersPerMeter(double.PositiveInfinity)); + var exception2 = Record.Exception(() => AreaPerLength.FromSquareMetersPerMeter(double.NegativeInfinity)); + + Assert.Null(exception1); + Assert.Null(exception2); + } + + [Fact] + public void FromSquareMetersPerMeter_WithNanValue_DoNotThrowsArgumentException() + { + var exception = Record.Exception(() => AreaPerLength.FromSquareMetersPerMeter(double.NaN)); + + Assert.Null(exception); + } + + [Fact] + public void As() + { + var squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + AssertEx.EqualTolerance(SquareCentimetersPerMeterInOneSquareMeterPerMeter, squaremeterpermeter.As(AreaPerLengthUnit.SquareCentimeterPerMeter), SquareCentimetersPerMeterTolerance); + AssertEx.EqualTolerance(SquareFeetPerFootInOneSquareMeterPerMeter, squaremeterpermeter.As(AreaPerLengthUnit.SquareFootPerFoot), SquareFeetPerFootTolerance); + AssertEx.EqualTolerance(SquareInchesPerFootInOneSquareMeterPerMeter, squaremeterpermeter.As(AreaPerLengthUnit.SquareInchPerFoot), SquareInchesPerFootTolerance); + AssertEx.EqualTolerance(SquareInchesPerInchInOneSquareMeterPerMeter, squaremeterpermeter.As(AreaPerLengthUnit.SquareInchPerInch), SquareInchesPerInchTolerance); + AssertEx.EqualTolerance(SquareMetersPerMeterInOneSquareMeterPerMeter, squaremeterpermeter.As(AreaPerLengthUnit.SquareMeterPerMeter), SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(SquareMillimetersPerMeterInOneSquareMeterPerMeter, squaremeterpermeter.As(AreaPerLengthUnit.SquareMillimeterPerMeter), SquareMillimetersPerMeterTolerance); + } + + [Fact] + public virtual void BaseUnit_HasSIBase() + { + var baseUnitInfo = AreaPerLength.Info.BaseUnitInfo; + Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits)); + } + + [Fact] + public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits() + { + var quantity = new AreaPerLength(value: 1, unit: AreaPerLength.BaseUnit); + var expectedValue = quantity.As(AreaPerLength.Info.GetDefaultUnit(UnitSystem.SI)); + + var convertedValue = quantity.As(UnitSystem.SI); + + Assert.Equal(expectedValue, convertedValue); + } + + [Fact] + public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull() + { + var quantity = new AreaPerLength(value: 1, unit: AreaPerLength.BaseUnit); + UnitSystem nullUnitSystem = null!; + Assert.Throws(() => quantity.As(nullUnitSystem)); + } + + [Fact] + public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var quantity = new AreaPerLength(value: 1, unit: AreaPerLength.BaseUnit); + var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits); + Assert.Throws(() => quantity.As(unsupportedUnitSystem)); + } + + [Fact] + public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits() + { + var quantity = new AreaPerLength(value: 1, unit: AreaPerLength.BaseUnit); + var expectedUnit = AreaPerLength.Info.GetDefaultUnit(UnitSystem.SI); + var expectedValue = quantity.As(expectedUnit); + + AreaPerLength convertedQuantity = quantity.ToUnit(UnitSystem.SI); + + Assert.Equal(expectedUnit, convertedQuantity.Unit); + Assert.Equal(expectedValue, convertedQuantity.Value); + } + + [Fact] + public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull() + { + UnitSystem nullUnitSystem = null!; + var quantity = new AreaPerLength(value: 1, unit: AreaPerLength.BaseUnit); + Assert.Throws(() => quantity.ToUnit(nullUnitSystem)); + } + + [Fact] + public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits); + var quantity = new AreaPerLength(value: 1, unit: AreaPerLength.BaseUnit); + Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem)); + } + + [Theory] + [InlineData("en-US", "4.2 cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter, 4.2)] + [InlineData("en-US", "4.2 ft²/ft", AreaPerLengthUnit.SquareFootPerFoot, 4.2)] + [InlineData("en-US", "4.2 in²/ft", AreaPerLengthUnit.SquareInchPerFoot, 4.2)] + [InlineData("en-US", "4.2 in²/in", AreaPerLengthUnit.SquareInchPerInch, 4.2)] + [InlineData("en-US", "4.2 m²/m", AreaPerLengthUnit.SquareMeterPerMeter, 4.2)] + [InlineData("en-US", "4.2 mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter, 4.2)] + public void Parse(string culture, string quantityString, AreaPerLengthUnit expectedUnit, double expectedValue) + { + using var _ = new CultureScope(culture); + var parsed = AreaPerLength.Parse(quantityString); + Assert.Equal(expectedUnit, parsed.Unit); + Assert.Equal(expectedValue, parsed.Value); + } + + [Theory] + [InlineData("en-US", "4.2 cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter, 4.2)] + [InlineData("en-US", "4.2 ft²/ft", AreaPerLengthUnit.SquareFootPerFoot, 4.2)] + [InlineData("en-US", "4.2 in²/ft", AreaPerLengthUnit.SquareInchPerFoot, 4.2)] + [InlineData("en-US", "4.2 in²/in", AreaPerLengthUnit.SquareInchPerInch, 4.2)] + [InlineData("en-US", "4.2 m²/m", AreaPerLengthUnit.SquareMeterPerMeter, 4.2)] + [InlineData("en-US", "4.2 mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter, 4.2)] + public void TryParse(string culture, string quantityString, AreaPerLengthUnit expectedUnit, double expectedValue) + { + using var _ = new CultureScope(culture); + Assert.True(AreaPerLength.TryParse(quantityString, out AreaPerLength parsed)); + Assert.Equal(expectedUnit, parsed.Unit); + Assert.Equal(expectedValue, parsed.Value); + } + + [Theory] + [InlineData("cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void ParseUnit_WithUsEnglishCurrentCulture(string abbreviation, AreaPerLengthUnit expectedUnit) + { + // Fallback culture "en-US" is always localized + using var _ = new CultureScope("en-US"); + AreaPerLengthUnit parsedUnit = AreaPerLength.ParseUnit(abbreviation); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void ParseUnit_WithUnsupportedCurrentCulture_FallsBackToUsEnglish(string abbreviation, AreaPerLengthUnit expectedUnit) + { + // Currently, no abbreviations are localized for Icelandic, so it should fall back to "en-US" when parsing. + using var _ = new CultureScope("is-IS"); + AreaPerLengthUnit parsedUnit = AreaPerLength.ParseUnit(abbreviation); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("en-US", "ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("en-US", "in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("en-US", "in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("en-US", "m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("en-US", "mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void ParseUnit_WithCurrentCulture(string culture, string abbreviation, AreaPerLengthUnit expectedUnit) + { + using var _ = new CultureScope(culture); + AreaPerLengthUnit parsedUnit = AreaPerLength.ParseUnit(abbreviation); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("en-US", "ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("en-US", "in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("en-US", "in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("en-US", "m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("en-US", "mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void ParseUnit_WithCulture(string culture, string abbreviation, AreaPerLengthUnit expectedUnit) + { + AreaPerLengthUnit parsedUnit = AreaPerLength.ParseUnit(abbreviation, CultureInfo.GetCultureInfo(culture)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void TryParseUnit_WithUsEnglishCurrentCulture(string abbreviation, AreaPerLengthUnit expectedUnit) + { + // Fallback culture "en-US" is always localized + using var _ = new CultureScope("en-US"); + Assert.True(AreaPerLength.TryParseUnit(abbreviation, out AreaPerLengthUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void TryParseUnit_WithUnsupportedCurrentCulture_FallsBackToUsEnglish(string abbreviation, AreaPerLengthUnit 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(AreaPerLength.TryParseUnit(abbreviation, out AreaPerLengthUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("en-US", "ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("en-US", "in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("en-US", "in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("en-US", "m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("en-US", "mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void TryParseUnit_WithCurrentCulture(string culture, string abbreviation, AreaPerLengthUnit expectedUnit) + { + using var _ = new CultureScope(culture); + Assert.True(AreaPerLength.TryParseUnit(abbreviation, out AreaPerLengthUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", "cm²/m", AreaPerLengthUnit.SquareCentimeterPerMeter)] + [InlineData("en-US", "ft²/ft", AreaPerLengthUnit.SquareFootPerFoot)] + [InlineData("en-US", "in²/ft", AreaPerLengthUnit.SquareInchPerFoot)] + [InlineData("en-US", "in²/in", AreaPerLengthUnit.SquareInchPerInch)] + [InlineData("en-US", "m²/m", AreaPerLengthUnit.SquareMeterPerMeter)] + [InlineData("en-US", "mm²/m", AreaPerLengthUnit.SquareMillimeterPerMeter)] + public void TryParseUnit_WithCulture(string culture, string abbreviation, AreaPerLengthUnit expectedUnit) + { + Assert.True(AreaPerLength.TryParseUnit(abbreviation, CultureInfo.GetCultureInfo(culture), out AreaPerLengthUnit parsedUnit)); + Assert.Equal(expectedUnit, parsedUnit); + } + + [Theory] + [InlineData("en-US", AreaPerLengthUnit.SquareCentimeterPerMeter, "cm²/m")] + [InlineData("en-US", AreaPerLengthUnit.SquareFootPerFoot, "ft²/ft")] + [InlineData("en-US", AreaPerLengthUnit.SquareInchPerFoot, "in²/ft")] + [InlineData("en-US", AreaPerLengthUnit.SquareInchPerInch, "in²/in")] + [InlineData("en-US", AreaPerLengthUnit.SquareMeterPerMeter, "m²/m")] + [InlineData("en-US", AreaPerLengthUnit.SquareMillimeterPerMeter, "mm²/m")] + public void GetAbbreviationForCulture(string culture, AreaPerLengthUnit unit, string expectedAbbreviation) + { + var defaultAbbreviation = AreaPerLength.GetAbbreviation(unit, CultureInfo.GetCultureInfo(culture)); + Assert.Equal(expectedAbbreviation, defaultAbbreviation); + } + + [Fact] + public void GetAbbreviationWithDefaultCulture() + { + Assert.All(AreaPerLength.Units, unit => + { + var expectedAbbreviation = UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit); + + var defaultAbbreviation = AreaPerLength.GetAbbreviation(unit); + + Assert.Equal(expectedAbbreviation, defaultAbbreviation); + }); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit(AreaPerLengthUnit unit) + { + var inBaseUnits = AreaPerLength.From(1.0, AreaPerLength.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(AreaPerLengthUnit unit) + { + var quantity = AreaPerLength.From(3.0, unit); + var toUnitWithSameUnit = quantity.ToUnit(unit); + Assert.Equal(quantity, toUnitWithSameUnit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(AreaPerLengthUnit unit) + { + Assert.All(AreaPerLength.Units.Where(u => u != AreaPerLength.BaseUnit), fromUnit => + { + var quantity = AreaPerLength.From(3.0, fromUnit); + var converted = quantity.ToUnit(unit); + Assert.Equal(converted.Unit, unit); + }); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public virtual void ToUnit_FromDefaultQuantity_ReturnsQuantityWithGivenUnit(AreaPerLengthUnit unit) + { + var quantity = default(AreaPerLength); + var converted = quantity.ToUnit(unit); + Assert.Equal(converted.Unit, unit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit_FromIQuantity_ReturnsTheExpectedIQuantity(AreaPerLengthUnit unit) + { + var quantity = AreaPerLength.From(3, AreaPerLength.BaseUnit); + AreaPerLength 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() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + AssertEx.EqualTolerance(1, AreaPerLength.FromSquareCentimetersPerMeter(squaremeterpermeter.SquareCentimetersPerMeter).SquareMetersPerMeter, SquareCentimetersPerMeterTolerance); + AssertEx.EqualTolerance(1, AreaPerLength.FromSquareFeetPerFoot(squaremeterpermeter.SquareFeetPerFoot).SquareMetersPerMeter, SquareFeetPerFootTolerance); + AssertEx.EqualTolerance(1, AreaPerLength.FromSquareInchesPerFoot(squaremeterpermeter.SquareInchesPerFoot).SquareMetersPerMeter, SquareInchesPerFootTolerance); + AssertEx.EqualTolerance(1, AreaPerLength.FromSquareInchesPerInch(squaremeterpermeter.SquareInchesPerInch).SquareMetersPerMeter, SquareInchesPerInchTolerance); + AssertEx.EqualTolerance(1, AreaPerLength.FromSquareMetersPerMeter(squaremeterpermeter.SquareMetersPerMeter).SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(1, AreaPerLength.FromSquareMillimetersPerMeter(squaremeterpermeter.SquareMillimetersPerMeter).SquareMetersPerMeter, SquareMillimetersPerMeterTolerance); + } + + [Fact] + public void ArithmeticOperators() + { + AreaPerLength v = AreaPerLength.FromSquareMetersPerMeter(1); + AssertEx.EqualTolerance(-1, -v.SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(2, (AreaPerLength.FromSquareMetersPerMeter(3)-v).SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(2, (v + v).SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(10, (v*10).SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(10, (10*v).SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(2, (AreaPerLength.FromSquareMetersPerMeter(10)/5).SquareMetersPerMeter, SquareMetersPerMeterTolerance); + AssertEx.EqualTolerance(2, AreaPerLength.FromSquareMetersPerMeter(10)/AreaPerLength.FromSquareMetersPerMeter(5), SquareMetersPerMeterTolerance); + } + + [Fact] + public void ComparisonOperators() + { + AreaPerLength oneSquareMeterPerMeter = AreaPerLength.FromSquareMetersPerMeter(1); + AreaPerLength twoSquareMetersPerMeter = AreaPerLength.FromSquareMetersPerMeter(2); + + Assert.True(oneSquareMeterPerMeter < twoSquareMetersPerMeter); + Assert.True(oneSquareMeterPerMeter <= twoSquareMetersPerMeter); + Assert.True(twoSquareMetersPerMeter > oneSquareMeterPerMeter); + Assert.True(twoSquareMetersPerMeter >= oneSquareMeterPerMeter); + + Assert.False(oneSquareMeterPerMeter > twoSquareMetersPerMeter); + Assert.False(oneSquareMeterPerMeter >= twoSquareMetersPerMeter); + Assert.False(twoSquareMetersPerMeter < oneSquareMeterPerMeter); + Assert.False(twoSquareMetersPerMeter <= oneSquareMeterPerMeter); + } + + [Fact] + public void CompareToIsImplemented() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + Assert.Equal(0, squaremeterpermeter.CompareTo(squaremeterpermeter)); + Assert.True(squaremeterpermeter.CompareTo(AreaPerLength.Zero) > 0); + Assert.True(AreaPerLength.Zero.CompareTo(squaremeterpermeter) < 0); + } + + [Fact] + public void CompareToThrowsOnTypeMismatch() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + Assert.Throws(() => squaremeterpermeter.CompareTo(new object())); + } + + [Fact] + public void CompareToThrowsOnNull() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + Assert.Throws(() => squaremeterpermeter.CompareTo(null)); + } + + [Theory] + [InlineData(1, AreaPerLengthUnit.SquareMeterPerMeter, 1, AreaPerLengthUnit.SquareMeterPerMeter, true)] // Same value and unit. + [InlineData(1, AreaPerLengthUnit.SquareMeterPerMeter, 2, AreaPerLengthUnit.SquareMeterPerMeter, false)] // Different value. + [InlineData(2, AreaPerLengthUnit.SquareMeterPerMeter, 1, AreaPerLengthUnit.SquareCentimeterPerMeter, false)] // Different value and unit. + [InlineData(1, AreaPerLengthUnit.SquareMeterPerMeter, 1, AreaPerLengthUnit.SquareCentimeterPerMeter, false)] // Different unit. + public void Equals_ReturnsTrue_IfValueAndUnitAreEqual(double valueA, AreaPerLengthUnit unitA, double valueB, AreaPerLengthUnit unitB, bool expectEqual) + { + var a = new AreaPerLength(valueA, unitA); + var b = new AreaPerLength(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 = AreaPerLength.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 EqualsReturnsFalseOnTypeMismatch() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + Assert.False(squaremeterpermeter.Equals(new object())); + } + + [Fact] + public void EqualsReturnsFalseOnNull() + { + AreaPerLength squaremeterpermeter = AreaPerLength.FromSquareMetersPerMeter(1); + Assert.False(squaremeterpermeter.Equals(null)); + } + + [Theory] + [InlineData(1, 2)] + [InlineData(100, 110)] + [InlineData(100, 90)] + public void Equals_WithTolerance(double firstValue, double secondValue) + { + var quantity = AreaPerLength.FromSquareMetersPerMeter(firstValue); + var otherQuantity = AreaPerLength.FromSquareMetersPerMeter(secondValue); + AreaPerLength maxTolerance = quantity > otherQuantity ? quantity - otherQuantity : otherQuantity - quantity; + var largerTolerance = maxTolerance * 1.1; + var smallerTolerance = maxTolerance / 1.1; + Assert.True(quantity.Equals(quantity, AreaPerLength.Zero)); + Assert.True(quantity.Equals(quantity, maxTolerance)); + Assert.True(quantity.Equals(otherQuantity, maxTolerance)); + Assert.True(quantity.Equals(otherQuantity, largerTolerance)); + Assert.False(quantity.Equals(otherQuantity, smallerTolerance)); + } + + [Fact] + public void Equals_WithNegativeTolerance_ThrowsArgumentOutOfRangeException() + { + var quantity = AreaPerLength.FromSquareMetersPerMeter(1); + var negativeTolerance = AreaPerLength.FromSquareMetersPerMeter(-1); + Assert.Throws(() => quantity.Equals(quantity, negativeTolerance)); + } + + [Fact] + public void HasAtLeastOneAbbreviationSpecified() + { + var units = Enum.GetValues(); + foreach (var unit in units) + { + var defaultAbbreviation = UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit); + } + } + + [Fact] + public void BaseDimensionsShouldNeverBeNull() + { + Assert.False(AreaPerLength.BaseDimensions is null); + } + + [Fact] + public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture() + { + using var _ = new CultureScope("en-US"); + Assert.Equal("1 cm²/m", new AreaPerLength(1, AreaPerLengthUnit.SquareCentimeterPerMeter).ToString()); + Assert.Equal("1 ft²/ft", new AreaPerLength(1, AreaPerLengthUnit.SquareFootPerFoot).ToString()); + Assert.Equal("1 in²/ft", new AreaPerLength(1, AreaPerLengthUnit.SquareInchPerFoot).ToString()); + Assert.Equal("1 in²/in", new AreaPerLength(1, AreaPerLengthUnit.SquareInchPerInch).ToString()); + Assert.Equal("1 m²/m", new AreaPerLength(1, AreaPerLengthUnit.SquareMeterPerMeter).ToString()); + Assert.Equal("1 mm²/m", new AreaPerLength(1, AreaPerLengthUnit.SquareMillimeterPerMeter).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 cm²/m", new AreaPerLength(1, AreaPerLengthUnit.SquareCentimeterPerMeter).ToString(swedishCulture)); + Assert.Equal("1 ft²/ft", new AreaPerLength(1, AreaPerLengthUnit.SquareFootPerFoot).ToString(swedishCulture)); + Assert.Equal("1 in²/ft", new AreaPerLength(1, AreaPerLengthUnit.SquareInchPerFoot).ToString(swedishCulture)); + Assert.Equal("1 in²/in", new AreaPerLength(1, AreaPerLengthUnit.SquareInchPerInch).ToString(swedishCulture)); + Assert.Equal("1 m²/m", new AreaPerLength(1, AreaPerLengthUnit.SquareMeterPerMeter).ToString(swedishCulture)); + Assert.Equal("1 mm²/m", new AreaPerLength(1, AreaPerLengthUnit.SquareMillimeterPerMeter).ToString(swedishCulture)); + } + + [Fact] + public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture() + { + var _ = new CultureScope(CultureInfo.InvariantCulture); + Assert.Equal("0.1 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s1")); + Assert.Equal("0.12 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s2")); + Assert.Equal("0.123 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s3")); + Assert.Equal("0.1235 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s4")); + } + + [Fact] + public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture() + { + var culture = CultureInfo.InvariantCulture; + Assert.Equal("0.1 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s1", culture)); + Assert.Equal("0.12 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s2", culture)); + Assert.Equal("0.123 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s3", culture)); + Assert.Equal("0.1235 m²/m", new AreaPerLength(0.123456, AreaPerLengthUnit.SquareMeterPerMeter).ToString("s4", culture)); + } + + [Theory] + [InlineData(null)] + [InlineData("en-US")] + public void ToString_NullFormat_DefaultsToGeneralFormat(string cultureName) + { + var quantity = AreaPerLength.FromSquareMetersPerMeter(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 = AreaPerLength.FromSquareMetersPerMeter(1.0); + Assert.Equal(quantity.ToString(format, CultureInfo.CurrentCulture), quantity.ToString(format, null)); + } + + [Fact] + public void GetHashCode_Equals() + { + var quantity = AreaPerLength.FromSquareMetersPerMeter(1.0); + Assert.Equal(Comparison.GetHashCode(quantity.Unit, quantity.Value), quantity.GetHashCode()); + } + + [Theory] + [InlineData(1.0)] + [InlineData(-1.0)] + public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value) + { + var quantity = AreaPerLength.FromSquareMetersPerMeter(value); + Assert.Equal(AreaPerLength.FromSquareMetersPerMeter(-value), -quantity); + } + } +} diff --git a/UnitsNet/GeneratedCode/Quantities/AreaPerLength.g.cs b/UnitsNet/GeneratedCode/Quantities/AreaPerLength.g.cs new file mode 100644 index 0000000000..41f43b00bc --- /dev/null +++ b/UnitsNet/GeneratedCode/Quantities/AreaPerLength.g.cs @@ -0,0 +1,830 @@ +//------------------------------------------------------------------------------ +// +// 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.Globalization; +using System.Resources; +using System.Runtime.Serialization; +#if NET +using System.Numerics; +#endif + +#nullable enable + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// + /// The magnitude of area per unit length, typically used in structural engineering to specify distributed reinforcement. + /// + [DataContract] + [DebuggerTypeProxy(typeof(QuantityDisplay))] + public readonly partial struct AreaPerLength : + IArithmeticQuantity, +#if NET7_0_OR_GREATER + IDivisionOperators, + 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 AreaPerLengthUnit? _unit; + + /// + /// Provides detailed information about the quantity, including its name, base unit, unit mappings, base dimensions, and conversion functions. + /// + public sealed class AreaPerLengthInfo: QuantityInfo + { + /// + public AreaPerLengthInfo(string name, AreaPerLengthUnit baseUnit, IEnumerable> unitMappings, AreaPerLength zero, BaseDimensions baseDimensions, + QuantityFromDelegate fromDelegate, ResourceManager? unitAbbreviations) + : base(name, baseUnit, unitMappings, zero, baseDimensions, fromDelegate, unitAbbreviations) + { + } + + /// + public AreaPerLengthInfo(string name, AreaPerLengthUnit baseUnit, IEnumerable> unitMappings, AreaPerLength zero, BaseDimensions baseDimensions) + : this(name, baseUnit, unitMappings, zero, baseDimensions, AreaPerLength.From, new ResourceManager("UnitsNet.GeneratedCode.Resources.AreaPerLength", typeof(AreaPerLength).Assembly)) + { + } + + /// + /// Creates a new instance of the class with the default settings for the AreaPerLength quantity. + /// + /// A new instance of the class with the default settings. + public static AreaPerLengthInfo CreateDefault() + { + return new AreaPerLengthInfo(nameof(AreaPerLength), DefaultBaseUnit, GetDefaultMappings(), new AreaPerLength(0, DefaultBaseUnit), DefaultBaseDimensions); + } + + /// + /// Creates a new instance of the class with the default settings for the AreaPerLength quantity and a callback for customizing the default unit mappings. + /// + /// + /// A callback function for customizing the default unit mappings. + /// + /// + /// A new instance of the class with the default settings. + /// + public static AreaPerLengthInfo CreateDefault(Func>, IEnumerable>> customizeUnits) + { + return new AreaPerLengthInfo(nameof(AreaPerLength), DefaultBaseUnit, customizeUnits(GetDefaultMappings()), new AreaPerLength(0, DefaultBaseUnit), DefaultBaseDimensions); + } + + /// + /// The for is [L]. + /// + public static BaseDimensions DefaultBaseDimensions { get; } = new BaseDimensions(1, 0, 0, 0, 0, 0, 0); + + /// + /// The default base unit of AreaPerLength is SquareMeterPerMeter. All conversions, as defined in the , go via this value. + /// + public static AreaPerLengthUnit DefaultBaseUnit { get; } = AreaPerLengthUnit.SquareMeterPerMeter; + + /// + /// Retrieves the default mappings for . + /// + /// An of representing the default unit mappings for AreaPerLength. + public static IEnumerable> GetDefaultMappings() + { + yield return new (AreaPerLengthUnit.SquareCentimeterPerMeter, "SquareCentimeterPerMeter", "SquareCentimetersPerMeter", BaseUnits.Undefined); + yield return new (AreaPerLengthUnit.SquareFootPerFoot, "SquareFootPerFoot", "SquareFeetPerFoot", BaseUnits.Undefined); + yield return new (AreaPerLengthUnit.SquareInchPerFoot, "SquareInchPerFoot", "SquareInchesPerFoot", BaseUnits.Undefined); + yield return new (AreaPerLengthUnit.SquareInchPerInch, "SquareInchPerInch", "SquareInchesPerInch", BaseUnits.Undefined); + yield return new (AreaPerLengthUnit.SquareMeterPerMeter, "SquareMeterPerMeter", "SquareMetersPerMeter", new BaseUnits(length: LengthUnit.Meter)); + yield return new (AreaPerLengthUnit.SquareMillimeterPerMeter, "SquareMillimeterPerMeter", "SquareMillimetersPerMeter", BaseUnits.Undefined); + } + } + + static AreaPerLength() + { + Info = AreaPerLengthInfo.CreateDefault(); + 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 AreaPerLength(double value, AreaPerLengthUnit 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 AreaPerLength(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 => Info.BaseDimensions; + + /// + /// The base unit of AreaPerLength, which is SquareMeterPerMeter. All conversions go via this value. + /// + public static AreaPerLengthUnit BaseUnit => Info.BaseUnitInfo.Value; + + /// + /// All units of measurement for the AreaPerLength quantity. + /// + public static IReadOnlyCollection Units => Info.Units; + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit SquareMeterPerMeter. + /// + public static AreaPerLength Zero => Info.Zero; + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + /// + public AreaPerLengthUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + /// + public QuantityInfo QuantityInfo => Info; + + #region Explicit implementations + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + UnitKey IQuantity.UnitKey => UnitKey.ForUnit(Unit); + +#if NETSTANDARD2_0 + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + IQuantityInstanceInfo IQuantityOfType.QuantityInfo => Info; + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + QuantityInfo IQuantity.QuantityInfo => Info; + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + QuantityInfo IQuantity.QuantityInfo => Info; + + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + Enum IQuantity.Unit => Unit; +#endif + + #endregion + + #endregion + + #region Conversion Properties + + /// + /// Gets a value of this quantity converted into + /// + public double SquareCentimetersPerMeter => As(AreaPerLengthUnit.SquareCentimeterPerMeter); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareFeetPerFoot => As(AreaPerLengthUnit.SquareFootPerFoot); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareInchesPerFoot => As(AreaPerLengthUnit.SquareInchPerFoot); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareInchesPerInch => As(AreaPerLengthUnit.SquareInchPerInch); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareMetersPerMeter => As(AreaPerLengthUnit.SquareMeterPerMeter); + + /// + /// Gets a value of this quantity converted into + /// + public double SquareMillimetersPerMeter => As(AreaPerLengthUnit.SquareMillimeterPerMeter); + + #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: AreaPerLengthUnit -> BaseUnit + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareCentimeterPerMeter, AreaPerLengthUnit.SquareMeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareMeterPerMeter)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareFootPerFoot, AreaPerLengthUnit.SquareMeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareMeterPerMeter)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareInchPerFoot, AreaPerLengthUnit.SquareMeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareMeterPerMeter)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareInchPerInch, AreaPerLengthUnit.SquareMeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareMeterPerMeter)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMillimeterPerMeter, AreaPerLengthUnit.SquareMeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareMeterPerMeter)); + + // Register in unit converter: BaseUnit <-> BaseUnit + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareMeterPerMeter, quantity => quantity); + + // Register in unit converter: BaseUnit -> AreaPerLengthUnit + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareCentimeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareCentimeterPerMeter)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareFootPerFoot, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareFootPerFoot)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareInchPerFoot, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareInchPerFoot)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareInchPerInch, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareInchPerInch)); + unitConverter.SetConversionFunction(AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareMillimeterPerMeter, quantity => quantity.ToUnit(AreaPerLengthUnit.SquareMillimeterPerMeter)); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(AreaPerLengthUnit 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(AreaPerLengthUnit unit, IFormatProvider? provider) + { + return UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareCentimetersPerMeter(double value) + { + return new AreaPerLength(value, AreaPerLengthUnit.SquareCentimeterPerMeter); + } + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareFeetPerFoot(double value) + { + return new AreaPerLength(value, AreaPerLengthUnit.SquareFootPerFoot); + } + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareInchesPerFoot(double value) + { + return new AreaPerLength(value, AreaPerLengthUnit.SquareInchPerFoot); + } + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareInchesPerInch(double value) + { + return new AreaPerLength(value, AreaPerLengthUnit.SquareInchPerInch); + } + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareMetersPerMeter(double value) + { + return new AreaPerLength(value, AreaPerLengthUnit.SquareMeterPerMeter); + } + + /// + /// Creates a from . + /// + public static AreaPerLength FromSquareMillimetersPerMeter(double value) + { + return new AreaPerLength(value, AreaPerLengthUnit.SquareMillimeterPerMeter); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// AreaPerLength unit value. + public static AreaPerLength From(double value, AreaPerLengthUnit fromUnit) + { + return new AreaPerLength(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 AreaPerLength 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 AreaPerLength 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 AreaPerLength 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 AreaPerLength 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 AreaPerLengthUnit 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 AreaPerLengthUnit ParseUnit(string str, IFormatProvider? provider) + { + return UnitParser.Default.Parse(str, Info.UnitInfos, provider).Value; + } + + /// + public static bool TryParseUnit([NotNullWhen(true)]string? str, out AreaPerLengthUnit 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 AreaPerLengthUnit unit) + { + return UnitParser.Default.TryParse(str, Info, provider, out unit); + } + + #endregion + + #region Arithmetic Operators + + /// Negate the value. + public static AreaPerLength operator -(AreaPerLength right) + { + return new AreaPerLength(-right.Value, right.Unit); + } + + /// Get from adding two . + public static AreaPerLength operator +(AreaPerLength left, AreaPerLength right) + { + return new AreaPerLength(left.Value + right.ToUnit(left.Unit).Value, left.Unit); + } + + /// Get from subtracting two . + public static AreaPerLength operator -(AreaPerLength left, AreaPerLength right) + { + return new AreaPerLength(left.Value - right.ToUnit(left.Unit).Value, left.Unit); + } + + /// Get from multiplying value and . + public static AreaPerLength operator *(double left, AreaPerLength right) + { + return new AreaPerLength(left * right.Value, right.Unit); + } + + /// Get from multiplying value and . + public static AreaPerLength operator *(AreaPerLength left, double right) + { + return new AreaPerLength(left.Value * right, left.Unit); + } + + /// Get from dividing by value. + public static AreaPerLength operator /(AreaPerLength left, double right) + { + return new AreaPerLength(left.Value / right, left.Unit); + } + + /// Get ratio value from dividing by . + public static double operator /(AreaPerLength left, AreaPerLength right) + { + return left.SquareMetersPerMeter / right.SquareMetersPerMeter; + } + + #endregion + + #region Equality / IComparable + + /// Returns true if less or equal to. + public static bool operator <=(AreaPerLength left, AreaPerLength right) + { + return left.Value <= right.ToUnit(left.Unit).Value; + } + + /// Returns true if greater than or equal to. + public static bool operator >=(AreaPerLength left, AreaPerLength right) + { + return left.Value >= right.ToUnit(left.Unit).Value; + } + + /// Returns true if less than. + public static bool operator <(AreaPerLength left, AreaPerLength right) + { + return left.Value < right.ToUnit(left.Unit).Value; + } + + /// Returns true if greater than. + public static bool operator >(AreaPerLength left, AreaPerLength 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(AreaPerLength other, AreaPerLength 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 ==(AreaPerLength left, AreaPerLength 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(AreaPerLength other, AreaPerLength 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 !=(AreaPerLength left, AreaPerLength right) + { + return !(left == right); + } + + /// + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("Use Equals(AreaPerLength other, AreaPerLength 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 AreaPerLength otherQuantity)) + return false; + + return Equals(otherQuantity); + } + + /// + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("Use Equals(AreaPerLength other, AreaPerLength 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(AreaPerLength other) + { + return new { Value, Unit }.Equals(new { other.Value, other.Unit }); + } + + #pragma warning restore CS0809 + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current AreaPerLength. + public override int GetHashCode() + { + return Comparison.GetHashCode(Unit, Value); + } + + /// 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 AreaPerLength otherQuantity)) throw new ArgumentException("Expected type AreaPerLength.", 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(AreaPerLength other) + { + return _value.CompareTo(other.ToUnit(this.Unit).Value); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(AreaPerLengthUnit unit) + { + if (Unit == unit) + return Value; + + return ToUnit(unit).Value; + } + + /// + public double As(UnitKey unitKey) + { + return As(unitKey.ToUnit()); + } + + /// + /// Converts this AreaPerLength to another AreaPerLength with the unit representation . + /// + /// The unit to convert to. + /// A AreaPerLength with the specified unit. + public AreaPerLength ToUnit(AreaPerLengthUnit 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 AreaPerLength with the specified unit. + public AreaPerLength ToUnit(AreaPerLengthUnit 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(AreaPerLength), Unit, typeof(AreaPerLength), unit), out var conversionFunction)) + { + // See if the unit converter has an extensibility conversion registered. + return (AreaPerLength)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 UnitNotFoundException($"Can't 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(AreaPerLengthUnit unit, [NotNullWhen(true)] out AreaPerLength? converted) + { + if (Unit == unit) + { + converted = this; + return true; + } + + AreaPerLength? convertedOrNull = (Unit, unit) switch + { + // AreaPerLengthUnit -> BaseUnit + (AreaPerLengthUnit.SquareCentimeterPerMeter, AreaPerLengthUnit.SquareMeterPerMeter) => new AreaPerLength(_value * 1e-4, AreaPerLengthUnit.SquareMeterPerMeter), + (AreaPerLengthUnit.SquareFootPerFoot, AreaPerLengthUnit.SquareMeterPerMeter) => new AreaPerLength(_value * 0.09290304 / 0.3048, AreaPerLengthUnit.SquareMeterPerMeter), + (AreaPerLengthUnit.SquareInchPerFoot, AreaPerLengthUnit.SquareMeterPerMeter) => new AreaPerLength(_value * 0.00064516 / 0.3048, AreaPerLengthUnit.SquareMeterPerMeter), + (AreaPerLengthUnit.SquareInchPerInch, AreaPerLengthUnit.SquareMeterPerMeter) => new AreaPerLength(_value * 0.00064516 / 0.0254, AreaPerLengthUnit.SquareMeterPerMeter), + (AreaPerLengthUnit.SquareMillimeterPerMeter, AreaPerLengthUnit.SquareMeterPerMeter) => new AreaPerLength(_value * 1e-6, AreaPerLengthUnit.SquareMeterPerMeter), + + // BaseUnit -> AreaPerLengthUnit + (AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareCentimeterPerMeter) => new AreaPerLength(_value / 1e-4, AreaPerLengthUnit.SquareCentimeterPerMeter), + (AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareFootPerFoot) => new AreaPerLength(_value * 0.3048 / 0.09290304, AreaPerLengthUnit.SquareFootPerFoot), + (AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareInchPerFoot) => new AreaPerLength(_value * 0.3048 / 0.00064516, AreaPerLengthUnit.SquareInchPerFoot), + (AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareInchPerInch) => new AreaPerLength(_value * 0.0254 / 0.00064516, AreaPerLengthUnit.SquareInchPerInch), + (AreaPerLengthUnit.SquareMeterPerMeter, AreaPerLengthUnit.SquareMillimeterPerMeter) => new AreaPerLength(_value / 1e-6, AreaPerLengthUnit.SquareMillimeterPerMeter), + + _ => null + }; + + if (convertedOrNull is null) + { + converted = default; + return false; + } + + converted = convertedOrNull.Value; + return true; + } + + #region Explicit implementations + + double IQuantity.As(Enum unit) + { + if (unit is not AreaPerLengthUnit typedUnit) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(AreaPerLengthUnit)} is supported.", nameof(unit)); + + return As(typedUnit); + } + + /// + IQuantity IQuantity.ToUnit(Enum unit) + { + if (!(unit is AreaPerLengthUnit typedUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(AreaPerLengthUnit)} is supported.", nameof(unit)); + + return ToUnit(typedUnit, DefaultConversionFunctions); + } + + /// + IQuantity IQuantity.ToUnit(AreaPerLengthUnit unit) => ToUnit(unit); + + #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 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 c7a7c3af9c..2a5a068039 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -44,6 +44,7 @@ internal static class DefaultProvider Area.Info, AreaDensity.Info, AreaMomentOfInertia.Info, + AreaPerLength.Info, BitRate.Info, BrakeSpecificFuelConsumption.Info, CoefficientOfThermalExpansion.Info, @@ -176,6 +177,7 @@ internal static void RegisterUnitConversions(UnitConverter unitConverter) Area.RegisterDefaultConversions(unitConverter); AreaDensity.RegisterDefaultConversions(unitConverter); AreaMomentOfInertia.RegisterDefaultConversions(unitConverter); + AreaPerLength.RegisterDefaultConversions(unitConverter); BitRate.RegisterDefaultConversions(unitConverter); BrakeSpecificFuelConsumption.RegisterDefaultConversions(unitConverter); CoefficientOfThermalExpansion.RegisterDefaultConversions(unitConverter); diff --git a/UnitsNet/GeneratedCode/Resources/AreaPerLength.restext b/UnitsNet/GeneratedCode/Resources/AreaPerLength.restext new file mode 100644 index 0000000000..de4a127b1f --- /dev/null +++ b/UnitsNet/GeneratedCode/Resources/AreaPerLength.restext @@ -0,0 +1,6 @@ +SquareCentimetersPerMeter=cm²/m +SquareFeetPerFoot=ft²/ft +SquareInchesPerFoot=in²/ft +SquareInchesPerInch=in²/in +SquareMetersPerMeter=m²/m +SquareMillimetersPerMeter=mm²/m diff --git a/UnitsNet/GeneratedCode/Units/AreaPerLengthUnit.g.cs b/UnitsNet/GeneratedCode/Units/AreaPerLengthUnit.g.cs new file mode 100644 index 0000000000..7dad5d4754 --- /dev/null +++ b/UnitsNet/GeneratedCode/Units/AreaPerLengthUnit.g.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. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum AreaPerLengthUnit + { + SquareCentimeterPerMeter = 10, + SquareFootPerFoot = 4, + SquareInchPerFoot = 3, + SquareInchPerInch = 7, + SquareMeterPerMeter = 1, + SquareMillimeterPerMeter = 5, + } + + #pragma warning restore 1591 +}