From 70d1105fff476bd2d64c52a863870284f9a62376 Mon Sep 17 00:00:00 2001 From: UrielZyx Date: Sun, 3 Mar 2024 00:08:23 +0200 Subject: [PATCH 1/2] Add type safety and improve type inference in UnitsMath.cs --- UnitsNet/UnitMath.cs | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/UnitsNet/UnitMath.cs b/UnitsNet/UnitMath.cs index 472f25c9b7..22c8ee1915 100644 --- a/UnitsNet/UnitMath.cs +++ b/UnitsNet/UnitMath.cs @@ -30,8 +30,9 @@ public static TQuantity Abs(this TQuantity value) where TQuantity : I /// /// source contains quantity types different from . /// - public static TQuantity Sum(this IEnumerable source, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Sum(this IEnumerable source, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return (TQuantity) Quantity.From(source.Sum(x => x.As(unitType)), unitType); } @@ -44,7 +45,8 @@ public static TQuantity Sum(this IEnumerable source, Enum /// A transform function to apply to each element. /// The desired unit type for the resulting quantity /// The type of the elements of source. - /// The type of quantity that is produced by this operation + /// The type of quantity that is produced by this operation. + /// The type of unit enum. /// The sum of the projected values, represented in the specified unit type. /// /// source or selector is null. @@ -52,8 +54,9 @@ public static TQuantity Sum(this IEnumerable source, Enum /// /// source contains quantity types different from . /// - public static TQuantity Sum(this IEnumerable source, Func selector, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Sum(this IEnumerable source, Func selector, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return source.Select(selector).Sum(unitType); } @@ -79,8 +82,9 @@ public static TQuantity Min(TQuantity val1, TQuantity val2) where TQu /// /// source contains quantity types different from . /// - public static TQuantity Min(this IEnumerable source, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Min(this IEnumerable source, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return (TQuantity) Quantity.From(source.Min(x => x.As(unitType)), unitType); } @@ -93,7 +97,8 @@ public static TQuantity Min(this IEnumerable source, Enum /// A transform function to apply to each element. /// The desired unit type for the resulting quantity /// The type of the elements of source. - /// The type of quantity that is produced by this operation + /// The type of quantity that is produced by this operation. + /// The type of unit enum. /// The min of the projected values, represented in the specified unit type. /// /// source or selector is null. @@ -102,8 +107,9 @@ public static TQuantity Min(this IEnumerable source, Enum /// /// source contains quantity types different from . /// - public static TQuantity Min(this IEnumerable source, Func selector, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Min(this IEnumerable source, Func selector, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return source.Select(selector).Min(unitType); } @@ -129,8 +135,9 @@ public static TQuantity Max(TQuantity val1, TQuantity val2) where TQu /// /// source contains quantity types different from . /// - public static TQuantity Max(this IEnumerable source, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Max(this IEnumerable source, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return (TQuantity) Quantity.From(source.Max(x => x.As(unitType)), unitType); } @@ -143,7 +150,8 @@ public static TQuantity Max(this IEnumerable source, Enum /// A transform function to apply to each element. /// The desired unit type for the resulting quantity /// The type of the elements of source. - /// The type of quantity that is produced by this operation + /// The type of quantity that is produced by this operation. + /// The type of unit enum. /// The max of the projected values, represented in the specified unit type. /// /// source or selector is null. @@ -152,8 +160,9 @@ public static TQuantity Max(this IEnumerable source, Enum /// /// source contains quantity types different from . /// - public static TQuantity Max(this IEnumerable source, Func selector, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Max(this IEnumerable source, Func selector, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return source.Select(selector).Max(unitType); } @@ -169,8 +178,9 @@ public static TQuantity Max(this IEnumerable source /// /// source contains quantity types different from . /// - public static TQuantity Average(this IEnumerable source, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Average(this IEnumerable source, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return (TQuantity) Quantity.From(source.Average(x => x.As(unitType)), unitType); } @@ -183,7 +193,8 @@ public static TQuantity Average(this IEnumerable source, E /// A transform function to apply to each element. /// The desired unit type for the resulting quantity /// The type of the elements of source. - /// The type of quantity that is produced by this operation + /// The type of quantity that is produced by this operation. + /// The type of unit enum. /// The average of the projected values, represented in the specified unit type. /// /// source or selector is null. @@ -192,8 +203,9 @@ public static TQuantity Average(this IEnumerable source, E /// /// source contains quantity types different from . /// - public static TQuantity Average(this IEnumerable source, Func selector, Enum unitType) - where TQuantity : IQuantity + public static TQuantity Average(this IEnumerable source, Func selector, TUnitType unitType) + where TUnitType : Enum + where TQuantity : IQuantity { return source.Select(selector).Average(unitType); } From 7524e128eafecd59668383e1182c003d8fa92331 Mon Sep 17 00:00:00 2001 From: UrielZyx Date: Sun, 3 Mar 2024 00:41:48 +0200 Subject: [PATCH 2/2] Remove obsolete tests --- UnitsNet.Tests/UnitMathTests.cs | 64 --------------------------------- 1 file changed, 64 deletions(-) diff --git a/UnitsNet.Tests/UnitMathTests.cs b/UnitsNet.Tests/UnitMathTests.cs index fbb4f1d16e..1972e6fdc5 100644 --- a/UnitsNet.Tests/UnitMathTests.cs +++ b/UnitsNet.Tests/UnitMathTests.cs @@ -45,14 +45,6 @@ public void AbsoluteValueOfNullReferenceThrowsException() Assert.Throws(() => quantity.Abs()); } - [Fact] - public void AverageOfDifferentUnitsThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), Volume.FromLiters(50)}; - - Assert.Throws(() => units.Average(LengthUnit.Centimeter)); - } - [Fact] public void AverageOfEmptySourceThrowsException() { @@ -61,14 +53,6 @@ public void AverageOfEmptySourceThrowsException() Assert.Throws(() => units.Average(LengthUnit.Centimeter)); } - [Fact] - public void AverageOfLengthsWithNullValueThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), null!}; - - Assert.Throws(() => units.Average(LengthUnit.Centimeter)); - } - [Fact] public void AverageOfLengthsCalculatesCorrectly() { @@ -119,22 +103,6 @@ public void MaxOfTwoLengthsReturnsTheLargestValue() Assert.Equal(LengthUnit.Meter, max.Unit); } - [Fact] - public void MaxOfDifferentUnitsThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), Volume.FromLiters(50)}; - - Assert.Throws(() => units.Max(LengthUnit.Centimeter)); - } - - [Fact] - public void MaxOfLengthsWithNullValueThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), null!}; - - Assert.Throws(() => units.Max(LengthUnit.Centimeter)); - } - [Fact] public void MaxOfEmptySourceThrowsException() { @@ -193,22 +161,6 @@ public void MinOfTwoLengthsReturnsTheSmallestValue() Assert.Equal(LengthUnit.Centimeter, min.Unit); } - [Fact] - public void MinOfDifferentUnitsThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), Volume.FromLiters(50)}; - - Assert.Throws(() => units.Min(LengthUnit.Centimeter)); - } - - [Fact] - public void MinOfLengthsWithNullValueThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), null!}; - - Assert.Throws(() => units.Min(LengthUnit.Centimeter)); - } - [Fact] public void MinOfEmptySourceThrowsException() { @@ -255,22 +207,6 @@ public void MinOfLengthsWithSelectorCalculatesCorrectly() Assert.Equal(LengthUnit.Centimeter, min.Unit); } - [Fact] - public void SumOfDifferentUnitsThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), Volume.FromLiters(50)}; - - Assert.Throws(() => units.Sum(LengthUnit.Centimeter)); - } - - [Fact] - public void SumOfLengthsWithNullValueThrowsException() - { - var units = new IQuantity[] {Length.FromMeters(1), null!}; - - Assert.Throws(() => units.Sum(LengthUnit.Centimeter)); - } - [Fact] public void SumOfEmptySourceReturnsZero() {