Implement Get-Random -Count without specifying an InputObject list (#9111)
This commit is contained in:
parent
49c38684a3
commit
e553aef0fb
|
@ -281,7 +281,7 @@ namespace Microsoft.PowerShell.Commands
|
|||
/// <summary>
|
||||
/// Number of items to output (number of list items or of numbers).
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = GetRandomCommand.RandomListItemParameterSet)]
|
||||
[Parameter]
|
||||
[ValidateRange(1, int.MaxValue)]
|
||||
public int Count { get; set; } = 1;
|
||||
|
||||
|
@ -397,11 +397,14 @@ namespace Microsoft.PowerShell.Commands
|
|||
ThrowMinGreaterThanOrEqualMax(minValue, maxValue);
|
||||
}
|
||||
|
||||
int randomNumber = Generator.Next(minValue, maxValue);
|
||||
Debug.Assert(minValue <= randomNumber, "lower bound <= random number");
|
||||
Debug.Assert(randomNumber < maxValue, "random number < upper bound");
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
int randomNumber = Generator.Next(minValue, maxValue);
|
||||
Debug.Assert(minValue <= randomNumber, "lower bound <= random number");
|
||||
Debug.Assert(randomNumber < maxValue, "random number < upper bound");
|
||||
|
||||
WriteObject(randomNumber);
|
||||
WriteObject(randomNumber);
|
||||
}
|
||||
}
|
||||
else if ((IsInt64(maxOperand) || IsInt(maxOperand)) && (IsInt64(minOperand) || IsInt(minOperand)))
|
||||
{
|
||||
|
@ -413,11 +416,14 @@ namespace Microsoft.PowerShell.Commands
|
|||
ThrowMinGreaterThanOrEqualMax(minValue, maxValue);
|
||||
}
|
||||
|
||||
Int64 randomNumber = GetRandomInt64(minValue, maxValue);
|
||||
Debug.Assert(minValue <= randomNumber, "lower bound <= random number");
|
||||
Debug.Assert(randomNumber < maxValue, "random number < upper bound");
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
Int64 randomNumber = GetRandomInt64(minValue, maxValue);
|
||||
Debug.Assert(minValue <= randomNumber, "lower bound <= random number");
|
||||
Debug.Assert(randomNumber < maxValue, "random number < upper bound");
|
||||
|
||||
WriteObject(randomNumber);
|
||||
WriteObject(randomNumber);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -429,11 +435,14 @@ namespace Microsoft.PowerShell.Commands
|
|||
ThrowMinGreaterThanOrEqualMax(minValue, maxValue);
|
||||
}
|
||||
|
||||
double randomNumber = GetRandomDouble(minValue, maxValue);
|
||||
Debug.Assert(minValue <= randomNumber, "lower bound <= random number");
|
||||
Debug.Assert(randomNumber < maxValue, "random number < upper bound");
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
double randomNumber = GetRandomDouble(minValue, maxValue);
|
||||
Debug.Assert(minValue <= randomNumber, "lower bound <= random number");
|
||||
Debug.Assert(randomNumber < maxValue, "random number < upper bound");
|
||||
|
||||
WriteObject(randomNumber);
|
||||
WriteObject(randomNumber);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (EffectiveParameterSet == MyParameterSet.RandomListItem)
|
||||
|
|
|
@ -59,14 +59,29 @@ Describe "Get-Random DRT Unit Tests" -Tags "CI" {
|
|||
)
|
||||
|
||||
# minimum is always set to the actual low end of the range, details refer to closed issue #887.
|
||||
It "get a correct random number for '<Name>'" -TestCases $testData {
|
||||
It "Should return a correct random number for '<Name>'" -TestCases $testData {
|
||||
param($maximum, $minimum, $greaterThan, $lessThan, $type)
|
||||
|
||||
$result = Get-Random -Maximum $maximum -Minimum $minimum
|
||||
$result | Should -BeGreaterThan $greaterThan
|
||||
$result | Should -BeLessThan $lessThan
|
||||
$result | Should -BeOfType $type
|
||||
}
|
||||
|
||||
It "Should return correct random numbers for '<Name>' with Count specified" -TestCases $testData {
|
||||
param($maximum, $minimum, $greaterThan, $lessThan, $type)
|
||||
|
||||
$result = Get-Random -Maximum $maximum -Minimum $minimum -Count 1
|
||||
$result | Should -BeGreaterThan $greaterThan
|
||||
$result | Should -BeLessThan $lessThan
|
||||
$result | Should -BeOfType $type
|
||||
|
||||
$result = Get-Random -Maximum $maximum -Minimum $minimum -Count 3
|
||||
foreach ($randomNumber in $result) {
|
||||
$randomNumber | Should -BeGreaterThan $greaterThan
|
||||
$randomNumber | Should -BeLessThan $lessThan
|
||||
$randomNumber | Should -BeOfType $type
|
||||
}
|
||||
}
|
||||
|
||||
It "Should be able to throw error when '<Name>'" -TestCases $testDataForError {
|
||||
|
@ -111,7 +126,7 @@ Describe "Get-Random" -Tags "CI" {
|
|||
|
||||
It "Should return a number from 1,2,3,5,8,13 " {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13
|
||||
$randomNumber | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
}
|
||||
|
||||
It "Should return an array " {
|
||||
|
@ -123,21 +138,21 @@ Describe "Get-Random" -Tags "CI" {
|
|||
It "Should return three random numbers for array of 1,2,3,5,8,13 " {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3
|
||||
$randomNumber.Count | Should -Be 3
|
||||
$randomNumber[0] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[1] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[2] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[3] | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should return all the numbers for array of 1,2,3,5,8,13 in no particular order" {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count ([int]::MaxValue)
|
||||
$randomNumber.Count | Should -Be 6
|
||||
$randomNumber[0] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[1] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[2] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[3] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[4] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[5] | Should -Be (1 -or 2 -or 3 -or 5 -or 8 -or 13)
|
||||
$randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[3] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[4] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[5] | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
$randomNumber[6] | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
|
@ -163,6 +178,7 @@ Describe "Get-Random" -Tags "CI" {
|
|||
$secondRandomNumber = Get-Random 34359738367 -SetSeed 20
|
||||
$firstRandomNumber | Should -Be @secondRandomNumber
|
||||
}
|
||||
|
||||
It "Should throw an error because the hexadecimal number is to large " {
|
||||
{ Get-Random 0x07FFFFFFFFFFFFFFFF } | Should -Throw "Value was either too large or too small for a UInt32"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue