Skip to content

Commit e32a276

Browse files
ygorshkovygorshkov
andauthored
Fix a bug in BaseDimensions.IsBaseQuantity() (#1430)
Current implementation of `BaseDimensions.IsBaseQuantity()` ignores any dimension which power is not `1`. So it treats derived quantities (like _Acceleration_ shown in unit test) as base ones. --------- Co-authored-by: ygorshkov <yury.gorshkov@wartsila.com>
1 parent b57ac63 commit e32a276

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

UnitsNet.Tests/BaseDimensionsTests.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,53 @@ public void ConstructorImplementedCorrectly()
2828
[InlineData(0, 0, 0, 0, 1, 0, 0)]
2929
[InlineData(0, 0, 0, 0, 0, 1, 0)]
3030
[InlineData(0, 0, 0, 0, 0, 0, 1)]
31-
public void IsBaseQuantityImplementedProperly(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
31+
public void IsBaseQuantity_ForBaseQuantity_ReturnsTrue(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
3232
{
3333
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-
3634
Assert.True(baseDimensions.IsBaseQuantity());
35+
}
36+
37+
[Theory]
38+
[InlineData(2, 0, 0, 0, 0, 0, 0)]
39+
[InlineData(0, 2, 0, 0, 0, 0, 0)]
40+
[InlineData(0, 0, 2, 0, 0, 0, 0)]
41+
[InlineData(0, 0, 0, 2, 0, 0, 0)]
42+
[InlineData(0, 0, 0, 0, 2, 0, 0)]
43+
[InlineData(0, 0, 0, 0, 0, 2, 0)]
44+
[InlineData(0, 0, 0, 0, 0, 0, 2)]
45+
public void IsBaseQuantity_ForDerivedQuantity_ReturnsFalse(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
46+
{
47+
var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
48+
Assert.False(derivedDimensions.IsBaseQuantity());
49+
}
50+
51+
[Theory]
52+
[InlineData(1, 1, 0, 0, 0, 0, 0)]
53+
[InlineData(0, 2, 1, 0, 0, 0, 0)]
54+
[InlineData(0, 2, 1, 1, 0, 0, 0)]
55+
[InlineData(1, 2, 1, 1, 1, 1, 1)]
56+
[InlineData(0, 0, 1, 2,-2, 0, 0)]
57+
[InlineData(0, 0, 2,-1, 0, 0, 0)]
58+
[InlineData(0, 0, 0,-3, 1, 0, 0)]
59+
[InlineData(0, 0, 0, 0,-4,-4, 0)]
60+
public void IsBaseQuantity_ForMultipleDimensions_ReturnsFalse(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
61+
{
62+
var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
3763
Assert.False(derivedDimensions.IsBaseQuantity());
3864
}
3965

66+
[Fact]
67+
public void IsBaseQuantity_ForDimensionless_ReturnsFalse()
68+
{
69+
Assert.False(BaseDimensions.Dimensionless.IsBaseQuantity());
70+
}
71+
72+
[Fact]
73+
public void IsBaseQuantity_ForAcceleration_ReturnsFalse()
74+
{
75+
Assert.False(Acceleration.BaseDimensions.IsBaseQuantity());
76+
}
77+
4078
[Theory]
4179
[InlineData(2, 0, 0, 0, 0, 0, 0)]
4280
[InlineData(0, 2, 0, 0, 0, 0, 0)]

UnitsNet/BaseDimensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu
3131
public bool IsBaseQuantity()
3232
{
3333
var dimensionsArray = new[] { Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity };
34-
bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1;
34+
bool onlyOneEqualsOne = dimensionsArray.Select(dimension => dimension is 0 or 1 ? dimension : 2).Sum() == 1;
3535
return onlyOneEqualsOne;
3636
}
3737

0 commit comments

Comments
 (0)