Wednesday, May 18, 2011

Test Cases for the Sort Function

So, I need two more things to finish up from last time:

  • Test cases
  • A way to evaluate test cases.

Let's start with the test cases I did for the isSorted script block:


# Test Cases:


$sorted = 1,2

$sorted[0] = 1,2,3,4,5 #Expected True

$sorted[1] = 1,1,1,1,1 #Expected True


$unsorted = 1,2,3

$unsorted[0] = 1,2,3,4,3 #Expected False

$unsorted[1] = 3,2,3,4,5 #Expected False

$unsorted[2] = 3,2,3,4,3 #Expected False


Sorted and not sorted doesn't matter. The sort function will return a sorted (I assume, if it's working) array. There may be a difference in how easy it is for the sort to happen (rabbits versus turtles), but let's worry about that later.

# 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


Each of these arrays can be sorted by the script block. To make sure that the thing is working, I could sort each list and compare it to the sorted version of that list.

Or, considering that I've already written a script block that tests whether an array is in the right order, I could use that, as follows:


foreach ($a in $testCase)

{

if (!(&$isSorted (&$bubble01 $a))) {"FAIL: List is sorted: $a"}

}


(I had to play around a little to get the nested invoking right. I tested it by leaving out the ! Symbol to see that it would produce a $true in every case.)

(Using the previously written script block might be suspect from the point of view of testing. After all, it was the basis of the bubble sort code. Still, it was tested and seems to work.)

So, now I have a way of reading test cases in and evaluating that the sort mechanism is working. What this doesn't tell us is how easy the sort is for each test case and how we could compare it to other sort functions.


No comments:

Post a Comment