-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add Equals() to IQuantity interfaces (#1215)
Ref #1193 In v5, the default equality implementation changed to strict equality and the existing methods to compare across units with a tolerance, but this was not available in `IQuantity` interfaces. ### Changes - Add `Equals(IQuantity? other, IQuantity tolerance)` to `IQuantity` - Add `Equals(TSelf? other, TSelf tolerance)` to `IQuantity<TSelf, TUnitType, TValueType>` for strongly typed comparisons - Obsolete `Equals(TQuantity other, double tolerance, ComparisonType comparisonType)` method in quantity types
- Loading branch information
1 parent
5ecf8e9
commit ce42aa8
Showing
126 changed files
with
3,753 additions
and
1,458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// Licensed under MIT No Attribution, see LICENSE file at the root. | ||
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
||
using System; | ||
using System.Globalization; | ||
using UnitsNet.Units; | ||
using Xunit; | ||
|
||
|
@@ -16,5 +18,84 @@ public void GetHashCodeForDifferentQuantitiesWithSameValuesAreNotEqual() | |
|
||
Assert.NotEqual(length.GetHashCode(), area.GetHashCode()); | ||
} | ||
|
||
[Theory] | ||
[InlineData("10 m", "9.89 m" , "0.1 m", false)] // +/- 0.1m absolute tolerance and some additional margin tolerate rounding errors in test case. | ||
[InlineData("10 m", "9.91 m" , "0.1 m", true)] | ||
[InlineData("10 m", "10.09 m", "0.1 m", true)] | ||
[InlineData("10 m", "1009 cm", "0.1 m", true)] // Different unit, still equal. | ||
[InlineData("10 m", "10.11 m", "0.1 m", false)] | ||
[InlineData("10 m", "8.9 m" , "0.1 m", false)] // +/- 1m relative tolerance (10%) and some additional margin tolerate rounding errors in test case. | ||
public void Equals_IGenericEquatableQuantity(string q1String, string q2String, string toleranceString, bool expectedEqual) | ||
{ | ||
// This interfaces implements .NET generic math interfaces. | ||
IQuantity<Length, LengthUnit, double> q1 = ParseLength(q1String); | ||
IQuantity<Length, LengthUnit, double> q2 = ParseLength(q2String); | ||
IQuantity<Length, LengthUnit, double> tolerance = ParseLength(toleranceString); | ||
|
||
Assert.Equal(expectedEqual, q1.Equals(q2, tolerance)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("10 m", "9.89 m" , "0.1 m", false)] // +/- 0.1m absolute tolerance and some additional margin tolerate rounding errors in test case. | ||
[InlineData("10 m", "9.91 m" , "0.1 m", true)] | ||
[InlineData("10 m", "10.09 m", "0.1 m", true)] | ||
[InlineData("10 m", "1009 cm", "0.1 m", true)] // Different unit, still equal. | ||
[InlineData("10 m", "10.11 m", "0.1 m", false)] | ||
[InlineData("10 m", "8.9 m" , "0.1 m", false)] // +/- 1m relative tolerance (10%) and some additional margin tolerate rounding errors in test case. | ||
public void Equals_IQuantity(string q1String, string q2String, string toleranceString, bool expectedEqual) | ||
{ | ||
IQuantity q1 = ParseLength(q1String); | ||
IQuantity q2 = ParseLength(q2String); | ||
IQuantity tolerance = ParseLength(toleranceString); | ||
|
||
Assert.NotEqual(q1, q2); // Strict equality should not be equal. | ||
Assert.Equal(expectedEqual, q1.Equals(q2, tolerance)); | ||
} | ||
|
||
[Fact] | ||
public void Equals_IQuantity_OtherIsNull_ReturnsFalse() | ||
{ | ||
IQuantity q1 = ParseLength("10 m"); | ||
IQuantity? q2 = null; | ||
IQuantity tolerance = ParseLength("0.1 m"); | ||
|
||
Assert.False(q1.Equals(q2, tolerance)); | ||
} | ||
|
||
[Fact] | ||
public void Equals_IQuantity_OtherIsDifferentType_ReturnsFalse() | ||
{ | ||
IQuantity q1 = ParseLength("10 m"); | ||
IQuantity q2 = Mass.FromKilograms(10); | ||
IQuantity tolerance = Mass.FromKilograms(0.1); | ||
|
||
Assert.False(q1.Equals(q2, tolerance)); | ||
} | ||
|
||
[Fact] | ||
public void Equals_IQuantity_ToleranceIsDifferentType_Throws() | ||
{ | ||
IQuantity q1 = ParseLength("10 m"); | ||
IQuantity q2 = ParseLength("10 m"); | ||
IQuantity tolerance = Mass.FromKilograms(0.1); | ||
|
||
Assert.Throws<ArgumentException>(() => q1.Equals(q2, tolerance)); | ||
} | ||
|
||
[Fact] | ||
public void Equals_GenericEquatableIQuantity_OtherIsNull_ReturnsFalse() | ||
{ | ||
IQuantity<Length, LengthUnit, double> q1 = ParseLength("10 m"); | ||
IQuantity<Length, LengthUnit, double>? q2 = null; | ||
IQuantity<Length, LengthUnit, double> tolerance = ParseLength("0.1 m"); | ||
|
||
Assert.False(q1.Equals(q2, tolerance)); | ||
} | ||
|
||
private static Length ParseLength(string str) | ||
{ | ||
return Length.Parse(str, CultureInfo.InvariantCulture); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.