Monday, April 4, 2011

Sanity Check and Models of Sticks

First up, let's get removeStick working:

public bool removeStick(int position)

{

if (!(isValid(position))) return false;

if (!getStick(position).Value) return false;

stickList.Remove(getStick(position));

return true;

}


This is in the “easier than I thought” category, but maybe I shouldn't have been surprised. I tried to use good design principles, as well as I understand them. That means that removeStick contains all the code about moving. I've already made the underlying changes to the approach. After putting that work in, implementing the rest of it should get easier, not harder.

Running the tests, by the way, went fine. No FAILs.

Of course, what if the testing code is wrong? It's not like I haven't made mistakes in creating tests before.

So, I need to do some sanity checking.

First of all, does my Display PowerShell function still work? Good question, since it was some of the klugiest code I've written in years.


> $testDisplay = new Board(15)


> $testDisplay.addStick(1)

True


> $testDisplay.addStick(6)

True


> display $testDisplay

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


Good. Let's try a couple more:


> $testDisplay.addStick(60)

True



> $testDisplay.addStick(60)

False



> display $testDisplay

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



> $testDisplay.moveStick(60, 54)

True



> display $testDisplay

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



> $testDisplay.moveStick(54, 60)

True



> display $testDisplay

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



> $testDisplay.moveStick(60, 55)

True



> display $testDisplay

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


The font spacing in the blog makes it hard to read, but all these boards line up correctly.

Before I leave the display stuff, I'm going to create a sanity test, just to make sure that the system doesn't go off the rails do to some other test failing.

Here's the PowerShell code:


function sanityCheck {

$testGeneral = new Board(60)

$testGeneral.AddStick(1)

$testGeneral.AddStick(5)

$testGeneral.AddStick(6)

$testGeneral.AddStick(11)

$testGeneral.AddStick(50)

$testGeneral.AddStick(55)

$testGeneral.AddStick(56)

$testGeneral.AddStick(60)

"Expected: "

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

"Actual: "

display $testGeneral

}


In the March 29 post, I listed three goals:

  • I still need to write either a method or a test that displays the whole board. DONE

  • I want to generalize the code so that it can be used for boards of different sizes. DONE FROM THE BOARD'S POINT OF VIEW

  • I might want to model the sticks, well, more as objects. Right now the sticks are positions -- more about this as I address it. DONE, PARTLY

For the second goal, the code inside Node is going to have to change. That's were the position numbers are assigned to nodes. That's the only place in the whole design that knows the number associated with a position on the board.

As for the third goal, using the List to model the sticks is a little more like the real world (each stick object has one entry on the list), but we could do more by making the moveStick method simply change the location of a stick.

Let's give it a try. In doMove, the following lines of code:


board.removeStick(origin);

board.addStick(destination);


Will be replaced with this code:


board.getStick(origin).Location = destination;


And that made PowerShell ISE freeze up.


Never a good sign.


Here's the offending test:


$testStick = new Stick (1)


if ($testStick.Location -ne 1) {"FAIL: Location should be 1."}


try {

$testStick.Location = 12

{"FAIL: Location should be a read-only property."}

}

catch {

}


This test is no longer true, of course. I had to make the Location property writable in order for the doMove method to be able to move a stick.


Still, every time I try to set the Location property, I get the same serious hang. Just a second.


Oh – I thought that I might have implemented the property wrong – and I did. Here's what it should look like:


public int Location

{

get {

return location;

}

set {

location = value;

}

}


Note the lowercase “l” in “location = value;” I had it as “Location”.


Running the test now gives the following result:


"FAIL: Location should be a read-only property."


Since the test is now out of date, I need to change the test.


And that fixes that.

No comments:

Post a Comment