Skip to content

Commit 86bfe3c

Browse files
Ferit Tunçerangularsen
authored andcommitted
Add quantity SpecificVolume (#351)
Add specific volume and conversions. Add operator overloads to Density.
1 parent 14c4022 commit 86bfe3c

File tree

10 files changed

+1142
-0
lines changed

10 files changed

+1142
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated (once) by \generate-code.bat, but will not be
4+
// regenerated when it already exists. The purpose of creating this file is to make
5+
// it easier to remember to implement all the unit conversion test cases.
6+
//
7+
// Whenever a new unit is added to this quantity and \generate-code.bat is run,
8+
// the base test class will get a new abstract property and cause a compile error
9+
// in this derived class, reminding the developer to implement the test case
10+
// for the new unit.
11+
//
12+
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
13+
//
14+
// Add CustomCode\Quantities\MyUnit.extra.cs files to add code to generated quantities.
15+
// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior.
16+
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities.
17+
//
18+
// </auto-generated>
19+
//------------------------------------------------------------------------------
20+
21+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
22+
// https://github.com/angularsen/UnitsNet
23+
//
24+
// Permission is hereby granted, free of charge, to any person obtaining a copy
25+
// of this software and associated documentation files (the "Software"), to deal
26+
// in the Software without restriction, including without limitation the rights
27+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
// copies of the Software, and to permit persons to whom the Software is
29+
// furnished to do so, subject to the following conditions:
30+
//
31+
// The above copyright notice and this permission notice shall be included in
32+
// all copies or substantial portions of the Software.
33+
//
34+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40+
// THE SOFTWARE.
41+
42+
43+
using System;
44+
using Xunit;
45+
46+
namespace UnitsNet.Tests.CustomCode
47+
{
48+
public class SpecificVolumeTests : SpecificVolumeTestsBase
49+
{
50+
51+
protected override double CubicMetersPerKilogramInOneCubicMeterPerKilogram => 1;
52+
53+
54+
[Fact]
55+
public static void SpecificVolumeTimesMassEqualsVolume()
56+
{
57+
Volume volume = SpecificVolume.FromCubicMetersPerKilogram(5) * Mass.FromKilograms(10);
58+
Assert.Equal(volume, Volume.FromCubicMeters(50));
59+
}
60+
61+
[Fact]
62+
public static void ConstantOverSpecificVolumeEqualsDensity()
63+
{
64+
Density density = 5 / SpecificVolume.FromCubicMetersPerKilogram(20);
65+
Assert.Equal(density, Density.FromKilogramsPerCubicMeter(0.25));
66+
}
67+
68+
69+
70+
}
71+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by \generate-code.bat.
4+
//
5+
// Changes to this file will be lost when the code is regenerated.
6+
// The build server regenerates the code before each build and a pre-build
7+
// step will regenerate the code on each local build.
8+
//
9+
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
10+
//
11+
// Add CustomCode\Quantities\MyUnit.extra.cs files to add code to generated quantities.
12+
// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior.
13+
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities.
14+
//
15+
// </auto-generated>
16+
//------------------------------------------------------------------------------
17+
18+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
19+
// https://github.com/angularsen/UnitsNet
20+
//
21+
// Permission is hereby granted, free of charge, to any person obtaining a copy
22+
// of this software and associated documentation files (the "Software"), to deal
23+
// in the Software without restriction, including without limitation the rights
24+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
// copies of the Software, and to permit persons to whom the Software is
26+
// furnished to do so, subject to the following conditions:
27+
//
28+
// The above copyright notice and this permission notice shall be included in
29+
// all copies or substantial portions of the Software.
30+
//
31+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37+
// THE SOFTWARE.
38+
39+
using System;
40+
using UnitsNet.Units;
41+
using Xunit;
42+
43+
// Disable build warning CS1718: Comparison made to same variable; did you mean to compare something else?
44+
#pragma warning disable 1718
45+
46+
// ReSharper disable once CheckNamespace
47+
namespace UnitsNet.Tests
48+
{
49+
/// <summary>
50+
/// Test of SpecificVolume.
51+
/// </summary>
52+
// ReSharper disable once PartialTypeWithSinglePart
53+
public abstract partial class SpecificVolumeTestsBase
54+
{
55+
protected abstract double CubicMetersPerKilogramInOneCubicMeterPerKilogram { get; }
56+
57+
// ReSharper disable VirtualMemberNeverOverriden.Global
58+
protected virtual double CubicMetersPerKilogramTolerance { get { return 1e-5; } }
59+
// ReSharper restore VirtualMemberNeverOverriden.Global
60+
61+
[Fact]
62+
public void CubicMeterPerKilogramToSpecificVolumeUnits()
63+
{
64+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
65+
AssertEx.EqualTolerance(CubicMetersPerKilogramInOneCubicMeterPerKilogram, cubicmeterperkilogram.CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
66+
}
67+
68+
[Fact]
69+
public void FromValueAndUnit()
70+
{
71+
AssertEx.EqualTolerance(1, SpecificVolume.From(1, SpecificVolumeUnit.CubicMeterPerKilogram).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
72+
}
73+
74+
[Fact]
75+
public void As()
76+
{
77+
var cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
78+
AssertEx.EqualTolerance(CubicMetersPerKilogramInOneCubicMeterPerKilogram, cubicmeterperkilogram.As(SpecificVolumeUnit.CubicMeterPerKilogram), CubicMetersPerKilogramTolerance);
79+
}
80+
81+
[Fact]
82+
public void ConversionRoundTrip()
83+
{
84+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
85+
AssertEx.EqualTolerance(1, SpecificVolume.FromCubicMetersPerKilogram(cubicmeterperkilogram.CubicMetersPerKilogram).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
86+
}
87+
88+
[Fact]
89+
public void ArithmeticOperators()
90+
{
91+
SpecificVolume v = SpecificVolume.FromCubicMetersPerKilogram(1);
92+
AssertEx.EqualTolerance(-1, -v.CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
93+
AssertEx.EqualTolerance(2, (SpecificVolume.FromCubicMetersPerKilogram(3)-v).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
94+
AssertEx.EqualTolerance(2, (v + v).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
95+
AssertEx.EqualTolerance(10, (v*10).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
96+
AssertEx.EqualTolerance(10, (10*v).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
97+
AssertEx.EqualTolerance(2, (SpecificVolume.FromCubicMetersPerKilogram(10)/5).CubicMetersPerKilogram, CubicMetersPerKilogramTolerance);
98+
AssertEx.EqualTolerance(2, SpecificVolume.FromCubicMetersPerKilogram(10)/SpecificVolume.FromCubicMetersPerKilogram(5), CubicMetersPerKilogramTolerance);
99+
}
100+
101+
[Fact]
102+
public void ComparisonOperators()
103+
{
104+
SpecificVolume oneCubicMeterPerKilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
105+
SpecificVolume twoCubicMetersPerKilogram = SpecificVolume.FromCubicMetersPerKilogram(2);
106+
107+
Assert.True(oneCubicMeterPerKilogram < twoCubicMetersPerKilogram);
108+
Assert.True(oneCubicMeterPerKilogram <= twoCubicMetersPerKilogram);
109+
Assert.True(twoCubicMetersPerKilogram > oneCubicMeterPerKilogram);
110+
Assert.True(twoCubicMetersPerKilogram >= oneCubicMeterPerKilogram);
111+
112+
Assert.False(oneCubicMeterPerKilogram > twoCubicMetersPerKilogram);
113+
Assert.False(oneCubicMeterPerKilogram >= twoCubicMetersPerKilogram);
114+
Assert.False(twoCubicMetersPerKilogram < oneCubicMeterPerKilogram);
115+
Assert.False(twoCubicMetersPerKilogram <= oneCubicMeterPerKilogram);
116+
}
117+
118+
[Fact]
119+
public void CompareToIsImplemented()
120+
{
121+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
122+
Assert.Equal(0, cubicmeterperkilogram.CompareTo(cubicmeterperkilogram));
123+
Assert.True(cubicmeterperkilogram.CompareTo(SpecificVolume.Zero) > 0);
124+
Assert.True(SpecificVolume.Zero.CompareTo(cubicmeterperkilogram) < 0);
125+
}
126+
127+
[Fact]
128+
public void CompareToThrowsOnTypeMismatch()
129+
{
130+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
131+
Assert.Throws<ArgumentException>(() => cubicmeterperkilogram.CompareTo(new object()));
132+
}
133+
134+
[Fact]
135+
public void CompareToThrowsOnNull()
136+
{
137+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
138+
Assert.Throws<ArgumentNullException>(() => cubicmeterperkilogram.CompareTo(null));
139+
}
140+
141+
142+
[Fact]
143+
public void EqualityOperators()
144+
{
145+
SpecificVolume a = SpecificVolume.FromCubicMetersPerKilogram(1);
146+
SpecificVolume b = SpecificVolume.FromCubicMetersPerKilogram(2);
147+
148+
// ReSharper disable EqualExpressionComparison
149+
Assert.True(a == a);
150+
Assert.True(a != b);
151+
152+
Assert.False(a == b);
153+
Assert.False(a != a);
154+
// ReSharper restore EqualExpressionComparison
155+
}
156+
157+
[Fact]
158+
public void EqualsIsImplemented()
159+
{
160+
SpecificVolume v = SpecificVolume.FromCubicMetersPerKilogram(1);
161+
Assert.True(v.Equals(SpecificVolume.FromCubicMetersPerKilogram(1), SpecificVolume.FromCubicMetersPerKilogram(CubicMetersPerKilogramTolerance)));
162+
Assert.False(v.Equals(SpecificVolume.Zero, SpecificVolume.FromCubicMetersPerKilogram(CubicMetersPerKilogramTolerance)));
163+
}
164+
165+
[Fact]
166+
public void EqualsReturnsFalseOnTypeMismatch()
167+
{
168+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
169+
Assert.False(cubicmeterperkilogram.Equals(new object()));
170+
}
171+
172+
[Fact]
173+
public void EqualsReturnsFalseOnNull()
174+
{
175+
SpecificVolume cubicmeterperkilogram = SpecificVolume.FromCubicMetersPerKilogram(1);
176+
Assert.False(cubicmeterperkilogram.Equals(null));
177+
}
178+
}
179+
}

UnitsNet.sln.DotSettings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Full/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Full"&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_BOTH&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_EXPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CSReorderTypeMembers&gt;True&lt;/CSReorderTypeMembers&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;XMLReformatCode&gt;True&lt;/XMLReformatCode&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="True" ArrangeBraces="False" ArrangeAttributes="True" ArrangeArgumentsStyle="True" /&gt;&lt;CSEnforceVarKeywordUsageSettings&gt;True&lt;/CSEnforceVarKeywordUsageSettings&gt;&lt;CSMakeAutoPropertyGetOnly&gt;True&lt;/CSMakeAutoPropertyGetOnly&gt;&lt;CSArrangeQualifiers&gt;True&lt;/CSArrangeQualifiers&gt;&lt;CSFixBuiltinTypeReferences&gt;True&lt;/CSFixBuiltinTypeReferences&gt;&lt;/Profile&gt;</s:String>
55
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">Full</s:String>
6+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
67
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">160</s:Int64>
78
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForBuiltInTypes/@EntryValue">UseVarWhenEvident</s:String>
89
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseVarWhenEvident</s:String>
@@ -27,6 +28,11 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&#xD;
2728
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&#xD;
2829
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN&#xD;
2930
THE SOFTWARE.</s:String>
31+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
32+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
33+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
34+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
3035
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
36+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
3137
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
3238
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 SpecificVolume
31+
#else
32+
public partial struct SpecificVolume
33+
#endif
34+
{
35+
36+
#if !WINDOWS_UWP
37+
public static Density operator /(double constant, SpecificVolume volume)
38+
{
39+
return Density.FromKilogramsPerCubicMeter(constant / volume.CubicMetersPerKilogram);
40+
}
41+
42+
public static Volume operator *(SpecificVolume volume, Mass mass)
43+
{
44+
return Volume.FromCubicMeters(volume.CubicMetersPerKilogram * mass.Kilograms);
45+
}
46+
47+
#endif
48+
}
49+
}

0 commit comments

Comments
 (0)