diff --git a/source/TestUtils/PeanutButter.RandomGenerators.Tests/TestRandomValueGen.cs b/source/TestUtils/PeanutButter.RandomGenerators.Tests/TestRandomValueGen.cs index 4a34218473..51d9158bea 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators.Tests/TestRandomValueGen.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators.Tests/TestRandomValueGen.cs @@ -2825,6 +2825,80 @@ public void GetRandomOfTShouldNotClobberStaticFields() } } + [TestFixture] + public class WhenGivenLowerBoundOnly: TestBase + { + [Test] + [Repeat(NORMAL_RANDOM_TEST_CYCLES)] + public void ShouldProduceIntWithinRange() + { + // Arrange + var lowerBound = GetRandomInt(1000, 10000); + // Act + var result = GetRandomInt(lowerBound); + // Assert + Expect(result) + .To.Be.Greater.Than + .Or.Equal.To(lowerBound); + } + + [Test] + [Repeat(NORMAL_RANDOM_TEST_CYCLES)] + public void ShouldProduceLongWithinRange() + { + // Arrange + var lowerBound = GetRandomLong(1000, 10000); + // Act + var result = GetRandomLong(lowerBound); + // Assert + Expect(result) + .To.Be.Greater.Than + .Or.Equal.To(lowerBound); + } + + [Test] + [Repeat(NORMAL_RANDOM_TEST_CYCLES)] + public void ShouldProduceDoubleWithinRange() + { + // Arrange + var lowerBound = GetRandomDouble(1000, 10000); + // Act + var result = GetRandomDouble(lowerBound); + // Assert + Expect(result) + .To.Be.Greater.Than + .Or.Equal.To(lowerBound); + } + + [Test] + [Repeat(NORMAL_RANDOM_TEST_CYCLES)] + public void ShouldProduceDecimalWithinRange() + { + // Arrange + var lowerBound = GetRandomDecimal(1000, 10000); + // Act + var result = GetRandomDecimal(lowerBound); + // Assert + Expect(result) + .To.Be.Greater.Than + .Or.Equal.To(lowerBound); + } + + [Test] + [Repeat(NORMAL_RANDOM_TEST_CYCLES)] + public void ShouldProduceFloatWithinRange() + { + // Arrange + var lowerBound = GetRandomFloat(1000, 10000); + // Act + var result = GetRandomFloat(lowerBound); + // Assert + Expect(result) + .To.Be.Greater.Than + .Or.Equal.To(lowerBound); + } + } + internal static class Matchers { diff --git a/source/TestUtils/PeanutButter.RandomGenerators/RandomValueGen.cs b/source/TestUtils/PeanutButter.RandomGenerators/RandomValueGen.cs index 418501f747..f53f749087 100644 --- a/source/TestUtils/PeanutButter.RandomGenerators/RandomValueGen.cs +++ b/source/TestUtils/PeanutButter.RandomGenerators/RandomValueGen.cs @@ -116,6 +116,21 @@ public static T GetRandom() /// public static class DefaultRanges { + /// + /// Default minimum number of words to generate + /// + public const int MIN_WORDS = 10; + + /// + /// Default maximum number of words to generate + /// + public const int MAX_WORDS = 50; + + /// + /// Default range of the number of words to generate + /// + public const int DEFAULT_WORD_RANGE = MAX_WORDS - MIN_WORDS; + /// /// Default minimum length of random strings /// @@ -131,6 +146,11 @@ public static class DefaultRanges /// public const int MAXLENGTH_BYTES = 1024; + /// + /// Defines the range of default bytes + /// + public const int DEFAULT_BYTES_RANGE = MAXLENGTH_BYTES - MINLENGTH_BYTES; + /// /// Default minimum integer value returned /// @@ -141,6 +161,11 @@ public static class DefaultRanges /// public const int MAX_INT_VALUE = 10; + /// + /// Defines the range of default integer max / min + /// + public const int DEFAULT_INT_RANGE = MAX_INT_VALUE - MIN_INT_VALUE; + /// /// Default minimum long value returned /// @@ -151,6 +176,11 @@ public static class DefaultRanges /// public const int MAX_LONG_VALUE = 1000; + /// + /// Defines the range of default long min / max + /// + public const int DEFAULT_LONG_RANGE = MAX_LONG_VALUE - MIN_LONG_VALUE; + /// /// Default minimum number of items in a random collection /// @@ -170,6 +200,33 @@ public static class DefaultRanges private static readonly Random RandomGenerator = new Random(); private const string DEFAULT_RANDOM_STRING_CHARS = "abcdefghijklmnopqrstuvwxyz1234567890"; + /// + /// Produces a random integer between 0 and 10 inclusive + /// + /// + public static int GetRandomInt() + { + return GetRandomInt( + DefaultRanges.MIN_INT_VALUE, + DefaultRanges.MAX_INT_VALUE + ); + } + + /// + /// Produces an integer between the provided value and + /// that value + 10, inclusive + /// + /// + /// + public static int GetRandomInt( + int minValue) + { + return GetRandomInt( + minValue, + minValue + DefaultRanges.DEFAULT_INT_RANGE + ); + } + /// /// Returns a random integer within the specified range /// @@ -177,8 +234,8 @@ public static class DefaultRanges /// Maximum value to consider /// Random integer between minValue and maxValue (inclusive) public static int GetRandomInt( - int minValue = DefaultRanges.MIN_INT_VALUE, - int maxValue = DefaultRanges.MAX_INT_VALUE) + int minValue, + int maxValue) { return (int) GetRandomLong(minValue, maxValue); } @@ -212,6 +269,32 @@ public static string GetRandomMIMEType() return MimeTypes[idx]; } + /// + /// Produces a random long between 0 and 1000, inclusive + /// + /// + public static long GetRandomLong() + { + return GetRandomLong( + DefaultRanges.MIN_LONG_VALUE + ); + } + + /// + /// Returns a random long between the provided min value and + /// that value + 1000, inclusive + /// + /// + /// + public static long GetRandomLong( + long minValue) + { + return GetRandomLong( + minValue, + minValue + DefaultRanges.DEFAULT_LONG_RANGE + ); + } + /// /// Returns a random long within the specified range /// @@ -219,8 +302,8 @@ public static string GetRandomMIMEType() /// Maximum value to consider /// Random integer between minValue and maxValue (inclusive) public static long GetRandomLong( - long minValue = DefaultRanges.MIN_LONG_VALUE, - long maxValue = DefaultRanges.MAX_LONG_VALUE) + long minValue, + long maxValue) { if (minValue > maxValue) { @@ -511,6 +594,30 @@ public static TimeSpan GetRandomTimeSpan() : value; } + /// + /// Produces a random double value between 0 and 10 inclusive + /// + /// + public static double GetRandomDouble() + { + return GetRandomDouble(DefaultRanges.MIN_INT_VALUE); + } + + /// + /// Produces a random double value between the provides + /// double value and that value + 10, inclusive + /// + /// + /// + public static double GetRandomDouble( + double minValue) + { + return GetRandomDouble( + minValue, + minValue + DefaultRanges.DEFAULT_INT_RANGE + ); + } + /// /// Gets a random double value within the specified range /// @@ -518,8 +625,8 @@ public static TimeSpan GetRandomTimeSpan() /// Maximum value to consider /// Double value within the specified range public static double GetRandomDouble( - double min = DefaultRanges.MIN_INT_VALUE, - double max = DefaultRanges.MAX_INT_VALUE + double min, + double max ) { return (RandomGenerator.NextDouble() * (max - min)) + min; @@ -539,6 +646,59 @@ public static TimeSpan GetRandomTimeSpan() return (decimal) GetRandomDouble((double) min, (double) max); } + /// + /// Produces a random decimal between 0 and 10 inclusive + /// + /// + public static decimal GetRandomDecimal() + { + return GetRandomDecimal( + DefaultRanges.MIN_INT_VALUE + ); + } + + /// + /// Produces a random decimal between the provided + /// minValue and that value + 10, inclusive + /// + /// + /// + public static decimal GetRandomDecimal( + decimal minValue + ) + { + return GetRandomDecimal( + minValue, + minValue + DefaultRanges.DEFAULT_INT_RANGE + ); + } + + /// + /// Produces a random float between 0 and 10 inclusive + /// + /// + public static float GetRandomFloat() + { + return GetRandomFloat( + DefaultRanges.MIN_INT_VALUE + ); + } + + /// + /// Produces a random float between the provided + /// minValue and that value + 10, inclusive + /// + /// + /// + public static float GetRandomFloat( + float minValue) + { + return GetRandomFloat( + minValue, + minValue + DefaultRanges.MIN_INT_VALUE + ); + } + /// /// Gets a random float value within the specified range /// @@ -546,12 +706,38 @@ public static TimeSpan GetRandomTimeSpan() /// Maximum value to consider /// Float value within the specified range public static float GetRandomFloat( - float min = 0f, - float max = DefaultRanges.MAX_INT_VALUE) + float min, + float max) { return (float) GetRandomDouble(min, max); } + /// + /// Produces a random time of day + /// + /// + public static TimeSpan GetRandomTimeOfDay() + { + return GetRandomTimeOfDay( + TimeSpan.FromSeconds(0) + ); + } + + /// + /// Produces a random time of day from the provided + /// minimum, inclusive + /// + /// + /// + public static TimeSpan GetRandomTimeOfDay( + TimeSpan min) + { + return GetRandomTimeOfDay( + min, + TimeSpan.FromSeconds(DefaultRanges.ONE_DAY_IN_SECONDS) + ); + } + /// /// Returns a random time of day /// @@ -559,11 +745,11 @@ public static TimeSpan GetRandomTimeSpan() /// Maximum time to consider /// Timespan representing a time on a day, clamped to within 24 hours public static TimeSpan GetRandomTimeOfDay( - TimeSpan? min = null, - TimeSpan? max = null) + TimeSpan min, + TimeSpan max) { - var minSeconds = min?.TotalSeconds ?? 0; - var maxSeconds = max?.TotalSeconds ?? DefaultRanges.ONE_DAY_IN_SECONDS; + var minSeconds = min.TotalSeconds; + var maxSeconds = max.TotalSeconds; if (minSeconds < 0) { minSeconds = 0; @@ -579,6 +765,32 @@ public static TimeSpan GetRandomTimeSpan() ); } + /// + /// Produces an array of random bytes between 0 and 1024 + /// in length, inclusive + /// + /// + public static byte[] GetRandomBytes() + { + return GetRandomBytes(DefaultRanges.MINLENGTH_BYTES); + } + + /// + /// Produces some random bytes, of at least minLength + /// in size, up to that length + 1024, inclusive + /// + /// + /// + public static byte[] GetRandomBytes( + int minLength + ) + { + return GetRandomBytes( + minLength, + minLength + DefaultRanges.DEFAULT_BYTES_RANGE + ); + } + /// /// Gets a randomly-sized, randomly-filled byte array /// @@ -586,8 +798,8 @@ public static TimeSpan GetRandomTimeSpan() /// Maximum size of the result /// Randomly-filled byte array public static byte[] GetRandomBytes( - int minLength = DefaultRanges.MINLENGTH_BYTES, - int maxLength = DefaultRanges.MAXLENGTH_BYTES + int minLength, + int maxLength ) { var bytes = new byte[RandomGenerator.Next(minLength, maxLength)]; @@ -603,7 +815,13 @@ public static TimeSpan GetRandomTimeSpan() /// Random email-like string public static string GetRandomEmail() { - return string.Join(string.Empty, GetRandomString(), "@", GetRandomString(), ".com"); + return string.Join( + string.Empty, + GetRandomString(), + "@", + GetRandomString(), + GetRandomFrom(new[] { ".com", ".net", ".org" }) + ); } /// @@ -613,7 +831,11 @@ public static string GetRandomEmail() /// String which is a random filename with a 3 character extension public static string GetRandomFileName() { - return string.Join(".", GetRandomString(10, 20), GetRandomString(3, 3)); + return string.Join( + ".", + GetRandomString(10, 20), + GetRandomString(3, 3) + ); } /// @@ -631,19 +853,44 @@ public static string GetRandomWindowsPath() new[] { drive }.And(folders.ToArray())); } + /// + /// Produces a collection of words between 10 and 50 words in + /// length, inclusive + /// + /// + public static string GetRandomWords() + { + return GetRandomWords(DefaultRanges.MIN_WORDS); + } + + /// + /// Produces a collection of words with count of at + /// least minWords up to and including minWords + 50 + /// + /// + /// + public static string GetRandomWords( + int minWords) + { + return GetRandomWords( + minWords, + minWords + DefaultRanges.DEFAULT_WORD_RANGE + ); + } + /// /// Gets some random pseudo-words. Note that they (probably) won't be /// readable words -- just a collection of strings with whitespace in between. /// Think of this as something like Lorei Ipsum, except with zero meaning. /// - /// Minimum number of "words" to return - /// Maximum number of "words" to return + /// Minimum number of "words" to return + /// Maximum number of "words" to return /// Block of text with "words" and whitespace public static string GetRandomWords( - int min = 10, - int max = 50) + int minWords, + int maxWords) { - var actual = GetRandomInt(min, max); + var actual = GetRandomInt(minWords, maxWords); var words = new List(); for (var i = 0; i < actual; i++) {