Friday, April 22, 2011

A Clone Method and IsEqualTo Method

Today I'm going to write tests for the dropStickRecurse method and the fall method. I should have the tests for fall already done.

Yesterday, I wrote a script that runs all the test scripts. Now, to add a test, all I need to do is write a new script and save it as a *ps1 file.

The tests for Node.fall were already done. I just copied them over.

For dropStickRecurse, I had a function that took a starting and expected value, added a stick to the starting point in a new board, dropped it, and checked whether there was a stick at the expected location. This is not valid for any board with more than one stick. After all, the expected location could have had a stick already.

Then, I created 5 arrays of values: one for each location at the bottom of the board. Every stick on the board, if it is the only stick on the board, will fall to one of these five positions. Any stick already at one of those locations can't fall, so will have the same value, but the method will return false.

Wrap this in a foreach statement, and acceptable values are checked.

$endValue56 = 1, 6, 12, 17, 23, 28, 34, 39, 45, 50

# Test each value in isolation:

foreach ($location in $endValue56)

{

testDropStickRecurseAlone $location 56

}

for ($a = 56; $a -lt 61; $a++)

{

testDropStickRecurseAlone $a $a 1 # the 1 indicates that this is expected to have a value of false.

}


Oh, and I need to check unacceptable values.


for ($a = 56; $a -lt 61; $a++)

{

testDropStickRecurseAlone $a $a 1

}


testDropStickRecurse 0 1

testDropStickRecurse -1 1

testDropStickRecurse 61 1

I had to make a new function. It didn't make sense to have one that took an expected value that you could never provide.

So, the next problem that I have is finding a way to provide a board to a function which will add a stick and drop it and then check whether it fell to the expected location.

And things just got more interesting.

So, here's my plan:

  • Create function that takes a board object as a parameter.

  • Make a copy of the board.

  • Add a stick at the specified position.

  • Drop the stick.

  • Add a stick to the new board at the expected position.

  • Compare the two boards, using GetHashcode.

Cool – makes sense, nice and clear. What's the problem?

I need to make a copy of the object – a deep copy, for which I don't have code.

GetHashCode is not valid for Board. It can't be. There are 60 locations, each of which can be true or false, which means you need 60 bits to express it. Int gives you 32 bits.

Here's the actual list of what I need to do:

  • Create function that takes a board object as a parameter.

  • Write a method to copy a Board.

  • Make a copy of the board.

  • Add a stick at the specified position.

  • Drop the stick.

  • Add a stick to the new board at the expected position.

  • Write code to compare two Board objects.

  • Compare the board objects.

So, here's my Clone method. This does not implement the Cloneable interface. I'm putting it in the testing part of my code so that it doesn't get compiled for a final version of the class.

// Clone method: for now, just used in testing

public Board Clone()

{

Board newBoard = new Board(max);

foreach (Stick stick in stickList)

{

newBoard.addStick(stick.Location);

}

return newBoard;

}


It calls the Board constructor using the same max value as it has. Then it goes through its list of sticks and creates a new stick for each of its sticks.

Little warning, here: this is based on assumption that there are no sticks with a false value. Sticks with a false value are used elsewhere as an error code.

Next, we need an equals:

public bool isEqualTo(Board x)

{

start:

foreach (Stick stick in stickList)

{

foreach (Stick otherStick in x.stickList)

{

if (stick.Location == otherStick.Location) {goto start;}

}

return false;

}

}

This won't work, though. All this checks is whether there is a stick in x with the same location as every stick in the current object. X could have more sticks, of course. Let's try this:

public bool isEqualTo(Board x)

{

start:

foreach (Stick stick in stickList)

{

foreach (Stick otherStick in x.stickList)

{

if (stick.Location == otherStick.Location) {goto start;}

}

return false;

}

start2:

foreach (Stick otherStick in x.stickList)

{

foreach (Stick stick in stickList)

{

if (otherStick.Location == stick.Location) {goto start2;}

}

return false;

}

return true;

}

Does it work? I'll do some ad hoc testing.


blog> $a = new Board(60)


blog> $b = new Board(60)


blog> $a.isEqualTo($b)

True (EXPECTED)


blog> $b.isEqualTo($a)

True (EXPECTED)


blog> $a.addStick(1)

True (EXPECTED)


blog> $b.isEqualTo($a)

False (EXPECTED)


blog> $a.isEqualTo($b)

False (EXPECTED)


Everything is fine, until I tried the following:


blog>$b.addStick(1)

True


blog> $a.isEqualTo($b)

It got hung, somewhere. It was unresponsive. I suspect an infinite loop.

Oh, I seem to have mis-used the goto keyword. What a surprise.

I worked isEqualTo over and got better results:

blog> $a.addStick(1)

True


blog> $b.isEqualTo($a)

False


blog> $a.isEqualTo($b)

False

And does it return false if the sticks are in different locations?

blog> $b = new Board(60)


blog> $b.addStick(2)

True


blog> $b.isEqualTo($a)

False


blog> $a.isEqualTo($b)

False

OK, that's all the changes in the code. Here's what I've added today:

public Board Clone()

{

Board newBoard = new Board(max);

foreach (Stick stick in stickList)

{

newBoard.addStick(stick.Location);

}

return newBoard;

}

// Just used in testing

public bool isEqualTo(Board x)

{

foreach (Stick stick in stickList)

{

foreach (Stick otherStick in x.stickList)

{

if (stick.Location == otherStick.Location) {goto start;}

}

return false;

start: continue;

}

foreach (Stick otherStick in x.stickList)

{

foreach (Stick stick in stickList)

{

if (otherStick.Location == stick.Location) {goto start2;}

}

return false;

start2: continue;

}

return true;

}

I'll implement the testing next time.

No comments:

Post a Comment