Sunday, July 31, 2011

CQ Simulation: Adding the Seats to a Queue

OK, up today: Adding seats to the simulation.

I'd already added the dataUser objects to the simulation as a list. I'll do the same with seats, as below:

public class Simulation

{

private int simTick = 0;

private List[LESSTHAN]DataUser[GREATERTHAN] computerQueue;

private List[LESSTHAN]Seat[GREATERTHAN] seats;

public Simulation (int numberOfSeats, int timePerSeat)

{

computerQueue = new List[LESSTHAN]DataUser[GREATERTHAN]();

seats = new List[LESSTHAN]Seat[GREATERTHAN]();

for (int i = 0; i [LESSTHAN] numberOfSeats; i++)

{

seats.Add(new Seat(timePerSeat));

}

}


I left out a closing paren, which I've added. Now it is compiling.

Because I changed the constructor, my tests no longer work. I'll fix the existing tests first, then write new tests for the new behavior.

Here's the simple test I came up with for the seats queue:


# Test size of computer queue

$testSimulation = new Simulation(6, 30)

if ($testSimulation.seatQueueSize() -ne 6) {"FAIL: Wrong number of seats added to queue."}

Next up: Some kind of input validation on these values. Values like 0 or -1 are just useless and will probably break the List setup.

Saturday, July 30, 2011

CQ Simulation: The Seat Class

Today, I'm going to work on a Seat class. If you remember from the description of the problem, there is a queue of people waiting for a number of computers. They get to use a computer for a length of time. That's what the Seat class will model.

I don't have as much “thinking out loud” stuff for this. It's pretty straight-forward coding, I think. Here's the class as it stands now:

public class Seat

{

private int useTime;

private bool occupied = false;

public Seat (int x)

{

useTime = x;

}

public bool Free

{

get {

return (!occupied);

}

set {

occupied = !value;

}

}

public void tick()

{

if (occupied) {

useTime--;

if (useTime [LESSTHAN] 0) useTime = 0;

}

}

public bool timeLeft()

{

if (useTime [GREATERTHAN] 0) return true;

return false;

}

#if DEBUG

// TESTING CODE

#endif

}


And tests:


# Test the Free property

$testSeat = new Seat(60)


$testSeat.Free = $true

if (!($testSeat.Free)) {"FAIL: Free should be True."}


$testSeat.Free = $false

if ($testSeat.Free) {"FAIL: Free should be False."}



# Test the tick method and the timeLeft method



$testSeat = new Seat(60)

$testSeat.Free = $true


for ($a = 0; $a -lt 100; $a++) {

$testSeat.tick()

}


if (!($testSeat.timeLeft())) { "FAIL: Tick method should not be decrementing variable."}



#test when seat occupied

$testSeat = new Seat(60)


$testSeat.Free = $false


for ($a = 0; $a -lt 59; $a++) {

$testSeat.tick()

}


if (!($testSeat.timeleft())) { "FAIL: Time left should still be above 0."}



$testSeat.tick()



if ($testSeat.timeleft()) { "FAIL: Tick method should have reduced time left to 0."}


I spoke too soon. There is some thinking I need to do.

When a user sits down, the clock is reset to allow the user the full amount of time. This means that I have to do that, to start with.

But how?

I only have one variable for time left over. This object doesn't remember how much time a user can have.

So I did some more coding and came up with this:


public class Seat

{

private int useTime;

private int timeForThisSeat;

private bool occupied = false;

public Seat (int x)

{

timeForThisSeat = x;

useTime = timeForThisSeat;

}

public bool Free

{

get {

return (!occupied);

}

set {

occupied = !value;

useTime = timeForThisSeat;

}

}

public void tick()

{

if (occupied) {

useTime--;

if (useTime [LESSTHAN] 0) useTime = 0;

}

}

public bool timeLeft()

{

if (useTime [GREATERTHAN] 0) return true;

return false;

}

#if DEBUG

// TESTING CODE

#endif

}


And now I need to test that behavior.



$testSeat = new Seat(60)

$testSeat.Free = $false


for ($a = 0; $a -lt 59; $a++) { # decrement to 1

$testSeat.tick()

}


$testsdf = $testSeat.timeLeft()

$testSeat.Free = $true

$testSeat.Free = $false

$testSeat.tick() # decrement to 0, if and only if the reset fails.


if (!($testSeat.timeLeft())) {"FAIL: Value of time left should have been reset."}


I ticked down to 1, then reset the Free value then ticked again. If the reset worked, the seat will have time left after one tick.

(I could have created a seat with 1 tick, but I like to cut and paste from other tests.)

OK, so that is the basic behavior of Seat.

Next time, I'll add Seats to the Simulation class.

Saturday, July 23, 2011

The flow of execution for this program is (will be):

