Monday, April 11, 2011

Testing the Fall Method 4

I spent some time over the weekend updating my code comments. I also included a note to myself to work on the code for the fall method. I have confidence that it works because of the testing I've done, but something that over-complicated is just asking for trouble.

I also regularized my terminology. The term LOCATION refers to the number, from 1 to 60, on the board for a stick. The term POSITION refers only to the four positions around a node. I was using position, sometimes, for the both, but that gets confusing fast.

Some of the testing code still uses position wrong.

On Friday, I spend some time revising my testing function for the fall method. Today, I want to get serious about testing the falls. After flailing around for half an hour, I decided to organize stuff into truth-table-like diagrams. Here's what I mean:


origin stick: 6


Section of the grid:


| 6

. _ 12

| 17


So, 12 and 17 can each either be filled with a stick or not:


12 | 17 | expected | return

open filled

open open

filled filled

filled open


There are two things that happen as a result of a fall: the stick has an end position and the method returns true or false, depending on whether the stick fell as far as it could fall. Here are the expected results:


12 | 17 | expected

open filled 12 (the stick cannot fall all the way to 17)

open open 17

filled filled 6 (the stick cannot fall at all)

filled open 6 (the stick cannot fall through 12, which is filled.


If the fall is interrupted, the result is false, as below.


12 | 17 | expected | return

open filled 12 false

open open 17 true

filled filled 6 false

filled open 6 false


OK, I can write tests for those cases:


$test = new Board(60)

$temp = $test.addStick(17)

testFallMethodCompound 6 12 $test false


$temp = $test = new Board(60)

testFallMethodCompound 6 17 $test


$test = new Board(60)

$temp = $test.addStick(12)

$temp = $test.addStick(17)

testFallMethodCompound 6 6 $test false


$test = new Board(60)

$temp = $test.addStick(12)

testFallMethodCompound 6 6 $test false


These work as expected.

This case is on an edge, so there are two sticks (12 and 17) that are relevant in addition to the initial location.

For a stick out in the middle of stuff, like 8, there will be three relevant sticks: 13, 14, and 19.


. .|8

13_._14

. .|19


Here is the fall table:


13 | 14 | 19 | expected | return

open open open 19 true

open open filled 14 false

open filled open 19 true

open filled filled 13 false

filled open open 19 true

filled open filled 14 false

filled filled open 8 false

filled filled filled 8 false


Look at the second case:


open open filled 14 false


That follows from the implementation. The node checks position 1 before checking position 3, so if it can only fall halfway, it should fall there.

So, I'm going to write tests, just like the last bunch. I'll let you know if there's any unexpected results.

No, they all worked. I should probably do one on the right edge of the array as well, like 11, 16, 22:


| 11

. _ 16

| 22


16 | 22 | expected | return

open open 22

open filled 16 false

filled open 11 false

filled filled 11 false


Before I wrap for the day, I need to fix the rest of the source code for the use of the word POSITION. I was going to leave it wrong just in the testing code, but that's asking for trouble later on.

I made one additional change to the source: getPosition became getLocation. I also touched up a couple of comments. Running the tests threw a bunch of FAILs, but there were only three or four places where the tests needed to be updated.

That's it for today. I'm going to be busy tomorrow, so I'll post the C# code as it stands now. The tasks I've been thinking about for the future are:

  • Tidy up the fall method
  • Add a “drop all the way” method that lets a stick fall as far as it can down the board.
  • Start thinking about how to change the size of the board.

No comments:

Post a Comment