I think I'm about done with the Sort Project. I got a chance to play with script blocks – the original point of the exercise – as well as set up a test harness that can handle any number of sort techniques and compare them against each other.
I've done some clean-up work on the script:
# cd C:\Users\tdsherer\documents\programming\powershellprog\functional
# Test cases and tests for sorts
# Run necessary scripts:
. .\sortNOTEST.ps1 # loads $isSorted
. .\bubble01.ps1 # loads $bubble01
. .\bubble02.ps1 # loads $bubble02
# Test Cases:
$testCase = 1..5
$testCase[0] = 1,2,3,4,5
$testCase[1] = 1,1,1,1,1
$testCase[2] = 1,2,3,4,3
$testCase[3] = 3,2,3,4,5
$testCase[4] = 3,2,3,4,3
# functions
function runTest ($listToSort, $algorithmToUse, $label)
{
$temp = &{$listToSort}
if (!(&$isSorted (&$algorithmToUse $temp))) {"FAIL: List is sorted: $listToSort"}
$label + ": Number of swaps per compares for $a is " + $global:countSwaps / $global:countCompares
}
# tests
foreach ($a in $testCase)
{
runTest $a $bubble01 '$bubble01'
runTest $a $bubble02 '$bubble02'
' '
}
The runTest function takes a list of different sort techniques – formatted as script blocks assigned to a variable. Both the number of sorts and the test cases used with them can be increased without changing the basic script.
Tomorrow, I'll start laying out what I expect my next project will be: GUI elements in PowerShell.
No comments:
Post a Comment