So, my wife and I were cooking pasta last night – linguine, I think, the thick flat noodles. I put my hand up to my forehead – not quite smacking myself – and said, “That's it. That's why it didn't work.”
My wife looked at me kind of strange.
Here's what I realized: the nodes at that bottom edge of the board still have four positions. The ones that are outside the board simply have a location value of -1.
So, the stick fell from position 59 to position -1. There is nothing preventing a location of -1 to be assigned a stick. Also, only positions 1-60 are displayed, so there's no way to “see” where the stick ended up.
OK, first, this can't happen. I'll need to make changes to the Stick class that don't allow a -1 position to have a stick.
Then I'll need to fix the fall method to not do that.
Added to the Location property:
if (value < 0) throw new Exception();
And here is the test function for sets of values:
> testFallMethod 9 20
(No FAILs)
> testFallMethod 59 59
Exception calling "dropStick" with "1" argument(s): "Exception of type 'System.Exception' was thrown."
OK, just need to clean up the test a little. Here's the function again:
function testFallMethod ($start, $expected)
{
$testBoard = new Board(60)
if (!($testBoard.addStick($start))) {"FAIL: Could not add stick at position $start."}
if (!($testBoard.dropStick($start))) {"FAIL: Fall method returned value of false for position $start."}
if (!($testBoard.getStick($expected).Value)) {"FAIL: Stick from position $start did not fall to expected position $expected"}
}
And here's the new version:
function testFallMethod ($start, $expected)
{
$testBoard = new Board(60)
if (!($testBoard.addStick($start))) {"FAIL: Could not add stick at position $start."}
try {
if (!($testBoard.dropStick($start))) {"FAIL: Fall method returned value of false for position $start."}
}
catch { "FAIL: Fall method caused stick at $start to fall outside the board." }
if (!($testBoard.getStick($expected).Value)) {"FAIL: Stick from position $start did not fall to expected position $expected"}
}
The second clause is compound: it has two ways to fail. In principle, one FAIL could hide another, but since every fail has to be resolved, that just implies order for the debugging.
Here's some ad hoc testing:
> testFallMethod 9 20
> testFallMethod 9 40
FAIL: Stick from position 9 did not fall to expected position 40.
> testFallMethod 59 59
FAIL: Fall method caused stick at 59 to fall outside the board.
The first two are as expected. The third FAIL is the result of the fall method code problem that started this post. I'm going to deal with that now.
OK – I did a real hack job on this, but let's see if it works.
> testFallMethod 9 20
> testFallMethod 9 24
FAIL: Stick from position 9 did not fall to expected position 24.
> testFallMethod 59 59
FAIL: Fall method returned value of false for position 59.
Well, that's a problem. There are times when the fall method should fail.
Here we go:
function testFallMethod ($start, $expected, $fail = 0)
{
$testBoard = new Board(60)
if (!($testBoard.addStick($start))) {"FAIL: Could not add stick at position $start."}
try {
if ((!($testBoard.dropStick($start))) -and ($fail -eq 0 )) {"FAIL: Fall method returned value of false for position $start."}
}
catch { "FAIL: Fall method caused stick at $start to fall outside the board." }
if (!($testBoard.getStick($expected).Value)) {"FAIL: Stick from position $start did not fall to expected position $expected."}
}
An expected FAIL will have a third parameter with a value not 0.
> testFallMethod 59 59 1
> testFallMethod 59 59
FAIL: Fall method returned value of false for position 59.
Tomorrow: More tests for fall, including what happens if a stick can't fall – that is, if there is another stick in the way.
No comments:
Post a Comment