Handle array size inputs less than 1.

This commit is contained in:
Kristian Stolen
2022-01-13 16:41:24 +08:00
parent 4f2bc6f98c
commit 7d14c37aaa
2 changed files with 14 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
using FsCheck.Xunit; using FsCheck.Xunit;
using Reverse.Tests.Generators; using Reverse.Tests.Generators;
using System;
using System.Linq; using System.Linq;
using Xunit; using Xunit;
@@ -7,6 +8,14 @@ namespace Reverse.Tests
{ {
public class ReverserTests public class ReverserTests
{ {
[Fact]
public void Constructor_CannotAcceptNumberLessThanZero()
{
Action action = () => new Reverser(0);
Assert.Throws<ArgumentOutOfRangeException>(action);
}
[Property(Arbitrary = new[] { typeof(PositiveIntegerGenerator) })] [Property(Arbitrary = new[] { typeof(PositiveIntegerGenerator) })]
public void Constructor_CreatesRandomArrayOfSpecifiedLength(int size) public void Constructor_CreatesRandomArrayOfSpecifiedLength(int size)
{ {

View File

@@ -43,6 +43,11 @@ namespace Reverse
private int[] CreateRandomArray(int size) private int[] CreateRandomArray(int size)
{ {
if (size < 1)
{
throw new ArgumentOutOfRangeException(nameof(size), "Array size must be a positive integer");
}
var array = new int[size]; var array = new int[size];
for (int i = 1; i <= size; i++) for (int i = 1; i <= size; i++)
{ {