Friday, April 8, 2011

Testing the Fall Method 3

Yesterday, I wrote a test function for the fall method. The next step is to write a series of tests for various positions.

The following should be valid falls:

1 6

6 17

50 56

56 56

2 7

7 18

5 10

11 22

38 43

55 60

60 60

Four of these should fail: 50 56, 56 56, 55 60, and 60 60.


Only two fail:


FAIL: Fall method returned value of false for position 56.

FAIL: Fall method returned value of false for position 60.


55 only falls to 60 then the method returns a false. Hold on a second.

The fall method is used recursively. It calls itself if the stick needs to fall two places (9 to 15, then 15 to 20, say). The second call, though, returns a value – true or false – which is simply ignored.

And that's a problem.

One thing that the design allows is for the future addition of a recursive fall – call fall on a stick and have it fall as far as it can. So, 9 would fall to 20 and then another call would have 20 fall to 31, then 42, then 53, then 59.

Unless there is a stick in the way.

Having a return of false provides a way to stop the recursive call when you really can't fall any more.

But that's easy to fix – instead of calling the fall method and then returning true when it comes back, just return the value of the returned value from the fall method. So:


else if ((!isFilled(position[3], board)) && (position[3] > -1))

{

doMove(position[0], position[3]);

return fall(position[3]);

}

FAIL: Fall method returned value of false for position 50.

FAIL: Fall method returned value of false for position 56.

FAIL: Fall method returned value of false for position 55.

FAIL: Fall method returned value of false for position 60.


This is correct. For these four positions, the stick cannot fall all the way down. These shouldn't be FAILs, though, since this is expected. This change to the tests does the trick.


testFallMethod 1 6

testFallMethod 6 17

testFallMethod 50 56 1

testFallMethod 56 56 1

testFallMethod 2 7

testFallMethod 7 18

testFallMethod 5 10

testFallMethod 11 22

testFallMethod 38 43

testFallMethod 55 60 1

testFallMethod 60 60 1


Tests that are expected to fail are marked with a 1.

So, what happens when there's already a stick in the way?

I wrote a version of the test function that takes a board as a parameter:

function testFallMethodCompound ($start, $expected, $testBoard, $fail = 0)

{

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."}

}

Then I used it to test a board with a stick in place that blocks the fall of the stick:

> $test = new Board(60)


> $test.addStick(9)

True


> testFallMethodCompound 4 9 $test


No FAIL. Here's the display of the board:

> display $test

o o o o o o

| |

o o o o o o

o o o o o o

o o o o o o

o o o o o o

o o o o o o


This is the right output – the stick couldn't fall into 9, so if fell into ten.

The test didn't return a FAIL since there is a stick at 9. Actually, maybe that's not a problem. We're testing something different here – trying to get a FAIL when there's a stick in the way.

Let's try again, now that a stick in 4 has nowhere to fall:


> testFallMethodCompound 4 9 $test

FAIL: Fall method returned value of false for position 4.

Here's the case I was looking for: location 50 to location 56. Location 50 can only fall to position 56. If there is a stick there, the result should be false.


$test = new Board(60)

$temp = $test.addStick(56)

testFallMethodCompound 50 56 $test


FAIL: Fall method returned value of false for position 50.


This is expected, so this should have the $fail parameter set:


testFallMethodCompound 50 56 $test 1

It's good now. Next week: testing beyond the corners.

No comments:

Post a Comment