  • Create any number of users and add them to the simulation.
  • Advance the simulation any number of ticks.
  • Poll the simulation for results when desired.
  • Close the simulation.

So, for the Simulation class, I need to be able to do four things:

  • Accept users data objects.
  • Advance the simulation by ticks.
  • Poll the simulation for results.
I don't know whether I'll include a final polling that also closes the simulation. In principle, I could also have a reset method, but maybe in a later version.

I'm going to start with the tick method. I'll start with a simple test:

$test = new Simulation

if ($test.ticksNumber() -ne 0) { "FAIL: Should be 0 ticks." }


$test.tick()

if ($test.ticksNumber() -ne 1) { "FAIL: Should be 0 ticks." }


Of course, this uses some new testing code I just added:


#if DEBUG

// TESTING CODE

public int queueSize()

{

return computerQueue.Count;

}

public int ticksNumber()

{

return simTick;

}

#endif


And what else?

Side Task: There are three states that a dataUser can be in: Waiting, Sitting, and Done. I had used a boolean to capture the difference between waiting and sitting, but I'd need to add another and I don't want to.

Instead, I made it into an enum. It was pretty straightforward.

But that is all I have time for today.



Sunday, July 17, 2011

CQ Simulation: Validation of Objects

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.

Saturday, July 16, 2011

CQ Simulation: Consuming Objects

Today, I'm going to write a class that consumes the DataUser object. It will maintain a list of DataUser objects and process the list.


I plan to use the List[LESS_THAN][GREATER_THAN] type for this. It will allow the list to grow and shrink. I'm not sure what all I will need to do to manipulate the list, but List[LESS_THAN][GREATER_THAN] is well documented and does a lot.


(As an aside, when I started out programming I always shied away from things that seemed this powerful. I assumed that something that did as much as List would take up lots of resources for no good reason.


As I did more programming, especially C, I learned that this wasn't true. Lists are generally implemented as linked lists. Each item has a pointer to the next item. So, when a list has 5 items it takes up x amount of space, 10 items, about 2 times x. Using List for just a few items is not a huge overhead hit, as I was imagining.)


Here's the list field:

...

using System.Collections.Generic;



public class Simulation

{

private List[LESS_THAN]DataUser[GREATER_THAN] computerQueue;

}

…


Here's a constructor. All it does is initializes the list.


public Simulation ()

{

computerQueue = new List[LESS_THAN]DataUser[GREATER_THAN]();

}


And now for some tests.


All I can think of for the constructor is an instantiation test:



try {

$a = new Simulation


}

catch {"FAIL: Simulation constructor failed."}



(To review, the way that I do tests is to write something that is silent when nothing goes wrong. I also want it to display my own FAIL message, not a general PS message.)


Next, the Simulation object needs to consume DataUser objects. I'll also stub out a tick() method for later.


public void takeUser (DataUser x)

{

computerQueue.Add(x);

}

public void tick()

{

}


And a test:



$test = new Simulation;

$dataUser = new DataUser(7, 9, 9)


$test.takeUser($dataUser)


I need to play around with this, of course, but the method does work.


Next time, whatever validation I decide to do with the objects as it reads them in.

Friday, July 8, 2011

CQ Simulation: Testing

Well, this is why tests should only have one aspect of the program that they test.

Here's the code around what happens during a tick:

if (thisTick < queueUpTime) return;


if (sitting) { patienceTime--; }


else { desiredTime--; }

And I got it backwards: Patience time should be decremented when you are waiting in line, not after you are sitting at a computer.

But the first version of the test didn't find that -- not minor -- problem.

I'll fix it and clean up the tests:


# Test sitting field.


$testSitting = new DataUser (100, 100, 100)


if ($testSitting.testSitting()) { "FAIL: sitting variable should be false." }


$testSitting.sitDown()


if (!($testSitting.testSitting())) { "FAIL: sitting variable should be true." }



# Test patience field


$testTick = new DataUser (7, 1, 100)



$testTick.tick(9)


try {


$testTick.tick(9)


"FAIL: Tick should decrement patience field."


}



catch {}



# Test desired time field:


$testTick = new DataUser (7, 100, 1)


$testTick.sitDown()


$testTick.tick(9)



try {


$testTick.tick(9)


"FAIL: Tick should decrement desired time field."


}



catch {}

And now it is testing what it is supposed to be testing and doing what it is supposed to be doing.

Monday, July 4, 2011

CQ Simulation: DataUser Design

I've been away from this blog too long -- it took me a while to warm up and start getting things done.

Today, I started in on the DataUser class. This class models a single data user as it comes into line, waits, gets a computer, uses the computer, and leaves.

I may have to include more state information -- for after the user is done -- but for now, this is the fields:

// FIELDS
private int queueUpTime; // The number of the tick at which this user joins the line.
private int patienceTime; // The number of ticks that this user is willing to wait.
private int desiredTime; // How many ticks the user want to use the computer.
private bool sitting = false;

The constructor is trivial, just taking three ints and assigning them to the first three values.

When the user gets to sit down, the state (represented by sitting) changes. Here's the code for that:

public void sitDown()
{
sitting = true;
}

And I don't write code without writing tests for it:

$testSitting = new DataUser (100, 100, 100)
if ($testSitting.testSitting()) { "FAIL: sitting variable should be false." }
$testSitting.sitDown()
if (!($testSitting.testSitting())) { "FAIL: sitting variable should be true." }

Next, I need code to change the values of the user's patience (decreasing as the user waits in line) and the time the user will use the computer (decreasing as the user uses the computer.)

public void tick (int thisTick)
{
if (thisTick LESS_THAN queueUpTime) return;
if (sitting) { patienceTime--; }
else { desiredTime--; }
... }

Notice that the system doesn't make these changes unless the user is in line.

I wanted to make sure that there were no negative values for these variables, so I included some testing code:

public void tick (int thisTick)
{
if (thisTick LESS_THAN queueUpTime) return;
if (sitting) { patienceTime--; }
else { desiredTime--; }
#if DEBUG
if (queueUpTime LESS_THAN 0) { throw new Exception(); }
if (patienceTime LESS_THAN 0) { throw new Exception(); }
if (desiredTime LESS_THAN 0) { throw new Exception(); }
#endif
}

I guess it wouldn't hurt if this were included in final code - just looks ugly. Other parts of the design might make this irrelevant, so I'll just leave it.

And here is the testing code:

$testTick = new DataUser (7, 10, 10)

try
{
for ($a = 0; $a -lt 25; $a++) {
$testTick.tick(9)
}

"FAIL: Test code should throw an exception for a negative value."
}
catch { }

$testTick = new DataUser (7, 10, 10)
$testTick.sitDown()
try
{
for ($a = 0; $a -lt 25; $a++) {
$testTick.tick(9)
}

"FAIL: Test code should throw an exception for a negative value."
}
catch { }

This code tests that there is a decrement and that decrementing too far breaks the system. I don't like the idea that I'm testing two things at once. Let me go play with that a bit.

But this will do for today.

Saturday, July 2, 2011

CQ Simulation: Getting Started

Enough ruminating. I'm going to get started.

First up, I'm going to copy the project from Watch. That was the one where I implemented what I learned with the first project.

I'll change the name to ComputerQueue and then edit the contained files to reflect that.

I had to delete all the tests from the test directory.

I replaced the .cs contents with stubbed code:

#define DEBUG
using System;





public class DataUser
{
// FIELDS

// CONSTRUCTORS

// PROPERTIES

// METHODS

#if DEBUG
// TESTING CODE

#endif
// OVERRIDES

}







public class DataOut
{
// FIELDS

// CONSTRUCTORS

// PROPERTIES

// METHODS

#if DEBUG
// TESTING CODE

#endif
// OVERRIDES

}

My general test code was so generic that I didn't have to change anything except the directory that it runs in.

And we are good, for now.

Next time: Some code.