Skip to content

Commit 3944464

Browse files
Linear density enhancements (#1251)
Added operator overloads for all relations between LinearDensity, Mass and Length, with unit tests. resolves #1250
1 parent 085aaac commit 3944464

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

UnitsNet.Tests/CustomCode/LinearDensityTests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,12 @@ public void LinearDensityDividedByDensityEqualsArea()
6363
Area area = LinearDensity.FromGramsPerCentimeter(10) / Density.FromGramsPerCubicCentimeter(2);
6464
Assert.Equal(5, area.SquareCentimeters);
6565
}
66+
67+
[Fact]
68+
public void LinearDensityTimesLengthEqualsMass()
69+
{
70+
Mass mass = LinearDensity.FromGramsPerCentimeter(10) * Length.FromCentimeters(2);
71+
Assert.Equal(20, mass.Grams);
72+
}
6673
}
6774
}

UnitsNet.Tests/CustomCode/MassTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ public void MassTimesAccelerationEqualsForce()
118118
Assert.Equal(force, Force.FromNewtons(54));
119119
}
120120

121+
[Fact]
122+
public void MassDividedByLengthEqualsLinearDensity()
123+
{
124+
LinearDensity linearDensity = Mass.FromKilograms(18) / Length.FromMeters(3);
125+
Assert.Equal(linearDensity, LinearDensity.FromKilogramsPerMeter(6));
126+
}
127+
128+
[Fact]
129+
public void MassDividedByLinearDensityEqualsLength()
130+
{
131+
Length length = Mass.FromKilograms(18) / LinearDensity.FromKilogramsPerMeter(3);
132+
Assert.Equal(length, Length.FromMeters(6));
133+
}
134+
121135
[Fact]
122136
public void NegativeMassToStonePoundsReturnsCorrectValues()
123137
{

UnitsNet/CustomCode/Quantities/LinearDensity.extra.cs

+6
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ public partial struct LinearDensity
1717
{
1818
return Area.FromSquareMeters(linearDensity.KilogramsPerMeter / density.KilogramsPerCubicMeter);
1919
}
20+
21+
/// <summary>Get <see cref="Mass"/> from <see cref="LinearDensity"/> times <see cref="Length"/>.</summary>
22+
public static Mass operator *(LinearDensity linearDensity, Length length)
23+
{
24+
return Mass.FromKilograms(linearDensity.KilogramsPerMeter * length.Meters);
25+
}
2026
}
2127
}

UnitsNet/CustomCode/Quantities/Mass.extra.cs

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ public static Mass FromStonePounds(double stone, double pounds)
9999
{
100100
return Force.FromNewtons(mass.Kilograms*acceleration.MetersPerSecondSquared);
101101
}
102+
103+
/// <summary>Get <see cref="LinearDensity"/> from <see cref="Mass"/> times <see cref="Length"/>.</summary>
104+
public static LinearDensity operator /(Mass mass, Length length)
105+
{
106+
return LinearDensity.FromKilogramsPerMeter(mass.Kilograms / length.Meters);
107+
}
108+
109+
/// <summary>Get <see cref="Length"/> from <see cref="Mass"/> divided by <see cref="LinearDensity"/>.</summary>
110+
public static Length operator /(Mass mass, LinearDensity linearDensity)
111+
{
112+
return Length.FromMeters(mass.Kilograms / linearDensity.KilogramsPerMeter);
113+
}
102114
}
103115

104116
/// <summary>

0 commit comments

Comments
 (0)