Tip:
Highlight text to annotate it
X
Now let's really quickly look at mine.
It doesn't necessarily need to be the case that yours looks very much like mine at all
as long as it accomplishes an effect that's somewhat similar.
So what we're going to do here is set N, the size of the queue, to 4,
create a new queue of that size, and immediately call checkRep
because basically, every time we do a queue operation,
we'd like to do the checkRep.
So if anything goes wrong with the queue, we detect it as early as possible.
We also initialize an l to be empty.
So this l and the q are always going to contain the same number of elements
in the same order, so l and q are always going to have the same contents.
So now we're going to do 10,000 random tests,
and each test is going to be randomly either an enqueue or a dequeue.
And if it's an enqueue, we're going to enqueue a random integer.
After enqueueing, we call checkRep,
and if the enqueue succeeded, we're going to append the item that we enqueued
to our list which is mirroring the queue's contents
and also increment a variable saying how many times this operation succeeded.
On the other hand, if the enqueue doesn't succeed,
I want to assert that the length of our list is equal to N--
that is to say, the list has the same number of elements as a full queue--
assert that the queue is full, call checkRep again, and also increment a counter
indicating we performed an add operation on a full queue.
We're not going to talk about why these counters matter just quite yet,
but we're going to look at this a little bit later. The dequeue structure is exactly analogous.
We're going to issue a dequeue operation with 50% probability,
checkRep, and then we're going to do sort of the analogous empty checking operations
that we did and empty checking operations if the dequeue operation failed.
And if the dequeue succeeded, we're going to pop off an element off our list
and also ensure that it's exactly the value that we expect.
So once that loop terminates, we're going to go into a loop
whose purpose is to empty out the elements of the queue
and finally, after we've drained the queue of all elements,
we're going to assert that our list which mirrors the queue is also empty.
So that's my little random tester for the queue.