diff --git a/Common/UnitDefinitions/RadiationExposure.json b/Common/UnitDefinitions/RadiationExposure.json new file mode 100644 index 0000000000..6684939d72 --- /dev/null +++ b/Common/UnitDefinitions/RadiationExposure.json @@ -0,0 +1,49 @@ +{ + "Name": "RadiationExposure", + "BaseUnit": "CoulombPerKilogram", + "XmlDocSummary": "Radiation exposure is a measure of the ionization of air due to ionizing radiation from photons.", + "XmlDocsRemarks": "https://en.wikipedia.org/wiki/Radiation_exposure", + "BaseDimensions": { + "M": -1, + "T": 1, + "I": 1 + }, + "Units": [ + { + "SingularName": "CoulombPerKilogram", + "PluralName": "CoulombsPerKilogram", + "FromUnitToBaseFunc": "{x}", + "FromBaseToUnitFunc": "{x}", + "BaseUnits": { + "M": "Kilogram", + "T": "Second", + "I": "Ampere" + }, + "Prefixes": [ "Pico", "Nano", "Micro", "Milli" ], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "C/kg" ] + } + ] + }, + { + "SingularName": "Roentgen", + "PluralName": "Roentgens", + "FromUnitToBaseFunc": "{x} * 2.58e-4", + "FromBaseToUnitFunc": "{x} / 2.58e-4", + "Prefixes": [ "Micro", "Milli" ], + "BaseUnits": { + "M": "Kilogram", + "T": "Second", + "I": "Ampere" + }, + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "R" ] + } + ] + } + ] +} \ No newline at end of file diff --git a/Common/UnitEnumValues.g.json b/Common/UnitEnumValues.g.json index 5bc81f4e52..4d909db5bb 100644 --- a/Common/UnitEnumValues.g.json +++ b/Common/UnitEnumValues.g.json @@ -1825,5 +1825,15 @@ "Picobecquerel": 36, "Picocurie": 35, "Picorutherford": 25 + }, + "RadiationExposure": { + "CoulombPerKilogram": 10, + "MicrocoulombPerKilogram": 3, + "MillicoulombPerKilogram": 2, + "NanocoulombPerKilogram": 6, + "PicocoulombPerKilogram": 5, + "Roentgen": 4, + "Microroentgen": 12, + "Milliroentgen": 13 } } diff --git a/UnitsNet.NanoFramework/GeneratedCode/Quantities/RadiationExposure.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Quantities/RadiationExposure.g.cs new file mode 100644 index 0000000000..73ea4a7735 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/Quantities/RadiationExposure.g.cs @@ -0,0 +1,250 @@ +//------------------------------------------------------------------------------ +// +// 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 +{ + /// + /// + /// Radiation exposure is a measure of the ionization of air due to ionizing radiation from photons. + /// + public struct RadiationExposure + { + /// + /// The numeric value this quantity was constructed with. + /// + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + private readonly RadiationExposureUnit _unit; + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + /// + public RadiationExposureUnit 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. + /// If value is NaN or Infinity. + public RadiationExposure(double value, RadiationExposureUnit unit) + { + _value = value; + _unit = unit; + } + + /// + /// The base unit of Duration, which is Second. All conversions go via this value. + /// + public static RadiationExposureUnit BaseUnit { get; } = RadiationExposureUnit.CoulombPerKilogram; + + /// + /// Represents the largest possible value of Duration + /// + public static RadiationExposure MaxValue { get; } = new RadiationExposure(double.MaxValue, BaseUnit); + + /// + /// Represents the smallest possible value of Duration + /// + public static RadiationExposure MinValue { get; } = new RadiationExposure(double.MinValue, BaseUnit); + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit Second. + /// + public static RadiationExposure Zero { get; } = new RadiationExposure(0, BaseUnit); + #region Conversion Properties + + /// + /// Gets a value of this quantity converted into + /// + public double CoulombsPerKilogram => As(RadiationExposureUnit.CoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double MicrocoulombsPerKilogram => As(RadiationExposureUnit.MicrocoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double Microroentgens => As(RadiationExposureUnit.Microroentgen); + + /// + /// Gets a value of this quantity converted into + /// + public double MillicoulombsPerKilogram => As(RadiationExposureUnit.MillicoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double Milliroentgens => As(RadiationExposureUnit.Milliroentgen); + + /// + /// Gets a value of this quantity converted into + /// + public double NanocoulombsPerKilogram => As(RadiationExposureUnit.NanocoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double PicocoulombsPerKilogram => As(RadiationExposureUnit.PicocoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double Roentgens => As(RadiationExposureUnit.Roentgen); + + #endregion + + #region Static Factory Methods + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromCoulombsPerKilogram(double coulombsperkilogram) => new RadiationExposure(coulombsperkilogram, RadiationExposureUnit.CoulombPerKilogram); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMicrocoulombsPerKilogram(double microcoulombsperkilogram) => new RadiationExposure(microcoulombsperkilogram, RadiationExposureUnit.MicrocoulombPerKilogram); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMicroroentgens(double microroentgens) => new RadiationExposure(microroentgens, RadiationExposureUnit.Microroentgen); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMillicoulombsPerKilogram(double millicoulombsperkilogram) => new RadiationExposure(millicoulombsperkilogram, RadiationExposureUnit.MillicoulombPerKilogram); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMilliroentgens(double milliroentgens) => new RadiationExposure(milliroentgens, RadiationExposureUnit.Milliroentgen); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromNanocoulombsPerKilogram(double nanocoulombsperkilogram) => new RadiationExposure(nanocoulombsperkilogram, RadiationExposureUnit.NanocoulombPerKilogram); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromPicocoulombsPerKilogram(double picocoulombsperkilogram) => new RadiationExposure(picocoulombsperkilogram, RadiationExposureUnit.PicocoulombPerKilogram); + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromRoentgens(double roentgens) => new RadiationExposure(roentgens, RadiationExposureUnit.Roentgen); + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// RadiationExposure unit value. + public static RadiationExposure From(double value, RadiationExposureUnit fromUnit) + { + return new RadiationExposure(value, fromUnit); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(RadiationExposureUnit unit) => GetValueAs(unit); + + /// + /// Converts this Duration to another Duration with the unit representation . + /// + /// A Duration with the specified unit. + public RadiationExposure ToUnit(RadiationExposureUnit unit) + { + var convertedValue = GetValueAs(unit); + return new RadiationExposure(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 + { + RadiationExposureUnit.CoulombPerKilogram => _value, + RadiationExposureUnit.MicrocoulombPerKilogram => (_value) * 1e-6d, + RadiationExposureUnit.Microroentgen => (_value * 2.58e-4) * 1e-6d, + RadiationExposureUnit.MillicoulombPerKilogram => (_value) * 1e-3d, + RadiationExposureUnit.Milliroentgen => (_value * 2.58e-4) * 1e-3d, + RadiationExposureUnit.NanocoulombPerKilogram => (_value) * 1e-9d, + RadiationExposureUnit.PicocoulombPerKilogram => (_value) * 1e-12d, + RadiationExposureUnit.Roentgen => _value * 2.58e-4, + _ => throw new NotImplementedException($"Can not convert {Unit} to base units.") + }; + } + + private double GetValueAs(RadiationExposureUnit unit) + { + if (Unit == unit) + return _value; + + var baseUnitValue = GetValueInBaseUnit(); + + return unit switch + { + RadiationExposureUnit.CoulombPerKilogram => baseUnitValue, + RadiationExposureUnit.MicrocoulombPerKilogram => (baseUnitValue) / 1e-6d, + RadiationExposureUnit.Microroentgen => (baseUnitValue / 2.58e-4) / 1e-6d, + RadiationExposureUnit.MillicoulombPerKilogram => (baseUnitValue) / 1e-3d, + RadiationExposureUnit.Milliroentgen => (baseUnitValue / 2.58e-4) / 1e-3d, + RadiationExposureUnit.NanocoulombPerKilogram => (baseUnitValue) / 1e-9d, + RadiationExposureUnit.PicocoulombPerKilogram => (baseUnitValue) / 1e-12d, + RadiationExposureUnit.Roentgen => baseUnitValue / 2.58e-4, + _ => throw new NotImplementedException($"Can not convert {Unit} to {unit}.") + }; + } + + #endregion + } +} + diff --git a/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/RadiationExposure.nfproj b/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/RadiationExposure.nfproj new file mode 100644 index 0000000000..f2b96d78df --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/RadiationExposure.nfproj @@ -0,0 +1,42 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + {69b383c7-828a-1857-cbe2-f11b9ba77320} + Library + Properties + 512 + UnitsNet + UnitsNet.RadiationExposure + v1.0 + bin\$(Configuration)\$(AssemblyName).xml + + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll + True + True + + + + + + + + + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/UnitsNet.NanoFramework.RadiationExposure.nuspec b/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/UnitsNet.NanoFramework.RadiationExposure.nuspec new file mode 100644 index 0000000000..470443e550 --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/UnitsNet.NanoFramework.RadiationExposure.nuspec @@ -0,0 +1,26 @@ + + + + UnitsNet.nanoFramework.RadiationExposure + 5.41.0 + Units.NET RadiationExposure - nanoFramework + Andreas Gullberg Larsen,nanoframework + UnitsNet + MIT + https://github.com/angularsen/UnitsNet + false + Adds RadiationExposure 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 radiationexposure unit units quantity quantities measurement si metric imperial abbreviation abbreviations convert conversion parse immutable + + + + + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/packages.config b/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/packages.config new file mode 100644 index 0000000000..313a8dccdf --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/RadiationExposure/packages.config @@ -0,0 +1,4 @@ + + + + diff --git a/UnitsNet.NanoFramework/GeneratedCode/Units/RadiationExposureUnit.g.cs b/UnitsNet.NanoFramework/GeneratedCode/Units/RadiationExposureUnit.g.cs new file mode 100644 index 0000000000..8e50cdd24f --- /dev/null +++ b/UnitsNet.NanoFramework/GeneratedCode/Units/RadiationExposureUnit.g.cs @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// 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 RadiationExposureUnit + { + CoulombPerKilogram = 10, + MicrocoulombPerKilogram = 3, + Microroentgen = 12, + MillicoulombPerKilogram = 2, + Milliroentgen = 13, + NanocoulombPerKilogram = 6, + PicocoulombPerKilogram = 5, + Roentgen = 4, + } + + #pragma warning restore 1591 +} diff --git a/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln b/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln index ce672dd107..4ae1dab440 100644 --- a/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln +++ b/UnitsNet.NanoFramework/GeneratedCode/UnitsNet.nanoFramework.sln @@ -170,6 +170,8 @@ Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "Pressure", "Pressure\Pressu EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "PressureChangeRate", "PressureChangeRate\PressureChangeRate.nfproj", "{c9e4cfa9-c5d3-5e9d-243c-106ba4b6a447}" EndProject +Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "RadiationExposure", "RadiationExposure\RadiationExposure.nfproj", "{69b383c7-828a-1857-cbe2-f11b9ba77320}" +EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "Radioactivity", "Radioactivity\Radioactivity.nfproj", "{abf4b42f-e999-2a78-403f-fb09524f186a}" EndProject Project("{d608a2b1-6ead-4383-a205-ad1ce69d9ef7}") = "Ratio", "Ratio\Ratio.nfproj", "{b0be4813-d93e-7b06-d422-23ca28adf22a}" @@ -756,6 +758,12 @@ Global {c9e4cfa9-c5d3-5e9d-243c-106ba4b6a447}.Release|Any CPU.ActiveCfg = Release|Any CPU {c9e4cfa9-c5d3-5e9d-243c-106ba4b6a447}.Release|Any CPU.Build.0 = Release|Any CPU {c9e4cfa9-c5d3-5e9d-243c-106ba4b6a447}.Release|Any CPU.Deploy.0 = Release|Any CPU +{69b383c7-828a-1857-cbe2-f11b9ba77320}.Debug|Any CPU.ActiveCfg = Debug|Any CPU +{69b383c7-828a-1857-cbe2-f11b9ba77320}.Debug|Any CPU.Build.0 = Debug|Any CPU +{69b383c7-828a-1857-cbe2-f11b9ba77320}.Debug|Any CPU.Deploy.0 = Debug|Any CPU +{69b383c7-828a-1857-cbe2-f11b9ba77320}.Release|Any CPU.ActiveCfg = Release|Any CPU +{69b383c7-828a-1857-cbe2-f11b9ba77320}.Release|Any CPU.Build.0 = Release|Any CPU +{69b383c7-828a-1857-cbe2-f11b9ba77320}.Release|Any CPU.Deploy.0 = Release|Any CPU {abf4b42f-e999-2a78-403f-fb09524f186a}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {abf4b42f-e999-2a78-403f-fb09524f186a}.Debug|Any CPU.Build.0 = Debug|Any CPU {abf4b42f-e999-2a78-403f-fb09524f186a}.Debug|Any CPU.Deploy.0 = Debug|Any CPU diff --git a/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToRadiationExposureExtensionsTest.g.cs b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToRadiationExposureExtensionsTest.g.cs new file mode 100644 index 0000000000..e8906260d3 --- /dev/null +++ b/UnitsNet.NumberExtensions.Tests/GeneratedCode/NumberToRadiationExposureExtensionsTest.g.cs @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// 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.NumberToRadiationExposure; +using Xunit; + +namespace UnitsNet.Tests +{ + public class NumberToRadiationExposureExtensionsTests + { + [Fact] + public void NumberToCoulombsPerKilogramTest() => + Assert.Equal(RadiationExposure.FromCoulombsPerKilogram(2), 2.CoulombsPerKilogram()); + + [Fact] + public void NumberToMicrocoulombsPerKilogramTest() => + Assert.Equal(RadiationExposure.FromMicrocoulombsPerKilogram(2), 2.MicrocoulombsPerKilogram()); + + [Fact] + public void NumberToMicroroentgensTest() => + Assert.Equal(RadiationExposure.FromMicroroentgens(2), 2.Microroentgens()); + + [Fact] + public void NumberToMillicoulombsPerKilogramTest() => + Assert.Equal(RadiationExposure.FromMillicoulombsPerKilogram(2), 2.MillicoulombsPerKilogram()); + + [Fact] + public void NumberToMilliroentgensTest() => + Assert.Equal(RadiationExposure.FromMilliroentgens(2), 2.Milliroentgens()); + + [Fact] + public void NumberToNanocoulombsPerKilogramTest() => + Assert.Equal(RadiationExposure.FromNanocoulombsPerKilogram(2), 2.NanocoulombsPerKilogram()); + + [Fact] + public void NumberToPicocoulombsPerKilogramTest() => + Assert.Equal(RadiationExposure.FromPicocoulombsPerKilogram(2), 2.PicocoulombsPerKilogram()); + + [Fact] + public void NumberToRoentgensTest() => + Assert.Equal(RadiationExposure.FromRoentgens(2), 2.Roentgens()); + + } +} diff --git a/UnitsNet.NumberExtensions/GeneratedCode/NumberToRadiationExposureExtensions.g.cs b/UnitsNet.NumberExtensions/GeneratedCode/NumberToRadiationExposureExtensions.g.cs new file mode 100644 index 0000000000..d24f2b7f0a --- /dev/null +++ b/UnitsNet.NumberExtensions/GeneratedCode/NumberToRadiationExposureExtensions.g.cs @@ -0,0 +1,100 @@ +//------------------------------------------------------------------------------ +// +// 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.NumberToRadiationExposure +{ + /// + /// A number to RadiationExposure Extensions + /// + public static class NumberToRadiationExposureExtensions + { + /// + public static RadiationExposure CoulombsPerKilogram(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromCoulombsPerKilogram(Convert.ToDouble(value)); + + /// + public static RadiationExposure MicrocoulombsPerKilogram(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromMicrocoulombsPerKilogram(Convert.ToDouble(value)); + + /// + public static RadiationExposure Microroentgens(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromMicroroentgens(Convert.ToDouble(value)); + + /// + public static RadiationExposure MillicoulombsPerKilogram(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromMillicoulombsPerKilogram(Convert.ToDouble(value)); + + /// + public static RadiationExposure Milliroentgens(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromMilliroentgens(Convert.ToDouble(value)); + + /// + public static RadiationExposure NanocoulombsPerKilogram(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromNanocoulombsPerKilogram(Convert.ToDouble(value)); + + /// + public static RadiationExposure PicocoulombsPerKilogram(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromPicocoulombsPerKilogram(Convert.ToDouble(value)); + + /// + public static RadiationExposure Roentgens(this T value) + where T : notnull +#if NET7_0_OR_GREATER + , INumber +#endif + => RadiationExposure.FromRoentgens(Convert.ToDouble(value)); + + } +} diff --git a/UnitsNet.Tests/CustomCode/RadiationExposureTests.cs b/UnitsNet.Tests/CustomCode/RadiationExposureTests.cs new file mode 100644 index 0000000000..19b52baf95 --- /dev/null +++ b/UnitsNet.Tests/CustomCode/RadiationExposureTests.cs @@ -0,0 +1,20 @@ +// 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 RadiationExposureTests : RadiationExposureTestsBase + { + protected override bool SupportsSIUnitSystem => true; + protected override double CoulombsPerKilogramInOneCoulombPerKilogram => 1; + protected override double MillicoulombsPerKilogramInOneCoulombPerKilogram => 1e+3; + protected override double MicrocoulombsPerKilogramInOneCoulombPerKilogram => 1e+6; + protected override double NanocoulombsPerKilogramInOneCoulombPerKilogram => 1e+9; + protected override double PicocoulombsPerKilogramInOneCoulombPerKilogram => 1e+12; + protected override double RoentgensInOneCoulombPerKilogram => 3875.9689922; + protected override double MilliroentgensInOneCoulombPerKilogram => 3875.9689922e+3; + protected override double MicroroentgensInOneCoulombPerKilogram => 3875.9689922e+6; + } +} diff --git a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs index 9e18cd461c..adfc1e4d48 100644 --- a/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs +++ b/UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs @@ -118,6 +118,7 @@ void Assertion(int expectedValue, Enum expectedUnit, IQuantity quantity) Assertion(3, PowerRatioUnit.DecibelWatt, Quantity.From(3, PowerRatioUnit.DecibelWatt)); Assertion(3, PressureUnit.Torr, Quantity.From(3, PressureUnit.Torr)); Assertion(3, PressureChangeRateUnit.PoundForcePerSquareInchPerSecond, Quantity.From(3, PressureChangeRateUnit.PoundForcePerSquareInchPerSecond)); + Assertion(3, RadiationExposureUnit.Roentgen, Quantity.From(3, RadiationExposureUnit.Roentgen)); Assertion(3, RadioactivityUnit.Terarutherford, Quantity.From(3, RadioactivityUnit.Terarutherford)); Assertion(3, RatioUnit.Percent, Quantity.From(3, RatioUnit.Percent)); Assertion(3, RatioChangeRateUnit.PercentPerSecond, Quantity.From(3, RatioChangeRateUnit.PercentPerSecond)); @@ -247,6 +248,7 @@ public void QuantityInfo_IsSameAsStaticInfoProperty() Assertion(PowerRatio.Info, PowerRatio.Zero); Assertion(Pressure.Info, Pressure.Zero); Assertion(PressureChangeRate.Info, PressureChangeRate.Zero); + Assertion(RadiationExposure.Info, RadiationExposure.Zero); Assertion(Radioactivity.Info, Radioactivity.Zero); Assertion(Ratio.Info, Ratio.Zero); Assertion(RatioChangeRate.Info, RatioChangeRate.Zero); @@ -376,6 +378,7 @@ public void Dimensions_IsSameAsStaticBaseDimensions() Assertion(PowerRatio.BaseDimensions, PowerRatio.Zero); Assertion(Pressure.BaseDimensions, Pressure.Zero); Assertion(PressureChangeRate.BaseDimensions, PressureChangeRate.Zero); + Assertion(RadiationExposure.BaseDimensions, RadiationExposure.Zero); Assertion(Radioactivity.BaseDimensions, Radioactivity.Zero); Assertion(Ratio.BaseDimensions, Ratio.Zero); Assertion(RatioChangeRate.BaseDimensions, RatioChangeRate.Zero); diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/RadiationExposureTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/RadiationExposureTestsBase.g.cs new file mode 100644 index 0000000000..31f2bf9527 --- /dev/null +++ b/UnitsNet.Tests/GeneratedCode/TestsBase/RadiationExposureTestsBase.g.cs @@ -0,0 +1,890 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using UnitsNet.Tests.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 RadiationExposure. + /// +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class RadiationExposureTestsBase : QuantityTestsBase + { + protected abstract double CoulombsPerKilogramInOneCoulombPerKilogram { get; } + protected abstract double MicrocoulombsPerKilogramInOneCoulombPerKilogram { get; } + protected abstract double MicroroentgensInOneCoulombPerKilogram { get; } + protected abstract double MillicoulombsPerKilogramInOneCoulombPerKilogram { get; } + protected abstract double MilliroentgensInOneCoulombPerKilogram { get; } + protected abstract double NanocoulombsPerKilogramInOneCoulombPerKilogram { get; } + protected abstract double PicocoulombsPerKilogramInOneCoulombPerKilogram { get; } + protected abstract double RoentgensInOneCoulombPerKilogram { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double CoulombsPerKilogramTolerance { get { return 1e-5; } } + protected virtual double MicrocoulombsPerKilogramTolerance { get { return 1e-5; } } + protected virtual double MicroroentgensTolerance { get { return 1e-5; } } + protected virtual double MillicoulombsPerKilogramTolerance { get { return 1e-5; } } + protected virtual double MilliroentgensTolerance { get { return 1e-5; } } + protected virtual double NanocoulombsPerKilogramTolerance { get { return 1e-5; } } + protected virtual double PicocoulombsPerKilogramTolerance { get { return 1e-5; } } + protected virtual double RoentgensTolerance { get { return 1e-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(RadiationExposureUnit unit) + { + return unit switch + { + RadiationExposureUnit.CoulombPerKilogram => (CoulombsPerKilogramInOneCoulombPerKilogram, CoulombsPerKilogramTolerance), + RadiationExposureUnit.MicrocoulombPerKilogram => (MicrocoulombsPerKilogramInOneCoulombPerKilogram, MicrocoulombsPerKilogramTolerance), + RadiationExposureUnit.Microroentgen => (MicroroentgensInOneCoulombPerKilogram, MicroroentgensTolerance), + RadiationExposureUnit.MillicoulombPerKilogram => (MillicoulombsPerKilogramInOneCoulombPerKilogram, MillicoulombsPerKilogramTolerance), + RadiationExposureUnit.Milliroentgen => (MilliroentgensInOneCoulombPerKilogram, MilliroentgensTolerance), + RadiationExposureUnit.NanocoulombPerKilogram => (NanocoulombsPerKilogramInOneCoulombPerKilogram, NanocoulombsPerKilogramTolerance), + RadiationExposureUnit.PicocoulombPerKilogram => (PicocoulombsPerKilogramInOneCoulombPerKilogram, PicocoulombsPerKilogramTolerance), + RadiationExposureUnit.Roentgen => (RoentgensInOneCoulombPerKilogram, RoentgensTolerance), + _ => throw new NotSupportedException() + }; + } + + public static IEnumerable UnitTypes = new List + { + new object[] { RadiationExposureUnit.CoulombPerKilogram }, + new object[] { RadiationExposureUnit.MicrocoulombPerKilogram }, + new object[] { RadiationExposureUnit.Microroentgen }, + new object[] { RadiationExposureUnit.MillicoulombPerKilogram }, + new object[] { RadiationExposureUnit.Milliroentgen }, + new object[] { RadiationExposureUnit.NanocoulombPerKilogram }, + new object[] { RadiationExposureUnit.PicocoulombPerKilogram }, + new object[] { RadiationExposureUnit.Roentgen }, + }; + + [Fact] + public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit() + { + var quantity = new RadiationExposure(); + Assert.Equal(0, quantity.Value); + Assert.Equal(RadiationExposureUnit.CoulombPerKilogram, quantity.Unit); + } + + [Fact] + public void Ctor_WithInfinityValue_ThrowsArgumentException() + { + Assert.Throws(() => new RadiationExposure(double.PositiveInfinity, RadiationExposureUnit.CoulombPerKilogram)); + Assert.Throws(() => new RadiationExposure(double.NegativeInfinity, RadiationExposureUnit.CoulombPerKilogram)); + } + + [Fact] + public void Ctor_WithNaNValue_ThrowsArgumentException() + { + Assert.Throws(() => new RadiationExposure(double.NaN, RadiationExposureUnit.CoulombPerKilogram)); + } + + [Fact] + public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException() + { + Assert.Throws(() => new RadiationExposure(value: 1, unitSystem: null)); + } + + [Fact] + public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + Func TestCode = () => new RadiationExposure(value: 1, unitSystem: UnitSystem.SI); + if (SupportsSIUnitSystem) + { + var quantity = (RadiationExposure) TestCode(); + Assert.Equal(1, quantity.Value); + } + else + { + Assert.Throws(TestCode); + } + } + + [Fact] + public void RadiationExposure_QuantityInfo_ReturnsQuantityInfoDescribingQuantity() + { + var quantity = new RadiationExposure(1, RadiationExposureUnit.CoulombPerKilogram); + + QuantityInfo quantityInfo = quantity.QuantityInfo; + + Assert.Equal(RadiationExposure.Zero, quantityInfo.Zero); + Assert.Equal("RadiationExposure", quantityInfo.Name); + + var units = EnumUtils.GetEnumValues().OrderBy(x => x.ToString()).ToArray(); + var unitNames = units.Select(x => x.ToString()); + } + + [Fact] + public void CoulombPerKilogramToRadiationExposureUnits() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + AssertEx.EqualTolerance(CoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(MicrocoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.MicrocoulombsPerKilogram, MicrocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(MicroroentgensInOneCoulombPerKilogram, coulombperkilogram.Microroentgens, MicroroentgensTolerance); + AssertEx.EqualTolerance(MillicoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.MillicoulombsPerKilogram, MillicoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(MilliroentgensInOneCoulombPerKilogram, coulombperkilogram.Milliroentgens, MilliroentgensTolerance); + AssertEx.EqualTolerance(NanocoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.NanocoulombsPerKilogram, NanocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(PicocoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.PicocoulombsPerKilogram, PicocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(RoentgensInOneCoulombPerKilogram, coulombperkilogram.Roentgens, RoentgensTolerance); + } + + [Fact] + public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit() + { + var quantity00 = RadiationExposure.From(1, RadiationExposureUnit.CoulombPerKilogram); + AssertEx.EqualTolerance(1, quantity00.CoulombsPerKilogram, CoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.CoulombPerKilogram, quantity00.Unit); + + var quantity01 = RadiationExposure.From(1, RadiationExposureUnit.MicrocoulombPerKilogram); + AssertEx.EqualTolerance(1, quantity01.MicrocoulombsPerKilogram, MicrocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.MicrocoulombPerKilogram, quantity01.Unit); + + var quantity02 = RadiationExposure.From(1, RadiationExposureUnit.Microroentgen); + AssertEx.EqualTolerance(1, quantity02.Microroentgens, MicroroentgensTolerance); + Assert.Equal(RadiationExposureUnit.Microroentgen, quantity02.Unit); + + var quantity03 = RadiationExposure.From(1, RadiationExposureUnit.MillicoulombPerKilogram); + AssertEx.EqualTolerance(1, quantity03.MillicoulombsPerKilogram, MillicoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.MillicoulombPerKilogram, quantity03.Unit); + + var quantity04 = RadiationExposure.From(1, RadiationExposureUnit.Milliroentgen); + AssertEx.EqualTolerance(1, quantity04.Milliroentgens, MilliroentgensTolerance); + Assert.Equal(RadiationExposureUnit.Milliroentgen, quantity04.Unit); + + var quantity05 = RadiationExposure.From(1, RadiationExposureUnit.NanocoulombPerKilogram); + AssertEx.EqualTolerance(1, quantity05.NanocoulombsPerKilogram, NanocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.NanocoulombPerKilogram, quantity05.Unit); + + var quantity06 = RadiationExposure.From(1, RadiationExposureUnit.PicocoulombPerKilogram); + AssertEx.EqualTolerance(1, quantity06.PicocoulombsPerKilogram, PicocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.PicocoulombPerKilogram, quantity06.Unit); + + var quantity07 = RadiationExposure.From(1, RadiationExposureUnit.Roentgen); + AssertEx.EqualTolerance(1, quantity07.Roentgens, RoentgensTolerance); + Assert.Equal(RadiationExposureUnit.Roentgen, quantity07.Unit); + + } + + [Fact] + public void FromCoulombsPerKilogram_WithInfinityValue_ThrowsArgumentException() + { + Assert.Throws(() => RadiationExposure.FromCoulombsPerKilogram(double.PositiveInfinity)); + Assert.Throws(() => RadiationExposure.FromCoulombsPerKilogram(double.NegativeInfinity)); + } + + [Fact] + public void FromCoulombsPerKilogram_WithNanValue_ThrowsArgumentException() + { + Assert.Throws(() => RadiationExposure.FromCoulombsPerKilogram(double.NaN)); + } + + [Fact] + public void As() + { + var coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + AssertEx.EqualTolerance(CoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.CoulombPerKilogram), CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(MicrocoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.MicrocoulombPerKilogram), MicrocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(MicroroentgensInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.Microroentgen), MicroroentgensTolerance); + AssertEx.EqualTolerance(MillicoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.MillicoulombPerKilogram), MillicoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(MilliroentgensInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.Milliroentgen), MilliroentgensTolerance); + AssertEx.EqualTolerance(NanocoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.NanocoulombPerKilogram), NanocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(PicocoulombsPerKilogramInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.PicocoulombPerKilogram), PicocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(RoentgensInOneCoulombPerKilogram, coulombperkilogram.As(RadiationExposureUnit.Roentgen), RoentgensTolerance); + } + + [Fact] + public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported() + { + var quantity = new RadiationExposure(value: 1, unit: RadiationExposure.BaseUnit); + Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI); + + if (SupportsSIUnitSystem) + { + var value = Convert.ToDouble(AsWithSIUnitSystem()); + Assert.Equal(1, value); + } + else + { + Assert.Throws(AsWithSIUnitSystem); + } + } + + [Fact] + public void Parse() + { + try + { + var parsed = RadiationExposure.Parse("1 C/kg", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.CoulombsPerKilogram, CoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.CoulombPerKilogram, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 µC/kg", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.MicrocoulombsPerKilogram, MicrocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.MicrocoulombPerKilogram, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 µR", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.Microroentgens, MicroroentgensTolerance); + Assert.Equal(RadiationExposureUnit.Microroentgen, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 mC/kg", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.MillicoulombsPerKilogram, MillicoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.MillicoulombPerKilogram, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 mR", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.Milliroentgens, MilliroentgensTolerance); + Assert.Equal(RadiationExposureUnit.Milliroentgen, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 nC/kg", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.NanocoulombsPerKilogram, NanocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.NanocoulombPerKilogram, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 pC/kg", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.PicocoulombsPerKilogram, PicocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.PicocoulombPerKilogram, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsed = RadiationExposure.Parse("1 R", CultureInfo.GetCultureInfo("en-US")); + AssertEx.EqualTolerance(1, parsed.Roentgens, RoentgensTolerance); + Assert.Equal(RadiationExposureUnit.Roentgen, parsed.Unit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + } + + [Fact] + public void TryParse() + { + { + Assert.True(RadiationExposure.TryParse("1 C/kg", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.CoulombsPerKilogram, CoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.CoulombPerKilogram, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 µC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.MicrocoulombsPerKilogram, MicrocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.MicrocoulombPerKilogram, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 µR", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.Microroentgens, MicroroentgensTolerance); + Assert.Equal(RadiationExposureUnit.Microroentgen, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 mC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.MillicoulombsPerKilogram, MillicoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.MillicoulombPerKilogram, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 mR", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.Milliroentgens, MilliroentgensTolerance); + Assert.Equal(RadiationExposureUnit.Milliroentgen, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 nC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.NanocoulombsPerKilogram, NanocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.NanocoulombPerKilogram, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 pC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.PicocoulombsPerKilogram, PicocoulombsPerKilogramTolerance); + Assert.Equal(RadiationExposureUnit.PicocoulombPerKilogram, parsed.Unit); + } + + { + Assert.True(RadiationExposure.TryParse("1 R", CultureInfo.GetCultureInfo("en-US"), out var parsed)); + AssertEx.EqualTolerance(1, parsed.Roentgens, RoentgensTolerance); + Assert.Equal(RadiationExposureUnit.Roentgen, parsed.Unit); + } + + } + + [Fact] + public void ParseUnit() + { + try + { + var parsedUnit = RadiationExposure.ParseUnit("C/kg", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.CoulombPerKilogram, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("µC/kg", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.MicrocoulombPerKilogram, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("µR", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.Microroentgen, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("mC/kg", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.MillicoulombPerKilogram, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("mR", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.Milliroentgen, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("nC/kg", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.NanocoulombPerKilogram, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("pC/kg", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.PicocoulombPerKilogram, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + try + { + var parsedUnit = RadiationExposure.ParseUnit("R", CultureInfo.GetCultureInfo("en-US")); + Assert.Equal(RadiationExposureUnit.Roentgen, parsedUnit); + } catch (AmbiguousUnitParseException) { /* Some units have the same abbreviations */ } + + } + + [Fact] + public void TryParseUnit() + { + { + Assert.True(RadiationExposure.TryParseUnit("C/kg", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.CoulombPerKilogram, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("µC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.MicrocoulombPerKilogram, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("µR", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.Microroentgen, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("mC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.MillicoulombPerKilogram, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("mR", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.Milliroentgen, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("nC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.NanocoulombPerKilogram, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("pC/kg", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.PicocoulombPerKilogram, parsedUnit); + } + + { + Assert.True(RadiationExposure.TryParseUnit("R", CultureInfo.GetCultureInfo("en-US"), out var parsedUnit)); + Assert.Equal(RadiationExposureUnit.Roentgen, parsedUnit); + } + + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit(RadiationExposureUnit unit) + { + var inBaseUnits = RadiationExposure.From(1.0, RadiationExposure.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(RadiationExposureUnit unit) + { + var quantity = RadiationExposure.From(3.0, unit); + var toUnitWithSameUnit = quantity.ToUnit(unit); + Assert.Equal(quantity, toUnitWithSameUnit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit(RadiationExposureUnit unit) + { + // See if there is a unit available that is not the base unit, fallback to base unit if it has only a single unit. + var fromUnit = RadiationExposure.Units.First(u => u != RadiationExposure.BaseUnit); + + var quantity = RadiationExposure.From(3.0, fromUnit); + var converted = quantity.ToUnit(unit); + Assert.Equal(converted.Unit, unit); + } + + [Theory] + [MemberData(nameof(UnitTypes))] + public virtual void ToUnit_FromDefaultQuantity_ReturnsQuantityWithGivenUnit(RadiationExposureUnit unit) + { + var quantity = default(RadiationExposure); + var converted = quantity.ToUnit(unit); + Assert.Equal(converted.Unit, unit); + } + + [Fact] + public void ConversionRoundTrip() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + AssertEx.EqualTolerance(1, RadiationExposure.FromCoulombsPerKilogram(coulombperkilogram.CoulombsPerKilogram).CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromMicrocoulombsPerKilogram(coulombperkilogram.MicrocoulombsPerKilogram).CoulombsPerKilogram, MicrocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromMicroroentgens(coulombperkilogram.Microroentgens).CoulombsPerKilogram, MicroroentgensTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromMillicoulombsPerKilogram(coulombperkilogram.MillicoulombsPerKilogram).CoulombsPerKilogram, MillicoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromMilliroentgens(coulombperkilogram.Milliroentgens).CoulombsPerKilogram, MilliroentgensTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromNanocoulombsPerKilogram(coulombperkilogram.NanocoulombsPerKilogram).CoulombsPerKilogram, NanocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromPicocoulombsPerKilogram(coulombperkilogram.PicocoulombsPerKilogram).CoulombsPerKilogram, PicocoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(1, RadiationExposure.FromRoentgens(coulombperkilogram.Roentgens).CoulombsPerKilogram, RoentgensTolerance); + } + + [Fact] + public void ArithmeticOperators() + { + RadiationExposure v = RadiationExposure.FromCoulombsPerKilogram(1); + AssertEx.EqualTolerance(-1, -v.CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(2, (RadiationExposure.FromCoulombsPerKilogram(3)-v).CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(2, (v + v).CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(10, (v*10).CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(10, (10*v).CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(2, (RadiationExposure.FromCoulombsPerKilogram(10)/5).CoulombsPerKilogram, CoulombsPerKilogramTolerance); + AssertEx.EqualTolerance(2, RadiationExposure.FromCoulombsPerKilogram(10)/RadiationExposure.FromCoulombsPerKilogram(5), CoulombsPerKilogramTolerance); + } + + [Fact] + public void ComparisonOperators() + { + RadiationExposure oneCoulombPerKilogram = RadiationExposure.FromCoulombsPerKilogram(1); + RadiationExposure twoCoulombsPerKilogram = RadiationExposure.FromCoulombsPerKilogram(2); + + Assert.True(oneCoulombPerKilogram < twoCoulombsPerKilogram); + Assert.True(oneCoulombPerKilogram <= twoCoulombsPerKilogram); + Assert.True(twoCoulombsPerKilogram > oneCoulombPerKilogram); + Assert.True(twoCoulombsPerKilogram >= oneCoulombPerKilogram); + + Assert.False(oneCoulombPerKilogram > twoCoulombsPerKilogram); + Assert.False(oneCoulombPerKilogram >= twoCoulombsPerKilogram); + Assert.False(twoCoulombsPerKilogram < oneCoulombPerKilogram); + Assert.False(twoCoulombsPerKilogram <= oneCoulombPerKilogram); + } + + [Fact] + public void CompareToIsImplemented() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.Equal(0, coulombperkilogram.CompareTo(coulombperkilogram)); + Assert.True(coulombperkilogram.CompareTo(RadiationExposure.Zero) > 0); + Assert.True(RadiationExposure.Zero.CompareTo(coulombperkilogram) < 0); + } + + [Fact] + public void CompareToThrowsOnTypeMismatch() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.Throws(() => coulombperkilogram.CompareTo(new object())); + } + + [Fact] + public void CompareToThrowsOnNull() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.Throws(() => coulombperkilogram.CompareTo(null)); + } + + [Theory] + [InlineData(1, RadiationExposureUnit.CoulombPerKilogram, 1, RadiationExposureUnit.CoulombPerKilogram, true)] // Same value and unit. + [InlineData(1, RadiationExposureUnit.CoulombPerKilogram, 2, RadiationExposureUnit.CoulombPerKilogram, false)] // Different value. + [InlineData(2, RadiationExposureUnit.CoulombPerKilogram, 1, RadiationExposureUnit.MicrocoulombPerKilogram, false)] // Different value and unit. + [InlineData(1, RadiationExposureUnit.CoulombPerKilogram, 1, RadiationExposureUnit.MicrocoulombPerKilogram, false)] // Different unit. + public void Equals_ReturnsTrue_IfValueAndUnitAreEqual(double valueA, RadiationExposureUnit unitA, double valueB, RadiationExposureUnit unitB, bool expectEqual) + { + var a = new RadiationExposure(valueA, unitA); + var b = new RadiationExposure(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 = RadiationExposure.Zero; + + Assert.False(a.Equals((object)null)); + + // "The result of the expression is always 'false'..." + #pragma warning disable CS8073 + Assert.False(a == null); + Assert.False(null == a); + Assert.True(a != null); + Assert.True(null != a); + #pragma warning restore CS8073 + } + + [Fact] + public void Equals_RelativeTolerance_IsImplemented() + { + var v = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.True(v.Equals(RadiationExposure.FromCoulombsPerKilogram(1), CoulombsPerKilogramTolerance, ComparisonType.Relative)); + Assert.False(v.Equals(RadiationExposure.Zero, CoulombsPerKilogramTolerance, ComparisonType.Relative)); + } + + [Fact] + public void Equals_NegativeRelativeTolerance_ThrowsArgumentOutOfRangeException() + { + var v = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.Throws(() => v.Equals(RadiationExposure.FromCoulombsPerKilogram(1), -1, ComparisonType.Relative)); + } + + [Fact] + public void EqualsReturnsFalseOnTypeMismatch() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.False(coulombperkilogram.Equals(new object())); + } + + [Fact] + public void EqualsReturnsFalseOnNull() + { + RadiationExposure coulombperkilogram = RadiationExposure.FromCoulombsPerKilogram(1); + Assert.False(coulombperkilogram.Equals(null)); + } + + [Fact] + public void HasAtLeastOneAbbreviationSpecified() + { + var units = Enum.GetValues(typeof(RadiationExposureUnit)).Cast(); + foreach (var unit in units) + { + var defaultAbbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit); + } + } + + [Fact] + public void BaseDimensionsShouldNeverBeNull() + { + Assert.False(RadiationExposure.BaseDimensions is null); + } + + [Fact] + public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture() + { + var prevCulture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); + try { + Assert.Equal("1 C/kg", new RadiationExposure(1, RadiationExposureUnit.CoulombPerKilogram).ToString()); + Assert.Equal("1 µC/kg", new RadiationExposure(1, RadiationExposureUnit.MicrocoulombPerKilogram).ToString()); + Assert.Equal("1 µR", new RadiationExposure(1, RadiationExposureUnit.Microroentgen).ToString()); + Assert.Equal("1 mC/kg", new RadiationExposure(1, RadiationExposureUnit.MillicoulombPerKilogram).ToString()); + Assert.Equal("1 mR", new RadiationExposure(1, RadiationExposureUnit.Milliroentgen).ToString()); + Assert.Equal("1 nC/kg", new RadiationExposure(1, RadiationExposureUnit.NanocoulombPerKilogram).ToString()); + Assert.Equal("1 pC/kg", new RadiationExposure(1, RadiationExposureUnit.PicocoulombPerKilogram).ToString()); + Assert.Equal("1 R", new RadiationExposure(1, RadiationExposureUnit.Roentgen).ToString()); + } + finally + { + Thread.CurrentThread.CurrentCulture = prevCulture; + } + } + + [Fact] + public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCultureSinceThereAreNoMappings() + { + // Chose this culture, because we don't currently have any abbreviations mapped for that culture and we expect the en-US to be used as fallback. + var swedishCulture = CultureInfo.GetCultureInfo("sv-SE"); + + Assert.Equal("1 C/kg", new RadiationExposure(1, RadiationExposureUnit.CoulombPerKilogram).ToString(swedishCulture)); + Assert.Equal("1 µC/kg", new RadiationExposure(1, RadiationExposureUnit.MicrocoulombPerKilogram).ToString(swedishCulture)); + Assert.Equal("1 µR", new RadiationExposure(1, RadiationExposureUnit.Microroentgen).ToString(swedishCulture)); + Assert.Equal("1 mC/kg", new RadiationExposure(1, RadiationExposureUnit.MillicoulombPerKilogram).ToString(swedishCulture)); + Assert.Equal("1 mR", new RadiationExposure(1, RadiationExposureUnit.Milliroentgen).ToString(swedishCulture)); + Assert.Equal("1 nC/kg", new RadiationExposure(1, RadiationExposureUnit.NanocoulombPerKilogram).ToString(swedishCulture)); + Assert.Equal("1 pC/kg", new RadiationExposure(1, RadiationExposureUnit.PicocoulombPerKilogram).ToString(swedishCulture)); + Assert.Equal("1 R", new RadiationExposure(1, RadiationExposureUnit.Roentgen).ToString(swedishCulture)); + } + + [Fact] + public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture() + { + var oldCulture = CultureInfo.CurrentCulture; + try + { + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + Assert.Equal("0.1 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s1")); + Assert.Equal("0.12 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s2")); + Assert.Equal("0.123 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s3")); + Assert.Equal("0.1235 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s4")); + } + finally + { + CultureInfo.CurrentCulture = oldCulture; + } + } + + [Fact] + public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture() + { + var culture = CultureInfo.InvariantCulture; + Assert.Equal("0.1 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s1", culture)); + Assert.Equal("0.12 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s2", culture)); + Assert.Equal("0.123 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s3", culture)); + Assert.Equal("0.1235 C/kg", new RadiationExposure(0.123456, RadiationExposureUnit.CoulombPerKilogram).ToString("s4", culture)); + } + + [Theory] + [InlineData(null)] + [InlineData("en-US")] + public void ToString_NullFormat_DefaultsToGeneralFormat(string cultureName) + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(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 = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(quantity.ToString(format, CultureInfo.CurrentCulture), quantity.ToString(format, null)); + } + + [Fact] + public void Convert_ToBool_ThrowsInvalidCastException() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Throws(() => Convert.ToBoolean(quantity)); + } + + [Fact] + public void Convert_ToByte_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((byte)quantity.Value, Convert.ToByte(quantity)); + } + + [Fact] + public void Convert_ToChar_ThrowsInvalidCastException() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Throws(() => Convert.ToChar(quantity)); + } + + [Fact] + public void Convert_ToDateTime_ThrowsInvalidCastException() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Throws(() => Convert.ToDateTime(quantity)); + } + + [Fact] + public void Convert_ToDecimal_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((decimal)quantity.Value, Convert.ToDecimal(quantity)); + } + + [Fact] + public void Convert_ToDouble_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((double)quantity.Value, Convert.ToDouble(quantity)); + } + + [Fact] + public void Convert_ToInt16_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((short)quantity.Value, Convert.ToInt16(quantity)); + } + + [Fact] + public void Convert_ToInt32_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((int)quantity.Value, Convert.ToInt32(quantity)); + } + + [Fact] + public void Convert_ToInt64_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((long)quantity.Value, Convert.ToInt64(quantity)); + } + + [Fact] + public void Convert_ToSByte_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((sbyte)quantity.Value, Convert.ToSByte(quantity)); + } + + [Fact] + public void Convert_ToSingle_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((float)quantity.Value, Convert.ToSingle(quantity)); + } + + [Fact] + public void Convert_ToString_EqualsToString() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(quantity.ToString(), Convert.ToString(quantity)); + } + + [Fact] + public void Convert_ToUInt16_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((ushort)quantity.Value, Convert.ToUInt16(quantity)); + } + + [Fact] + public void Convert_ToUInt32_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((uint)quantity.Value, Convert.ToUInt32(quantity)); + } + + [Fact] + public void Convert_ToUInt64_EqualsValueAsSameType() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal((ulong)quantity.Value, Convert.ToUInt64(quantity)); + } + + [Fact] + public void Convert_ChangeType_SelfType_EqualsSelf() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(quantity, Convert.ChangeType(quantity, typeof(RadiationExposure))); + } + + [Fact] + public void Convert_ChangeType_UnitType_EqualsUnit() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(quantity.Unit, Convert.ChangeType(quantity, typeof(RadiationExposureUnit))); + } + + [Fact] + public void Convert_ChangeType_QuantityInfo_EqualsQuantityInfo() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(RadiationExposure.Info, Convert.ChangeType(quantity, typeof(QuantityInfo))); + } + + [Fact] + public void Convert_ChangeType_BaseDimensions_EqualsBaseDimensions() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(RadiationExposure.BaseDimensions, Convert.ChangeType(quantity, typeof(BaseDimensions))); + } + + [Fact] + public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Throws(() => Convert.ChangeType(quantity, typeof(QuantityFormatter))); + } + + [Fact] + public void GetHashCode_Equals() + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(1.0); + Assert.Equal(new {RadiationExposure.Info.Name, quantity.Value, quantity.Unit}.GetHashCode(), quantity.GetHashCode()); + } + + [Theory] + [InlineData(1.0)] + [InlineData(-1.0)] + public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value) + { + var quantity = RadiationExposure.FromCoulombsPerKilogram(value); + Assert.Equal(RadiationExposure.FromCoulombsPerKilogram(-value), -quantity); + } + } +} diff --git a/UnitsNet/GeneratedCode/Quantities/RadiationExposure.g.cs b/UnitsNet/GeneratedCode/Quantities/RadiationExposure.g.cs new file mode 100644 index 0000000000..ef1853eca5 --- /dev/null +++ b/UnitsNet/GeneratedCode/Quantities/RadiationExposure.g.cs @@ -0,0 +1,1070 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by \generate-code.bat. +// +// Changes to this file will be lost when the code is regenerated. +// The build server regenerates the code before each build and a pre-build +// step will regenerate the code on each local build. +// +// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units. +// +// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities. +// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities. +// +// +//------------------------------------------------------------------------------ + +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Linq; +using System.Runtime.Serialization; +using UnitsNet.InternalHelpers; +using UnitsNet.Units; + +#nullable enable + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// + /// Radiation exposure is a measure of the ionization of air due to ionizing radiation from photons. + /// + [DataContract] + public readonly partial struct RadiationExposure : + IArithmeticQuantity, + IComparable, + IComparable, + IConvertible, + IEquatable, + IFormattable + { + /// + /// The numeric value this quantity was constructed with. + /// + [DataMember(Name = "Value", Order = 0)] + private readonly double _value; + + /// + /// The unit this quantity was constructed with. + /// + [DataMember(Name = "Unit", Order = 1)] + private readonly RadiationExposureUnit? _unit; + + static RadiationExposure() + { + BaseDimensions = new BaseDimensions(0, -1, 1, 1, 0, 0, 0); + BaseUnit = RadiationExposureUnit.CoulombPerKilogram; + Units = Enum.GetValues(typeof(RadiationExposureUnit)).Cast().ToArray(); + Zero = new RadiationExposure(0, BaseUnit); + Info = new QuantityInfo("RadiationExposure", + new UnitInfo[] + { + new UnitInfo(RadiationExposureUnit.CoulombPerKilogram, "CoulombsPerKilogram", new BaseUnits(mass: MassUnit.Kilogram, time: DurationUnit.Second, current: ElectricCurrentUnit.Ampere), "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.MicrocoulombPerKilogram, "MicrocoulombsPerKilogram", BaseUnits.Undefined, "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.Microroentgen, "Microroentgens", BaseUnits.Undefined, "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.MillicoulombPerKilogram, "MillicoulombsPerKilogram", BaseUnits.Undefined, "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.Milliroentgen, "Milliroentgens", BaseUnits.Undefined, "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.NanocoulombPerKilogram, "NanocoulombsPerKilogram", BaseUnits.Undefined, "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.PicocoulombPerKilogram, "PicocoulombsPerKilogram", BaseUnits.Undefined, "RadiationExposure"), + new UnitInfo(RadiationExposureUnit.Roentgen, "Roentgens", new BaseUnits(mass: MassUnit.Kilogram, time: DurationUnit.Second, current: ElectricCurrentUnit.Ampere), "RadiationExposure"), + }, + BaseUnit, Zero, BaseDimensions); + + DefaultConversionFunctions = new UnitConverter(); + RegisterDefaultConversions(DefaultConversionFunctions); + } + + /// + /// Creates the quantity with the given numeric value and unit. + /// + /// The numeric value to construct this quantity with. + /// The unit representation to construct this quantity with. + /// If value is NaN or Infinity. + public RadiationExposure(double value, RadiationExposureUnit unit) + { + _value = Guard.EnsureValidNumber(value, nameof(value)); + _unit = unit; + } + + /// + /// Creates an instance of the quantity with the given numeric value in units compatible with the given . + /// If multiple compatible units were found, the first match is used. + /// + /// The numeric value to construct this quantity with. + /// The unit system to create the quantity with. + /// The given is null. + /// No unit was found for the given . + public RadiationExposure(double value, UnitSystem unitSystem) + { + if (unitSystem is null) throw new ArgumentNullException(nameof(unitSystem)); + + var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits); + var firstUnitInfo = unitInfos.FirstOrDefault(); + + _value = Guard.EnsureValidNumber(value, nameof(value)); + _unit = firstUnitInfo?.Value ?? throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem)); + } + + #region Static Properties + + /// + /// The containing the default generated conversion functions for instances. + /// + public static UnitConverter DefaultConversionFunctions { get; } + + /// + public static QuantityInfo Info { get; } + + /// + /// The of this quantity. + /// + public static BaseDimensions BaseDimensions { get; } + + /// + /// The base unit of RadiationExposure, which is CoulombPerKilogram. All conversions go via this value. + /// + public static RadiationExposureUnit BaseUnit { get; } + + /// + /// All units of measurement for the RadiationExposure quantity. + /// + public static RadiationExposureUnit[] Units { get; } + + /// + /// Gets an instance of this quantity with a value of 0 in the base unit CoulombPerKilogram. + /// + public static RadiationExposure Zero { get; } + + /// + public static RadiationExposure AdditiveIdentity => Zero; + + #endregion + + #region Properties + + /// + /// The numeric value this quantity was constructed with. + /// + public double Value => _value; + + /// + QuantityValue IQuantity.Value => _value; + + Enum IQuantity.Unit => Unit; + + /// + public RadiationExposureUnit Unit => _unit.GetValueOrDefault(BaseUnit); + + /// + public QuantityInfo QuantityInfo => Info; + + /// + QuantityInfo IQuantity.QuantityInfo => Info; + + /// + /// The of this quantity. + /// + public BaseDimensions Dimensions => RadiationExposure.BaseDimensions; + + #endregion + + #region Conversion Properties + + /// + /// Gets a value of this quantity converted into + /// + public double CoulombsPerKilogram => As(RadiationExposureUnit.CoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double MicrocoulombsPerKilogram => As(RadiationExposureUnit.MicrocoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double Microroentgens => As(RadiationExposureUnit.Microroentgen); + + /// + /// Gets a value of this quantity converted into + /// + public double MillicoulombsPerKilogram => As(RadiationExposureUnit.MillicoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double Milliroentgens => As(RadiationExposureUnit.Milliroentgen); + + /// + /// Gets a value of this quantity converted into + /// + public double NanocoulombsPerKilogram => As(RadiationExposureUnit.NanocoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double PicocoulombsPerKilogram => As(RadiationExposureUnit.PicocoulombPerKilogram); + + /// + /// Gets a value of this quantity converted into + /// + public double Roentgens => As(RadiationExposureUnit.Roentgen); + + #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: RadiationExposureUnit -> BaseUnit + unitConverter.SetConversionFunction(RadiationExposureUnit.MicrocoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.Microroentgen, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.MillicoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.Milliroentgen, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.NanocoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.PicocoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.Roentgen, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.CoulombPerKilogram)); + + // Register in unit converter: BaseUnit <-> BaseUnit + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram, quantity => quantity); + + // Register in unit converter: BaseUnit -> RadiationExposureUnit + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.MicrocoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.MicrocoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.Microroentgen, quantity => quantity.ToUnit(RadiationExposureUnit.Microroentgen)); + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.MillicoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.MillicoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.Milliroentgen, quantity => quantity.ToUnit(RadiationExposureUnit.Milliroentgen)); + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.NanocoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.NanocoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.PicocoulombPerKilogram, quantity => quantity.ToUnit(RadiationExposureUnit.PicocoulombPerKilogram)); + unitConverter.SetConversionFunction(RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.Roentgen, quantity => quantity.ToUnit(RadiationExposureUnit.Roentgen)); + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Unit abbreviation string. + public static string GetAbbreviation(RadiationExposureUnit 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(RadiationExposureUnit unit, IFormatProvider? provider) + { + return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider); + } + + #endregion + + #region Static Factory Methods + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromCoulombsPerKilogram(QuantityValue coulombsperkilogram) + { + double value = (double) coulombsperkilogram; + return new RadiationExposure(value, RadiationExposureUnit.CoulombPerKilogram); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMicrocoulombsPerKilogram(QuantityValue microcoulombsperkilogram) + { + double value = (double) microcoulombsperkilogram; + return new RadiationExposure(value, RadiationExposureUnit.MicrocoulombPerKilogram); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMicroroentgens(QuantityValue microroentgens) + { + double value = (double) microroentgens; + return new RadiationExposure(value, RadiationExposureUnit.Microroentgen); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMillicoulombsPerKilogram(QuantityValue millicoulombsperkilogram) + { + double value = (double) millicoulombsperkilogram; + return new RadiationExposure(value, RadiationExposureUnit.MillicoulombPerKilogram); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromMilliroentgens(QuantityValue milliroentgens) + { + double value = (double) milliroentgens; + return new RadiationExposure(value, RadiationExposureUnit.Milliroentgen); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromNanocoulombsPerKilogram(QuantityValue nanocoulombsperkilogram) + { + double value = (double) nanocoulombsperkilogram; + return new RadiationExposure(value, RadiationExposureUnit.NanocoulombPerKilogram); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromPicocoulombsPerKilogram(QuantityValue picocoulombsperkilogram) + { + double value = (double) picocoulombsperkilogram; + return new RadiationExposure(value, RadiationExposureUnit.PicocoulombPerKilogram); + } + + /// + /// Creates a from . + /// + /// If value is NaN or Infinity. + public static RadiationExposure FromRoentgens(QuantityValue roentgens) + { + double value = (double) roentgens; + return new RadiationExposure(value, RadiationExposureUnit.Roentgen); + } + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// RadiationExposure unit value. + public static RadiationExposure From(QuantityValue value, RadiationExposureUnit fromUnit) + { + return new RadiationExposure((double)value, fromUnit); + } + + #endregion + + #region Static Parse Methods + + /// + /// Parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.Parse("5.5 m", 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 RadiationExposure 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 RadiationExposure Parse(string str, IFormatProvider? provider) + { + return QuantityParser.Default.Parse( + str, + provider, + From); + } + + /// + /// Try to parse a string with one or two quantities of the format "<quantity> <unit>". + /// + /// String to parse. Typically in the form: {number} {unit} + /// Resulting unit quantity if successful. + /// + /// Length.Parse("5.5 m", CultureInfo.GetCultureInfo("en-US")); + /// + public static bool TryParse(string? str, out RadiationExposure 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(string? str, IFormatProvider? provider, out RadiationExposure result) + { + return QuantityParser.Default.TryParse( + str, + provider, + From, + out result); + } + + /// + /// Parse a unit string. + /// + /// String to parse. Typically in the form: {number} {unit} + /// + /// Length.ParseUnit("m", CultureInfo.GetCultureInfo("en-US")); + /// + /// The value of 'str' cannot be null. + /// Error parsing string. + public static RadiationExposureUnit 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 RadiationExposureUnit ParseUnit(string str, IFormatProvider? provider) + { + return UnitParser.Default.Parse(str, provider); + } + + /// + public static bool TryParseUnit(string str, out RadiationExposureUnit 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(string str, IFormatProvider? provider, out RadiationExposureUnit unit) + { + return UnitParser.Default.TryParse(str, provider, out unit); + } + + #endregion + + #region Arithmetic Operators + + /// Negate the value. + public static RadiationExposure operator -(RadiationExposure right) + { + return new RadiationExposure(-right.Value, right.Unit); + } + + /// Get from adding two . + public static RadiationExposure operator +(RadiationExposure left, RadiationExposure right) + { + return new RadiationExposure(left.Value + right.ToUnit(left.Unit).Value, left.Unit); + } + + /// Get from subtracting two . + public static RadiationExposure operator -(RadiationExposure left, RadiationExposure right) + { + return new RadiationExposure(left.Value - right.ToUnit(left.Unit).Value, left.Unit); + } + + /// Get from multiplying value and . + public static RadiationExposure operator *(double left, RadiationExposure right) + { + return new RadiationExposure(left * right.Value, right.Unit); + } + + /// Get from multiplying value and . + public static RadiationExposure operator *(RadiationExposure left, double right) + { + return new RadiationExposure(left.Value * right, left.Unit); + } + + /// Get from dividing by value. + public static RadiationExposure operator /(RadiationExposure left, double right) + { + return new RadiationExposure(left.Value / right, left.Unit); + } + + /// Get ratio value from dividing by . + public static double operator /(RadiationExposure left, RadiationExposure right) + { + return left.CoulombsPerKilogram / right.CoulombsPerKilogram; + } + + #endregion + + #region Equality / IComparable + + /// Returns true if less or equal to. + public static bool operator <=(RadiationExposure left, RadiationExposure right) + { + return left.Value <= right.ToUnit(left.Unit).Value; + } + + /// Returns true if greater than or equal to. + public static bool operator >=(RadiationExposure left, RadiationExposure right) + { + return left.Value >= right.ToUnit(left.Unit).Value; + } + + /// Returns true if less than. + public static bool operator <(RadiationExposure left, RadiationExposure right) + { + return left.Value < right.ToUnit(left.Unit).Value; + } + + /// Returns true if greater than. + public static bool operator >(RadiationExposure left, RadiationExposure 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(RadiationExposure other, RadiationExposure 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 ==(RadiationExposure left, RadiationExposure 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(RadiationExposure other, RadiationExposure 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 !=(RadiationExposure left, RadiationExposure right) + { + return !(left == right); + } + + /// + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("Use Equals(RadiationExposure other, RadiationExposure 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 RadiationExposure otherQuantity)) + return false; + + return Equals(otherQuantity); + } + + /// + /// Indicates strict equality of two quantities, where both and are exactly equal. + [Obsolete("Use Equals(RadiationExposure other, RadiationExposure 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(RadiationExposure other) + { + return new { Value, Unit }.Equals(new { other.Value, other.Unit }); + } + + #pragma warning restore CS0809 + + /// Compares the current with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit. + /// An object to compare with this instance. + /// + /// is not the same type as this instance. + /// + /// A value that indicates the relative order of the quantities being compared. The return value has these meanings: + /// + /// Value Meaning + /// Less than zero This instance precedes in the sort order. + /// Zero This instance occurs in the same position in the sort order as . + /// Greater than zero This instance follows in the sort order. + /// + /// + public int CompareTo(object? obj) + { + if (obj is null) throw new ArgumentNullException(nameof(obj)); + if (!(obj is RadiationExposure otherQuantity)) throw new ArgumentException("Expected type RadiationExposure.", 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(RadiationExposure other) + { + return _value.CompareTo(other.ToUnit(this.Unit).Value); + } + + /// + /// + /// Compare equality to another RadiationExposure within the given absolute or relative tolerance. + /// + /// + /// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a percentage of this quantity's value. will be converted into + /// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of + /// this quantity's value to be considered equal. + /// + /// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Relative); + /// + /// + /// + /// + /// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and + /// as a fixed number in this quantity's unit. will be converted into + /// this quantity's unit for comparison. + /// + /// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm). + /// + /// var a = Length.FromMeters(2.0); + /// var b = Length.FromInches(50.0); + /// a.Equals(b, 0.01, ComparisonType.Absolute); + /// + /// + /// + /// + /// Note that it is advised against specifying zero difference, due to the nature + /// of floating-point operations and using double internally. + /// + /// + /// The other quantity to compare to. + /// The absolute or relative tolerance value. Must be greater than or equal to 0. + /// The comparison type: either relative or absolute. + /// True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance. + [Obsolete("Use Equals(RadiationExposure other, RadiationExposure 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(RadiationExposure other, double tolerance, ComparisonType comparisonType) + { + if (tolerance < 0) + throw new ArgumentOutOfRangeException(nameof(tolerance), "Tolerance must be greater than or equal to 0."); + + return UnitsNet.Comparison.Equals( + referenceValue: this.Value, + otherValue: other.As(this.Unit), + tolerance: tolerance, + comparisonType: ComparisonType.Absolute); + } + + /// + public bool Equals(IQuantity? other, IQuantity tolerance) + { + return other is RadiationExposure otherTyped + && (tolerance is RadiationExposure toleranceTyped + ? true + : throw new ArgumentException($"Tolerance quantity ({tolerance.QuantityInfo.Name}) did not match the other quantities of type 'RadiationExposure'.", nameof(tolerance))) + && Equals(otherTyped, toleranceTyped); + } + + /// + public bool Equals(RadiationExposure other, RadiationExposure tolerance) + { + return UnitsNet.Comparison.Equals( + referenceValue: this.Value, + otherValue: other.As(this.Unit), + tolerance: tolerance.As(this.Unit), + comparisonType: ComparisonType.Absolute); + } + + /// + /// Returns the hash code for this instance. + /// + /// A hash code for the current RadiationExposure. + public override int GetHashCode() + { + return new { Info.Name, Value, Unit }.GetHashCode(); + } + + #endregion + + #region Conversion Methods + + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + public double As(RadiationExposureUnit unit) + { + if (Unit == unit) + return Value; + + return ToUnit(unit).Value; + } + + /// + public double As(UnitSystem unitSystem) + { + if (unitSystem is null) + throw new ArgumentNullException(nameof(unitSystem)); + + var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits); + + var firstUnitInfo = unitInfos.FirstOrDefault(); + if (firstUnitInfo == null) + throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem)); + + return As(firstUnitInfo.Value); + } + + /// + double IQuantity.As(Enum unit) + { + if (!(unit is RadiationExposureUnit typedUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(RadiationExposureUnit)} is supported.", nameof(unit)); + + return (double)As(typedUnit); + } + + /// + double IValueQuantity.As(Enum unit) + { + if (!(unit is RadiationExposureUnit typedUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(RadiationExposureUnit)} is supported.", nameof(unit)); + + return As(typedUnit); + } + + /// + /// Converts this RadiationExposure to another RadiationExposure with the unit representation . + /// + /// The unit to convert to. + /// A RadiationExposure with the specified unit. + public RadiationExposure ToUnit(RadiationExposureUnit 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 RadiationExposure with the specified unit. + public RadiationExposure ToUnit(RadiationExposureUnit 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(RadiationExposure), Unit, typeof(RadiationExposure), unit), out var conversionFunction)) + { + // See if the unit converter has an extensibility conversion registered. + return (RadiationExposure)conversionFunction(this); + } + else if (Unit != BaseUnit) + { + // Conversion to requested unit NOT found. Try to convert to BaseUnit, and then from BaseUnit to requested unit. + var inBaseUnits = ToUnit(BaseUnit); + return inBaseUnits.ToUnit(unit); + } + else + { + // No possible conversion + throw new NotImplementedException($"Can not convert {Unit} to {unit}."); + } + } + + /// + /// Attempts to convert this to another with the unit representation . + /// + /// The unit to convert to. + /// The converted in , if successful. + /// True if successful, otherwise false. + private bool TryToUnit(RadiationExposureUnit unit, [NotNullWhen(true)] out RadiationExposure? converted) + { + if (Unit == unit) + { + converted = this; + return true; + } + + RadiationExposure? convertedOrNull = (Unit, unit) switch + { + // RadiationExposureUnit -> BaseUnit + (RadiationExposureUnit.MicrocoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure((_value) * 1e-6d, RadiationExposureUnit.CoulombPerKilogram), + (RadiationExposureUnit.Microroentgen, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure((_value * 2.58e-4) * 1e-6d, RadiationExposureUnit.CoulombPerKilogram), + (RadiationExposureUnit.MillicoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure((_value) * 1e-3d, RadiationExposureUnit.CoulombPerKilogram), + (RadiationExposureUnit.Milliroentgen, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure((_value * 2.58e-4) * 1e-3d, RadiationExposureUnit.CoulombPerKilogram), + (RadiationExposureUnit.NanocoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure((_value) * 1e-9d, RadiationExposureUnit.CoulombPerKilogram), + (RadiationExposureUnit.PicocoulombPerKilogram, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure((_value) * 1e-12d, RadiationExposureUnit.CoulombPerKilogram), + (RadiationExposureUnit.Roentgen, RadiationExposureUnit.CoulombPerKilogram) => new RadiationExposure(_value * 2.58e-4, RadiationExposureUnit.CoulombPerKilogram), + + // BaseUnit -> RadiationExposureUnit + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.MicrocoulombPerKilogram) => new RadiationExposure((_value) / 1e-6d, RadiationExposureUnit.MicrocoulombPerKilogram), + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.Microroentgen) => new RadiationExposure((_value / 2.58e-4) / 1e-6d, RadiationExposureUnit.Microroentgen), + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.MillicoulombPerKilogram) => new RadiationExposure((_value) / 1e-3d, RadiationExposureUnit.MillicoulombPerKilogram), + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.Milliroentgen) => new RadiationExposure((_value / 2.58e-4) / 1e-3d, RadiationExposureUnit.Milliroentgen), + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.NanocoulombPerKilogram) => new RadiationExposure((_value) / 1e-9d, RadiationExposureUnit.NanocoulombPerKilogram), + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.PicocoulombPerKilogram) => new RadiationExposure((_value) / 1e-12d, RadiationExposureUnit.PicocoulombPerKilogram), + (RadiationExposureUnit.CoulombPerKilogram, RadiationExposureUnit.Roentgen) => new RadiationExposure(_value / 2.58e-4, RadiationExposureUnit.Roentgen), + + _ => null + }; + + if (convertedOrNull is null) + { + converted = default; + return false; + } + + converted = convertedOrNull.Value; + return true; + } + + /// + IQuantity IQuantity.ToUnit(Enum unit) + { + if (!(unit is RadiationExposureUnit typedUnit)) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(RadiationExposureUnit)} is supported.", nameof(unit)); + + return ToUnit(typedUnit, DefaultConversionFunctions); + } + + /// + public RadiationExposure ToUnit(UnitSystem unitSystem) + { + if (unitSystem is null) + throw new ArgumentNullException(nameof(unitSystem)); + + var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits); + + var firstUnitInfo = unitInfos.FirstOrDefault(); + if (firstUnitInfo == null) + throw new ArgumentException("No units were found for the given UnitSystem.", nameof(unitSystem)); + + return ToUnit(firstUnitInfo.Value); + } + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + IQuantity IQuantity.ToUnit(RadiationExposureUnit unit) => ToUnit(unit); + + /// + IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + /// + IValueQuantity IValueQuantity.ToUnit(Enum unit) + { + if (unit is not RadiationExposureUnit typedUnit) + throw new ArgumentException($"The given unit is of type {unit.GetType()}. Only {typeof(RadiationExposureUnit)} is supported.", nameof(unit)); + + return ToUnit(typedUnit); + } + + /// + IValueQuantity IValueQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem); + + #endregion + + #region ToString Methods + + /// + /// Gets the default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString("g"); + } + + /// + /// Gets the default string representation of value and unit using the given format provider. + /// + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + public string ToString(IFormatProvider? provider) + { + return ToString("g", provider); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using . + /// + /// The format string. + /// The string representation. + public string ToString(string? format) + { + return ToString(format, CultureInfo.CurrentCulture); + } + + /// + /// + /// Gets the string representation of this instance in the specified format string using the specified format provider, or if null. + /// + /// The format string. + /// Format to use for localization and number formatting. Defaults to if null. + /// The string representation. + public string ToString(string? format, IFormatProvider? provider) + { + return QuantityFormatter.Format(this, format, provider); + } + + #endregion + + #region IConvertible Methods + + TypeCode IConvertible.GetTypeCode() + { + return TypeCode.Object; + } + + bool IConvertible.ToBoolean(IFormatProvider? provider) + { + throw new InvalidCastException($"Converting {typeof(RadiationExposure)} to bool is not supported."); + } + + byte IConvertible.ToByte(IFormatProvider? provider) + { + return Convert.ToByte(_value); + } + + char IConvertible.ToChar(IFormatProvider? provider) + { + throw new InvalidCastException($"Converting {typeof(RadiationExposure)} to char is not supported."); + } + + DateTime IConvertible.ToDateTime(IFormatProvider? provider) + { + throw new InvalidCastException($"Converting {typeof(RadiationExposure)} to DateTime is not supported."); + } + + decimal IConvertible.ToDecimal(IFormatProvider? provider) + { + return Convert.ToDecimal(_value); + } + + double IConvertible.ToDouble(IFormatProvider? provider) + { + return Convert.ToDouble(_value); + } + + short IConvertible.ToInt16(IFormatProvider? provider) + { + return Convert.ToInt16(_value); + } + + int IConvertible.ToInt32(IFormatProvider? provider) + { + return Convert.ToInt32(_value); + } + + long IConvertible.ToInt64(IFormatProvider? provider) + { + return Convert.ToInt64(_value); + } + + sbyte IConvertible.ToSByte(IFormatProvider? provider) + { + return Convert.ToSByte(_value); + } + + float IConvertible.ToSingle(IFormatProvider? provider) + { + return Convert.ToSingle(_value); + } + + string IConvertible.ToString(IFormatProvider? provider) + { + return ToString("g", provider); + } + + object IConvertible.ToType(Type conversionType, IFormatProvider? provider) + { + if (conversionType == typeof(RadiationExposure)) + return this; + else if (conversionType == typeof(RadiationExposureUnit)) + return Unit; + else if (conversionType == typeof(QuantityInfo)) + return RadiationExposure.Info; + else if (conversionType == typeof(BaseDimensions)) + return RadiationExposure.BaseDimensions; + else + throw new InvalidCastException($"Converting {typeof(RadiationExposure)} to {conversionType} is not supported."); + } + + ushort IConvertible.ToUInt16(IFormatProvider? provider) + { + return Convert.ToUInt16(_value); + } + + uint IConvertible.ToUInt32(IFormatProvider? provider) + { + return Convert.ToUInt32(_value); + } + + ulong IConvertible.ToUInt64(IFormatProvider? provider) + { + return Convert.ToUInt64(_value); + } + + #endregion + } +} diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index 08cf42aeb4..3f09a75aa4 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -122,6 +122,7 @@ public partial class Quantity { "PowerRatio", PowerRatio.Info }, { "Pressure", Pressure.Info }, { "PressureChangeRate", PressureChangeRate.Info }, + { "RadiationExposure", RadiationExposure.Info }, { "Radioactivity", Radioactivity.Info }, { "Ratio", Ratio.Info }, { "RatioChangeRate", RatioChangeRate.Info }, @@ -256,6 +257,7 @@ public static IQuantity FromQuantityInfo(QuantityInfo quantityInfo, QuantityValu "PowerRatio" => PowerRatio.From(value, PowerRatio.BaseUnit), "Pressure" => Pressure.From(value, Pressure.BaseUnit), "PressureChangeRate" => PressureChangeRate.From(value, PressureChangeRate.BaseUnit), + "RadiationExposure" => RadiationExposure.From(value, RadiationExposure.BaseUnit), "Radioactivity" => Radioactivity.From(value, Radioactivity.BaseUnit), "Ratio" => Ratio.From(value, Ratio.BaseUnit), "RatioChangeRate" => RatioChangeRate.From(value, RatioChangeRate.BaseUnit), @@ -393,6 +395,7 @@ public static bool TryFrom(QuantityValue value, Enum? unit, [NotNullWhen(true)] PowerRatioUnit powerRatioUnit => PowerRatio.From(value, powerRatioUnit), PressureUnit pressureUnit => Pressure.From(value, pressureUnit), PressureChangeRateUnit pressureChangeRateUnit => PressureChangeRate.From(value, pressureChangeRateUnit), + RadiationExposureUnit radiationExposureUnit => RadiationExposure.From(value, radiationExposureUnit), RadioactivityUnit radioactivityUnit => Radioactivity.From(value, radioactivityUnit), RatioUnit ratioUnit => Ratio.From(value, ratioUnit), RatioChangeRateUnit ratioChangeRateUnit => RatioChangeRate.From(value, ratioChangeRateUnit), @@ -540,6 +543,7 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, Type _ when quantityType == typeof(PowerRatio) => parser.TryParse(quantityString, formatProvider, PowerRatio.From, out quantity), Type _ when quantityType == typeof(Pressure) => parser.TryParse(quantityString, formatProvider, Pressure.From, out quantity), Type _ when quantityType == typeof(PressureChangeRate) => parser.TryParse(quantityString, formatProvider, PressureChangeRate.From, out quantity), + Type _ when quantityType == typeof(RadiationExposure) => parser.TryParse(quantityString, formatProvider, RadiationExposure.From, out quantity), Type _ when quantityType == typeof(Radioactivity) => parser.TryParse(quantityString, formatProvider, Radioactivity.From, out quantity), Type _ when quantityType == typeof(Ratio) => parser.TryParse(quantityString, formatProvider, Ratio.From, out quantity), Type _ when quantityType == typeof(RatioChangeRate) => parser.TryParse(quantityString, formatProvider, RatioChangeRate.From, out quantity), @@ -668,6 +672,7 @@ internal static IEnumerable GetQuantityTypes() yield return typeof(PowerRatio); yield return typeof(Pressure); yield return typeof(PressureChangeRate); + yield return typeof(RadiationExposure); yield return typeof(Radioactivity); yield return typeof(Ratio); yield return typeof(RatioChangeRate); diff --git a/UnitsNet/GeneratedCode/Resources/RadiationExposure.restext b/UnitsNet/GeneratedCode/Resources/RadiationExposure.restext new file mode 100644 index 0000000000..d7725ef20e --- /dev/null +++ b/UnitsNet/GeneratedCode/Resources/RadiationExposure.restext @@ -0,0 +1,8 @@ +CoulombsPerKilogram=C/kg +MicrocoulombsPerKilogram=µC/kg +Microroentgens=µR +MillicoulombsPerKilogram=mC/kg +Milliroentgens=mR +NanocoulombsPerKilogram=nC/kg +PicocoulombsPerKilogram=pC/kg +Roentgens=R diff --git a/UnitsNet/GeneratedCode/Units/RadiationExposureUnit.g.cs b/UnitsNet/GeneratedCode/Units/RadiationExposureUnit.g.cs new file mode 100644 index 0000000000..8e50cdd24f --- /dev/null +++ b/UnitsNet/GeneratedCode/Units/RadiationExposureUnit.g.cs @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// 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 RadiationExposureUnit + { + CoulombPerKilogram = 10, + MicrocoulombPerKilogram = 3, + Microroentgen = 12, + MillicoulombPerKilogram = 2, + Milliroentgen = 13, + NanocoulombPerKilogram = 6, + PicocoulombPerKilogram = 5, + Roentgen = 4, + } + + #pragma warning restore 1591 +}