Wednesday, August 17, 2011

CQ Simulation: Testing the tick() Method

I sat down to write a simpler test for populating a seat. Here's the draft code:

# Test whether users are being assigned to seats.


$testSimulation = new Simulation(6, 30) # 6 seats at 30 minutes each


# Populuate the ComputerQueue list


for ($i = 0; $i -lt 25; $i++)

{

$testDataUser = new DataUser(($i+1), 100, 100) # large values for patience and time desired.

$testSimulation.takeUser($testDataUser)

}


$testSimulation.queueSize()

$testSimulation.tick()

$testSimulation.queueSize()

$testSimulation.tick()

$testSimulation.queueSize()

$testSimulation.tick()

$testSimulation.queueSize()

$testSimulation.tick()

$testSimulation.queueSize()

$testSimulation.tick()

$testSimulation.queueSize()

$testSimulation.tick()

$testSimulation.queueSize()


$testSimulation.tick()

$testSimulation.queueSize()


$testSimulation.tick()

$testSimulation.queueSize()


$testSimulation.tick()

$testSimulation.queueSize()


$testSimulation.tick()

$testSimulation.queueSize()


I expected this to fill the six seats and then spin its wheels. Like this:


25

24

23

22

21

20

19

19

19

19

19


Didn't happen. Instead, it just kept counting down.

So, is the code right or is the test right?

Looking at the code, there's no place where the seat is changed to no longer free. This is what I meant when I said I didn't like how complicated the tick() method was here.

I added a method that takes the DataUser and resets the Free value:


public void takeSeat(DataUser x)

{

currentUser = x;

Free = false;

}


And last thing for right now: Does the Seat class free up seats, as it should? I could inspect the code, but a test would be easier:


for ($1 = 0; $i -lt 200; $i++)

{

$testSimulation.tick()

$testSimulation.queueSize()


}


This works great – until the number of users reaches zero. Then I get an error from the code.

And I'll fix that next time.

No comments:

Post a Comment