From d9ffecc36013d053a456117a8dffa4d4ffb6b348 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Tue, 26 Apr 2016 02:16:43 -0700 Subject: [PATCH] Fix the CR issue for Get-Random --- test/powershell/Get-Random.Tests.ps1 | 280 ++++++--------------------- 1 file changed, 56 insertions(+), 224 deletions(-) diff --git a/test/powershell/Get-Random.Tests.ps1 b/test/powershell/Get-Random.Tests.ps1 index ac20889759..d1bda6375c 100644 --- a/test/powershell/Get-Random.Tests.ps1 +++ b/test/powershell/Get-Random.Tests.ps1 @@ -1,232 +1,64 @@ Describe "Get-Random DRT Unit Tests" -Tags DRT{ - - # -minimum is always set to the actual low end of the range, details refer to closed issue #887 - It "Tests for random-numbers mode" { - # Test for Int32 - $results = get-random - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -1 - $results | Should BeLessThan ([int32]::MaxValue) + $testData = @( + @{ Name = 'No params'; Maximum = $null; Minimum = $null; GreaterThan = -1; LessThan = ([int32]::MaxValue); Type = 'System.Int32' } + @{ Name = 'Just maximum: 100'; Maximum = 100; Minimum = $null; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'Just maximum: 10000'; Maximum = 10000; Minimum = $null; GreaterThan = -1; LessThan = 10000; Type = 'System.Int32' } + @{ Name = 'maximum:0, Minimum:-100'; Maximum = 0; Minimum = -100; GreaterThan = -101; LessThan = 0; Type = 'System.Int32' } + @{ Name = 'maximum:100, Minimum:-100'; Maximum = 100; Minimum = -100; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum:-100, Minimum:-200'; Maximum = -100; Minimum = -200; GreaterThan = -201; LessThan = -100; Type = 'System.Int32' } + @{ Name = 'maximum:(-100), Minimum:(-200)'; Maximum = (-100); Minimum = (-200); GreaterThan = -201; LessThan = -100; Type = 'System.Int32' } + @{ Name = 'maximum:8, Minimum:5'; Maximum = '8'; Minimum = 5; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' } + @{ Name = 'maximum:8, Minimum:5'; Maximum = 8; Minimum = '5'; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' } + @{ Name = 'maximum:+100, Minimum:0'; Maximum = +100; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum:+100, Minimum:0'; Maximum = '+100'; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum:+100, Minimum:-100'; Maximum = '+100'; Minimum = '-100'; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum:1kb, Minimum:0'; Maximum = '1kb'; Minimum = 0; GreaterThan = -1; LessThan = 1024; Type = 'System.Int32' } + @{ Name = 'Just maximum:MaxValue'; Maximum = ([int64]::MaxValue); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]::MaxValue); Type = 'System.Int64' } + @{ Name = 'Just maximum:([int64]100)'; Maximum = ([int64]100); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' } + @{ Name = 'Just maximum:100000000000'; Maximum = 100000000000; Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100000000000); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]0), Minimum:([int64]-100)'; Maximum = ([int64]0); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]100), Minimum:([int64]-100)'; Maximum = ([int64]100); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]100); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]-100), Minimum:([int64]-200)'; Maximum = ([int64]-100); Minimum = ([int64]-200); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' } + @{ Name = 'maximum:([int64](-100)), Minimum:([int64](-200))'; Maximum = ([int64](-100)); Minimum = ([int64](-200)); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' } + @{ Name = 'maximum:8, Minimum:([int64]5)'; Maximum = '8'; Minimum = ([int64]5); GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]8), Minimum:5'; Maximum = ([int64]8); Minimum = '5'; GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' } + @{ Name = 'maximum:+100, Minimum:([int64]0)'; Maximum = +100; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' } + @{ Name = 'maximum:+100, Minimum:([int64]0)'; Maximum = '+100'; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]0), Minimum:-100'; Maximum = ([int64]0); Minimum = '-100'; GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]1048585, Minimum:1mb'; Maximum = ([int64]1048585); Minimum = '1mb'; GreaterThan = ([int64]1048575); LessThan = ([int64]1048585); Type = 'System.Int64' } + @{ Name = 'maximum:1tb, Minimum:10mb'; Maximum = '1tb'; Minimum = '10mb'; GreaterThan = ([int64]10485759); LessThan = ([int64]1099511627776); Type = 'System.Int64' } + @{ Name = 'maximum:([int64]::MaxValue), Minimum:([int64]::MinValue)'; Maximum = ([int64]::MaxValue); Minimum = ([int64]::MinValue); GreaterThan = ([int64]::MinValue); LessThan = ([int64]::MaxValue); Type = 'System.Int64' } + @{ Name = 'maximum:([int64](([int]::MaxValue)+15)), Minimum:([int64](([int]::MaxValue)+10))'; Maximum = ([int64](([int]::MaxValue)+15)); Minimum = ([int64](([int]::MaxValue)+10)); GreaterThan = ([int64](([int]::MaxValue)+9)); LessThan = ([int64](([int]::MaxValue)+15)); Type = 'System.Int64' } + @{ Name = 'maximum:([int64](([int]::MaxValue)+150)), Minimum:([int64](([int]::MaxValue)+100))'; Maximum = ([int64](([int]::MaxValue)+150)); Minimum = ([int64](([int]::MaxValue)+100)); GreaterThan = ([int64](([int]::MaxValue)+99)); LessThan = ([int64](([int]::MaxValue)+150)); Type = 'System.Int64' } + @{ Name = 'maximum:100099000001, Minimum:100000000001'; Maximum = '100099000001'; Minimum = 100000000001; GreaterThan = ([int64]10000000000); LessThan = ([int64]100099000001); Type = 'System.Int64' } + @{ Name = 'maximum:100000002230, Minimum:100000002222'; Maximum = 100000002230; Minimum = '100000002222'; GreaterThan = ([int64]100000002221); LessThan = ([int64]100000002230); Type = 'System.Int64' } + @{ Name = 'maximum:90000000000, Minimum:4'; Maximum = 90000000000; Minimum = 4; GreaterThan = ([int64]3); LessThan = ([int64]90000000000); Type = 'System.Int64' } + @{ Name = 'Just maximum:100.0'; Maximum = 100.0; Minimum = $null; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' } + @{ Name = 'maximum:0.0, Minimum:-100.0'; Maximum = 0.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 0.0; Type = 'System.Double' } + @{ Name = 'maximum:100.0, Minimum:-100.0'; Maximum = 100.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 100.0; Type = 'System.Double' } + @{ Name = 'maximum:8.0, Minimum:5'; Maximum = 8.0; Minimum = 5; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' } + @{ Name = 'maximum:8, Minimum:5.0'; Maximum = 8; Minimum = 5.0; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' } + @{ Name = 'maximum:20., Minimum:0.0'; Maximum = 20.; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' } + @{ Name = 'maximum:20., Minimum:0.0'; Maximum = '20.'; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' } + @{ Name = 'maximum:+100.0, Minimum:0'; Maximum = +100.0; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' } + @{ Name = 'maximum:+100.0, Minimum:0'; Maximum = '+100.0'; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' } + @{ Name = 'Just Minimum:1.0e+100'; Maximum = $null; Minimum = 1.0e+100; GreaterThan = 1.0e+99; LessThan = ([double]::MaxValue); Type = 'System.Double' } + @{ Name = 'maximum:([double]::MaxValue), Minimum:([double]::MinValue)'; Maximum = ([double]::MaxValue); Minimum = ([double]::MinValue); GreaterThan = ([double]::MinValue); LessThan = ([double]::MaxValue); Type = 'System.Double' } - $results = get-random 100 - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -1 - $results | Should BeLessThan 100 + ) - $x = 10000 - $results = get-random $x - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -1 - $results | Should BeLessThan 10000 + # 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 ''" -TestCases $testData { + param($maximum, $minimum, $greaterThan, $lessThan, $type) - $results = get-random -Minimum -100 -Maximum 0 - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -101 - $results | Should BeLessThan 0 - - $results = get-random -Minimum -100 -Maximum 100 - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -101 - $results | Should BeLessThan 100 - - $results = get-random -Minimum -200 -Maximum -100 - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -201 - $results | Should BeLessThan -100 - - $results = get-random -Minimum (-200) -Maximum (-100) - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -201 - $results | Should BeLessThan -100 - - $results = get-random -Minimum 5 -Maximum '8' - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan 4 - $results | Should BeLessThan 8 - - $results = get-random -Minimum '5' -Maximum 8 - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan 4 - $results | Should BeLessThan 8 - - $results = get-random -Minimum 0 -Maximum +100 - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -1 - $results | Should BeLessThan 100 - - $results = get-random -Minimum 0 -Maximum '+100' - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -1 - $results | Should BeLessThan 100 - - $results = get-random -Minimum '-100' -Maximum '+100' - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -101 - $results | Should BeLessThan 100 - - $results = get-random -Minimum 0 -Maximum '1kb' - $results.GetType().FullName | Should Be System.Int32 - $results | Should BeGreaterThan -1 - $results | Should BeLessThan 1024 - - #Test for Int64 - $results = get-random ([int64]::MaxValue) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-1) - $results | Should BeLessThan ([int64]::MaxValue) - - $results = get-random ([int64]100) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-1) - $results | Should BeLessThan ([int64]100) - - $results = get-random 100000000000 - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-1) - $results | Should BeLessThan ([int64]100000000000) - - $results = get-random -Minimum ([int64]-100) -Maximum ([int64]0) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-101) - $results | Should BeLessThan ([int64]0) - - $results = get-random -Minimum ([int64]-100) -Maximum ([int64]100) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-101) - $results | Should BeLessThan ([int64]100) - - $results = get-random -Minimum ([int64]-200) -Maximum ([int64]-100) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-201) - $results | Should BeLessThan ([int64]-100) - - $results = get-random -Minimum ([int64](-200)) -Maximum ([int64](-100)) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-201) - $results | Should BeLessThan ([int64]-100) - - - $results = get-random -Minimum ([int64]5) -Maximum '8' - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]4) - $results | Should BeLessThan ([int64]8) - - $results = get-random -Minimum '5' -Maximum ([int64]8) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]4) - $results | Should BeLessThan ([int64]8) - - $results = get-random -Minimum ([int64]0) -Maximum +100 - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-1) - $results | Should BeLessThan ([int64]100) - - $results = get-random -Minimum ([int64]0) -Maximum '+100' - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-1) - $results | Should BeLessThan ([int64]100) - - $results = get-random -Minimum '-100' -Maximum ([int64]0) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]-101) - $results | Should BeLessThan ([int64]0) - - $results = get-random -Minimum '1mb' -Maximum ([int64]1048585) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]1048575) - $results | Should BeLessThan ([int64]1048585) - - $results = get-random -Minimum '10mb' -Maximum '1tb' - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]10485759) - $results | Should BeLessThan ([int64]1099511627776) - - $results = get-random -Minimum ([int64]::MinValue) -Maximum ([int64]::MaxValue) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]::MinValue) - $results | Should BeLessThan ([int64]::MaxValue) - - $results = get-random -Minimum ([int64](([int]::MaxValue)+10)) -Maximum ([int64](([int]::MaxValue)+15)) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]([int32]::MaxValue + 9)) - $results | Should BeLessThan ([int64]([int32]::MaxValue + 15)) - - $results = get-random -Minimum ([int64](([int]::MaxValue)+100)) -Maximum ([int64](([int]::MaxValue)+150)) - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]([int32]::MaxValue + 99)) - $results | Should BeLessThan ([int64]([int32]::MaxValue + 150)) - - $results = get-random -Minimum 100000000001 -Maximum '100099000001' - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]100000000000) - $results | Should BeLessThan ([int64]100099000001) - - $results = get-random -Minimum '100000002222' -Maximum 100000002230 - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]100000002221) - $results | Should BeLessThan ([int64]100000002230) - - $results = get-random -Minimum 4 -Maximum 90000000000 - $results.GetType().FullName | Should Be System.Int64 - $results | Should BeGreaterThan ([int64]3) - $results | Should BeLessThan ([int64]90000000000) - - #Test for double - $results = get-random 100.0 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -1.0 - $results | Should BeLessThan 100.0 - - $results = get-random -Minimum -100.0 -Maximum 0.0 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -101.0 - $results | Should BeLessThan 0.0 - - $results = get-random -Minimum -100.0 -Maximum 100.0 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -101.0 - $results | Should BeLessThan 100.0 - - $results = get-random -Minimum 5 -Maximum 8.0 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan 4.0 - $results | Should BeLessThan 8.0 - - $results = get-random -Minimum 5.0 -Maximum 8 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan 4.0 - $results | Should BeLessThan 8.0 - - $results = get-random -Minimum 0.0 -Maximum 20. - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -1.0 - $results | Should BeLessThan 20.0 - - $results = get-random -Minimum 0.0 -Maximum '20.' - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -1.0 - $results | Should BeLessThan 20.0 - - $results = get-random -Minimum 0 -Maximum +100.0 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -1.0 - $results | Should BeLessThan 100.0 - - $results = get-random -Minimum 0 -Maximum '+100.0' - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan -1.0 - $results | Should BeLessThan 100.0 - - $results = get-random -Minimum 1.0e+100 - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan 1.0e+99 - $results | Should BeLessThan ([double]::MaxValue) - - $results = get-random -minimum ([double]::MinValue) -maximum ([double]::MaxValue) - $results.GetType().FullName | Should Be System.Double - $results | Should BeGreaterThan ([double]::MinValue) - $results | Should BeLessThan ([double]::MaxValue) + $result = Get-Random -Maximum $maximum -Minimum $minimum + $result | Should BeGreaterThan $greaterThan + $result | Should BeLessThan $lessThan + $result.GetType().FullName | Should Be $type + } + + It "Tests for random-numbers mode" { #Verify Error { get-random -Minimum 20 -Maximum 10 } | Should Throw "The Minimum value (20) cannot be greater than or equal to the Maximum value (10)" { get-random -Minimum 20 -Maximum 20 } | Should Throw "The Minimum value (20) cannot be greater than or equal to the Maximum value (20)"