Kata Step Five

Next test - We’d like to see that the queue is really storing items, and handing them back in FIFO order.

BoundedQueueTest.cpp

TEST_F(BoundedQueueTest, FirstInFirstOut) {
        q.enqueue(1);
        q.enqueue(2);
        ASSERT_EQ(1, q.dequeue());
}

Note: No mocking necessary here. Hope you can remember how to write a test without mocking...

See if you can make this compile, or read on:











Yes, we need to define a dequeue method.

BoundedQueue.h

public:
        const T& dequeue() { return 0; }

Yikes, that’s scary bad code. We just wanted something that will compile, so we can see that failing test. Now let’s hurry up and make the test pass!

See if you can make the test pass, or read on:











We can use the STL queue class. Remember to #include <queue>.

BoundedQueue.h

#include <queue>

template <typename T>
class BoundedQueue {
public:
        ...
        void enqueue(const T& item) {
                q.push(item);
                consumer.Resume();
        }

        const T& dequeue() {
                return q.front();
        }

private:
        ...
        std::queue<T> q;
};

Green bar!

I feel uncomfortable about this implementation of dequeue(), as it doesn't actually dequeue any items. But, rather than fix the implementation, I remember that I need to fix the tests first. There must be some weakness in my tests.

Go on to BoundedQueueKataStepSix.... or go back

BoundedQueueKataStepFive (last edited 2010-01-17 00:48:09 by KayJohansen)