-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from georgimanov/master
Added tests for RandomExtensions #12
- Loading branch information
Showing
9 changed files
with
518 additions
and
2 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Source/MoreDotNet.Test/Extensions/Common/RandomExtensions/NextBoolTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
namespace MoreDotNet.Tests.Extensions.Common.RandomExtensions | ||
{ | ||
using System; | ||
|
||
using MoreDotNet.Extensions.Common; | ||
|
||
using Xunit; | ||
|
||
public class NextBoolTests | ||
{ | ||
private const int NumberOfTests = 10; | ||
|
||
[Fact] | ||
public void NextBool_RandomIsNull_ShouldThrowArgumentNullException() | ||
{ | ||
Random random = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => random.NextBool()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(double.NegativeInfinity)] | ||
[InlineData(double.NaN)] | ||
[InlineData(-NumberOfTests)] | ||
[InlineData(0)] | ||
public void NextBool_ArgumentValueNotPositive_ShouldReturnFalse(double param) | ||
{ | ||
var random = new Random(); | ||
var result = random.NextBool(param); | ||
|
||
Assert.False(result); | ||
} | ||
|
||
[Theory] | ||
[InlineData(double.PositiveInfinity)] | ||
[InlineData(NumberOfTests)] | ||
[InlineData(1)] | ||
public void NextBool_ArgumentValuePositive_ShouldReturnTrue(double param) | ||
{ | ||
var random = new Random(); | ||
var result = random.NextBool(param); | ||
|
||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void NextBool_IteratesGenerations_ShouldReturnTrueOrFalseAtLeastOnce() | ||
{ | ||
var random = new Random(); | ||
int returnedTrueAsResult = 0; | ||
int returnedFalseAsResult = 0; | ||
for (int i = 0; i < NumberOfTests; i++) | ||
{ | ||
if (random.NextBool()) | ||
{ | ||
returnedTrueAsResult++; | ||
} | ||
else | ||
{ | ||
returnedFalseAsResult++; | ||
} | ||
} | ||
|
||
Assert.True(returnedTrueAsResult > 0); | ||
Assert.True(returnedFalseAsResult > 0); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
Source/MoreDotNet.Test/Extensions/Common/RandomExtensions/NextCharTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
namespace MoreDotNet.Tests.Extensions.Common.RandomExtensions | ||
{ | ||
using System; | ||
|
||
using Moq; | ||
using MoreDotNet.Extensions.Common; | ||
using MoreDotNet.Models; | ||
using Xunit; | ||
|
||
public class NextCharTests | ||
{ | ||
[Fact] | ||
public void NextChar_RandomIsNull_ShouldThrowArgumentNullException() | ||
{ | ||
Random random = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => random.NextChar()); | ||
} | ||
|
||
[Fact] | ||
public void NextChar_InvalidCharType_ShouldReturnAlphanumericChar() | ||
{ | ||
Random random = new Random(); | ||
var result = random.NextChar((CharType)(-1)); | ||
|
||
Assert.True(char.IsLetterOrDigit(result)); | ||
} | ||
|
||
[Theory] | ||
[InlineData('A', 0.168)] | ||
[InlineData('1', 0.15)] | ||
public void NextChar_ValidArguments_ShoudReturnAlphanumeriAny(char expectedResult, double returns) | ||
{ | ||
var randomMock = new Mock<Random>(); | ||
randomMock.Setup(r => r.Next(It.IsAny<int>(), It.IsAny<int>())).Returns(expectedResult); | ||
randomMock.Setup(r => r.NextDouble()).Returns(returns); | ||
char actualResult = randomMock.Object.NextChar(); | ||
|
||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
|
||
[Fact] | ||
public void NextChar_ValidArguments_ShoudReturnAnyUnicodeChar() | ||
{ | ||
var expectedChar = '2'; | ||
|
||
var randomMock = new Mock<Random>(); | ||
randomMock.Setup(r => r.Next(It.IsAny<int>(), It.IsAny<int>())).Returns(expectedChar); | ||
char actualResult = randomMock.Object.NextChar(CharType.AnyUnicode); | ||
|
||
Assert.Equal(expectedChar, actualResult); | ||
} | ||
|
||
[Theory] | ||
[InlineData('a', 0.51, CharType.AlphabeticLower)] | ||
[InlineData('A', 0.49, CharType.AlphabeticUpper)] | ||
[InlineData('A', 0.49, CharType.AlphabeticAny)] | ||
[InlineData('a', 0.51, CharType.AlphanumericLower)] | ||
[InlineData('A', 0.49, CharType.AlphanumericUpper)] | ||
[InlineData('A', 0.49, CharType.AlphanumericAny)] | ||
[InlineData('2', 0.1, CharType.Numeric)] | ||
public void NextChar_ValidArguments_ShoudReturnExpectedCharType(char expectedResult, double returns, CharType charType) | ||
{ | ||
var randomMock = new Mock<Random>(); | ||
randomMock.Setup(r => r.Next(It.IsAny<int>(), It.IsAny<int>())).Returns(expectedResult); | ||
randomMock.Setup(r => r.NextDouble()).Returns(returns); | ||
char actualResult = randomMock.Object.NextChar(charType); | ||
|
||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Source/MoreDotNet.Test/Extensions/Common/RandomExtensions/NextDateTime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
namespace MoreDotNet.Tests.Extensions.Common.RandomExtensions | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using MoreDotNet.Extensions.Common; | ||
using Xunit; | ||
|
||
public class NextDateTime | ||
{ | ||
private const int NumberOfTests = 1000; | ||
private readonly DateTime minDate = new DateTime(1999, 10, 31); | ||
private readonly DateTime maxDate = new DateTime(2019, 10, 31); | ||
|
||
[Fact] | ||
public void NextDateTime_RandomIsNull_ShouldThrow_ArgumentNullException() | ||
{ | ||
Random random = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => random.NextDateTime()); | ||
} | ||
|
||
[Fact] | ||
public void NextDateTime_MinValueIsGreaterThanMaxValue_ShouldThrowArgumentException() | ||
{ | ||
var random = new Random(); | ||
|
||
Assert.Throws<ArgumentException>(() => random.NextDateTime(this.maxDate, this.minDate)); | ||
} | ||
|
||
[Fact] | ||
public void NextDateTime_ValidArguments_ShouldReturnDistinctValues99PercentOfTimes() | ||
{ | ||
var random = new Random(); | ||
var dates = new HashSet<DateTime>(); | ||
for (int i = 0; i < NumberOfTests; i++) | ||
{ | ||
dates.Add(random.NextDateTime()); | ||
} | ||
|
||
Assert.True(dates.Count() >= (NumberOfTests * 99 / 100)); | ||
} | ||
|
||
[Fact] | ||
public void NextDateTime_ValidArguments_ShouldReturnResultBetweenMinAndMaxValue() | ||
{ | ||
var random = new Random(); | ||
for (int i = 0; i < NumberOfTests; i++) | ||
{ | ||
var result = random.NextDateTime(this.minDate, this.maxDate); | ||
|
||
Assert.True(result >= this.minDate && result < this.maxDate); | ||
} | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
Source/MoreDotNet.Test/Extensions/Common/RandomExtensions/NextDoubleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
namespace MoreDotNet.Tests.Extensions.Common.RandomExtensions | ||
{ | ||
using System; | ||
|
||
using Moq; | ||
using MoreDotNet.Extensions.Common; | ||
|
||
using Xunit; | ||
|
||
public class NextDoubleTests | ||
{ | ||
private const int NumberOfTests = 1000; | ||
private const double DoubleResult = 0.15; | ||
private const double MinValue = -50; | ||
private const double MaxValue = 150; | ||
|
||
[Fact] | ||
public void NextDouble_RandomIsNull_ShouldThrow_ArgumentNullException() | ||
{ | ||
Random random = null; | ||
|
||
Assert.Throws<ArgumentNullException>(() => random.NextDouble(MinValue, MaxValue)); | ||
} | ||
|
||
[Fact] | ||
public void NextDouble_ValidArguments_ShouldReturnResultsBetweenMinAndMaxValue() | ||
{ | ||
var random = new Random(); | ||
for (int i = 0; i < NumberOfTests; i++) | ||
{ | ||
var result = random.NextDouble(MinValue, MaxValue); | ||
|
||
Assert.True(result >= MinValue && result < MaxValue); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void NextDouble_MinValueIsGreaterThanMaxValue_ShouldThrow_ArgumentException() | ||
{ | ||
var random = new Random(); | ||
|
||
Assert.Throws<ArgumentException>(() => random.NextDouble(MaxValue, MinValue)); | ||
} | ||
|
||
[Fact] | ||
public void NextDouble_MockedRandom_ShouldReturn_PredifinedValue() | ||
{ | ||
var randomMock = new Mock<Random>(); | ||
randomMock.Setup(r => r.NextDouble()).Returns(DoubleResult); | ||
var actualResult = randomMock.Object.NextDouble(MinValue, MaxValue); | ||
var expectedResult = MinValue + (DoubleResult * (MaxValue - MinValue)); | ||
|
||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
|
||
[Theory] | ||
[InlineData(double.MinValue, double.MaxValue)] | ||
[InlineData(MinValue, MaxValue)] | ||
public void NextDouble_ValidArguments_ShoulReturnValidResults(double min, double max) | ||
{ | ||
var random = new Random(); | ||
var result = random.NextDouble(min, max); | ||
|
||
Assert.True(result >= min && result < max); | ||
} | ||
|
||
[Theory] | ||
[InlineData(double.NaN, MaxValue)] | ||
[InlineData(MinValue, double.NaN)] | ||
[InlineData(double.NaN, double.NaN)] | ||
[InlineData(double.NegativeInfinity, double.PositiveInfinity)] | ||
[InlineData(double.NegativeInfinity, MaxValue)] | ||
public void NextDouble_InfinityOrNanValuesParams_ShoulReturn_NaNValues(double min, double max) | ||
{ | ||
var random = new Random(); | ||
var result = random.NextDouble(min, max); | ||
|
||
Assert.Equal(double.NaN, result); | ||
} | ||
|
||
[Fact] | ||
public void NextDouble_PositiveInfinityAsMaxValue_ShoulReturnPositiveInfinity() | ||
{ | ||
var random = new Random(); | ||
var result = random.NextDouble(MinValue, double.PositiveInfinity); | ||
|
||
Assert.Equal(double.PositiveInfinity, result); | ||
} | ||
} | ||
} |
Oops, something went wrong.