Skip to content

Commit 158118f

Browse files
committed
add tests
1 parent b903502 commit 158118f

File tree

124 files changed

+1041
-4881
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+1041
-4881
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace UnitsNet
7575
IComparable<{_quantity.Name}>,
7676
IConvertible,
7777
IEquatable<{_quantity.Name}>,
78-
IEquatableQuantity<{_quantity.Name}>,
78+
IEquatableQuantity<{_quantity.Name}, {_quantity.ValueType}>,
7979
IFormattable");
8080

8181
Writer.WL($@"
@@ -824,46 +824,13 @@ public int CompareTo({_quantity.Name} other)
824824
return _value.CompareTo(other.ToUnit(this.Unit).Value);
825825
}}
826826
827-
/// <summary>
828-
/// <para>
829-
/// Compare equality to another {_quantity.Name} within the given absolute or relative tolerance.
830-
/// </para>
831-
/// <para>
832-
/// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and
833-
/// <paramref name=""other""/> as a percentage of this quantity's value. <paramref name=""other""/> will be converted into
834-
/// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of
835-
/// this quantity's value to be considered equal.
836-
/// <example>
837-
/// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm).
838-
/// <code>
839-
/// var a = Length.FromMeters(2.0);
840-
/// var b = Length.FromInches(50.0);
841-
/// a.Equals(b, 0.01, ComparisonType.Relative);
842-
/// </code>
843-
/// </example>
844-
/// </para>
845-
/// <para>
846-
/// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and
847-
/// <paramref name=""other""/> as a fixed number in this quantity's unit. <paramref name=""other""/> will be converted into
848-
/// this quantity's unit for comparison.
849-
/// <example>
850-
/// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm).
851-
/// <code>
852-
/// var a = Length.FromMeters(2.0);
853-
/// var b = Length.FromInches(50.0);
854-
/// a.Equals(b, 0.01, ComparisonType.Absolute);
855-
/// </code>
856-
/// </example>
857-
/// </para>
858-
/// <para>
859-
/// Note that it is advised against specifying zero difference, due to the nature
860-
/// of floating-point operations and using {_valueType} internally.
861-
/// </para>
862-
/// </summary>
863-
/// <param name=""other"">The other quantity to compare to.</param>
864-
/// <param name=""tolerance"">The absolute or relative tolerance value. Must be greater than or equal to 0.</param>
865-
/// <param name=""comparisonType"">The comparison type: either relative or absolute.</param>
866-
/// <returns>True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance.</returns>
827+
/// <inheritdoc />
828+
public bool Equals(IQuantity? other, double tolerance, ComparisonType comparisonType)
829+
{{
830+
return other is {_quantity.Name} otherTyped && Equals(otherTyped, tolerance, comparisonType);
831+
}}
832+
833+
/// <inheritdoc />
867834
public bool Equals({_quantity.Name} other, {_quantity.ValueType} tolerance, ComparisonType comparisonType)
868835
{{
869836
if (tolerance < 0)

UnitsNet.Tests/CustomQuantities/HowMuch.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public HowMuch(double value, HowMuchUnit unit)
4242

4343
public double As(UnitSystem unitSystem) => throw new NotImplementedException();
4444

45+
public bool Equals(IQuantity? other, double tolerance, ComparisonType comparisonType)
46+
{
47+
return other is HowMuch otherTyped && otherTyped.Unit == Unit && otherTyped.Value.Equals(Value);
48+
}
49+
4550
public IQuantity ToUnit(Enum unit)
4651
{
4752
if (unit is HowMuchUnit howMuchUnit) return new HowMuch(As(unit), howMuchUnit);

UnitsNet.Tests/DummyIQuantity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ internal class DummyIQuantity : IQuantity
1717

1818
public double As(UnitSystem unitSystem ) => throw new NotImplementedException();
1919

20+
public bool Equals(IQuantity? other, double tolerance, ComparisonType comparisonType) => throw new NotImplementedException();
21+
2022
public string ToString(IFormatProvider? provider) => throw new NotImplementedException();
2123

2224
public string ToString(string? format, IFormatProvider? formatProvider) => throw new NotImplementedException();

UnitsNet.Tests/QuantityTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System.Globalization;
45
using UnitsNet.Units;
56
using Xunit;
67

@@ -16,5 +17,41 @@ public void GetHashCodeForDifferentQuantitiesWithSameValuesAreNotEqual()
1617

1718
Assert.NotEqual(length.GetHashCode(), area.GetHashCode());
1819
}
20+
21+
[Theory]
22+
[InlineData("10 m", "9.89 m" , ComparisonType.Absolute, 0.1, false)] // +/- 0.1m absolute tolerance and some additional margin tolerate rounding errors in test case.
23+
[InlineData("10 m", "9.91 m" , ComparisonType.Absolute, 0.1, true)]
24+
[InlineData("10 m", "10.09 m", ComparisonType.Absolute, 0.1, true)]
25+
[InlineData("10 m", "10.11 m", ComparisonType.Absolute, 0.1, false)]
26+
[InlineData("10 m", "8.9 m" , ComparisonType.Absolute, 0.1, false)] // +/- 1m relative tolerance (10%) and some additional margin tolerate rounding errors in test case.
27+
[InlineData("10 m", "9.1 m" , ComparisonType.Relative, 0.1, true)]
28+
[InlineData("10 m", "10.9 m" , ComparisonType.Relative, 0.1, true)]
29+
[InlineData("10 m", "11.1 m" , ComparisonType.Relative, 0.1, false)]
30+
public void Equals_IEquatableQuantity(string q1String, string q2String, ComparisonType comparisonType, double tolerance, bool expectedEqual)
31+
{
32+
IEquatableQuantity<Length, double> q1 = Length.Parse(q1String, CultureInfo.InvariantCulture);
33+
var q2 = Length.Parse(q2String, CultureInfo.InvariantCulture);
34+
35+
Assert.NotStrictEqual(q1, q2); // Strict equality should not be equal.
36+
Assert.Equal(expectedEqual, q1.Equals(q2, tolerance, comparisonType));
37+
}
38+
39+
[Theory]
40+
[InlineData("10 m", "9.89 m" , ComparisonType.Absolute, 0.1, false)] // +/- 0.1m absolute tolerance and some additional margin tolerate rounding errors in test case.
41+
[InlineData("10 m", "9.91 m" , ComparisonType.Absolute, 0.1, true)]
42+
[InlineData("10 m", "10.09 m", ComparisonType.Absolute, 0.1, true)]
43+
[InlineData("10 m", "10.11 m", ComparisonType.Absolute, 0.1, false)]
44+
[InlineData("10 m", "8.9 m" , ComparisonType.Absolute, 0.1, false)] // +/- 1m relative tolerance (10%) and some additional margin tolerate rounding errors in test case.
45+
[InlineData("10 m", "9.1 m" , ComparisonType.Relative, 0.1, true)]
46+
[InlineData("10 m", "10.9 m" , ComparisonType.Relative, 0.1, true)]
47+
[InlineData("10 m", "11.1 m" , ComparisonType.Relative, 0.1, false)]
48+
public void Equals_IQuantity(string q1String, string q2String, ComparisonType comparisonType, double tolerance, bool expectedEqual)
49+
{
50+
IQuantity q1 = Quantity.Parse(CultureInfo.InvariantCulture, typeof(Length), q1String);
51+
IQuantity q2 = Quantity.Parse(CultureInfo.InvariantCulture, typeof(Length), q2String);
52+
53+
Assert.NotEqual(q1, q2); // Strict equality should not be equal.
54+
Assert.Equal(expectedEqual, q1.Equals(q2, tolerance, comparisonType));
55+
}
1956
}
2057
}

UnitsNet/GeneratedCode/Quantities/Acceleration.g.cs

Lines changed: 8 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.g.cs

Lines changed: 8 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.g.cs

Lines changed: 8 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)