Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve difference calculation of overflowing integrals #1875

Merged
merged 2 commits into from Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/ByteAssertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -13,10 +14,10 @@ internal ByteAssertions(byte value)
{
}

private protected override byte? CalculateDifferenceForFailureMessage(byte expected)
private protected override string CalculateDifferenceForFailureMessage(byte subject, byte expected)
{
var difference = (byte?)(Subject - expected);
return difference != 0 ? difference : null;
int difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/DecimalAssertions.cs
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -14,12 +15,12 @@ internal DecimalAssertions(decimal value)
{
}

private protected override decimal? CalculateDifferenceForFailureMessage(decimal expected)
private protected override string CalculateDifferenceForFailureMessage(decimal subject, decimal expected)
{
try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
decimal difference = checked(subject - expected);
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
}
catch (OverflowException)
{
Expand Down
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/DoubleAssertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -15,10 +16,10 @@ internal DoubleAssertions(double value)

private protected override bool IsNaN(double value) => double.IsNaN(value);

private protected override double? CalculateDifferenceForFailureMessage(double expected)
private protected override string CalculateDifferenceForFailureMessage(double subject, double expected)
{
var difference = Subject - expected;
return difference != 0 ? difference : null;
var difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
9 changes: 5 additions & 4 deletions Src/FluentAssertions/Numeric/Int16Assertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -13,15 +14,15 @@ internal Int16Assertions(short value)
{
}

private protected override short? CalculateDifferenceForFailureMessage(short expected)
private protected override string CalculateDifferenceForFailureMessage(short subject, short expected)
{
if (Subject < 10 && expected < 10)
if (subject < 10 && expected < 10)
{
return null;
}

var difference = (short?)(Subject - expected);
return difference != 0 ? difference : null;
int difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
19 changes: 6 additions & 13 deletions Src/FluentAssertions/Numeric/Int32Assertions.cs
@@ -1,5 +1,5 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -14,22 +14,15 @@ internal Int32Assertions(int value)
{
}

private protected override int? CalculateDifferenceForFailureMessage(int expected)
private protected override string CalculateDifferenceForFailureMessage(int subject, int expected)
{
if (Subject is > 0 and < 10 && expected is > 0 and < 10)
if (subject is > 0 and < 10 && expected is > 0 and < 10)
{
return null;
}

try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
long difference = (long)subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
19 changes: 6 additions & 13 deletions Src/FluentAssertions/Numeric/Int64Assertions.cs
@@ -1,5 +1,5 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -14,22 +14,15 @@ internal Int64Assertions(long value)
{
}

private protected override long? CalculateDifferenceForFailureMessage(long expected)
private protected override string CalculateDifferenceForFailureMessage(long subject, long expected)
{
if (Subject is > 0 and < 10 && expected is > 0 and < 10)
if (subject is > 0 and < 10 && expected is > 0 and < 10)
{
return null;
}

try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
decimal difference = (decimal)subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/NullableByteAssertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -13,10 +14,10 @@ internal NullableByteAssertions(byte? value)
{
}

private protected override byte? CalculateDifferenceForFailureMessage(byte expected)
private protected override string CalculateDifferenceForFailureMessage(byte subject, byte expected)
{
var difference = (byte?)(Subject - expected);
return difference != 0 ? difference : null;
int difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/NullableDecimalAssertions.cs
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -14,12 +15,12 @@ internal NullableDecimalAssertions(decimal? value)
{
}

private protected override decimal? CalculateDifferenceForFailureMessage(decimal expected)
private protected override string CalculateDifferenceForFailureMessage(decimal subject, decimal expected)
{
try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
decimal difference = checked(subject - expected);
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
catch (OverflowException)
{
Expand Down
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/NullableDoubleAssertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -15,10 +16,10 @@ internal NullableDoubleAssertions(double? value)

private protected override bool IsNaN(double value) => double.IsNaN(value);

private protected override double? CalculateDifferenceForFailureMessage(double expected)
private protected override string CalculateDifferenceForFailureMessage(double subject, double expected)
{
var difference = Subject - expected;
return difference != 0 ? difference : null;
double difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
9 changes: 5 additions & 4 deletions Src/FluentAssertions/Numeric/NullableInt16Assertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -13,15 +14,15 @@ internal NullableInt16Assertions(short? value)
{
}

private protected override short? CalculateDifferenceForFailureMessage(short expected)
private protected override string CalculateDifferenceForFailureMessage(short subject, short expected)
{
if (Subject < 10 && expected < 10)
if (subject < 10 && expected < 10)
{
return null;
}

var difference = (short?)(Subject - expected);
return difference != 0 ? difference : null;
int difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
19 changes: 6 additions & 13 deletions Src/FluentAssertions/Numeric/NullableInt32Assertions.cs
@@ -1,5 +1,5 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -14,22 +14,15 @@ internal NullableInt32Assertions(int? value)
{
}

private protected override int? CalculateDifferenceForFailureMessage(int expected)
private protected override string CalculateDifferenceForFailureMessage(int subject, int expected)
{
if (Subject is > 0 and < 10 && expected is > 0 and < 10)
if (subject is > 0 and < 10 && expected is > 0 and < 10)
{
return null;
}

try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
long difference = (long)subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
19 changes: 6 additions & 13 deletions Src/FluentAssertions/Numeric/NullableInt64Assertions.cs
@@ -1,5 +1,5 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -14,22 +14,15 @@ internal NullableInt64Assertions(long? value)
{
}

private protected override long? CalculateDifferenceForFailureMessage(long expected)
private protected override string CalculateDifferenceForFailureMessage(long subject, long expected)
{
if (Subject is > 0 and < 10 && expected is > 0 and < 10)
if (subject is > 0 and < 10 && expected is > 0 and < 10)
{
return null;
}

try
{
var difference = checked(Subject - expected);
return difference != 0 ? difference : null;
}
catch (OverflowException)
{
return null;
}
decimal difference = (decimal)subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/NullableSByteAssertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -13,10 +14,10 @@ internal NullableSByteAssertions(sbyte? value)
{
}

private protected override sbyte? CalculateDifferenceForFailureMessage(sbyte expected)
private protected override string CalculateDifferenceForFailureMessage(sbyte subject, sbyte expected)
{
var difference = (sbyte?)(Subject - expected);
return difference != 0 ? difference : null;
int difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
7 changes: 4 additions & 3 deletions Src/FluentAssertions/Numeric/NullableSingleAssertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -15,10 +16,10 @@ internal NullableSingleAssertions(float? value)

private protected override bool IsNaN(float value) => float.IsNaN(value);

private protected override float? CalculateDifferenceForFailureMessage(float expected)
private protected override string CalculateDifferenceForFailureMessage(float subject, float expected)
{
var difference = Subject - expected;
return difference != 0 ? difference : null;
float difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}
9 changes: 5 additions & 4 deletions Src/FluentAssertions/Numeric/NullableUInt16Assertions.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Globalization;

namespace FluentAssertions.Numeric
{
Expand All @@ -13,15 +14,15 @@ internal NullableUInt16Assertions(ushort? value)
{
}

private protected override ushort? CalculateDifferenceForFailureMessage(ushort expected)
private protected override string CalculateDifferenceForFailureMessage(ushort subject, ushort expected)
{
if (Subject < 10 && expected < 10)
if (subject < 10 && expected < 10)
{
return null;
}

var difference = (ushort?)(Subject - expected);
return difference != 0 ? difference : null;
int difference = subject - expected;
return difference != 0 ? difference.ToString(CultureInfo.InvariantCulture) : null;
}
}
}