Skip to content

Commit 5f625cb

Browse files
committed
Implementing dimensionless base dimension
1 parent 1c02a3d commit 5f625cb

8 files changed

+115
-7
lines changed

UnitsNet.Tests/BaseDimensionsTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,56 @@ namespace UnitsNet.Tests
66
{
77
public class BaseDimensionsTests
88
{
9+
[Fact]
10+
public void ConstructorImplementedCorrectly()
11+
{
12+
var baseDimensions = new BaseDimensions(1, 2, 3, 4, 5, 6, 7);
13+
14+
Assert.True(baseDimensions.Length == 1);
15+
Assert.True(baseDimensions.Mass == 2);
16+
Assert.True(baseDimensions.Time == 3);
17+
Assert.True(baseDimensions.Current == 4);
18+
Assert.True(baseDimensions.Temperature == 5);
19+
Assert.True(baseDimensions.Amount == 6);
20+
Assert.True(baseDimensions.LuminousIntensity == 7);
21+
}
22+
23+
[Theory]
24+
[InlineData(1, 0, 0, 0, 0, 0, 0)]
25+
[InlineData(0, 1, 0, 0, 0, 0, 0)]
26+
[InlineData(0, 0, 1, 0, 0, 0, 0)]
27+
[InlineData(0, 0, 0, 1, 0, 0, 0)]
28+
[InlineData(0, 0, 0, 0, 1, 0, 0)]
29+
[InlineData(0, 0, 0, 0, 0, 1, 0)]
30+
[InlineData(0, 0, 0, 0, 0, 0, 1)]
31+
public void IsBaseImplementedProperly(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
32+
{
33+
var baseDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
34+
var derivedDimensions = new BaseDimensions(length * 2, mass * 2, time * 2, current * 2, temperature * 2, amount * 2, luminousIntensity * 2);
35+
36+
Assert.True(baseDimensions.IsBase());
37+
Assert.False(derivedDimensions.IsBase());
38+
Assert.False(BaseDimensions.Dimensionless.IsBase());
39+
}
40+
41+
[Theory]
42+
[InlineData(2, 0, 0, 0, 0, 0, 0)]
43+
[InlineData(0, 2, 0, 0, 0, 0, 0)]
44+
[InlineData(0, 0, 2, 0, 0, 0, 0)]
45+
[InlineData(0, 0, 0, 2, 0, 0, 0)]
46+
[InlineData(0, 0, 0, 0, 2, 0, 0)]
47+
[InlineData(0, 0, 0, 0, 0, 2, 0)]
48+
[InlineData(0, 0, 0, 0, 0, 0, 2)]
49+
public void IsDerivedImplementedSuccessfully(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
50+
{
51+
var baseDimensions = new BaseDimensions(length / 2, mass / 2, time / 2, current / 2, temperature / 2, amount / 2, luminousIntensity / 2);
52+
var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
53+
54+
Assert.False(baseDimensions.IsDerived());
55+
Assert.True(derivedDimensions.IsDerived());
56+
Assert.False(BaseDimensions.Dimensionless.IsDerived());
57+
}
58+
959
[Fact]
1060
public void EqualsWorksAsExpected()
1161
{
@@ -663,5 +713,21 @@ public void GetHashCodeWorksProperly()
663713
Assert.True(baseDimensions1.GetHashCode() != baseDimensions2.GetHashCode());
664714
Assert.True(baseDimensions1.GetHashCode() == baseDimensions3.GetHashCode());
665715
}
716+
717+
[Fact]
718+
public void DimensionLessPropertyIsCorrect()
719+
{
720+
Assert.True(BaseDimensions.Dimensionless == new BaseDimensions(0, 0, 0, 0, 0, 0, 0));
721+
}
722+
723+
[Fact]
724+
public void IsDimensionLessMethodImplementedCorrectly()
725+
{
726+
Assert.True(BaseDimensions.Dimensionless.IsDimensionless());
727+
Assert.True(new BaseDimensions(0, 0, 0, 0, 0, 0, 0).IsDimensionless());
728+
729+
// Example case
730+
Assert.True(Level.BaseDimensions.IsDimensionless());
731+
}
666732
}
667733
}

UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public sealed partial class Information : IQuantity
6666

6767
static Information()
6868
{
69-
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
69+
BaseDimensions = BaseDimensions.Dimensionless;
7070
}
7171
/// <summary>
7272
/// Creates the quantity with a value of 0 in the base unit Bit.

UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public sealed partial class Level : IQuantity
6666

6767
static Level()
6868
{
69-
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
69+
BaseDimensions = BaseDimensions.Dimensionless;
7070
}
7171
/// <summary>
7272
/// Creates the quantity with a value of 0 in the base unit Decibel.

UnitsNet/BaseDimensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu
4141
LuminousIntensity = luminousIntensity;
4242
}
4343

44+
/// <summary>
45+
/// Checks if this base dimensions object represents a base quantity.
46+
/// </summary>
47+
/// <returns>True if this object represents a base quantity, otherwise false.</returns>
48+
public bool IsBase()
49+
{
50+
var dimensionsArray = new int[]{Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity};
51+
bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1;
52+
return onlyOneEqualsOne;
53+
}
54+
55+
/// <summary>
56+
/// Checks if this base dimensions object represents a derived quantity.
57+
/// </summary>
58+
/// <returns>True if this object represents a derived quantity, otherwise false.</returns>
59+
public bool IsDerived()
60+
{
61+
return !IsBase() && !IsDimensionless();
62+
}
63+
64+
/// <summary>
65+
/// Checks if this base dimensions object represents a dimensionless quantity.
66+
/// </summary>
67+
/// <returns>True if this object represents a dimensionless quantity, otherwise false.</returns>
68+
public bool IsDimensionless()
69+
{
70+
return this == Dimensionless;
71+
}
72+
4473
/// <inheritdoc />
4574
public override bool Equals(object obj)
4675
{
@@ -223,5 +252,7 @@ private static void AppendDimensionString(StringBuilder sb, string name, int val
223252
/// Gets the luminous intensity dimensions (J).
224253
/// </summary>
225254
public int LuminousIntensity{ get; }
255+
256+
public static BaseDimensions Dimensionless { get; } = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
226257
}
227258
}

UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public partial struct Information : IQuantity<InformationUnit>, IComparable, ICo
6363

6464
static Information()
6565
{
66-
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
66+
BaseDimensions = BaseDimensions.Dimensionless;
6767
}
6868

6969
/// <summary>

UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public partial struct Level : IQuantity<LevelUnit>, IComparable, IComparable<Lev
6363

6464
static Level()
6565
{
66-
BaseDimensions = new BaseDimensions(0, 0, 0, 0, 0, 0, 0);
66+
BaseDimensions = BaseDimensions.Dimensionless;
6767
}
6868

6969
/// <summary>

UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,15 @@ if ($obsoleteAttribute)
112112
113113
static $quantityName()
114114
{
115-
"@; if($baseDimensions)
115+
"@; if($baseDimensions -eq $null -or ( $baseDimensions.Length -eq 0 -and $baseDimensions.Mass -eq 0 -and $baseDimensions.Time -eq 0 -and $baseDimensions.ElectricCurrent -eq 0 -and $baseDimensions.Temperature -eq 0 -and $baseDimensions.AmountOfSubstance -eq 0 -and $baseDimensions.LuminousIntensity -eq 0 ) )
116+
{@"
117+
BaseDimensions = BaseDimensions.Dimensionless;
118+
"@; }
119+
else
116120
{@"
117121
BaseDimensions = new BaseDimensions($($baseDimensions.Length), $($baseDimensions.Mass), $($baseDimensions.Time), $($baseDimensions.ElectricCurrent), $($baseDimensions.Temperature), $($baseDimensions.AmountOfSubstance), $($baseDimensions.LuminousIntensity));
118-
"@; }@"
122+
"@; }
123+
@"
119124
}
120125
"@; # Windows Runtime Component requires a default constructor
121126
if ($wrc) {@"

UnitsNet/Scripts/Include-GenerateUnitTestBaseClassSourceCode.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ namespace UnitsNet.Tests
264264
}
265265
266266
[Fact]
267-
public void AllUnitsHaveAtLeastOneAbbreviationSpecified()
267+
public void HasAtLeastOneAbbreviationSpecified()
268268
{
269269
var units = Enum.GetValues(typeof($unitEnumName)).Cast<$unitEnumName>();
270270
foreach(var unit in units)
@@ -275,6 +275,12 @@ namespace UnitsNet.Tests
275275
var defaultAbbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit);
276276
}
277277
}
278+
279+
[Fact]
280+
public void BaseDimensionsShouldNeverBeNull()
281+
{
282+
Assert.False($quantityName.BaseDimensions is null);
283+
}
278284
}
279285
}
280286
"@;

0 commit comments

Comments
 (0)