Skip to content

Commit 1e276bd

Browse files
committed
Added operator overloads b/w Density, SpecificWeight, & Acceleration
Density = SpecificWeight / Acceleration
1 parent 6624735 commit 1e276bd

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
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/SpecificWeightTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,19 @@ public void SpecificWeightTimesLengthEqualsPressure()
6565
Pressure pressure = SpecificWeight.FromNewtonsPerCubicMeter(10) * Length.FromMeters(2);
6666
Assert.Equal(Pressure.FromPascals(20), pressure);
6767
}
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+
}
6882
}
6983
}
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/SpecificWeight.extra.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public partial struct SpecificWeight
3636
{
3737
return new Pressure(specificWeight.NewtonsPerCubicMeter * length.Meters, UnitsNet.Units.PressureUnit.Pascal);
3838
}
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+
}
3949
#endif
4050
}
4151
}

0 commit comments

Comments
 (0)