-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Improved arithmetic overflow detection to fix #125
- Loading branch information
Showing
4 changed files
with
137 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Mages.Core.Tokens; | ||
using NUnit.Framework; | ||
|
||
namespace Mages.Core.Tests | ||
{ | ||
[TestFixture] | ||
public class NumberHelperTests | ||
{ | ||
[TestCase(0u, 0u)] | ||
[TestCase(1u, 0u)] | ||
[TestCase(0u, 1u)] | ||
[TestCase(1u, 1u)] | ||
[TestCase(123u, 123u)] | ||
[TestCase(20u, 123u)] | ||
[TestCase(10u, ulong.MaxValue / 10)] | ||
[TestCase(0u, ulong.MaxValue)] | ||
public void TryMultiply_happy_case(ulong x, ulong y) | ||
{ | ||
var result = NumberHelper.TryMultiply(x, y, out var actual); | ||
Assert.AreEqual(checked(x * y), actual); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestCase(ulong.MaxValue, 2u)] | ||
[TestCase(ulong.MaxValue, ulong.MaxValue)] | ||
[TestCase(ulong.MaxValue / 2 + 1, 3u)] | ||
[TestCase(ulong.MaxValue / 3 + 1, 4u)] | ||
[TestCase(ulong.MaxValue - 1, 2u)] | ||
[TestCase(ulong.MaxValue / 10 * 9 + 1, 2u)] | ||
[TestCase(0xFFFFFFFFFFFFFFFFu, 2u)] | ||
[TestCase(8409014139716477191u, 10u)] | ||
public void TryMultiply_overflow_case(ulong x, ulong y) | ||
{ | ||
var result = NumberHelper.TryMultiply(x, y, out var actual); | ||
Assert.AreEqual(0, actual); | ||
Assert.IsFalse(result); | ||
} | ||
|
||
[TestCase(0u, 0u)] | ||
[TestCase(1u, 0u)] | ||
[TestCase(0u, 1u)] | ||
[TestCase(1u, 1u)] | ||
[TestCase(123u, 123u)] | ||
[TestCase(20u, 123u)] | ||
[TestCase(0u, ulong.MaxValue)] | ||
[TestCase(ulong.MaxValue, 0u)] | ||
[TestCase(ulong.MaxValue - 10, 10u)] | ||
public void TryAdd_happy_case(ulong x, ulong y) | ||
{ | ||
var result = NumberHelper.TryAdd(x, y, out var actual); | ||
Assert.AreEqual(checked(x + y), actual); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestCase(1u, ulong.MaxValue)] | ||
[TestCase(ulong.MaxValue, 1u)] | ||
[TestCase(ulong.MaxValue, ulong.MaxValue)] | ||
[TestCase(ulong.MaxValue / 2 + 1, ulong.MaxValue / 2 + 2)] | ||
[TestCase(ulong.MaxValue - 1, 2u)] | ||
[TestCase(0xFFFFFFFFFFFFFFFFu, 1u)] | ||
public void TryAdd_overflow_case(ulong x, ulong y) | ||
{ | ||
var result = NumberHelper.TryAdd(x, y, out var actual); | ||
Assert.AreEqual(0, actual); | ||
Assert.IsFalse(result); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace Mages.Core.Tokens; | ||
|
||
/// <summary> | ||
/// Helper methods for number arithmetics. | ||
/// </summary> | ||
internal static class NumberHelper | ||
{ | ||
/// <summary> | ||
/// Multiplies two unsigned numbers if their product does not exceed | ||
/// <see cref="ulong.MaxValue"/>. | ||
/// </summary> | ||
/// <returns>A value indicating whether the numbers were multiplied.</returns> | ||
public static bool TryMultiply(ulong x, ulong y, out ulong product) | ||
{ | ||
if (x == 0 || y == 0) | ||
{ | ||
product = 0; | ||
return true; | ||
} | ||
|
||
// Check for overflow | ||
if (x > ulong.MaxValue / y) | ||
{ | ||
product = 0; | ||
return false; | ||
} | ||
|
||
product = x * y; | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Adds two unsigned numbers if their sum does not exceed | ||
/// <see cref="ulong.MaxValue"/>. | ||
/// </summary> | ||
/// <returns>A value indicating whether the numbers were added.</returns> | ||
public static bool TryAdd(ulong x, ulong y, out ulong sum) | ||
{ | ||
// Check for overflow | ||
if (x > ulong.MaxValue - y) | ||
{ | ||
sum = 0; | ||
return false; | ||
} | ||
|
||
sum = x + y; | ||
return true; | ||
} | ||
} |
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