Skip to content

Commit 4415358

Browse files
sequc82angularsen
authored andcommitted
Conversion between pressure measurement references (#726)
* From YektaMirkan/Units - Added reference pressure property - by gratestas * From YektaMirkan/Units - Added enum of pressure reference - by gratestas * From YektaMirkan/Units - Added pressure wrapper for pressure reference conversion - by gratestas * Broke out tests into individual assertions. Corrected test values in assertions. * Removed line in ReferencePressure.As that was causing sign errors and incorrect vaules and failed test results. * Corrected issues with pressures less than absolute zero * Improved code coverage and corrected exception handling * Renamed unit tests * Sorted unit tests in PressureTests * Updated target framework to latest minor in all projects
1 parent 305ef08 commit 4415358

File tree

11 files changed

+366
-20
lines changed

11 files changed

+366
-20
lines changed

CodeGen/CodeGen.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<LangVersion>latest</LangVersion>
67
</PropertyGroup>
78

89
<ItemGroup>

UnitsNet.Serialization.JsonNet.CompatibilityTests/UnitsNet.Serialization.JsonNet.CompatibilityTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
55
<RootNamespace>UnitsNet.Serialization.JsonNet.CompatibilityTests</RootNamespace>
6-
<LangVersion>7.3</LangVersion>
6+
<LangVersion>latest</LangVersion>
77
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
88
<IsTestProject>true</IsTestProject>
99
</PropertyGroup>

UnitsNet.Serialization.JsonNet.Tests/UnitsNet.Serialization.JsonNet.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
55
<RootNamespace>UnitsNet.Serialization.JsonNet.Tests</RootNamespace>
6-
<LangVersion>7.3</LangVersion>
6+
<LangVersion>latest</LangVersion>
77
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
88
<IsTestProject>true</IsTestProject>
99
</PropertyGroup>

UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<!-- Assembly and msbuild properties -->
2020
<PropertyGroup>
2121
<AssemblyVersion>4.0.0.0</AssemblyVersion> <!-- Should reflect major part of Version -->
22-
<LangVersion>7.3</LangVersion>
22+
<LangVersion>latest</LangVersion>
2323
<RootNamespace>UnitsNet.Serialization.JsonNet</RootNamespace>
2424
<TargetFrameworks>netstandard2.0;net40</TargetFrameworks>
2525
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>

UnitsNet.Tests/CustomCode/PressureTests.cs

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System;
5+
using UnitsNet.CustomCode.Units;
6+
using UnitsNet.CustomCode.Wrappers;
47
using Xunit;
58

69
namespace UnitsNet.Tests.CustomCode
710
{
811
public class PressureTests : PressureTestsBase
912
{
10-
protected override double AtmospheresInOnePascal => 9.8692*1E-6;
13+
protected override double AtmospheresInOnePascal => 9.8692 * 1E-6;
1114

1215
protected override double BarsInOnePascal => 1E-5;
1316

@@ -47,15 +50,15 @@ public class PressureTests : PressureTestsBase
4750

4851
protected override double PoundsForcePerSquareInchInOnePascal => 1.450377377302092e-4;
4952

50-
protected override double TechnicalAtmospheresInOnePascal => 1.0197*1E-5;
53+
protected override double TechnicalAtmospheresInOnePascal => 1.0197 * 1E-5;
5154

5255
protected override double TonnesForcePerSquareCentimeterInOnePascal => 1.019716212977928e-8;
5356

5457
protected override double TonnesForcePerSquareMeterInOnePascal => 1.019716212977928e-4;
5558

5659
protected override double TonnesForcePerSquareMillimeterInOnePascal => 1.019716212977928e-10;
5760

58-
protected override double TorrsInOnePascal => 7.5006*1E-3;
61+
protected override double TorrsInOnePascal => 7.5006 * 1E-3;
5962

6063
protected override double CentibarsInOnePascal => 1e-3;
6164

@@ -90,32 +93,144 @@ public class PressureTests : PressureTestsBase
9093

9194
protected override double MillipascalsInOnePascal => 1e3;
9295

96+
[Fact]
97+
public void Absolute_WithAbsolutePressureReference_IsEqual()
98+
{
99+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Absolute);
100+
AssertEx.EqualTolerance(3, refPressure.Absolute.Atmospheres, AtmospheresTolerance);
101+
}
102+
103+
[Fact]
104+
public void Absolute_WithDefaultPressureReference_IsEqual()
105+
{
106+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3));
107+
AssertEx.EqualTolerance(3, refPressure.Absolute.Atmospheres, AtmospheresTolerance);
108+
}
109+
110+
[Fact]
111+
public void Absolute_WithGaugePressureReference_IsOneMore()
112+
{
113+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge);
114+
AssertEx.EqualTolerance(4, refPressure.Absolute.Atmospheres, AtmospheresTolerance);
115+
}
116+
117+
[Fact]
118+
public void Absolute_WithNegativeAbsolutePressureReference_ThrowsArgumentOutOfRangeException()
119+
{
120+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(-3), PressureReference.Absolute);
121+
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Absolute.Atmospheres);
122+
}
123+
124+
[Fact]
125+
public void Absolute_WithNegativeGaugePressureReference_ThrowsArgumentOutOfRangeException()
126+
{
127+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(-3), PressureReference.Gauge);
128+
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Absolute.Atmospheres);
129+
}
130+
131+
[Fact]
132+
public void Absolute_WithVacuumPressureReference_IsOneLessAtmosphereNegative()
133+
{
134+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum);
135+
AssertEx.EqualTolerance(0, refPressure.Absolute.Atmospheres, AtmospheresTolerance);
136+
}
137+
138+
[Fact]
139+
public void Absolute_WithVacuumPressureReference_ThrowsArgumentOutOfRangeException()
140+
{
141+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Vacuum);
142+
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Absolute.Atmospheres);
143+
}
144+
93145
[Fact]
94146
public void AreaTimesPressureEqualsForce()
95147
{
96-
Force force = Area.FromSquareMeters(3)*Pressure.FromPascals(20);
148+
var force = Area.FromSquareMeters(3) * Pressure.FromPascals(20);
97149
Assert.Equal(force, Force.FromNewtons(60));
98150
}
99151

