I spent an hour just coding and playing around in the Spring class. Let's see where things stand.
I wrote a constructor for Spring that initialized the hands for the watch. I used an array, instead of a list – it should be adequate and I can retrofit a list in later, if it seems like a good idea.
public Spring ()
{
hand = new Hand[4];
hand[0] = new Hand(60, 1); // second hand
hand[1] = new Hand(3600, 60); // minute hand
hand[2] = new Hand(86400, 3600); // hour hand
hand[3] = new Hand(172800,43200 ); // day hand
}
I didn't write a test for this because that would just be “$test = new Spring” which is going to be included in all my tests anyway.
public int wind()
{
if (mainSpring LT 100000) // Maximum tension the spring can take.
{
mainSpring++;
}
return mainSpring;
}
I can test this. (Oh, there isn't any way to know how tightly wound the spring is without winding it.)
# Testing Spring Wind method
#
$testSpring = new Spring
for ($a = 1; $a -lt 100000; $a++)
{
if ($testSpring.wind( ) -ne $a) {"FAIL:"}
}
for ($a = 100000; $a -lt 200000; $a++)
{
if ($testSpring.wind( ) -ne 100000) {"FAIL:"}
}
This will verify that the spring can be wound, but only so far.
So, next up is the tick method.
public bool tick()
{
if (mainSpring GT 1) return false;
bool temp = true; // Local variable to keep track of results
foreach (Hand x in hand) // For each hand in the array. All take ticks
{ // Does the tick as a side-effect
temp = (x.tick() | temp); // Allows a single false to return a false value.
}
mainSpring--;
return temp;
}
I don't see how one of the ticks calls on a hand could fail, but if it does, I want that failure to be passed up to the mainSpring.
Here's some testing:
$testSpring = new Spring
if ($testSpring.tick()) {"FAIL: First tick should fail since the spring has not been wound."}
for ($a = 1; $a -lt 75; $a++)
{
if ($testSpring.wind( ) -ne $a) {"FAIL: Wind should work."}
}
for ($a = 1; $a -lt 75; $a++)
{
if (!($testSpring.tick( ))) {"FAIL: Tick should work."}
}
if ($testSpring.tick()) {"FAIL: This tick should fail since the spring has not been wound."}
Finally, I wrote a Display method that would show me that state of the watch:
public void display ()
{
Console.Write(hand[3].Display + " ");
Console.Write(hand[2].Display + " ");
Console.Write(hand[1].Display + " ");
Console.Write(hand[0].Display + " ");
}
Yeah, it's Q&D, but it does what it is supposed to do.
Oh, and it gave me an unexpected result:
PowerShell: $testSpring = new Spring
PowerShell: $testSpring.display()
4 24 60 60
4? why 4?
Ad hoc testing time:
PowerShell: for ($i = 1; $i -lt 43201; $i++) { $testSpring.wind(); $testSpring.tick();}
PowerShell: $testSpring.display()
1 12 60 60
Wait a minute – I have the numbers wrong in the mainSpring constructor. This is how it should read:
public Spring ()
{
hand = new Hand[4];
hand[0] = new Hand(60, 1); // second hand
hand[1] = new Hand(3600, 60); // minute hand
hand[2] = new Hand(43200, 3600); // hour hand
hand[3] = new Hand(86400, 43200); // day hand
}
And this gives the following:
2 12 60 60
PowerShell: for ($i = 1; $i -lt 43201; $i++) { $temp = $testSpring.wind(); $temp = $testSpring.tick();}
PowerShell: $testSpring.display()
1 12 60 60
OK, that is the basic code for this project.
Next time, I'll write method documentation and that will be a wrap.
No comments:
Post a Comment