Skip to content

Commit 1e5394d

Browse files
gojanpaoloangularsen
authored andcommitted
Add operators for Volume = VolumeFlow/TimeSpan (or Duration) (#379)
1 parent a75b955 commit 1e5394d

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

UnitsNet.Tests/CustomCode/DurationTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,12 @@ public static void TimeSpanNotEqualToDurationShouldReturnCorrectly()
207207
Duration duration = Duration.FromHours(11);
208208
Assert.True(timeSpan != duration, "timeSpan should not be equal to duration");
209209
}
210+
211+
[Fact]
212+
public void DurationTimesVolumeFlowEqualsVolume()
213+
{
214+
Volume volume = Duration.FromSeconds(20) * VolumeFlow.FromCubicMetersPerSecond(2);
215+
Assert.Equal(Volume.FromCubicMeters(40), volume);
216+
}
210217
}
211218
}

UnitsNet.Tests/CustomCode/VolumeFlowTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4040
// THE SOFTWARE.
4141

42+
using System;
43+
using Xunit;
44+
4245
namespace UnitsNet.Tests.CustomCode
4346
{
4447
public class VolumeFlowTests : VolumeFlowTestsBase
@@ -88,5 +91,19 @@ public class VolumeFlowTests : VolumeFlowTestsBase
8891
protected override double UsGallonsPerHourInOneCubicMeterPerSecond => 9.510193884893328E5;
8992

9093
protected override double UsGallonsPerSecondInOneCubicMeterPerSecond => 2.64172052358148E2;
94+
95+
[Fact]
96+
public void VolumeFlowTimesTimeSpanEqualsVolume()
97+
{
98+
Volume volume = VolumeFlow.FromCubicMetersPerSecond(20) * TimeSpan.FromSeconds(2);
99+
Assert.Equal(Volume.FromCubicMeters(40), volume);
100+
}
101+
102+
[Fact]
103+
public void VolumeFlowTimesDurationEqualsVolume()
104+
{
105+
Volume volume = VolumeFlow.FromCubicMetersPerSecond(20) * Duration.FromSeconds(2);
106+
Assert.Equal(Volume.FromCubicMeters(40), volume);
107+
}
91108
}
92109
}

UnitsNet.Tests/CustomCode/VolumeTests.cs

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

22+
using System;
2223
using Xunit;
2324

2425
namespace UnitsNet.Tests.CustomCode
@@ -125,5 +126,26 @@ public void VolumeTimesDensityEqualsMass()
125126
Mass mass = Volume.FromCubicMeters(2)*Density.FromKilogramsPerCubicMeter(3);
126127
Assert.Equal(mass, Mass.FromKilograms(6));
127128
}
129+
130+
[Fact]
131+
public void VolumeDividedByTimeSpanEqualsVolumeFlow()
132+
{
133+
VolumeFlow volumeFlow = Volume.FromCubicMeters(20) / TimeSpan.FromSeconds(2);
134+
Assert.Equal(VolumeFlow.FromCubicMetersPerSecond(10), volumeFlow);
135+
}
136+
137+
[Fact]
138+
public void VolumeDividedByDurationEqualsVolumeFlow()
139+
{
140+
VolumeFlow volumeFlow = Volume.FromCubicMeters(20) / Duration.FromSeconds(2);
141+
Assert.Equal(VolumeFlow.FromCubicMetersPerSecond(10), volumeFlow);
142+
}
143+
144+
[Fact]
145+
public void VolumeDividedByVolumeFlowEqualsTimeSpan()
146+
{
147+
TimeSpan timeSpan = Volume.FromCubicMeters(20) / VolumeFlow.FromCubicMetersPerSecond(2);
148+
Assert.Equal(TimeSpan.FromSeconds(10), timeSpan);
149+
}
128150
}
129151
}

UnitsNet/CustomCode/Quantities/Duration.extra.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public static explicit operator Duration(TimeSpan duration)
113113
{
114114
return timeSpan.TotalSeconds != duration.Seconds;
115115
}
116+
117+
public static Volume operator *(Duration duration, VolumeFlow volumeFlow)
118+
{
119+
return Volume.FromCubicMeters(volumeFlow.CubicMetersPerSecond * duration.Seconds);
120+
}
116121
#endif
117122

118123
/// <summary>

UnitsNet/CustomCode/Quantities/Volume.extra.cs

Lines changed: 17 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 System;
23+
2224
namespace UnitsNet
2325
{
2426
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
@@ -41,6 +43,21 @@ public partial struct Volume
4143
{
4244
return Length.FromMeters(volume.CubicMeters / area.SquareMeters);
4345
}
46+
47+
public static VolumeFlow operator /(Volume volume, Duration duration)
48+
{
49+
return VolumeFlow.FromCubicMetersPerSecond(volume.CubicMeters / duration.Seconds);
50+
}
51+
52+
public static VolumeFlow operator /(Volume volume, TimeSpan timeSpan)
53+
{
54+
return VolumeFlow.FromCubicMetersPerSecond(volume.CubicMeters / timeSpan.Seconds);
55+
}
56+
57+
public static TimeSpan operator /(Volume volume, VolumeFlow volumeFlow)
58+
{
59+
return TimeSpan.FromSeconds(volume.CubicMeters / volumeFlow.CubicMetersPerSecond);
60+
}
4461
#endif
4562
}
4663
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
using System;
23+
24+
namespace UnitsNet
25+
{
26+
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
27+
// Public structures can't have any members other than public fields, and those fields must be value types or strings.
28+
// 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.
29+
#if WINDOWS_UWP
30+
public sealed partial class VolumeFlow
31+
#else
32+
public partial struct VolumeFlow
33+
#endif
34+
{
35+
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
36+
#if !WINDOWS_UWP
37+
public static Volume operator *(VolumeFlow volumeFlow, TimeSpan timeSpan)
38+
{
39+
return Volume.FromCubicMeters(volumeFlow.CubicMetersPerSecond * timeSpan.Seconds);
40+
}
41+
42+
public static Volume operator *(VolumeFlow volumeFlow, Duration duration)
43+
{
44+
return Volume.FromCubicMeters(volumeFlow.CubicMetersPerSecond * duration.Seconds);
45+
}
46+
#endif
47+
}
48+
}

0 commit comments

Comments
 (0)