From 3437566ebdb8edd8121b2d3835817aa38cbf96cb Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Thu, 5 May 2016 03:08:20 -0700 Subject: [PATCH] Fix the CR issues for Get-Random --- test/powershell/Get-Random.Tests.ps1 | 128 ++++++++++++++------------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/test/powershell/Get-Random.Tests.ps1 b/test/powershell/Get-Random.Tests.ps1 index d1bda6375..58cca7cb9 100644 --- a/test/powershell/Get-Random.Tests.ps1 +++ b/test/powershell/Get-Random.Tests.ps1 @@ -1,49 +1,60 @@ Describe "Get-Random DRT Unit Tests" -Tags DRT{ $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' } + @{ Name = 'no params'; Maximum = $null; Minimum = $null; GreaterThan = -1; LessThan = ([int32]::MaxValue); Type = 'System.Int32' } + @{ Name = 'only positive maximum number'; Maximum = 100; Minimum = $null; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum set to 0, Minimum to a negative number'; Maximum = 0; Minimum = -100; GreaterThan = -101; LessThan = 0; Type = 'System.Int32' } + @{ Name = 'positive maximum, negative Minimum'; Maximum = 100; Minimum = -100; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'both negative'; Maximum = -100; Minimum = -200; GreaterThan = -201; LessThan = -100; Type = 'System.Int32' } + @{ Name = 'both negative with parentheses'; Maximum = (-100); Minimum = (-200); GreaterThan = -201; LessThan = -100; Type = 'System.Int32' } + @{ Name = 'maximum enclosed in quote'; Maximum = '8'; Minimum = 5; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' } + @{ Name = 'minimum enclosed in quote'; Maximum = 8; Minimum = '5'; GreaterThan = 4; LessThan = 8; Type = 'System.Int32' } + @{ Name = 'maximum with plus sign'; Maximum = +100; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum with plus sign and quote'; Maximum = '+100'; Minimum = 0; GreaterThan = -1; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'both with quote'; Maximum = '+100'; Minimum = '-100'; GreaterThan = -101; LessThan = 100; Type = 'System.Int32' } + @{ Name = 'maximum set to kb'; Maximum = '1kb'; Minimum = 0; GreaterThan = -1; LessThan = 1024; Type = 'System.Int32' } + @{ Name = 'maximum is Int64.MaxValue'; Maximum = ([int64]::MaxValue); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]::MaxValue); Type = 'System.Int64' } + @{ Name = 'maximum is a 64-bit integer'; Maximum = ([int64]100); Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' } + @{ Name = 'maximum set to a large integer greater than int32.MaxValue'; Maximum = 100000000000; Minimum = $null; GreaterThan = ([int64]-1); LessThan = ([int64]100000000000); Type = 'System.Int64' } + @{ Name = 'maximum set to 0, Minumum set to a negative 64-bit integer'; Maximum = ([int64]0); Minimum = ([int64]-100); GreaterThan = ([int64]-101); LessThan = ([int64]0); Type = 'System.Int64' } + @{ 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' } + @{ Name = 'both are negative 64-bit number'; Maximum = ([int64]-100); Minimum = ([int64]-200); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' } + @{ Name = 'both are negative 64-bit number with parentheses'; Maximum = ([int64](-100)); Minimum = ([int64](-200)); GreaterThan = ([int64]-201); LessThan = ([int64]-100); Type = 'System.Int64' } + @{ Name = 'max is 32-bit, min is 64-bit integer'; Maximum = '8'; Minimum = ([int64]5); GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' } + @{ Name = 'max is 64-bit, min is 32-bit integer'; Maximum = ([int64]8); Minimum = '5'; GreaterThan = ([int64]4); LessThan = ([int64]8); Type = 'System.Int64' } + @{ 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' } + @{ Name = 'max set to a 32-bit integer with quote'; Maximum = '+100'; Minimum = ([int64]0); GreaterThan = ([int64]-1); LessThan = ([int64]100); Type = 'System.Int64' } + @{ 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' } + @{ 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' } + @{ Name = 'max set to 1tb, min set to 10 mb'; Maximum = '1tb'; Minimum = '10mb'; GreaterThan = ([int64]10485759); LessThan = ([int64]1099511627776); Type = 'System.Int64' } + @{ Name = 'max is int64.MaxValue, min is Int64.MinValue'; Maximum = ([int64]::MaxValue); Minimum = ([int64]::MinValue); GreaterThan = ([int64]::MinValue); LessThan = ([int64]::MaxValue); Type = 'System.Int64' } + @{ 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' } + @{ 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' } + @{ 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' } + @{ Name = 'max is greater than int32.MaxValue without specified type'; Maximum = 90000000000; Minimum = 4; GreaterThan = ([int64]3); LessThan = ([int64]90000000000); Type = 'System.Int64' } + @{ Name = 'max is a double-precision number'; Maximum = 100.0; Minimum = $null; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' } + @{ Name = 'both are double-precision numbers, min is negative.'; Maximum = 0.0; Minimum = -100.0; GreaterThan = -101.0; LessThan = 0.0; Type = 'System.Double' } + @{ 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' } + @{ Name = 'max is a double-precision number, min is int32'; Maximum = 8.0; Minimum = 5; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' } + @{ Name = 'min is a double-precision number, max is int32'; Maximum = 8; Minimum = 5.0; GreaterThan = 4.0; LessThan = 8.0; Type = 'System.Double' } + @{ Name = 'max set to a special double number'; Maximum = 20.; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' } + @{ Name = 'max is double with quote'; Maximum = '20.'; Minimum = 0.0; GreaterThan = -1.0; LessThan = 20.0; Type = 'System.Double' } + @{ Name = 'max is double with plus sign'; Maximum = +100.0; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' } + @{ Name = 'max is doulbe with plus sign and enclosed in quote'; Maximum = '+100.0'; Minimum = 0; GreaterThan = -1.0; LessThan = 100.0; Type = 'System.Double' } + @{ 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' } + @{ Name = 'max is Double.MaxValue, min is Double.MinValue'; Maximum = ([double]::MaxValue); Minimum = ([double]::MinValue); GreaterThan = ([double]::MinValue); LessThan = ([double]::MaxValue); Type = 'System.Double' } + + ) + + $testDataForError = @( + @{ Name = 'Min is greater than max and all are positive 32-bit integer'; Maximum = 10; Minimum = 20} + @{ Name = 'Min and Max are same and all are positive 32-bit integer'; Maximum = 20; Minimum = 20} + @{ Name = 'Min is greater than max and all are negative 32-bit integer'; Maximum = -20; Minimum = -10} + @{ Name = 'Min and Max are same and all are negative 32-bit integer'; Maximum = -20; Minimum = -20} + @{ Name = 'Min is greater than max and all are positive double-precision number'; Maximum = 10.0; Minimum = 20.0} + @{ Name = 'Min and Max are same and all are positive double-precision number'; Maximum = 20.0; Minimum = 20.0} + @{ Name = 'Min is greater than max and all are negative double-precision number'; Maximum = -20.0; Minimum = -10.0} + @{ Name = 'Min and Max are same and all are negative double-precision number'; Maximum = -20.0; Minimum = -20.0} + @{ Name = 'Max is a negative number, min is the default number '; Maximum = -10; Minimum = $null} ) @@ -58,28 +69,23 @@ Describe "Get-Random DRT Unit Tests" -Tags DRT{ } - 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)" - { get-random -Minimum -10 -Maximum -20 } | Should Throw "The Minimum value (-10) cannot be greater than or equal to the Maximum value (-20)" - { get-random -Minimum -20 -Maximum -20 } | Should Throw "The Minimum value (-20) cannot be greater than or equal to the Maximum value (-20)" - { get-random -Minimum 20.0 -Maximum 10.0 } | Should Throw "The Minimum value (20) cannot be greater than or equal to the Maximum value (10)" - { get-random -Minimum 20.0 -Maximum 20.0 } | Should Throw "The Minimum value (20) cannot be greater than or equal to the Maximum value (20)" - { get-random -Minimum -10.0 -Maximum -20.0 } | Should Throw "The Minimum value (-10) cannot be greater than or equal to the Maximum value (-20)" - { get-random -Minimum -20.0 -Maximum -20.0 } | Should Throw "The Minimum value (-20) cannot be greater than or equal to the Maximum value (-20)" - { get-random -10 } | Should Throw "The Minimum value (0) cannot be greater than or equal to the Maximum value (-10)" - { $x = -10; get-random $x } | Should Throw "The Minimum value (0) cannot be greater than or equal to the Maximum value (-10)" + It "Should be able to throw error when ''" -TestCases $testDataForError { + param($maximum, $minimum) + try + { + Get-Random -Minimum $minimum -Maximum $maximum + throw "OK" + } + catch + { + $_.FullyQualifiedErrorId | Should Be "MinGreaterThanOrEqualMax,Microsoft.PowerShell.Commands.GetRandomCommand" + } } It "Tests for setting the seed" { $result1 = (get-random -SetSeed 123), (get-random) $result2 = (get-random -SetSeed 123), (get-random) - $result1.Length | Should Be $result2.Length - for($i = 0;$i -lt $result1.Length;$i++) - { - $result1[$i] | Should Be $result2[$i] - } + $result1 | Should Be $result2 } }