Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-Random.Tests.ps1210 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-Random DRT Unit Tests" -Tags "CI" {4    $testData = @(5        @{ Name = 'no params'; Maximum = $null; Minimum = $null; GreaterThan = -1; LessThan = ([int32]::MaxValue); Type = 'System.Int32' }6        @{ Name = 'only positive maximum number'; Maximum = 100; Minimum = $null; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' }7        @{ Name = 'maximum set to 0, Minimum to a negative number'; Maximum = 0; Minimum = -100; GreaterThan = -101; LessThan = 0; Type = 'System.Int32' }8        @{ Name = 'positive maximum, negative Minimum'; Maximum = 100; Minimum = -100; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' }9        @{ Name = 'both negative'; Maximum = -100; Minimum = -200; GreaterThan = -201; LessThan = -100; Type = 'System.Int32' }10        @{ Name = 'both negative with parentheses'; Maximum = (-100); Minimum = (-200); GreaterThan = -201; LessThan = -100; Type = 'System.Int32' }11        @{ Name = 'maximum enclosed in quote'; Maximum = '8'; Minimum = 5; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' }12        @{ Name = 'minimum enclosed in quote'; Maximum = 8; Minimum = '5'; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' }13        @{ Name = 'maximum with plus sign'; Maximum = +100; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' }14        @{ Name = 'maximum with plus sign and quote'; Maximum = '+100'; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' }15        @{ Name = 'both with quote'; Maximum = '+100'; Minimum = '-100'; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' }16        @{ Name = 'maximum set to kb'; Maximum = '1kb'; Minimum = 0; GreaterThan = -1; LessThan = 1024; Type = 'System.Int32' }17        @{ Name = 'maximum is Int64.MaxValue'; Maximum = ([int64]::MaxValue); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]::MaxValue); Type = 'System.Int64' }18        @{ Name = 'maximum is a 64-bit integer'; Maximum = ([int64]100); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' }19        @{ Name = 'maximum set to a large integer greater than int32.MaxValue'; Maximum = 100000000000; Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100000000000); Type = 'System.Int64' }20        @{ Name = 'maximum set to 0, Minimum set to a negative 64-bit integer'; Maximum = ([int64]0); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' }21        @{ Name = 'maximum set to positive 64-bit number, min set to negative 64-bit number'; Maximum = ([int64]100); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]100); Type = 'System.Int64' }22        @{ Name = 'both are negative 64-bit number'; Maximum = ([int64]-100); Minimum = ([int64]-200); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' }23        @{ Name = 'both are negative 64-bit number with parentheses'; Maximum = ([int64](-100)); Minimum = ([int64](-200)); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' }24        @{ Name = 'max is 32-bit, min is 64-bit integer'; Maximum = '8'; Minimum = ([int64]5); GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' }25        @{ Name = 'max is 64-bit, min is 32-bit integer'; Maximum = ([int64]8); Minimum = '5'; GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' }26        @{ Name = 'max set to a 32-bit integer, min set to [int64]0'; Maximum = +100; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' }27        @{ Name = 'max set to a 32-bit integer with quote'; Maximum = '+100'; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' }28        @{ Name = 'max is [int64]0, min is a 32-bit integer'; Maximum = ([int64]0); Minimum = '-100'; GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' }29        @{ Name = 'min set to 1MB, max set to a 64-bit integer greater than min'; Maximum = ([int64]1048585); Minimum = '1mb'; GreaterThan = ([int64]1048575); LessThan = ([int64]1048585); Type = 'System.Int64' }30        @{ Name = 'max set to 1tb, min set to 10 mb'; Maximum = '1tb'; Minimum = '10mb'; GreaterThan = ([int64]10485759); LessThan = ([int64]1099511627776); Type = 'System.Int64' }31        @{ Name = 'max is int64.MaxValue, min is Int64.MinValue'; Maximum = ([int64]::MaxValue); Minimum = ([int64]::MinValue); GreaterThan = ([int64]::MinValue); LessThan = ([int64]::MaxValue); Type = 'System.Int64' }32        @{ Name = 'both are int64.MaxValue plus a 32-bit integer'; Maximum = ([int64](([int]::MaxValue)+15)); Minimum = ([int64](([int]::MaxValue)+10)); GreaterThan = ([int64](([int]::MaxValue)+9)); LessThan = ([int64](([int]::MaxValue)+15)); Type = 'System.Int64' }33        @{ Name = 'both are greater than int32.MaxValue without specified type, and max with quote'; Maximum = '100099000001'; Minimum = 100000000001; GreaterThan = ([int64]10000000000); LessThan = ([int64]100099000001); Type = 'System.Int64' }34        @{ Name = 'both are greater than int32.MaxValue without specified type, and min with quote'; Maximum = 100000002230; Minimum = '100000002222'; GreaterThan = ([int64]100000002221); LessThan = ([int64]100000002230); Type = 'System.Int64' }35        @{ Name = 'max is greater than int32.MaxValue without specified type'; Maximum = 90000000000; Minimum = 4; GreaterThan = ([int64]3); LessThan = ([int64]90000000000); Type = 'System.Int64' }36        @{ Name = 'max is a double-precision number'; Maximum = 100.0; Minimum = $null; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' }37        @{ Name = 'both are double-precision numbers, min is negative.'; Maximum = 0.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 0.0; Type = 'System.Double' }38        @{ Name = 'both are double-precision number, max is positive, min is negative.'; Maximum = 100.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 100.0; Type = 'System.Double' }39        @{ Name = 'max is a double-precision number, min is int32'; Maximum = 8.0; Minimum = 5; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' }40        @{ Name = 'min is a double-precision number, max is int32'; Maximum = 8; Minimum = 5.0; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' }41        @{ Name = 'max set to a special double number'; Maximum = 20.; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' }42        @{ Name = 'max is double with quote'; Maximum = '20.'; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' }43        @{ Name = 'max is double with plus sign'; Maximum = +100.0; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' }44        @{ Name = 'max is double with plus sign and enclosed in quote'; Maximum = '+100.0'; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' }45        @{ Name = 'both set to the special numbers as 1.0e+xx '; Maximum = $null; Minimum = 1.0e+100; GreaterThan = 1.0e+99; LessThan = ([double]::MaxValue); Type = 'System.Double' }46        @{ Name = 'max is Double.MaxValue, min is Double.MinValue'; Maximum = ([double]::MaxValue); Minimum = ([double]::MinValue); GreaterThan = ([double]::MinValue); LessThan = ([double]::MaxValue); Type = 'System.Double' }47    )48 49    $testDataForError = @(50        @{ Name = 'Min is greater than max and all are positive 32-bit integer'; Maximum = 10; Minimum = 20}51        @{ Name = 'Min and Max are same and all are positive 32-bit integer'; Maximum = 20; Minimum = 20}52        @{ Name = 'Min is greater than max and all are negative 32-bit integer'; Maximum = -20; Minimum = -10}53        @{ Name = 'Min and Max are same and all are negative 32-bit integer'; Maximum = -20; Minimum = -20}54        @{ Name = 'Min is greater than max and all are positive double-precision number'; Maximum = 10.0; Minimum = 20.0}55        @{ Name = 'Min and Max are same and all are positive double-precision number'; Maximum = 20.0; Minimum = 20.0}56        @{ Name = 'Min is greater than max and all are negative double-precision number'; Maximum = -20.0; Minimum = -10.0}57        @{ Name = 'Min and Max are same and all are negative double-precision number'; Maximum = -20.0; Minimum = -20.0}58        @{ Name = 'Max is a negative number, min is the default number '; Maximum = -10; Minimum = $null}59    )60 61    # minimum is always set to the actual low end of the range, details refer to closed issue #887.62    It "Should return a correct random number for '<Name>'" -TestCases $testData {63        param($maximum, $minimum, $greaterThan, $lessThan, $type)64 65        $result = Get-Random -Maximum $maximum -Minimum $minimum66        $result | Should -BeGreaterThan $greaterThan67        $result | Should -BeLessThan $lessThan68        $result | Should -BeOfType $type69    }70 71    It "Should return correct random numbers for '<Name>' with Count specified" -TestCases $testData {72        param($maximum, $minimum, $greaterThan, $lessThan, $type)73 74        $result = Get-Random -Maximum $maximum -Minimum $minimum -Count 175        $result | Should -BeGreaterThan $greaterThan76        $result | Should -BeLessThan $lessThan77        $result | Should -BeOfType $type78 79        $result = Get-Random -Maximum $maximum -Minimum $minimum -Count 380        foreach ($randomNumber in $result) {81            $randomNumber | Should -BeGreaterThan $greaterThan82            $randomNumber | Should -BeLessThan $lessThan83            $randomNumber | Should -BeOfType $type84        }85    }86 87    It "Should be able to throw error when '<Name>'" -TestCases $testDataForError {88        param($maximum, $minimum)89        { Get-Random -Minimum $minimum -Maximum $maximum } | Should -Throw -ErrorId "MinGreaterThanOrEqualMax,Microsoft.PowerShell.Commands.GetRandomCommand"90    }91 92    It "Tests for setting the seed" {93        $result1 = (Get-Random -SetSeed 123), (Get-Random)94        $result2 = (Get-Random -SetSeed 123), (Get-Random)95        $result1 | Should -Be $result296    }97}98 99Describe "Get-Random" -Tags "CI" {100    It "Should return a random number greater than -1" {101        Get-Random | Should -BeGreaterThan -1102    }103 104    It "Should return a random number less than 100" {105        Get-Random -Maximum 100 | Should -BeLessThan 100106        Get-Random -Maximum 100 | Should -BeGreaterThan -1107    }108 109    It "Should return a random number less than 100 and greater than -100 " {110        $randomNumber = Get-Random -Minimum -100 -Maximum 100111        $randomNumber | Should -BeLessThan 100112        $randomNumber | Should -BeGreaterThan -101113    }114 115    It "Should return a random number less than 20.93 and greater than 10.7 " {116        $randomNumber = Get-Random -Minimum 10.7 -Maximum 20.93117        $randomNumber | Should -BeLessThan 20.93118        $randomNumber | Should -BeGreaterThan 10.7119    }120 121    It "Should return same number for both Get-Random when switch SetSeed is used " {122        $firstRandomNumber = Get-Random -Maximum 100 -SetSeed 23123        $secondRandomNumber = Get-Random -Maximum 100 -SetSeed 23124        $firstRandomNumber | Should -Be $secondRandomNumber125    }126 127    It "Should return a number from 1,2,3,5,8,13 " {128        $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13129        $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13130    }131 132    It "Should return an array " {133        $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3134        $randomNumber.Count | Should -Be 3135        ,$randomNumber | Should -BeOfType System.Array136    }137 138    It "Should return three random numbers for array of 1,2,3,5,8,13 " {139        $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3140        $randomNumber.Count | Should -Be 3141        $randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13142        $randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13143        $randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13144        $randomNumber[3] | Should -BeNullOrEmpty145    }146 147    It "Should return all the numbers for array of 1,2,3,5,8,13 in no particular order" {148        $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count ([int]::MaxValue)149        $randomNumber.Count | Should -Be 6150        $randomNumber[0] | Should -BeIn 1, 2, 3, 5, 8, 13151        $randomNumber[1] | Should -BeIn 1, 2, 3, 5, 8, 13152        $randomNumber[2] | Should -BeIn 1, 2, 3, 5, 8, 13153        $randomNumber[3] | Should -BeIn 1, 2, 3, 5, 8, 13154        $randomNumber[4] | Should -BeIn 1, 2, 3, 5, 8, 13155        $randomNumber[5] | Should -BeIn 1, 2, 3, 5, 8, 13156        $randomNumber[6] | Should -BeNullOrEmpty157    }158 159    It "Should return all the numbers for array of 1,2,3,5,8,13 in randomized order when the Shuffle switch is used" {160        $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Shuffle161        $randomNumber.Count | Should -Be 6162        $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13163    }164 165    It "Should return a single random item when -Shuffle:`$false is used" {166        $randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Shuffle:$false167        $randomNumber.Count | Should -Be 1168        $randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13169    }170 171    It "Should return for a string collection " {172        $randomNumber = Get-Random -InputObject "red", "yellow", "blue"173        $randomNumber | Should -Be ("red" -or "yellow" -or "blue")174    }175 176    It "Should return a number for hexadecimal " {177        $randomNumber = Get-Random 0x07FFFFFFFFF178        $randomNumber | Should -BeLessThan 549755813887179        $randomNumber | Should -BeGreaterThan 0180    }181 182    It "Should return false, check two random numbers are not equal when not using the SetSeed switch " {183        $firstRandomNumber = Get-Random184        $secondRandomNumber = Get-Random185        $firstRandomNumber | Should -Not -Be $secondRandomNumber186    }187 188    It "Should return the same number for hexadecimal number and regular number when the switch SetSeed is used " {189        $firstRandomNumber = Get-Random 0x07FFFFFFFF -SetSeed 20190        $secondRandomNumber = Get-Random 34359738367 -SetSeed 20191        $firstRandomNumber | Should -Be @secondRandomNumber192    }193 194    It "Should throw an error because the hexadecimal number is to large " {195        { Get-Random 0x07FFFFFFFFFFFFFFFF } | Should -Throw "Value was either too large or too small for a UInt32"196    }197 198    It "Should accept collection containing empty string for -InputObject" {199        1..10 | ForEach-Object {200            Get-Random -InputObject @('a','b','') | Should -BeIn 'a','b',''201        }202    }203 204    It "Should accept `$null in collection for -InputObject" {205        1..10 | ForEach-Object {206            Get-Random -InputObject @('a','b',$null) | Should -BeIn 'a','b',$null207        }208    }209}210