1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

Add exhaustive Random tests

Introduces NextInt16 and NextByte
This commit is contained in:
Oliver Booth 2022-04-25 22:14:11 +01:00
parent 3df08760e8
commit 2ead3cbb8a
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 407 additions and 4 deletions

View File

@ -0,0 +1,245 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Core;
namespace X10D.Tests.Core;
[TestClass]
public class RandomTests
{
[TestMethod]
public void NextBoolean_ShouldBeFalse_GivenSeed1234()
{
var random = new Random(1234);
Assert.IsFalse(random.NextBoolean());
}
[TestMethod]
public void NextBoolean_ShouldThrow_GivenNull()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextBoolean());
}
[TestMethod]
public void NextByte_ShouldBe101_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(101, random.NextByte());
}
[TestMethod]
public void NextByte_WithMax10_ShouldBe3_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3, random.NextByte(10));
}
[TestMethod]
public void NextByte_WithMin0Max10_ShouldBe3_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3, random.NextByte(0, 10));
}
[TestMethod]
public void NextByte_ShouldThrow_GivenNull()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextByte());
Assert.ThrowsException<ArgumentNullException>(() => random!.NextByte(10));
Assert.ThrowsException<ArgumentNullException>(() => random!.NextByte(0, 10));
}
[TestMethod]
public void NextDouble_WithMax10_ShouldBe3point9908097935797695_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3.9908097935797695, random.NextDouble(10));
}
[TestMethod]
public void NextDouble_WithMin0Max10_ShouldBe3point9908097935797695_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3.9908097935797695, random.NextDouble(0, 10));
}
[TestMethod]
public void NextDouble_ShouldThrow_GivenNull()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextDouble(10));
Assert.ThrowsException<ArgumentNullException>(() => random!.NextDouble(0, 10));
}
[TestMethod]
public void NextDouble_ShouldThrow_GivenMaxLessThan0()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => random.NextDouble(-1));
}
[TestMethod]
public void NextDouble_ShouldThrow_GivenMaxLessThanMin()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentException>(() => random.NextDouble(0, -1));
}
[TestMethod]
public void NextEnum_ShouldBeTuesday_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(DayOfWeek.Tuesday, random.Next<DayOfWeek>());
}
[TestMethod]
public void NextEnum_ShouldThrow_GivenNull()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.Next<DayOfWeek>());
}
[TestMethod]
public void NextFrom_ShouldThrow_GivenNullRandom()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextFrom(""));
}
[TestMethod]
public void NextFrom_ShouldThrow_GivenNullSource()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentNullException>(() => random.NextFrom((string?)null!));
}
[TestMethod]
public void NextFrom_ShouldEnumerate_GivenNonList()
{
IEnumerable<int> Source()
{
yield return 0;
}
var random = new Random(1234);
Assert.AreEqual(0, random.NextFrom(Source()));
}
[TestMethod]
public void NextInt16_ShouldBe13076_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(13076, random.NextInt16());
}
[TestMethod]
public void NextInt16_WithMax10_ShouldBe3_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3, random.NextInt16(10));
}
[TestMethod]
public void NextInt16_WithMin0Max10_ShouldBe3_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3, random.NextInt16(0, 10));
}
[TestMethod]
public void NextInt16_ShouldThrow_GivenMaxLessThan0()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => random.NextInt16(-1));
}
[TestMethod]
public void NextInt16_ShouldThrow_GivenNull()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextInt16());
Assert.ThrowsException<ArgumentNullException>(() => random!.NextInt16(10));
Assert.ThrowsException<ArgumentNullException>(() => random!.NextInt16(0, 10));
}
[TestMethod]
public void NextSingle_WithMax10_ShouldBe3point99081_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3.99081f, random.NextSingle(10));
}
[TestMethod]
public void NextSingle_WithMin0Max10_ShouldBe3point99081_GivenSeed1234()
{
var random = new Random(1234);
Assert.AreEqual(3.99081f, random.NextSingle(0, 10));
}
[TestMethod]
public void NextSingle_ShouldThrow_GivenNull()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextSingle(10));
Assert.ThrowsException<ArgumentNullException>(() => random!.NextSingle(0, 10));
}
[TestMethod]
public void NextSingle_ShouldThrow_GivenMaxLessThan0()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => random.NextSingle(-1));
}
[TestMethod]
public void NextSingle_ShouldThrow_GivenMaxLessThanMin()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentException>(() => random.NextSingle(0, -1));
}
[TestMethod]
public void NextString_ShouldBe_kxiyiyvnqi_GivenSeed1234()
{
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
var random = new Random(1234);
Assert.AreEqual("kxiyiyvnqi", random.NextString(alphabet.ToCharArray(), 10));
}
[TestMethod]
public void NextString_ShouldBeEmpty_GivenLength0()
{
var random = new Random(1234);
Assert.AreEqual(string.Empty, random.NextString(ArraySegment<char>.Empty, 0));
}
[TestMethod]
public void NextString_ShouldBeLength1_GivenLength1()
{
var random = new Random(1234);
Assert.AreEqual(1, random.NextString("hello world".ToCharArray(), 1).Length);
}
[TestMethod]
public void NextString_ShouldThrow_GivenNullRandom()
{
Random? random = null;
Assert.ThrowsException<ArgumentNullException>(() => random!.NextString(ArraySegment<char>.Empty, 0));
}
[TestMethod]
public void NextString_ShouldThrow_GivenNullSource()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentNullException>(() => random.NextString(null!, 0));
}
[TestMethod]
public void NextString_ShouldThrow_GivenNegativeLength()
{
var random = new Random(1234);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => random.NextString(ArraySegment<char>.Empty, -1));
}
}

View File

@ -2,7 +2,7 @@
using System.Text;
using X10D.Math;
namespace X10D;
namespace X10D.Core;
/// <summary>
/// Extension methods for <see cref="System.Random" />.
@ -64,7 +64,7 @@ public static class RandomExtensions
/// <paramref name="maxValue" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="maxValue" /> is less than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static double NextDouble(this Random random, double maxValue)
{
if (random is null)
@ -106,7 +106,7 @@ public static class RandomExtensions
if (maxValue < minValue)
{
throw new ArgumentOutOfRangeException(ExceptionMessages.MaxValueGreaterThanEqualToMinValue);
throw new ArgumentException(ExceptionMessages.MaxValueGreaterThanEqualToMinValue);
}
return MathUtility.Lerp(minValue, maxValue, random.NextDouble());
@ -156,6 +156,154 @@ public static class RandomExtensions
return list[random.Next(list.Count)];
}
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>
/// An 8-bit unsigned integer that is greater than or equal to 0, and less than <see cref="byte.MaxValue" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static byte NextByte(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return random.NextByte(byte.MaxValue);
}
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or
/// equal to 0.
/// </param>
/// <returns>
/// An 8-bit unsigned integer that is greater than or equal to 0, and less than <paramref name="maxValue" />; that is, the
/// range of return values ordinarily includes 0 but not <paramref name="maxValue" />. However, if
/// <paramref name="maxValue" /> equals 0, <paramref name="maxValue" /> is returned.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static byte NextByte(this Random random, byte maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return random.NextByte(0, maxValue);
}
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="minValue">The inclusive lower bound of the random number to be generated.</param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or
/// equal to <paramref name="minValue" />.
/// </param>
/// <returns>
/// An 8-bit unsigned integer greater than or equal to <paramref name="minValue" /> and less than
/// <paramref name="maxValue" />; that is, the range of return values includes <paramref name="minValue" /> but not
/// <paramref name="maxValue" />. If <paramref name="minValue" /> equals <paramref name="maxValue" />,
/// <paramref name="minValue" /> is returned.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="minValue" /> is greater than <paramref name="maxValue" />.
/// </exception>
public static byte NextByte(this Random random, byte minValue, byte maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return (byte)random.Next(minValue, maxValue);
}
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>
/// An 16-bit signed integer that is greater than or equal to 0, and less than <see cref="short.MaxValue" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static short NextInt16(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return random.NextInt16(short.MaxValue);
}
/// <summary>
/// Returns a non-negative random integer that is less than the specified maximum.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or
/// equal to 0.
/// </param>
/// <returns>
/// A 16-bit signed integer that is greater than or equal to 0, and less than <paramref name="maxValue" />; that is, the
/// range of return values ordinarily includes 0 but not <paramref name="maxValue" />. However, if
/// <paramref name="maxValue" /> equals 0, <paramref name="maxValue" /> is returned.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static short NextInt16(this Random random, short maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxValue));
}
return random.NextInt16(0, maxValue);
}
/// <summary>
/// Returns a random integer that is within a specified range.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="minValue">The inclusive lower bound of the random number to be generated.</param>
/// <param name="maxValue">
/// The exclusive upper bound of the random number to be generated. <paramref name="maxValue" /> must be greater than or
/// equal to <paramref name="minValue" />.
/// </param>
/// <returns>
/// An 8-bit unsigned integer greater than or equal to <paramref name="minValue" /> and less than
/// <paramref name="maxValue" />; that is, the range of return values includes <paramref name="minValue" /> but not
/// <paramref name="maxValue" />. If <paramref name="minValue" /> equals <paramref name="maxValue" />,
/// <paramref name="minValue" /> is returned.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="minValue" /> is greater than <paramref name="maxValue" />.
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static short NextInt16(this Random random, short minValue, short maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return (short)random.Next(minValue, maxValue);
}
/// <summary>
/// Returns a non-negative random single-precision floating point number that is less than the specified maximum.
/// </summary>
@ -178,7 +326,7 @@ public static class RandomExtensions
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException(ExceptionMessages.MaxValueGreaterThanEqualTo0);
throw new ArgumentOutOfRangeException(nameof(maxValue));
}
return random.NextSingle(0, maxValue);
@ -203,6 +351,16 @@ public static class RandomExtensions
/// </exception>
public static float NextSingle(this Random random, float minValue, float maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (maxValue < minValue)
{
throw new ArgumentException(ExceptionMessages.MaxValueGreaterThanEqualToMinValue);
}
return MathUtility.Lerp(minValue, maxValue, random.NextSingle());
}