Skip to content

Commit b7c953e

Browse files
authored
Merge pull request #112 from feO2x/features/generic-is-greater-than-approximately
Generic IsLessThanOrApproximately/IsGreaterThanOrApproximately
2 parents 20b52ce + b02763a commit b7c953e

13 files changed

+1397
-13
lines changed

Code/Light.GuardClauses.Tests/CommonAssertions/IsApproximatelyTests.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public static void DoubleWithDefaultTolerance(double first, double second, bool
1717
[InlineData(1.1, 1.3, 0.5, true)]
1818
[InlineData(100.55, 100.555, 0.00001, false)]
1919
[InlineData(5.0, 14.999999, 10.0, true)]
20-
[InlineData(5.0, 15.0, 10.0, false)]
20+
[InlineData(5.0, 15.0, 10.0, true)]
21+
[InlineData(4.9998, 15.0, 10.0, false)]
2122
[InlineData(5.0, 15.0001, 10.0, false)]
2223
public static void DoubleWithCustomTolerance(double first, double second, double tolerance, bool expected) =>
2324
first.IsApproximately(second, tolerance).Should().Be(expected);
@@ -34,7 +35,8 @@ public static void FloatWithDefaultTolerance(float first, float second, bool exp
3435
[InlineData(1.1f, 1.3f, 0.5f, true)]
3536
[InlineData(100.55f, 100.555f, 0.00001f, false)]
3637
[InlineData(5.0f, 14.999999f, 10.0f, true)]
37-
[InlineData(5.0f, 15.0f, 10.0f, false)]
38+
[InlineData(5.0f, 15.0f, 10.0f, true)]
39+
[InlineData(4.99f, 15.0f, 10.0f, false)]
3840
[InlineData(5.0f, 15.0001f, 10.0f, false)]
3941
public static void FloatWithCustomTolerance(float first, float second, float tolerance, bool expected) =>
4042
first.IsApproximately(second, tolerance).Should().Be(expected);
@@ -44,7 +46,8 @@ public static void FloatWithCustomTolerance(float first, float second, float tol
4446
[InlineData(1.1, 1.3, 0.5, true)]
4547
[InlineData(100.55, 100.555, 0.00001, false)]
4648
[InlineData(5.0, 14.999999, 10.0, true)]
47-
[InlineData(5.0, 15.0, 10.0, false)]
49+
[InlineData(5.0, 15.0, 10.0, true)]
50+
[InlineData(5.0, 15.000001, 10.0, false)]
4851
[InlineData(5.0, 15.0001, 10.0, false)]
4952
public static void GenericDoubleWithCustomTolerance(double first, double second, double tolerance, bool expected) =>
5053
first.IsApproximately<double>(second, tolerance).Should().Be(expected);
@@ -53,14 +56,16 @@ public static void GenericDoubleWithCustomTolerance(double first, double second,
5356
[InlineData(1.1f, 1.3f, 0.5f, true)]
5457
[InlineData(100.55f, 100.555f, 0.00001f, false)]
5558
[InlineData(5.0f, 14.999999f, 10.0f, true)]
56-
[InlineData(5.0f, 15.0f, 10.0f, false)]
59+
[InlineData(5.0f, 15.0f, 10.0f, true)]
60+
[InlineData(5.0f, 15.01f, 10.0f, false)]
5761
[InlineData(5.0f, 15.0001f, 10.0f, false)]
5862
public static void GenericFloatWithCustomTolerance(float first, float second, float tolerance, bool expected) =>
5963
first.IsApproximately<float>(second, tolerance).Should().Be(expected);
6064

6165
[Theory]
6266
[InlineData(5, 10, 10, true)]
63-
[InlineData(5, 15, 10, false)]
67+
[InlineData(5, 15, 10, true)]
68+
[InlineData(4, 15, 10, false)]
6469
[InlineData(-5, 5, 12, true)]
6570
[InlineData(-100, 100, 199, false)]
6671
[InlineData(42, 42, 1, true)]
@@ -69,7 +74,8 @@ public static void GenericIntWithCustomTolerance(int first, int second, int tole
6974

7075
[Theory]
7176
[InlineData(5L, 10L, 10L, true)]
72-
[InlineData(5L, 15L, 10L, false)]
77+
[InlineData(5L, 15L, 10L, true)]
78+
[InlineData(5L, 16L, 10L, false)]
7379
[InlineData(-5L, 5L, 12L, true)]
7480
[InlineData(-100L, 100L, 199L, false)]
7581
[InlineData(42L, 42L, 1L, true)]
@@ -91,7 +97,7 @@ bool expected
9197
{ 1.1m, 1.3m, 0.5m, true },
9298
{ 100.55m, 100.555m, 0.00001m, false },
9399
{ 5.0m, 14.999999m, 10.0m, true },
94-
{ 5.0m, 15.0m, 10.0m, false },
100+
{ 5.0m, 15.0m, 9.99m, false },
95101
};
96102
#endif
97103
}

Code/Light.GuardClauses.Tests/CommonAssertions/IsGreaterThanOrApproximatelyTests.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,59 @@ public static void FloatWithDefaultTolerance(float first, float second, bool exp
3636
[InlineData(1.0f, 2.0f, 0.1f, false)]
3737
public static void FloatWIthCustomTolerance(float first, float second, float tolerance, bool expected) =>
3838
first.IsGreaterThanOrApproximately(second, tolerance).Should().Be(expected);
39-
}
39+
40+
#if NET8_0
41+
[Theory]
42+
[InlineData(15.91, 15.9, 0.1, true)]
43+
[InlineData(24.449, 24.45, 0.0001, false)]
44+
[InlineData(-3.12, -3.2, 0.001, true)]
45+
[InlineData(2.369, 2.37, 0.0005, false)]
46+
[InlineData(15.0, 14.0, 0.1, true)] // Greater than case
47+
[InlineData(14.95, 15.0, 0.1, true)] // Approximately equal case
48+
public static void GenericDoubleWithCustomTolerance(double first, double second, double tolerance, bool expected) =>
49+
first.IsGreaterThanOrApproximately<double>(second, tolerance).Should().Be(expected);
50+
51+
[Theory]
52+
[InlineData(2.0f, 1.0f, 0.1f, true)]
53+
[InlineData(1.0f, 1.0f, 0.1f, true)]
54+
[InlineData(1.0f, 1.1f, 0.01f, false)]
55+
[InlineData(1.0f, 2.0f, 0.1f, false)]
56+
[InlineData(2.1f, 2.0f, 0.01f, true)] // Greater than case
57+
public static void GenericFloatWithCustomTolerance(float first, float second, float tolerance, bool expected) =>
58+
first.IsGreaterThanOrApproximately<float>(second, tolerance).Should().Be(expected);
59+
60+
[Theory]
61+
[InlineData(10, 5, 1, true)] // Greater than case
62+
[InlineData(5, 5, 1, true)] // Equal case
63+
[InlineData(5, 6, 1, true)] // Approximately equal case
64+
[InlineData(5, 7, 1, false)] // Not greater than or approximately equal case
65+
public static void GenericIntWithCustomTolerance(int first, int second, int tolerance, bool expected) =>
66+
first.IsGreaterThanOrApproximately(second, tolerance).Should().Be(expected);
67+
68+
[Theory]
69+
[InlineData(10L, 5L, 1L, true)] // Greater than case
70+
[InlineData(5L, 5L, 1L, true)] // Equal case
71+
[InlineData(5L, 6L, 1L, true)] // Approximately equal case
72+
[InlineData(5L, 7L, 1L, false)] // Not greater than or approximately equal case
73+
public static void GenericLongWithCustomTolerance(long first, long second, long tolerance, bool expected) =>
74+
first.IsGreaterThanOrApproximately(second, tolerance).Should().Be(expected);
75+
76+
[Theory]
77+
[MemberData(nameof(DecimalTestData))]
78+
public static void GenericDecimalWithCustomTolerance(
79+
decimal first,
80+
decimal second,
81+
decimal tolerance,
82+
bool expected
83+
) =>
84+
first.IsGreaterThanOrApproximately(second, tolerance).Should().Be(expected);
85+
86+
public static TheoryData<decimal, decimal, decimal, bool> DecimalTestData() => new ()
87+
{
88+
{ 1.3m, 1.1m, 0.1m, true }, // Greater than case
89+
{ 1.1m, 1.1m, 0.1m, true }, // Equal case
90+
{ 1.0m, 1.1m, 0.2m, true }, // Approximately equal case
91+
{ 1.0m, 1.3m, 0.1m, false }, // Not greater than or approximately equal case
92+
};
93+
#endif
94+
}

Code/Light.GuardClauses.Tests/CommonAssertions/IsLessThanOrApproximatelyTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,56 @@ public static void FloatWithDefaultTolerance(float first, float second, bool exp
3636
[InlineData(0f, -1f, 0.9f, false)]
3737
public static void FloatWithCustomTolerance(float first, float second, float tolerance, bool expected) =>
3838
first.IsLessThanOrApproximately(second, tolerance).Should().Be(expected);
39+
40+
#if NET8_0
41+
[Theory]
42+
[InlineData(13.25, 13.5, 0.1, true)] // Less than case
43+
[InlineData(13.5, 13.5, 0.1, true)] // Equal case
44+
[InlineData(13.55, 13.5, 0.1, true)] // Approximately equal case
45+
[InlineData(13.7, 13.5, 0.1, false)] // Not less than or approximately equal case
46+
public static void GenericDoubleWithCustomTolerance(double first, double second, double tolerance, bool expected) =>
47+
first.IsLessThanOrApproximately<double>(second, tolerance).Should().Be(expected);
48+
49+
[Theory]
50+
[InlineData(13.25f, 13.5f, 0.1f, true)] // Less than case
51+
[InlineData(13.5f, 13.5f, 0.1f, true)] // Equal case
52+
[InlineData(13.55f, 13.5f, 0.1f, true)] // Approximately equal case
53+
[InlineData(13.7f, 13.5f, 0.1f, false)] // Not less than or approximately equal case
54+
public static void GenericFloatWithCustomTolerance(float first, float second, float tolerance, bool expected) =>
55+
first.IsLessThanOrApproximately<float>(second, tolerance).Should().Be(expected);
56+
57+
[Theory]
58+
[InlineData(5, 10, 1, true)] // Less than case
59+
[InlineData(5, 5, 1, true)] // Equal case
60+
[InlineData(6, 5, 1, true)] // Approximately equal case
61+
[InlineData(7, 5, 1, false)] // Not less than or approximately equal case
62+
public static void GenericIntWithCustomTolerance(int first, int second, int tolerance, bool expected) =>
63+
first.IsLessThanOrApproximately(second, tolerance).Should().Be(expected);
64+
65+
[Theory]
66+
[InlineData(5L, 10L, 1L, true)] // Less than case
67+
[InlineData(5L, 5L, 1L, true)] // Equal case
68+
[InlineData(6L, 5L, 1L, true)] // Approximately equal case
69+
[InlineData(7L, 5L, 1L, false)] // Not less than or approximately equal case
70+
public static void GenericLongWithCustomTolerance(long first, long second, long tolerance, bool expected) =>
71+
first.IsLessThanOrApproximately(second, tolerance).Should().Be(expected);
72+
73+
[Theory]
74+
[MemberData(nameof(DecimalTestData))]
75+
public static void GenericDecimalWithCustomTolerance(
76+
decimal first,
77+
decimal second,
78+
decimal tolerance,
79+
bool expected
80+
) =>
81+
first.IsLessThanOrApproximately(second, tolerance).Should().Be(expected);
82+
83+
public static TheoryData<decimal, decimal, decimal, bool> DecimalTestData() => new ()
84+
{
85+
{ 1.0m, 1.2m, 0.1m, true }, // Less than case
86+
{ 1.1m, 1.1m, 0.1m, true }, // Equal case
87+
{ 1.2m, 1.1m, 0.1m, true }, // Approximately equal case
88+
{ 1.3m, 1.1m, 0.1m, false }, // Not less than or approximately equal case
89+
};
90+
#endif
3991
}

0 commit comments

Comments
 (0)