mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
Protect against index out of range exceptions
This commit is contained in:
@@ -37,5 +37,16 @@ namespace Reverse.Tests
|
||||
|
||||
Assert.True(input.SequenceEqual(output));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reverse_WithIndexGreaterThanArrayLength_DoesNothing()
|
||||
{
|
||||
var input = new int[] { 1, 2 };
|
||||
var output = new int[] { 1, 2 };
|
||||
|
||||
Reverser.Reverse(input, input.Length + 1);
|
||||
|
||||
Assert.True(input.SequenceEqual(output));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
{
|
||||
public static void Reverse(int[] arrayToReverse, int indexToReverseTo)
|
||||
{
|
||||
if (indexToReverseTo > arrayToReverse.Length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < indexToReverseTo / 2; i++)
|
||||
{
|
||||
int temp = arrayToReverse[i];
|
||||
|
||||
Reference in New Issue
Block a user