Skip to content

Commit 04679b2

Browse files
authored
Merge pull request #445 from gojanpaolo/add-operator-overloads
Add operator overloads for SpecificWeight, Density, Length, Acceleratino and Pressure
2 parents 52d6109 + 1e276bd commit 04679b2

File tree

10 files changed

+175
-3
lines changed

10 files changed

+175
-3
lines changed

UnitsNet.Tests/CustomCode/AccelerationTests.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 AccelerationTests : AccelerationTestsBase
@@ -48,5 +50,12 @@ public class AccelerationTests : AccelerationTestsBase
4850
protected override double KnotsPerMinuteInOneMeterPerSecondSquared => 1.16630669546436E2;
4951

5052
protected override double KnotsPerSecondInOneMeterPerSecondSquared => 1.94384449244060;
53+
54+
[Fact]
55+
public void AccelerationTimesDensityEqualsSpecificWeight()
56+
{
57+
SpecificWeight specificWeight = Acceleration.FromMetersPerSecondSquared(10) * Density.FromKilogramsPerCubicMeter(2);
58+
Assert.Equal(SpecificWeight.FromNewtonsPerCubicMeter(20), specificWeight);
59+
}
5160
}
5261
}

UnitsNet.Tests/CustomCode/DensityTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,12 @@ public void DensityTimesSpeedEqualsMassFlux()
148148
MassFlux massFlux = Density.FromKilogramsPerCubicMeter(20) * Speed.FromMetersPerSecond(2);
149149
Assert.Equal(massFlux, MassFlux.FromKilogramsPerSecondPerSquareMeter(40));
150150
}
151+
152+
[Fact]
153+
public void DensityTimesAccelerationEqualsSpecificWeight()
154+
{
155+
SpecificWeight specificWeight = Density.FromKilogramsPerCubicMeter(10) * Acceleration.FromMetersPerSecondSquared(2);
156+
Assert.Equal(SpecificWeight.FromNewtonsPerCubicMeter(20), specificWeight);
157+
}
151158
}
152159
}

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,19 @@ 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+
}
124+
125+
[Fact]
126+
public void PressureDividedByLengthEqualsSpecificWeight()
127+
{
128+
SpecificWeight specificWeight = Pressure.FromPascals(20) / Length.FromMeters(2);
129+
Assert.Equal(SpecificWeight.FromNewtonsPerCubicMeter(10), specificWeight);
130+
}
117131
}
118132
}

UnitsNet.Tests/CustomCode/SpecificWeightTests.cs

Lines changed: 23 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,26 @@ 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+
}
68+
69+
[Fact]
70+
public void SpecificWeightDividedByDensityEqualsAcceleration()
71+
{
72+
Acceleration acceleration = SpecificWeight.FromNewtonsPerCubicMeter(40) / Density.FromKilogramsPerCubicMeter(4);
73+
Assert.Equal(Acceleration.FromMetersPerSecondSquared(10), acceleration);
74+
}
75+
76+
[Fact]
77+
public void SpecificWeightDividedByAccelerationEqualsDensity()
78+
{
79+
Density density = SpecificWeight.FromNewtonsPerCubicMeter(20) / Acceleration.FromMetersPerSecondSquared(2);
80+
Assert.Equal(Density.FromKilogramsPerCubicMeter(10), density);
81+
}
5982
}
6083
}
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 Acceleration
29+
#else
30+
public partial struct Acceleration
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 SpecificWeight operator *(Acceleration acceleration, Density density)
36+
{
37+
return new SpecificWeight(acceleration.MetersPerSecondSquared * density.KilogramsPerCubicMeter, UnitsNet.Units.SpecificWeightUnit.NewtonPerCubicMeter);
38+
}
39+
#endif
40+
}
41+
}

UnitsNet/CustomCode/Quantities/Density.extra.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public static Molarity ToMolarity(Density density, Mass molecularWeight)
6666
{
6767
return MassFlux.FromKilogramsPerSecondPerSquareMeter(density.KilogramsPerCubicMeter * speed.MetersPerSecond);
6868
}
69+
70+
public static SpecificWeight operator *(Density density, Acceleration acceleration)
71+
{
72+
return new SpecificWeight(density.KilogramsPerCubicMeter * acceleration.MetersPerSecondSquared, UnitsNet.Units.SpecificWeightUnit.NewtonPerCubicMeter);
73+
}
6974
#endif
7075
}
71-
}
76+
}

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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ 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+
}
50+
51+
public static SpecificWeight operator /(Pressure pressure, Length length)
52+
{
53+
return new SpecificWeight(pressure.Pascals / length.Meters, UnitsNet.Units.SpecificWeightUnit.NewtonPerCubicMeter);
54+
}
4555
#endif
4656
}
47-
}
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
40+
public static Acceleration operator /(SpecificWeight specificWeight, Density density)
41+
{
42+
return new Acceleration(specificWeight.NewtonsPerCubicMeter / density.KilogramsPerCubicMeter, UnitsNet.Units.AccelerationUnit.MeterPerSecondSquared);
43+
}
44+
45+
public static Density operator /(SpecificWeight specific, Acceleration acceleration)
46+
{
47+
return new Density(specific.NewtonsPerCubicMeter / acceleration.MetersPerSecondSquared, UnitsNet.Units.DensityUnit.KilogramPerCubicMeter);
48+
}
49+
#endif
50+
}
51+
}

0 commit comments

Comments
 (0)