Friday, March 18, 2011

Testing the Node Constructor Code, Part 2

So, where was I? Ah, yes, node number 5 isn't working as expected.

Expected: -1, -1, 11, 5

Actual: 5, -1, 16, 10

OK, is 4 working?

Expected: -1, 5, 10, 4

Actual: -1, 5, 10, 4

The way the code works, or should work, is that it creates a set of defaults for – um, wait a minute. What about position 11?

Expected: 11, -1, 22, 16

Actual: 16, -1, 27, 21

And 35?

Expected: 55, -1, -1, 60

Actual: 65, -1, -1, 70

My assignment equation is broken and in several ways. Here's the code again:

position[0] = location + (((location / 5) - 1) * 5);

I'm using (location / 5) to derive the row number. It shouldn't be 5, though, it should be 6.

That helps a bit.

Node 5 actual: 0, -1, 11, 5.

So, the 0 position is still wrong, but 1, 2, and 3 are working. Let's check 11 and 35:

11 actual: 11, -1, 22, 16

35 actual: 55, -1, -1, 60

OK, 5 is still broken. The code that assigns the 0 position for 5 is this:

// EDGE CASES: TOP EDGE

if (position[0] [LESS THAN SYMBOL] 0) position[0] = -1;

Oh – I forgot that the nodes are 0-based, but the sticks are numbered from 1 to 60. I was assuming that some node would have to have 0 as a value for 0. That isn't true.

if (position[0] [LESS THAN SYMBOL] 1) position[0] = -1;

This change should fix it.

(On the one hand, I feel kind of silly for missing these obvious things. On the other hand, if I had worked through this more carefully, and written the code more rigorously, I don't know how much more time that would take and I don't know what other kinds of mistakes I would have made. I think the point is, the testing code verifies for silly and non-silly mistakes at the same time.)

Now that I have better confidence that this is working, how about some better testing code – and fully automated, to boot!

Here's my first run at it. This function tests the positions associated with each node against the expected number, which is hard-coded.

function TestNumberOfPositionsAgainstExpected {

$ExpectedNumberOfPositions = 2, 3, 3, 3, 3, 2, 3, 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 3, 2, 3, 3, 3, 3, 2


$ActualNumberOfPositions = testAllNodesForNumberOfPositions


for ($i = 0; $i -lt 36; $i++) {

if ($ExpectedNumberOfPositions[$i] -ne $ActualNumberOfPositions[$i]) {

"FAILED at $i."

}

}

}


While I was walking from the coffee house, I thought of the next thing I want to try. I want to write a function that takes a 5 member array (position 0 – 3 will be expected values, position 4 will be the node number) and compares it to the value for any node.

I have this testing code from the other day to start with:

function testIndividualNode ($a) {

$NodeTestArray[$a].getPosition(0)

$NodeTestArray[$a].getPosition(1)

$NodeTestArray[$a].getPosition(2)

$NodeTestArray[$a].getPosition(3)

}


This is what I came up with:

function testIndividualNode ($a) {

if ($NodeTestArray[$a[4]].getPosition(0) -ne $a[0]) {"FAILED: Node $a[4], position 0."}

if ($NodeTestArray[$a[4]].getPosition(1) -ne $a[1]) {"FAILED: Node $a[4], position 1."}

if ($NodeTestArray[$a[4]].getPosition(2) -ne $a[2]) {"FAILED: Node $a[4], position 2."}

if ($NodeTestArray[$a[4]].getPosition(3) -ne $a[3]) {"FAILED: Node $a[4], position 3."}

}

Seems to work. I used node 0 as a test case. Next, I changed it so the first test would fail (made it read -eq not, -ne.)

prompt[GREATER THAN SYMBOL] testIndividualNode $x

FAILED: Node -1 1 6 -1 0[4], position 0.

I wasn't expecting the whole array to print. Next time I look up something about arrays in PS, I'll keep an eye out for that.

Now that I have a function for individual node testing, I can create a set of test data to be run. I've done this with an array of arrays so I can use the foreach command later. I chose selected nodes and figured the expected values by looking at my notes.

$corner0 = -1, 1, 6, -1, 0

$corner1 = -1, -1, 11, 5, 5

$corner2 = 50, 56, -1, -1, 30

$corner3 = 55, -1, -1, 60, 35

$topedge1 = -1, 2, 7, 1, 1

$topedge3 = -1, 4, 9, 3, 3

$rightedge11 = 11, -1, 22, 16, 11

$rightedge23 = 33, -1, 44, 38, 23

$bottomedge34 = 54, 60, -1, 59, 34

$bottomedge32 = 52, 58, -1, 57, 32

$rightedge6 = 6, 12, 17, -1, 6

$rightedge18 = 28, 34, 39, -1, 18

$interior7 = 7, 13, 18, 12, 7

$interior15 = 20, 26, 31, 25, 15

$interior27 = 42, 48, 53, 47, 27


$NodeTestCases = $corner0, $corner1, $corner2, $corner3, $topedge1, $topedge3, $rightedge11, $rightedge23, $bottomedge34, $bottomedge32, $rightedge6, $rightedge18, $interior7, $interior15, $interior27

Here's the foreach loop:

foreach ($case in $NodeTestCases) {

testIndividualNode $case

}

This is what I intended to write for these tests, but I absolutely have to make the following fixes:

  • Consistent use of variable names ($a, $b, $c, etc for counting variables, descriptive names for others)
  • Script comments describing how the tests work.
  • Check for unused code that I created as I played with this.

I'll post the whole script, as it stands, as a separate entry for today.

Next up: Creating the array of sticks and supporting code.




No comments:

Post a Comment