Looking at yesterday's code, I'm feeling the need to test. I don't see or can't imagine any problems with it, but isn't that the worst kind of code?
So, the first thing I need to do is put in method to tell which positions belong to a node:
// TESTING CODE
public int getPosition (int temp)
{
if (temp [LESS THAN SYMBOL] 0) return -1;
if (temp [GREATER THAN SYMBOL] 3) return -1;
return position[temp];
}
I thought about returning the position array, but I wanted to keep as much of the testing code out in the PowerShell script as possible.
So, then, I started noodling around with PowerShell – And I forgot how easy PS is:
# testing node setup
function createNodeTestArray {
for ($a = 0; $a -lt 36; $a++) {
new Node($a)
}
}
$NodeTestArray = createNodeTestArray
The function creates 36 new nodes to be assigned to the test array.
Also, I don't have to create all the nodes to do tests – since I'm testing on Node objects, I can create any node object and test just it.
So, first test: each node has either 2, 3, or 4 positions associated. Let's do a test for that.
function testNumberOfNodes ($b) {
$c = 0
for ($a = 0; $a -lt 4; $a++) {
if ($b.getPosition($a) -eq -1) {continue}
$c++
}
$c
}
This seemed to work for the one or two that I tried it out on. Let's do them all:
foreach $d in $NodeTestArray
{
testNumberOfNodes $d
}
Result: Missing opening '(' after keyword 'foreach'.
Let's try that again:
foreach ($d in $NodeTestArray)
{
testNumberOfNodes $d
}
And my code is broken.
3
4
4
4
4
3
3
4
4
4
4
3
.
.
.
The first of those should only have 2 nodes. I'm going to do a little ad hoc testing – see what the first node things it should be.
$NodeTestArray[0].getPosition(0)
$NodeTestArray[0].getPosition(1)
$NodeTestArray[0].getPosition(2)
$NodeTestArray[0].getPosition(3)
Result:
4
10
15
-1
Wow, is that messed up!
Let's try node 6:
$NodeTestArray[6].getPosition(0)
$NodeTestArray[6].getPosition(1)
$NodeTestArray[6].getPosition(2)
$NodeTestArray[6].getPosition(3)
11
17
22
-1
Expected result would be 6, 12, 17, -1.
Let's try 14. Expected results: 19, 25, 30, 24:
20
26
31
25
Wait a minute – that's shifted right by one.
OK, first up: simple typo:
position[0] = location + (((location / 5) - 1) + 5);
should be:
position[0] = location + (((location / 5) - 1) * 5);
New result:
19
25
30
24
Let's do the basic test again (reformatted into a grid):
2 3 3 3 3 3
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
Um, good. What's up with node 5? Expected result: -1, -1, 11, 5
5
-1
16
10
Well, is 0 working at least? Expected: -1, 1, 6, -1
And before I keep at this, I should make the four statements into a function, so I don't have to type everything all the time.
function testAllNodesForNumberOfPositions {
foreach ($d in $NodeTestArray)
{
testNumberOfNodes $d
}
}
function testIndividualNode ($a) {
$NodeTestArray[$a].getPosition(0)
$NodeTestArray[$a].getPosition(1)
$NodeTestArray[$a].getPosition(2)
$NodeTestArray[$a].getPosition(3)
}
-1
1
6
-1
(BTW, at this point, I'm working interactively in the powershell editor. For this kind of testing, all I have to do is type in the testing function. Once I have better confidence that the tested code works, I can automate this.)
Well, I'll pick this up tomorrow.
No comments:
Post a Comment