Skip to content

Commit

Permalink
fix test not overflowing
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLloyd committed Apr 27, 2024
1 parent 9dbc23b commit 3d59e86
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Tests/Allocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static int[] Allocate(int quantity, double[] weights)
var results = new int[weights.Length];
for (int i = 0; i < weights.Length; i++)
{
var allocation = (int)Math.Round(quantity * weights[i] / sumWeights, MidpointRounding.AwayFromZero);
var allocation = checked ((int)Math.Round(quantity * weights[i] / sumWeights, MidpointRounding.AwayFromZero));
residual -= allocation;
results[i] = allocation;
}
Expand Down
9 changes: 2 additions & 7 deletions Tests/Allocator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,8 @@ public void Allocate_NegativeExample()
[Fact]
public void Allocate_Exceptions()
{
var quantity = 0;
var weights_i = 0.0;
var sumWeights = 0.0;
var allocation = (int)Math.Round(quantity * weights_i / sumWeights, MidpointRounding.AwayFromZero);
Assert.Equal(int.MinValue, allocation);
//Assert.Throws<Exception>(() => Allocator.Allocate(0, [0.0, 0.0, 0.0]));
//Assert.Throws<Exception>(() => Allocator.Allocate(42, [1.0, -2.0, 1.0, 0]));
Assert.Throws<Exception>(() => Allocator.Allocate(0, [0.0, 0.0, 0.0]));
Assert.Throws<Exception>(() => Allocator.Allocate(42, [1.0, -2.0, 1.0, 1e-30]));
}

[Fact]
Expand Down

0 comments on commit 3d59e86

Please sign in to comment.