Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in BaseDimensions.IsBaseQuantity() #1430

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions UnitsNet.Tests/BaseDimensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,53 @@ public void ConstructorImplementedCorrectly()
[InlineData(0, 0, 0, 0, 1, 0, 0)]
[InlineData(0, 0, 0, 0, 0, 1, 0)]
[InlineData(0, 0, 0, 0, 0, 0, 1)]
public void IsBaseQuantityImplementedProperly(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
public void IsBaseQuantity_ForBaseQuantity_ReturnsTrue(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
{
var baseDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
var derivedDimensions = new BaseDimensions(length * 2, mass * 2, time * 2, current * 2, temperature * 2, amount * 2, luminousIntensity * 2);

Assert.True(baseDimensions.IsBaseQuantity());
}

[Theory]
[InlineData(2, 0, 0, 0, 0, 0, 0)]
[InlineData(0, 2, 0, 0, 0, 0, 0)]
[InlineData(0, 0, 2, 0, 0, 0, 0)]
[InlineData(0, 0, 0, 2, 0, 0, 0)]
[InlineData(0, 0, 0, 0, 2, 0, 0)]
[InlineData(0, 0, 0, 0, 0, 2, 0)]
[InlineData(0, 0, 0, 0, 0, 0, 2)]
public void IsBaseQuantity_ForDerivedQuantity_ReturnsFalse(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
{
var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
Assert.False(derivedDimensions.IsBaseQuantity());
}

[Theory]
[InlineData(1, 1, 0, 0, 0, 0, 0)]
[InlineData(0, 2, 1, 0, 0, 0, 0)]
[InlineData(0, 2, 1, 1, 0, 0, 0)]
[InlineData(1, 2, 1, 1, 1, 1, 1)]
[InlineData(0, 0, 1, 2,-2, 0, 0)]
[InlineData(0, 0, 2,-1, 0, 0, 0)]
[InlineData(0, 0, 0,-3, 1, 0, 0)]
[InlineData(0, 0, 0, 0,-4,-4, 0)]
public void IsBaseQuantity_ForMultipleDimensions_ReturnsFalse(int length, int mass, int time, int current, int temperature, int amount, int luminousIntensity)
{
var derivedDimensions = new BaseDimensions(length, mass, time, current, temperature, amount, luminousIntensity);
Assert.False(derivedDimensions.IsBaseQuantity());
}

[Fact]
public void IsBaseQuantity_ForDimensionless_ReturnsFalse()
{
Assert.False(BaseDimensions.Dimensionless.IsBaseQuantity());
}

[Fact]
public void IsBaseQuantity_ForAcceleration_ReturnsFalse()
{
Assert.False(Acceleration.BaseDimensions.IsBaseQuantity());
angularsen marked this conversation as resolved.
Show resolved Hide resolved
}

[Theory]
[InlineData(2, 0, 0, 0, 0, 0, 0)]
[InlineData(0, 2, 0, 0, 0, 0, 0)]
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/BaseDimensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public BaseDimensions(int length, int mass, int time, int current, int temperatu
public bool IsBaseQuantity()
{
var dimensionsArray = new[] { Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity };
bool onlyOneEqualsOne = dimensionsArray.Where(dimension => dimension == 1).Take(2).Count() == 1;
bool onlyOneEqualsOne = dimensionsArray.Select(dimension => dimension is 0 or 1 ? dimension : 2).Sum() == 1;
return onlyOneEqualsOne;
}

Expand Down