Today, I'm going to add some functionality to the takeUser method. Right now, it simply takes any DataUser passed to it. I want it to be able to validate input. For now, that will mean dropping users that are added “in the past”. You'll see what I mean.
The simulation is going to keep track of what time it is – how many ticks since the beginning of the simulation. Easy – just put in a field for time. Also, the tick method will increment the time, along with doing whatever else in involved in the simulation.
So, here's some code:
public void takeUser (DataUser x)
{
if (x.QueueUpTime [LESS_THAN] simTick)
{
#if DEBUG
// TESTING CODE
Console.WriteLine("Invalid DataUser object.");
#endif
return;
}
computerQueue.Add(x);
}
I don't know whether I want the method to simply drop them or to tell the user that there's a problem. For now, I'll make it testing code.
And speaking of testing –
# Just testing
$test = new Simulation
$dataUser = new DataUser(7, 9, 9)
$test.takeUser($dataUser)
# Testing validation
$test = new Simulation
$test.tick()
$dataUser = new DataUser(0, 9, 9)
"Expected: Invalid DataUser object."
$test.takeUser($dataUser)
I don't like this – I can't make the WriteLine output be silent. I'm going to rewrite it as an exception.
public void takeUser (DataUser x)
{
if (x.QueueUpTime [LESS_THAN] simTick)
{
#if DEBUG
// TESTING CODE
if (simTick [LESS_THAN] 1000) {throw new Exception();}
#endif
return;
}
computerQueue.Add(x);
}
(The conditional (simTick [LESS_THAN] 1000) is there to avoid an reachable code error.)
And I want to be able to tell how many objects are in the list, at least for testing:
#if DEBUG
// TESTING CODE
public int queueSize()
{
return computerQueue.Count;
}
#endif
And here are the final tests:
# Just testing: the simulation object should accept the DataUser.
$test = new Simulation
$dataUser = new DataUser(7, 9, 9)
$test.takeUser($dataUser)
if ($test.queueSize() -ne 1) { "FAIL: Queue should have one member." }
# Testing validation: the simulation object should reject the DataUser as in the past.
$test = new Simulation
$test.tick()
$dataUser = new DataUser(0, 9, 9)
try {
$test.takeUser($dataUser)
"FAIL: Expected exception for Invalid DataUser object."
} catch {}
if ($test.queueSize() -ne 0) {"FAIL: Queue should have zero members." }
These verify both that no exception is thrown and that the right number of objects are added to the list.
No comments:
Post a Comment