From ab1bc7ce230c40dbc848dbc78b7c858e1fae569c Mon Sep 17 00:00:00 2001 From: ygorshkov Date: Wed, 23 Oct 2024 17:09:04 +0200 Subject: [PATCH 1/4] + BaseDimensions.IsBaseQuantity unit test --- UnitsNet.Tests/BaseDimensionsTests.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index 9923cf59c6..d608257437 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -37,6 +37,12 @@ public void IsBaseQuantityImplementedProperly(int length, int mass, int time, in Assert.False(derivedDimensions.IsBaseQuantity()); } + [Fact] + public void IsBaseQuantityImplementedReallyProperly() + { + Assert.False(Acceleration.BaseDimensions.IsBaseQuantity()); + } + [Theory] [InlineData(2, 0, 0, 0, 0, 0, 0)] [InlineData(0, 2, 0, 0, 0, 0, 0)] From e737ef1d5af6aee1d3eefb53b7120a16ac7ce4f8 Mon Sep 17 00:00:00 2001 From: ygorshkov Date: Wed, 23 Oct 2024 17:31:27 +0200 Subject: [PATCH 2/4] * fix BaseDimensions.IsBaseQuantity implementation --- UnitsNet/BaseDimensions.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index 26b31de6fc..faa9f28c24 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -31,7 +31,22 @@ 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 = false; + foreach (var dimension in dimensionsArray) + { + if (1 == dimension) + { + if (onlyOneEqualsOne) + { + return false; + } + onlyOneEqualsOne = true; + } + else if (0 != dimension) + { + return false; + } + } return onlyOneEqualsOne; } From 6f9382b5cf0531ac1ff2ee55a7593efbd6491634 Mon Sep 17 00:00:00 2001 From: ygorshkov Date: Wed, 23 Oct 2024 21:09:06 +0200 Subject: [PATCH 3/4] * fix BaseDimensions.IsBaseQuantity with single LINQ --- UnitsNet/BaseDimensions.cs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/UnitsNet/BaseDimensions.cs b/UnitsNet/BaseDimensions.cs index faa9f28c24..b229b21501 100644 --- a/UnitsNet/BaseDimensions.cs +++ b/UnitsNet/BaseDimensions.cs @@ -31,22 +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 = false; - foreach (var dimension in dimensionsArray) - { - if (1 == dimension) - { - if (onlyOneEqualsOne) - { - return false; - } - onlyOneEqualsOne = true; - } - else if (0 != dimension) - { - return false; - } - } + bool onlyOneEqualsOne = dimensionsArray.Select(dimension => dimension is 0 or 1 ? dimension : 2).Sum() == 1; return onlyOneEqualsOne; } From 9d20fd8016296bf00aa63a26d84e3c5f0268381b Mon Sep 17 00:00:00 2001 From: ygorshkov Date: Fri, 25 Oct 2024 11:43:14 +0200 Subject: [PATCH 4/4] * IsBaseDimension unit tests --- UnitsNet.Tests/BaseDimensionsTests.cs | 40 ++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/UnitsNet.Tests/BaseDimensionsTests.cs b/UnitsNet.Tests/BaseDimensionsTests.cs index d608257437..c033c260e6 100644 --- a/UnitsNet.Tests/BaseDimensionsTests.cs +++ b/UnitsNet.Tests/BaseDimensionsTests.cs @@ -28,17 +28,49 @@ 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 IsBaseQuantityImplementedReallyProperly() + public void IsBaseQuantity_ForDimensionless_ReturnsFalse() + { + Assert.False(BaseDimensions.Dimensionless.IsBaseQuantity()); + } + + [Fact] + public void IsBaseQuantity_ForAcceleration_ReturnsFalse() { Assert.False(Acceleration.BaseDimensions.IsBaseQuantity()); }