diff --git a/CodeGen/CodeGen.csproj b/CodeGen/CodeGen.csproj index dad1d4208c..9a8a7c74c5 100644 --- a/CodeGen/CodeGen.csproj +++ b/CodeGen/CodeGen.csproj @@ -3,6 +3,7 @@ Exe netcoreapp2.1 + latest diff --git a/UnitsNet.Serialization.JsonNet.CompatibilityTests/UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj b/UnitsNet.Serialization.JsonNet.CompatibilityTests/UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj index bad6404562..6e724cbd19 100644 --- a/UnitsNet.Serialization.JsonNet.CompatibilityTests/UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj +++ b/UnitsNet.Serialization.JsonNet.CompatibilityTests/UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj @@ -3,7 +3,7 @@ netcoreapp2.1 UnitsNet.Serialization.JsonNet.CompatibilityTests - 7.3 + latest true true diff --git a/UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj b/UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj index 7822cbe17e..9fd7de9fab 100644 --- a/UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj +++ b/UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj @@ -3,7 +3,7 @@ netcoreapp2.1 UnitsNet.Serialization.JsonNet.Tests - 7.3 + latest true true diff --git a/UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.csproj b/UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.csproj index 34fd947682..1f4061d9a7 100644 --- a/UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.csproj +++ b/UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.csproj @@ -19,7 +19,7 @@ 4.0.0.0 - 7.3 + latest UnitsNet.Serialization.JsonNet netstandard2.0;net40 true diff --git a/UnitsNet.Tests/CustomCode/PressureTests.cs b/UnitsNet.Tests/CustomCode/PressureTests.cs index 645501b341..80288d8469 100644 --- a/UnitsNet.Tests/CustomCode/PressureTests.cs +++ b/UnitsNet.Tests/CustomCode/PressureTests.cs @@ -1,13 +1,16 @@ // 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.CustomCode.Units; +using UnitsNet.CustomCode.Wrappers; using Xunit; namespace UnitsNet.Tests.CustomCode { public class PressureTests : PressureTestsBase { - protected override double AtmospheresInOnePascal => 9.8692*1E-6; + protected override double AtmospheresInOnePascal => 9.8692 * 1E-6; protected override double BarsInOnePascal => 1E-5; @@ -47,7 +50,7 @@ public class PressureTests : PressureTestsBase protected override double PoundsForcePerSquareInchInOnePascal => 1.450377377302092e-4; - protected override double TechnicalAtmospheresInOnePascal => 1.0197*1E-5; + protected override double TechnicalAtmospheresInOnePascal => 1.0197 * 1E-5; protected override double TonnesForcePerSquareCentimeterInOnePascal => 1.019716212977928e-8; @@ -55,7 +58,7 @@ public class PressureTests : PressureTestsBase protected override double TonnesForcePerSquareMillimeterInOnePascal => 1.019716212977928e-10; - protected override double TorrsInOnePascal => 7.5006*1E-3; + protected override double TorrsInOnePascal => 7.5006 * 1E-3; protected override double CentibarsInOnePascal => 1e-3; @@ -90,32 +93,144 @@ public class PressureTests : PressureTestsBase protected override double MillipascalsInOnePascal => 1e3; + [Fact] + public void Absolute_WithAbsolutePressureReference_IsEqual() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Absolute); + AssertEx.EqualTolerance(3, refPressure.Absolute.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Absolute_WithDefaultPressureReference_IsEqual() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); + AssertEx.EqualTolerance(3, refPressure.Absolute.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Absolute_WithGaugePressureReference_IsOneMore() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge); + AssertEx.EqualTolerance(4, refPressure.Absolute.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Absolute_WithNegativeAbsolutePressureReference_ThrowsArgumentOutOfRangeException() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(-3), PressureReference.Absolute); + Assert.Throws(() => refPressure.Absolute.Atmospheres); + } + + [Fact] + public void Absolute_WithNegativeGaugePressureReference_ThrowsArgumentOutOfRangeException() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(-3), PressureReference.Gauge); + Assert.Throws(() => refPressure.Absolute.Atmospheres); + } + + [Fact] + public void Absolute_WithVacuumPressureReference_IsOneLessAtmosphereNegative() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum); + AssertEx.EqualTolerance(0, refPressure.Absolute.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Absolute_WithVacuumPressureReference_ThrowsArgumentOutOfRangeException() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Vacuum); + Assert.Throws(() => refPressure.Absolute.Atmospheres); + } + [Fact] public void AreaTimesPressureEqualsForce() { - Force force = Area.FromSquareMeters(3)*Pressure.FromPascals(20); + var force = Area.FromSquareMeters(3) * Pressure.FromPascals(20); Assert.Equal(force, Force.FromNewtons(60)); } [Fact] - public void PressureTimesAreaEqualsForce() + public void Gauge_WithDefaultPressureReference_IsOneLessAtmosphere() { - Force force = Pressure.FromPascals(20)*Area.FromSquareMeters(3); - Assert.Equal(force, Force.FromNewtons(60)); + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); + AssertEx.EqualTolerance(2, refPressure.Gauge.Atmospheres, AtmospheresTolerance); } [Fact] - public void PressureDividedBySpecificWeightEqualsLength() + public void Gauge_WithGaugePressureReference_IsEqual() { - Length length = Pressure.FromPascals(20) / SpecificWeight.FromNewtonsPerCubicMeter(2); - Assert.Equal(Length.FromMeters(10), length); + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge); + AssertEx.EqualTolerance(3, refPressure.Gauge.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Gauge_WithVacuumPressureReference_IsNegative() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum); + AssertEx.EqualTolerance(-1, refPressure.Gauge.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Gauge_WithVacuumPressureReference_ThrowsArgumentOutOfRangeException() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Vacuum); + Assert.Throws(() => refPressure.Gauge.Atmospheres); } [Fact] public void PressureDividedByLengthEqualsSpecificWeight() { - SpecificWeight specificWeight = Pressure.FromPascals(20) / Length.FromMeters(2); + var specificWeight = Pressure.FromPascals(20) / Length.FromMeters(2); Assert.Equal(SpecificWeight.FromNewtonsPerCubicMeter(10), specificWeight); } + + [Fact] + public void PressureDividedBySpecificWeightEqualsLength() + { + var length = Pressure.FromPascals(20) / SpecificWeight.FromNewtonsPerCubicMeter(2); + Assert.Equal(Length.FromMeters(10), length); + } + + [Fact] + public void PressureTimesAreaEqualsForce() + { + var force = Pressure.FromPascals(20) * Area.FromSquareMeters(3); + Assert.Equal(force, Force.FromNewtons(60)); + } + + // Pressure Measurement References + [Fact] + public void Reference_WithDefaultPressureReference_IsAbsolute() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); + Equals(PressureReference.Absolute, refPressure.Reference); + } + + [Fact] + public void ReferencesDoesNotContainUndefined() + { + Assert.DoesNotContain(PressureReference.Undefined, ReferencePressure.References); + } + + [Fact] + public void Vacuum_WithDefaultPressureReference_IsOneLessAtmosphereNegative() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3)); + AssertEx.EqualTolerance(-2, refPressure.Vacuum.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Vacuum_WithGaugePressureReference_IsNegative() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge); + AssertEx.EqualTolerance(-3, refPressure.Vacuum.Atmospheres, AtmospheresTolerance); + } + + [Fact] + public void Vacuum_WithVacuumPressureReference_IsEqual() + { + var refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum); + AssertEx.EqualTolerance(1, refPressure.Vacuum.Atmospheres, AtmospheresTolerance); + } } } diff --git a/UnitsNet.Tests/GeneratedCode/PressureTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/PressureTestsBase.g.cs index 0306563dd9..0e790567d1 100644 --- a/UnitsNet.Tests/GeneratedCode/PressureTestsBase.g.cs +++ b/UnitsNet.Tests/GeneratedCode/PressureTestsBase.g.cs @@ -649,5 +649,6 @@ public void BaseDimensionsShouldNeverBeNull() { Assert.False(Pressure.BaseDimensions is null); } + } } diff --git a/UnitsNet.Tests/UnitsNet.Tests.csproj b/UnitsNet.Tests/UnitsNet.Tests.csproj index 43e3cc5087..5e319ab79f 100644 --- a/UnitsNet.Tests/UnitsNet.Tests.csproj +++ b/UnitsNet.Tests/UnitsNet.Tests.csproj @@ -3,7 +3,7 @@ netcoreapp2.1 UnitsNet.Tests - 7.3 + latest true true diff --git a/UnitsNet/CustomCode/Units/PressureReference.cs b/UnitsNet/CustomCode/Units/PressureReference.cs new file mode 100644 index 0000000000..e2d224d60d --- /dev/null +++ b/UnitsNet/CustomCode/Units/PressureReference.cs @@ -0,0 +1,34 @@ +//------------------------------------------------------------------------------ +// +// 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.CustomCode.Units +{ + // Disable missing XML comment warnings for the generated unit enums. + #pragma warning disable 1591 + + public enum PressureReference + { + Undefined = 0, + Absolute, + Gauge, + Vacuum, + } +#pragma warning restore 1591 +} diff --git a/UnitsNet/CustomCode/Wrappers/ReferencePressure.cs b/UnitsNet/CustomCode/Wrappers/ReferencePressure.cs new file mode 100644 index 0000000000..c11d4a34c9 --- /dev/null +++ b/UnitsNet/CustomCode/Wrappers/ReferencePressure.cs @@ -0,0 +1,196 @@ +// 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.Linq; +using UnitsNet.CustomCode.Units; +using UnitsNet.Units; + +namespace UnitsNet.CustomCode.Wrappers +{ + /// + /// Pressure tied to a real-world reference, allowing conversion between references. + /// + /// + /// Absolute is referenced to true vacuum. + /// + /// + /// Gauge references the local atmospheric pressure. + /// + /// + /// Vacuum is the negative of the gauge. + /// + /// + /// + public struct ReferencePressure + { + /// + /// Represents the pressure at which _pressure is referenced (1 atm default) + /// + public Pressure AtmosphericPressure { get; set; } + + private static readonly Pressure DefaultAtmosphericPressure = new Pressure(1, PressureUnit.Atmosphere); + + /// + /// Gets a list of options: , + /// , and + /// + public static PressureReference[] References { get; } = + Enum.GetValues(typeof(PressureReference)).Cast().Except(new[] {PressureReference.Undefined}).ToArray(); + + /// + /// Initializes a new instance of the struct requiring measured + /// + /// parameter. Assumes the to , with 1 atm as + /// the atmospheric . + /// + /// The measured absolute + public ReferencePressure(Pressure pressure) : this(pressure, BaseReference) + { + } + + /// + /// Initializes a new instance of the struct requiring + /// measured and parameters. Assumes 1 atm as the atmospheric + /// . + /// + /// The measured + /// + /// The referenced for the measured + /// + public ReferencePressure(Pressure pressure, PressureReference reference) : this(pressure, reference, DefaultAtmosphericPressure) + { + } + + /// + /// Initializes a new instance of the struct requiring + /// measured , , and atmospheric + /// parameters + /// + /// The measured + /// + /// The referenced for the measured + /// + /// The atmospheric where the measurement was taken. + public ReferencePressure(Pressure pressure, PressureReference reference, Pressure atmosphericPressure) + { + Reference = reference; + Pressure = pressure; + AtmosphericPressure = atmosphericPressure; + } + + /// + /// Gets the of the + /// + public PressureReference Reference { get; } + + /// + /// The base reference representation of for the numeric value stored internally. All + /// conversions go via this value. + /// + public const PressureReference BaseReference = PressureReference.Absolute; + + /// + /// The at the given . + /// + public Pressure Pressure { get; } + + /// + /// Get Gauge . + /// It references pressure level above Atmospheric pressure. + /// + public Pressure Gauge => As(PressureReference.Gauge); + + /// + /// Get Absolute . + /// It is zero-referenced pressure to the perfect vacuum. + /// + public Pressure Absolute => As(PressureReference.Absolute); + + /// + /// Get Vacuum . + /// It is a negative Gauge pressure when Absolute pressure is below Atmospheric pressure. + /// + public Pressure Vacuum => As(PressureReference.Vacuum); + + /// + /// Converts to at + /// + /// The to convert to. + /// The at the specified + private Pressure As(PressureReference reference) + { + var converted = AsBaseNumericType(reference); + + return new Pressure(converted, Pressure.Unit); + } + + /// + /// Converts to at + /// + /// The to convert to. + /// The value of pressure at + private double AsBaseNumericType(PressureReference reference) + { + var baseReferenceValue = AsBaseReference(); + + if (Reference == reference) + { + return Pressure.Value; + } + + var negatingValue = Reference == PressureReference.Vacuum ? -1 : 1; + + switch (reference) + { + case PressureReference.Absolute: return baseReferenceValue; + case PressureReference.Gauge: return baseReferenceValue - AtmosphericPressure.ToUnit(Pressure.Unit).Value; + case PressureReference.Vacuum: return AtmosphericPressure.ToUnit(Pressure.Unit).Value - negatingValue * baseReferenceValue; + default: + throw new NotImplementedException($"Can not convert {Reference} to {reference}."); + } + } + + /// + /// Converts at to at + /// + /// + /// The value of pressure at the + private double AsBaseReference() + { + switch (Reference) + { + case PressureReference.Absolute: + { + if (Pressure.Value < 0) + { + throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero."); + } + + return Pressure.Value; + } + case PressureReference.Gauge: + { + if (Pressure.Value * -1 > AtmosphericPressure.ToUnit(Pressure.Unit).Value) + { + throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero."); + } + + return AtmosphericPressure.ToUnit(Pressure.Unit).Value + Pressure.Value; + } + case PressureReference.Vacuum: + { + if (Pressure.Value > AtmosphericPressure.ToUnit(Pressure.Unit).Value) + { + throw new ArgumentOutOfRangeException("Absolute pressure cannot be less than zero."); + } + + return AtmosphericPressure.ToUnit(Pressure.Unit).Value - Pressure.Value; + } + default: + throw new NotImplementedException($"Can not convert {Reference} to base reference."); + } + } + } +} diff --git a/UnitsNet/GeneratedCode/Units/PressureUnit.g.cs b/UnitsNet/GeneratedCode/Units/PressureUnit.g.cs index 3c4a239081..026472aad0 100644 --- a/UnitsNet/GeneratedCode/Units/PressureUnit.g.cs +++ b/UnitsNet/GeneratedCode/Units/PressureUnit.g.cs @@ -69,6 +69,5 @@ public enum PressureUnit TonneForcePerSquareMillimeter, Torr, } - - #pragma warning restore 1591 +#pragma warning restore 1591 } diff --git a/UnitsNet/UnitsNet.csproj b/UnitsNet/UnitsNet.csproj index 794589f98e..23fa3f55af 100644 --- a/UnitsNet/UnitsNet.csproj +++ b/UnitsNet/UnitsNet.csproj @@ -1,4 +1,4 @@ - + UnitsNet @@ -19,7 +19,7 @@ 4.0.0.0 - 7.3 + latest UnitsNet netstandard2.0;net40 true @@ -39,7 +39,7 @@ true UnitsNet - +