Skip to content

Commit 0022e05

Browse files
committed
Added operator overloads b/w Length, Pressure, & SpecificWeight
Length = Pressure / SpecificWeight
1 parent 52d6109 commit 0022e05

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ public void LengthTimesSpeedEqualsKinematicViscosity()
120120
Assert.Equal(KinematicViscosity.FromSquareMetersPerSecond(40), kinematicViscosity);
121121
}
122122

123+
[Fact]
124+
public void LengthTimesSpecificWeightEqualsPressure()
125+
{
126+
Pressure pressure = Length.FromMeters(2) * SpecificWeight.FromNewtonsPerCubicMeter(10);
127+
Assert.Equal(Pressure.FromPascals(20), pressure);
128+
}
129+
123130
[Fact]
124131
public void ToStringReturnsCorrectNumberAndUnitWithDefaultUnitWhichIsMeter()
125132
{

UnitsNet.Tests/CustomCode/PressureTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,12 @@ public void PressureTimesAreaEqualsForce()
114114
Force force = Pressure.FromPascals(20)*Area.FromSquareMeters(3);
115115
Assert.Equal(force, Force.FromNewtons(60));
116116
}
117+
118+
[Fact]
119+
public void PressureDividedBySpecificWeightEqualsLength()
120+
{
121+
Length length = Pressure.FromPascals(20) / SpecificWeight.FromNewtonsPerCubicMeter(2);
122+
Assert.Equal(Length.FromMeters(10), length);
123+
}
117124
}
118125
}

UnitsNet.Tests/CustomCode/SpecificWeightTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
using Xunit;
23+
2224
namespace UnitsNet.Tests.CustomCode
2325
{
2426
public class SpecificWeightTests : SpecificWeightTestsBase
@@ -56,5 +58,12 @@ public class SpecificWeightTests : SpecificWeightTestsBase
5658
protected override double TonnesForcePerCubicMillimeterInOneNewtonPerCubicMeter => 1.02e-10;
5759

5860
protected override double MeganewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1e-6;
61+
62+
[Fact]
63+
public void SpecificWeightTimesLengthEqualsPressure()
64+
{
65+
Pressure pressure = SpecificWeight.FromNewtonsPerCubicMeter(10) * Length.FromMeters(2);
66+
Assert.Equal(Pressure.FromPascals(20), pressure);
67+
}
5968
}
6069
}

UnitsNet/CustomCode/Quantities/Length.extra.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public static Length FromFeetInches(double feet, double inches)
111111
{
112112
return KinematicViscosity.FromSquareMetersPerSecond(length.Meters*speed.MetersPerSecond);
113113
}
114+
115+
public static Pressure operator *(Length length, SpecificWeight specificWeight)
116+
{
117+
return new Pressure(length.Meters * specificWeight.NewtonsPerCubicMeter, PressureUnit.Pascal);
118+
}
114119
#endif
115120
}
116121

@@ -142,4 +147,4 @@ public string ToString([CanBeNull] Culture cultureInfo)
142147
inchUnit);
143148
}
144149
}
145-
}
150+
}

UnitsNet/CustomCode/Quantities/Pressure.extra.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public partial struct Pressure
4242
{
4343
return Force.FromNewtons(pressure.Pascals * area.SquareMeters);
4444
}
45+
46+
public static Length operator /(Pressure pressure, SpecificWeight specificWeight)
47+
{
48+
return new Length(pressure.Pascals / specificWeight.NewtonsPerCubicMeter, UnitsNet.Units.LengthUnit.Meter);
49+
}
4550
#endif
4651
}
47-
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
namespace UnitsNet
23+
{
24+
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
25+
// Public structures can't have any members other than public fields, and those fields must be value types or strings.
26+
// Public classes must be sealed (NotInheritable in Visual Basic). If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic.
27+
#if WINDOWS_UWP
28+
public sealed partial class SpecificWeight
29+
#else
30+
public partial struct SpecificWeight
31+
#endif
32+
{
33+
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
34+
#if !WINDOWS_UWP
35+
public static Pressure operator *(SpecificWeight specificWeight, Length length)
36+
{
37+
return new Pressure(specificWeight.NewtonsPerCubicMeter * length.Meters, UnitsNet.Units.PressureUnit.Pascal);
38+
}
39+
#endif
40+
}
41+
}

0 commit comments

Comments
 (0)