100152
[Fact]
101-
public void PressureTimesAreaEqualsForce()
153+
public void Gauge_WithDefaultPressureReference_IsOneLessAtmosphere()
102154
{
103-
Force force = Pressure.FromPascals(20)*Area.FromSquareMeters(3);
104-
Assert.Equal(force, Force.FromNewtons(60));
155+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3));
156+
AssertEx.EqualTolerance(2, refPressure.Gauge.Atmospheres, AtmospheresTolerance);
105157
}
106158

107159
[Fact]
108-
public void PressureDividedBySpecificWeightEqualsLength()
160+
public void Gauge_WithGaugePressureReference_IsEqual()
109161
{
110-
Length length = Pressure.FromPascals(20) / SpecificWeight.FromNewtonsPerCubicMeter(2);
111-
Assert.Equal(Length.FromMeters(10), length);
162+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge);
163+
AssertEx.EqualTolerance(3, refPressure.Gauge.Atmospheres, AtmospheresTolerance);
164+
}
165+
166+
[Fact]
167+
public void Gauge_WithVacuumPressureReference_IsNegative()
168+
{
169+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum);
170+
AssertEx.EqualTolerance(-1, refPressure.Gauge.Atmospheres, AtmospheresTolerance);
171+
}
172+
173+
[Fact]
174+
public void Gauge_WithVacuumPressureReference_ThrowsArgumentOutOfRangeException()
175+
{
176+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Vacuum);
177+
Assert.Throws<ArgumentOutOfRangeException>(() => refPressure.Gauge.Atmospheres);
112178
}
113179

114180
[Fact]
115181
public void PressureDividedByLengthEqualsSpecificWeight()
116182
{
117-
SpecificWeight specificWeight = Pressure.FromPascals(20) / Length.FromMeters(2);
183+
var specificWeight = Pressure.FromPascals(20) / Length.FromMeters(2);
118184
Assert.Equal(SpecificWeight.FromNewtonsPerCubicMeter(10), specificWeight);
119185
}
186+
187+
[Fact]
188+
public void PressureDividedBySpecificWeightEqualsLength()
189+
{
190+
var length = Pressure.FromPascals(20) / SpecificWeight.FromNewtonsPerCubicMeter(2);
191+
Assert.Equal(Length.FromMeters(10), length);
192+
}
193+
194+
[Fact]
195+
public void PressureTimesAreaEqualsForce()
196+
{
197+
var force = Pressure.FromPascals(20) * Area.FromSquareMeters(3);
198+
Assert.Equal(force, Force.FromNewtons(60));
199+
}
200+
201+
// Pressure Measurement References
202+
[Fact]
203+
public void Reference_WithDefaultPressureReference_IsAbsolute()
204+
{
205+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3));
206+
Equals(PressureReference.Absolute, refPressure.Reference);
207+
}
208+
209+
[Fact]
210+
public void ReferencesDoesNotContainUndefined()
211+
{
212+
Assert.DoesNotContain(PressureReference.Undefined, ReferencePressure.References);
213+
}
214+
215+
[Fact]
216+
public void Vacuum_WithDefaultPressureReference_IsOneLessAtmosphereNegative()
217+
{
218+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3));
219+
AssertEx.EqualTolerance(-2, refPressure.Vacuum.Atmospheres, AtmospheresTolerance);
220+
}
221+
222+
[Fact]
223+
public void Vacuum_WithGaugePressureReference_IsNegative()
224+
{
225+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(3), PressureReference.Gauge);
226+
AssertEx.EqualTolerance(-3, refPressure.Vacuum.Atmospheres, AtmospheresTolerance);
227+
}
228+
229+
[Fact]
230+
public void Vacuum_WithVacuumPressureReference_IsEqual()
231+
{
232+
var refPressure = new ReferencePressure(Pressure.FromAtmospheres(1), PressureReference.Vacuum);
233+
AssertEx.EqualTolerance(1, refPressure.Vacuum.Atmospheres, AtmospheresTolerance);
234+
}
120235
}
121236
}

UnitsNet.Tests/GeneratedCode/PressureTestsBase.g.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/UnitsNet.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
55
<RootNamespace>UnitsNet.Tests</RootNamespace>
6-
<LangVersion>7.3</LangVersion>
6+
<LangVersion>latest</LangVersion>
77
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
88
<IsTestProject>true</IsTestProject>
99
</PropertyGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by \generate-code.bat.
4+
//
5+
// Changes to this file will be lost when the code is regenerated.
6+
// The build server regenerates the code before each build and a pre-build
7+
// step will regenerate the code on each local build.
8+
//
9+
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
10+
//
11+
// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities.
12+
// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities.
13+
//
14+
// </auto-generated>
15+
//------------------------------------------------------------------------------
16+
17+
// Licensed under MIT No Attribution, see LICENSE file at the root.
18+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
19+
20+
// ReSharper disable once CheckNamespace
21+
namespace UnitsNet.CustomCode.Units
22+
{
23+
// Disable missing XML comment warnings for the generated unit enums.
24+
#pragma warning disable 1591
25+
26+
public enum PressureReference
27+
{
28+
Undefined = 0,
29+
Absolute,
30+
Gauge,
31+
Vacuum,
32+
}
33+
#pragma warning restore 1591
34+
}

0 commit comments

Comments
 (0)